Kannel: Open Source WAP and SMS gateway  svn-r5335
gwmem.h File Reference

Go to the source code of this file.

Macros

#define malloc(n)   do_not_call_malloc_directly
 
#define calloc(a, b)   do_not_use_calloc
 
#define realloc(p, n)   do_not_call_realloc_directly
 
#define free(p)   do_not_call_free_directly
 
#define strdup(p)   do_not_call_strdup_directly
 

Functions

void * gw_native_noop (void *ptr)
 
void gw_native_init (void)
 
void gw_native_check_leaks (void)
 
void * gw_native_malloc (size_t size)
 
void * gw_native_calloc (int nmemb, size_t size)
 
void * gw_native_realloc (void *ptr, size_t size)
 
void gw_native_free (void *ptr)
 
char * gw_native_strdup (const char *str)
 
void gw_native_shutdown (void)
 
void gw_check_init_mem (int slow_flag)
 
void gw_check_check_leaks (void)
 
void * gw_check_malloc (size_t size, const char *filename, long line, const char *function)
 
void * gw_check_calloc (int nmemb, size_t size, const char *filename, long line, const char *function)
 
void * gw_check_realloc (void *p, size_t size, const char *filename, long line, const char *function)
 
void gw_check_free (void *p, const char *filename, long line, const char *function)
 
char * gw_check_strdup (const char *str, const char *filename, long line, const char *function)
 
int gw_check_is_allocated (void *p)
 
long gw_check_area_size (void *p)
 
void * gw_check_claim_area (void *p, const char *filename, long line, const char *function)
 
void gw_check_shutdown (void)
 

Macro Definition Documentation

◆ calloc

#define calloc (   a,
 
)    do_not_use_calloc

Definition at line 192 of file gwmem.h.

Referenced by gw_check_calloc(), gw_native_calloc(), and ws_calloc().

◆ free

#define free (   p)    do_not_call_free_directly

Definition at line 194 of file gwmem.h.

◆ malloc

#define malloc (   n)    do_not_call_malloc_directly

Definition at line 191 of file gwmem.h.

◆ realloc

#define realloc (   p,
 
)    do_not_call_realloc_directly

◆ strdup

#define strdup (   p)    do_not_call_strdup_directly

Function Documentation

◆ gw_check_area_size()

long gw_check_area_size ( void *  p)

Definition at line 723 of file gwmem-check.c.

References area::area, area::area_size, find_area(), lock(), size, unlock(), and warning().

724 {
725  struct area *area;
726  size_t size;
727 
728  lock();
729  area = find_area(p);
730  if (!area) {
731  unlock();
732  warning(0, "Area_size called on non-allocated area %p", p);
733  return -1;
734  }
735  size = area->area_size;
736  unlock();
737  return size;
738 }
static void lock(void)
Definition: gwmem-check.c:198
int size
Definition: wsasm.c:84
static void unlock(void)
Definition: gwmem-check.c:203
static struct area * find_area(unsigned char *p)
Definition: gwmem-check.c:358
size_t area_size
Definition: gwmem-check.c:150
void warning(int err, const char *fmt,...)
Definition: log.c:660
void * area
Definition: gwmem-check.c:149

◆ gw_check_calloc()

