1    | /***************************************
2    |   $Revision: 1.4 $
3    | 
4    |   Utilities (ut). memwrap.c - memory allocation wrappers. 
5    |                               Facilitate easy changing a memory allocation
6    | 			      library and provide uniform error codes.
7    | 
8    |   Status: NOT REVUED, TESTED, 
9    | 
10   |   Design and implementation by: Marek Bukowy
11   | 
12   |   ******************/ /******************
13   |   Copyright (c) 1999                              RIPE NCC
14   |  
15   |   All Rights Reserved
16   |   
17   |   Permission to use, copy, modify, and distribute this software and its
18   |   documentation for any purpose and without fee is hereby granted,
19   |   provided that the above copyright notice appear in all copies and that
20   |   both that copyright notice and this permission notice appear in
21   |   supporting documentation, and that the name of the author not be
22   |   used in advertising or publicity pertaining to distribution of the
23   |   software without specific, written prior permission.
24   |   
25   |   THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
26   |   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
27   |   AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
28   |   DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
29   |   AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
30   |   OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31   |   ***************************************/
32   | 
33   | #include <stdlib.h>
34   | #include <erroutines.h>
35   | #include <stubs.h>
36   | 
37   | static void
38   | wr_alloc_log(void *ptr, int len, char* comment, int line) 
39   | {
40   |   /* fprintf(stderr,"allocated %7d bytes at address %p in %s/%d\n",
41   | 	  len, ptr, comment, line);
42   |   */
43   | }
44   | 
45   | static void
46   | wr_free_log(void *ptr, char* comment, int line) 
47   | {
48   |   /*
49   |   fprintf(stderr,"freed some memory space at address %p in %s/%d\n", ptr, comment, line);
50   |   */
51   | }
52   | 
53   | 
54   | 
55   | er_ret_t 
56   | wr_real_malloc(void **ptr, size_t size, char* comment, int line) 
57   | {
58   |   if( (*ptr = malloc(size)) == NULL ) {
59   |     /* die; */ /* this should return an appropriate error number */
60   |     return UT_OUTMEM;
61   |   }
62   |   else {
63   |     wr_alloc_log(*ptr, size, comment, line);
64   |     return UT_OK;
65   |   }
66   | }
67   | 
68   | er_ret_t 
69   | wr_real_calloc(void **ptr, size_t num, size_t size, char* comment, int line) 
70   | {
71   | void *newalloc;
72   | 
73   |   newalloc = calloc(num, size);
74   | 
75   |   if( newalloc == NULL ) {
76   |     /*die; */ /* this should return an appropriate error number */
77   |     return UT_OUTMEM;
78   |   }
79   |   else {
80   |     *ptr=newalloc;
81   |     wr_alloc_log(*ptr, size*num, comment, line);
82   |     return UT_OK;
83   |   }
84   | }
85   | 
86   | 
87   | er_ret_t  
88   | wr_real_realloc(void **ptr, void *oldptr, size_t size, char* comment, int line) 
89   | {
90   |   if( (*ptr = realloc(oldptr, size)) == NULL ) {
91   |     /* die;  */ /* this should return an appropriate error number */
92   |    return UT_OUTMEM;
93   |   } 
94   |   else {
95   |     wr_alloc_log(*ptr, size, comment, line);
96   |     return UT_OK;
97   |   }
98   | }
99   | 
100  | er_ret_t 
101  | wr_real_free(void *ptr, char* comment, int line) 
102  | {
103  |   if( ptr == NULL ) {
104  |     die;
105  |   }
106  |   wr_free_log(ptr, comment, line);
107  |   free(ptr);
108  |   /* if we're tired of those dies, we can set the pointer to NULL after free */
109  |   return UT_OK;
110  | }
111  | 
112  |