1 | /*************************************** 2 | $Revision: 1.7 $ 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 | #include <glib.h> 37 | 38 | /* 39 | #define USE_LOGGING 40 | */ 41 | 42 | static void 43 | wr_alloc_log(void *ptr, int len, char* comment, int line) 44 | { 45 | fprintf(stderr,"allocated %7d bytes at address %p in %s/%d\n", 46 | len, ptr, comment, line); 47 | } 48 | 49 | static void 50 | wr_free_log(void *ptr, char* comment, int line) 51 | { 52 | fprintf(stderr,"freed some memory space at address %p in %s/%d\n", 53 | ptr, comment, line); 54 | } 55 | 56 | static void 57 | wr_free_list_log(void *ptr, char* comment, int line) 58 | { 59 | fprintf(stderr,"freeing list + elements at address %p in %s/%d\n", 60 | ptr, comment, line); 61 | } 62 | 63 | er_ret_t 64 | wr_real_malloc(void **ptr, size_t size, char* comment, int line) 65 | { 66 | if( (*ptr = malloc(size)) == NULL ) { 67 | /* die; */ /* this should return an appropriate error number */ 68 | return UT_OUTMEM; 69 | } 70 | else { 71 | #ifdef USE_LOGGING 72 | wr_alloc_log(*ptr, size, comment, line); 73 | #endif 74 | return UT_OK; 75 | } 76 | } 77 | 78 | er_ret_t 79 | wr_real_calloc(void **ptr, size_t num, size_t size, char* comment, int line) 80 | { 81 | void *newalloc; 82 | 83 | newalloc = calloc(num, size); 84 | 85 | if( newalloc == NULL ) { 86 | /*die; */ /* this should return an appropriate error number */ 87 | return UT_OUTMEM; 88 | } 89 | else { 90 | *ptr=newalloc; 91 | #ifdef USE_LOGGING 92 | wr_alloc_log(*ptr, size*num, comment, line); 93 | #endif 94 | return UT_OK; 95 | } 96 | } 97 | 98 | 99 | er_ret_t 100 | wr_real_realloc(void **ptr, void *oldptr, size_t size, char* comment, int line) 101 | { 102 | if( (*ptr = realloc(oldptr, size)) == NULL ) { 103 | /* die; */ /* this should return an appropriate error number */ 104 | return UT_OUTMEM; 105 | } 106 | else { 107 | #ifdef USE_LOGGING 108 | wr_free_log(oldptr, comment, line); 109 | wr_alloc_log(*ptr, size, comment, line); 110 | #endif 111 | return UT_OK; 112 | } 113 | } 114 | 115 | er_ret_t 116 | wr_real_free(void *ptr, char* comment, int line) 117 | { 118 | if( ptr == NULL ) { 119 | die; 120 | } 121 | #ifdef USE_LOGGING 122 | wr_free_log(ptr, comment, line); 123 | #endif 124 | free(ptr); 125 | /* if we're tired of those dies, we can set the pointer to NULL after free */ 126 | return UT_OK; 127 | } 128 | 129 | 130 | /* make a copy and return the pointer to the allocated area */ 131 | char * 132 | wr_string(char *text) 133 | { 134 | char *area; 135 | int len = strlen(text); 136 | 137 | wr_real_malloc( (void **) &area, len+1, "wr_string", len ); 138 | 139 | strcpy( area, text ); 140 | 141 | return area; 142 | } 143 | 144 | /* for GList's foreach */ 145 | static 146 | void 147 | wr_free_list_element(void *cpy, void *trash) 148 | { 149 | wr_real_free(cpy, "wr_free_list_element", 0 ); 150 | } 151 | 152 | /* for GList's foreach */ 153 | void 154 | wr_real_clear_list(GList **list, char* comment, int line) 155 | { 156 | /* allow NULL argument */ 157 | if( *list != NULL ) { 158 | 159 | #ifdef USE_LOGGING 160 | wr_free_list_log(*list, comment, line); 161 | #endif 162 | g_list_foreach(*list, wr_free_list_element, NULL); 163 | g_list_free(*list); 164 | *list = NULL; 165 | } 166 | }