void* gw_check_calloc ( int  nmemb,
size_t  size,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 537 of file gwmem-check.c.

References calloc, filename, gw_assert(), initialized, lock(), MARKER_SIZE, panic, record_allocation(), size, and unlock().

539 {
540  unsigned char *p;
541 
543 
544  /* ANSI C89 says malloc(0) is implementation-defined. Avoid it. */
545  gw_assert(size > 0);
546  gw_assert(nmemb > 0);
547 
548  p = calloc(1, (nmemb*size) + 2 * MARKER_SIZE);
549  if (p == NULL)
550  panic(errno, "Memory allocation of %ld bytes failed.", (long)size);
551 
552  p += MARKER_SIZE;
553 
554  lock();
555  record_allocation(p, size, filename, lineno, function);
556  unlock();
557 
558  return p;
559 }
static void lock(void)
Definition: gwmem-check.c:198
int size
Definition: wsasm.c:84
static void unlock(void)
Definition: gwmem-check.c:203
gw_assert(wtls_machine->packet_to_send !=NULL)
#define calloc(a, b)
Definition: gwmem.h:192
static struct area * record_allocation(unsigned char *p, size_t size, const char *filename, long lineno, const char *function)
Definition: gwmem-check.c:418
char filename[FILENAME_MAX+1]
Definition: log.c:171
#define MARKER_SIZE
Definition: gwmem-check.c:164
static int initialized
Definition: gwmem-check.c:127
#define panic
Definition: log.h:87

◆ gw_check_check_leaks()

void gw_check_check_leaks ( void  )

Definition at line 679 of file gwmem-check.c.

References allocated, area::area_size, check_marks(), debug(), drop_from_free_ring(), dump_area(), free_ring_len, gw_assert(), highest_num_allocations, highest_total_size, initialized, lock(), num_allocations, total_size, and unlock().

680 {
681  long calculated_size;
682  long index;
683 
685  lock();
686 
687  for (index = 0; index < free_ring_len; index++) {
688  drop_from_free_ring(index);
689  }
690  free_ring_len = 0;
691 
692  calculated_size = 0;
693  for (index = 0; index < num_allocations; index++) {
694  calculated_size += allocated[index].area_size;
695  }
696  gw_assert(calculated_size == total_size);
697 
698  debug("gwlib.gwmem", 0, "----------------------------------------");
699  debug("gwlib.gwmem", 0, "Current allocations: %ld areas, %ld bytes",
701  debug("gwlib.gwmem", 0, "Highest number of allocations: %ld areas",
703  debug("gwlib.gwmem", 0, "Highest memory usage: %ld bytes",
705  for (index = 0; index < num_allocations; index++) {
706  check_marks(&allocated[index], index);
707  dump_area(&allocated[index]);
708  }
709 
710  unlock();
711 }
static void lock(void)
Definition: gwmem-check.c:198
static void unlock(void)
Definition: gwmem-check.c:203
static void drop_from_free_ring(long index)
Definition: gwmem-check.c:462
gw_assert(wtls_machine->packet_to_send !=NULL)
static void dump_area(struct area *area)
Definition: gwmem-check.c:309
size_t area_size
Definition: gwmem-check.c:150
static long free_ring_len
Definition: gwmem-check.c:186
static int check_marks(struct area *area, long index)
Definition: gwmem-check.c:293
static long num_allocations
Definition: gwmem-check.c:182
static long total_size
Definition: gwmem-check.c:194
static int initialized
Definition: gwmem-check.c:127
static long highest_num_allocations
Definition: gwmem-check.c:190
void debug(const char *place, int err, const char *fmt,...)
Definition: log.c:726
static struct area allocated[MAX_ALLOCATIONS]
Definition: gwmem-check.c:177
static long highest_total_size
Definition: gwmem-check.c:192

◆ gw_check_claim_area()

void* gw_check_claim_area ( void *  p,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 654 of file gwmem-check.c.

References area::area, area::claimer, location::filename, filename, find_area(), location::function, location::lineno, lock(), panic, and unlock().

656 {
657  struct area *area;
658 
659  /* Allow this for the convenience of wrapper macros. */
660  if (p == NULL)
661  return NULL;
662 
663  lock();
664  area = find_area(p);
665  if (!area) {
666  unlock();
667  panic(0, "Claim_area called on non-allocated area");
668  }
669 
671  area->claimer.lineno = lineno;
672  area->claimer.function = function;
673  unlock();
674 
675  /* For convenience of calling macros */
676  return p;
677 }
static void lock(void)
Definition: gwmem-check.c:198
static void unlock(void)
Definition: gwmem-check.c:203
static struct area * find_area(unsigned char *p)
Definition: gwmem-check.c:358
const char * function
Definition: gwmem-check.c:141
const char * filename
Definition: gwmem-check.c:139
long lineno
Definition: gwmem-check.c:140
struct location claimer
Definition: gwmem-check.c:154
char filename[FILENAME_MAX+1]
Definition: log.c:171
#define panic
Definition: log.h:87
void * area
Definition: gwmem-check.c:149

◆ gw_check_free()

void gw_check_free ( void *  p,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 619 of file gwmem-check.c.

References area::area, find_area(), free_area(), gw_assert(), initialized, lock(), panic, and unlock().

621 {
622  struct area *area;
624 
625  if (p == NULL)
626  return;
627 
628  lock();
629  area = find_area(p);
630  if (!area) {
631  unlock();
632  panic(0, "Free called on non-allocated area");
633  }
634 
635  free_area(area);
636  unlock();
637 }
static void lock(void)
Definition: gwmem-check.c:198
static void unlock(void)
Definition: gwmem-check.c:203
static struct area * find_area(unsigned char *p)
Definition: gwmem-check.c:358
gw_assert(wtls_machine->packet_to_send !=NULL)
static void free_area(struct area *area)
Definition: gwmem-check.c:494
static int initialized
Definition: gwmem-check.c:127
#define panic
Definition: log.h:87
void * area
Definition: gwmem-check.c:149

◆ gw_check_init_mem()

void gw_check_init_mem ( int  slow_flag)

Definition at line 501 of file gwmem-check.c.

References gwmem_lock, initialized, mutex_init_static, and slow.

502 {
504  slow = slow_flag;
505  initialized = 1;
506 }
static int slow
Definition: gwmem-check.c:130
#define mutex_init_static(mutex)
Definition: thread.h:115
static int initialized
Definition: gwmem-check.c:127
static Mutex gwmem_lock
Definition: gwmem-check.c:135

◆ gw_check_is_allocated()

int gw_check_is_allocated ( void *  p)

Definition at line 713 of file gwmem-check.c.

References area::area, find_area(), lock(), and unlock().

714 {
715  struct area *area;
716 
717  lock();
718  area = find_area(p);
719  unlock();
720  return area != NULL;
721 }
static void lock(void)
Definition: gwmem-check.c:198
static void unlock(void)
Definition: gwmem-check.c:203
static struct area * find_area(unsigned char *p)
Definition: gwmem-check.c:358
void * area
Definition: gwmem-check.c:149

◆ gw_check_malloc()

void* gw_check_malloc ( size_t  size,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 514 of file gwmem-check.c.

References filename, fill(), gw_assert(), initialized, lock(), malloc(), MARKER_SIZE, NEW_AREA_PATTERN, panic, record_allocation(), size, and unlock().

Referenced by gw_check_realloc(), and gw_check_strdup().

516 {
517  unsigned char *p;
518 
520 
521  /* ANSI C89 says malloc(0) is implementation-defined. Avoid it. */
522  gw_assert(size > 0);
523 
524  p = malloc(size + 2 * MARKER_SIZE);
525  if (p == NULL)
526  panic(errno, "Memory allocation of %ld bytes failed.", (long)size);
527  p += MARKER_SIZE;
528 
529  lock();
531  record_allocation(p, size, filename, lineno, function);
532  unlock();
533 
534  return p;
535 }
static void lock(void)
Definition: gwmem-check.c:198
int size
Definition: wsasm.c:84
static void unlock(void)
Definition: gwmem-check.c:203
static void fill(unsigned char *p, size_t bytes, long pattern)
Definition: gwmem-check.c:229
gw_assert(wtls_machine->packet_to_send !=NULL)
void * malloc(YYSIZE_T)
static struct area * record_allocation(unsigned char *p, size_t size, const char *filename, long lineno, const char *function)
Definition: gwmem-check.c:418
#define NEW_AREA_PATTERN
Definition: gwmem-check.c:111
char filename[FILENAME_MAX+1]
Definition: log.c:171
#define MARKER_SIZE
Definition: gwmem-check.c:164
static int initialized
Definition: gwmem-check.c:127
#define panic
Definition: log.h:87

◆ gw_check_realloc()

void* gw_check_realloc ( void *  p,
size_t  size,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 561 of file gwmem-check.c.

References area::allocator, area::area, area::area_size, change_total_size(), endmark(), location::filename, filename, fill(), find_area(), free_area(), location::function, gw_assert(), gw_check_malloc(), initialized, location::lineno, lock(), malloc(), MARKER_SIZE, area::max_size, NEW_AREA_PATTERN, panic, area::reallocator, record_allocation(), round_pow2(), size, and unlock().

563 {
564  struct area *area;
565 
566  if (p == NULL)
567  return gw_check_malloc(size, filename, lineno, function);
568 
570  gw_assert(size > 0);
571 
572  lock();
573  area = find_area(p);
574  if (!area) {
575  unlock();
576  panic(0, "Realloc called on non-allocated area");
577  }
578 
579  if (size == area->area_size) {
580  /* No changes */
581  } else if (size <= area->max_size) {
583  area->area_size = size;
584  endmark(p, size);
585  } else if (size > area->max_size) {
586  /* The current block is not large enough for the reallocation.
587  * We will allocate a new block, copy the data over, and free
588  * the old block. We round the size up to a power of two,
589  * to prevent frequent reallocations. */
590  struct area *new_area;
591  size_t new_size;
592  unsigned char *new_p;
593 
594  new_size = round_pow2(size + 2 * MARKER_SIZE);
595  new_p = malloc(new_size);
596  new_size -= 2 * MARKER_SIZE;
597  new_p += MARKER_SIZE;
598  memcpy(new_p, p, area->area_size);
599  fill(new_p + area->area_size, size - area->area_size,
601  new_area = record_allocation(new_p, size,
605  new_area->max_size = new_size;
606  free_area(area);
607 
608  p = new_p;
609  area = new_area;
610  }
611 
613  area->reallocator.lineno = lineno;
614  area->reallocator.function = function;
615  unlock();
616  return p;
617 }
static void lock(void)
Definition: gwmem-check.c:198
int size
Definition: wsasm.c:84
static void unlock(void)
Definition: gwmem-check.c:203
static struct area * find_area(unsigned char *p)
Definition: gwmem-check.c:358
static void fill(unsigned char *p, size_t bytes, long pattern)
Definition: gwmem-check.c:229
gw_assert(wtls_machine->packet_to_send !=NULL)
const char * function
Definition: gwmem-check.c:141
static void endmark(unsigned char *p, size_t size)
Definition: gwmem-check.c:255
void * malloc(YYSIZE_T)
static void change_total_size(long change)
Definition: gwmem-check.c:411
const char * filename
Definition: gwmem-check.c:139
static struct area * record_allocation(unsigned char *p, size_t size, const char *filename, long lineno, const char *function)
Definition: gwmem-check.c:418
size_t area_size
Definition: gwmem-check.c:150
long lineno
Definition: gwmem-check.c:140
#define NEW_AREA_PATTERN
Definition: gwmem-check.c:111
char filename[FILENAME_MAX+1]
Definition: log.c:171
void * gw_check_malloc(size_t size, const char *filename, long lineno, const char *function)
Definition: gwmem-check.c:514
static unsigned long round_pow2(unsigned long num)
Definition: gwmem-check.c:208
#define MARKER_SIZE
Definition: gwmem-check.c:164
static void free_area(struct area *area)
Definition: gwmem-check.c:494
struct location reallocator
Definition: gwmem-check.c:153
static int initialized
Definition: gwmem-check.c:127
#define panic
Definition: log.h:87
struct location allocator
Definition: gwmem-check.c:152
size_t max_size
Definition: gwmem-check.c:151
void * area
Definition: gwmem-check.c:149

◆ gw_check_shutdown()

void gw_check_shutdown ( void  )

Definition at line 508 of file gwmem-check.c.

References gwmem_lock, initialized, and mutex_destroy().

509 {
511  initialized = 0;
512 }
void mutex_destroy(Mutex *mutex)
Definition: thread.c:97
static int initialized
Definition: gwmem-check.c:127
static Mutex gwmem_lock
Definition: gwmem-check.c:135

◆ gw_check_strdup()

char* gw_check_strdup ( const char *  str,
const char *  filename,
long  line,
const char *  function 
)

Definition at line 639 of file gwmem-check.c.

References filename, gw_assert(), gw_check_malloc(), initialized, and size.

641 {
642  char *copy;
643  int size;
644 
646  gw_assert(str != NULL);
647 
648  size = strlen(str) + 1;
649  copy = gw_check_malloc(size, filename, lineno, function);
650  memcpy(copy, str, size);
651  return copy;
652 }
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
char filename[FILENAME_MAX+1]
Definition: log.c:171
void * gw_check_malloc(size_t size, const char *filename, long lineno, const char *function)
Definition: gwmem-check.c:514
static int initialized
Definition: gwmem-check.c:127

◆ gw_native_calloc()

void* gw_native_calloc ( int  nmemb,
size_t  size 
)

Definition at line 94 of file gwmem-native.c.

References calloc, gw_assert(), panic, and size.

95 {
96  void *ptr;
97 
98  /* ANSI C89 says malloc(0) is implementation-defined. Avoid it. */
99  gw_assert(size > 0);
100  gw_assert(nmemb > 0);
101 
102  ptr = calloc(nmemb, size);
103  if (ptr == NULL)
104  panic(errno, "Memory allocation failed");
105 
106  return ptr;
107 }
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
#define calloc(a, b)
Definition: gwmem.h:192
#define panic
Definition: log.h:87

◆ gw_native_check_leaks()

void gw_native_check_leaks ( void  )

◆ gw_native_free()

void gw_native_free ( void *  ptr)

Definition at line 123 of file gwmem-native.c.

References free().

Referenced by gw_backtrace().

124 {
125  free(ptr);
126 }
void free(void *)

◆ gw_native_init()

void gw_native_init ( void  )

◆ gw_native_malloc()

void* gw_native_malloc ( size_t  size)

Definition at line 80 of file gwmem-native.c.

References gw_assert(), malloc(), panic, and size.

Referenced by gw_native_strdup().

81 {
82  void *ptr;
83 
84  /* ANSI C89 says malloc(0) is implementation-defined. Avoid it. */
85  gw_assert(size > 0);
86 
87  ptr = malloc(size);
88  if (ptr == NULL)
89  panic(errno, "Memory allocation failed");
90 
91  return ptr;
92 }
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
void * malloc(YYSIZE_T)
#define panic
Definition: log.h:87

◆ gw_native_noop()

void* gw_native_noop ( void *  ptr)

Definition at line 78 of file gwmem-native.c.

78 { return ptr; }

◆ gw_native_realloc()

void* gw_native_realloc ( void *  ptr,
size_t  size 
)

Definition at line 109 of file gwmem-native.c.

References gw_assert(), panic, realloc, and size.

110 {
111  void *new_ptr;
112 
113  gw_assert(size > 0);
114 
115  new_ptr = realloc(ptr, size);
116  if (new_ptr == NULL)
117  panic(errno, "Memory re-allocation failed");
118 
119  return new_ptr;
120 }
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
#define realloc(p, n)
Definition: gwmem.h:193
#define panic
Definition: log.h:87

◆ gw_native_shutdown()

void gw_native_shutdown ( void  )

◆ gw_native_strdup()

char* gw_native_strdup ( const char *  str)

Definition at line 129 of file gwmem-native.c.

References gw_assert(), gw_native_malloc(), and size.

130 {
131  char *copy;
132  int size;
133 
134  gw_assert(str != NULL);
135  size = strlen(str) + 1;
136 
137  copy = gw_native_malloc(size);
138  memcpy(copy, str, size);
139  return copy;
140 }
int size
Definition: wsasm.c:84
gw_assert(wtls_machine->packet_to_send !=NULL)
void * gw_native_malloc(size_t size)
Definition: gwmem-native.c:80
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.