Source File modules/ut/memwrap.c

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