Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

gwmem.h File Reference

This graph shows which files directly or indirectly include this file:

Included by dependency graph

Go to the source code of this file.

Defines

#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

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_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_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)


Define Documentation

#define calloc a,
 )     do_not_use_calloc
 

Definition at line 179 of file gwmem.h.

Referenced by ws_calloc().

#define free  )     do_not_call_free_directly
 

Definition at line 181 of file gwmem.h.

Referenced by build_box_arglist(), clear_extparms(), drop_from_free_ring(), dump_area(), DumpNode(), Free(), FreeNode(), gw_native_free(), main(), parse_headers(), Read_termstr(), Read_termstr_rtn(), ws_free(), and ws_realloc().

#define malloc  )     do_not_call_malloc_directly
 

Definition at line 178 of file gwmem.h.

Referenced by AddAttrStartLiteralNode(), AddAttrStartNode(), AddAttrValueNode(), AddCodepageLiteralTagNode(), AddCodepageTagNode(), AddDTDNode(), AddStringTableNode(), build_box_arglist(), gw_check_malloc(), gw_check_realloc(), gw_native_malloc(), main(), new_extparm(), NewNode(), parse_headers(), Read_bytes(), Read_termstr_rtn(), ReadBinary(), ws_malloc(), ws_realloc(), and xmalloc().

#define realloc p,
 )     do_not_call_realloc_directly
 

Definition at line 180 of file gwmem.h.

Referenced by CodepageAttrvalueName(), DumpNode(), gw_native_realloc(), Read_termstr_rtn(), ReadBinary(), and ws_realloc().


Function Documentation

long gw_check_area_size void *  p  ) 
 

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

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

00697 {
00698     struct area *area;
00699     size_t size;
00700 
00701     lock();
00702     area = find_area(p);
00703     if (!area) {
00704         unlock();
00705         warning(0, "Area_size called on non-allocated area %p", p);
00706         return -1;
00707     }
00708     size = area->area_size;
00709     unlock();
00710     return size;
00711 }

Here is the call graph for this function:

void gw_check_check_leaks void   ) 
 

Definition at line 652 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.

00653 {
00654     long calculated_size;
00655     long index;
00656 
00657     gw_assert(initialized);
00658     lock();
00659 
00660     for (index = 0; index < free_ring_len; index++) {
00661         drop_from_free_ring(index);
00662     }
00663     free_ring_len = 0;
00664 
00665     calculated_size = 0;
00666     for (index = 0; index < num_allocations; index++) {
00667         calculated_size += allocated[index].area_size;
00668     }
00669     gw_assert(calculated_size == total_size);
00670 
00671     debug("gwlib.gwmem", 0, "----------------------------------------");
00672     debug("gwlib.gwmem", 0, "Current allocations: %ld areas, %ld bytes",
00673           num_allocations, total_size);
00674     debug("gwlib.gwmem", 0, "Highest number of allocations: %ld areas",
00675           highest_num_allocations);
00676     debug("gwlib.gwmem", 0, "Highest memory usage: %ld bytes",
00677           highest_total_size);
00678     for (index = 0; index < num_allocations; index++) {
00679         check_marks(&allocated[index], index);
00680         dump_area(&allocated[index]);
00681     }
00682 
00683     unlock();
00684 }

Here is the call graph for this function:

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

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

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

00629 {
00630     struct area *area;
00631 
00632     /* Allow this for the convenience of wrapper macros. */
00633     if (p == NULL)
00634         return NULL;
00635 
00636     lock();
00637     area = find_area(p);
00638     if (!area) {
00639         unlock();
00640         panic(0, "Claim_area called on non-allocated area");
00641     }
00642 
00643     area->claimer.filename = filename;
00644     area->claimer.lineno = lineno;
00645     area->claimer.function = function;
00646     unlock();
00647 
00648     /* For convenience of calling macros */
00649     return p;
00650 }

Here is the call graph for this function:

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

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

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

00596 {
00597     struct area *area;
00598     gw_assert(initialized);
00599 
00600     if (p == NULL)
00601         return;
00602 
00603     lock();
00604     area = find_area(p);
00605     if (!area) {
00606         unlock();
00607         panic(0, "Free called on non-allocated area");
00608     }
00609 
00610     free_area(area);
00611     unlock();
00612 }

Here is the call graph for this function:

void gw_check_init_mem int  slow_flag  ) 
 

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

References gwmem_lock, initialized, mutex_init_static, and slow.

00501 {
00502     mutex_init_static(&gwmem_lock);
00503     slow = slow_flag;
00504     initialized = 1;
00505 }

int gw_check_is_allocated void *  p  ) 
 

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

References find_area(), lock, and unlock.

00687 {
00688     struct area *area;
00689 
00690     lock();
00691     area = find_area(p);
00692     unlock();
00693     return area != NULL;
00694 }

Here is the call graph for this function:

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

Definition at line 513 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().

00515 {
00516     unsigned char *p;
00517 
00518     gw_assert(initialized);
00519 
00520     /* ANSI C89 says malloc(0) is implementation-defined.  Avoid it. */
00521     gw_assert(size > 0);
00522 
00523     p = malloc(size + 2 * MARKER_SIZE);
00524     if (p == NULL)
00525         panic(errno, "Memory allocation of %ld bytes failed.", (long)size);
00526     p += MARKER_SIZE;
00527 
00528     lock();
00529     fill(p, size, NEW_AREA_PATTERN);
00530     record_allocation(p, size, filename, lineno, function);
00531     unlock();
00532 
00533     return p;
00534 }

Here is the call graph for this function:

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

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

References area::allocator, 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.

00538 {
00539     struct area *area;
00540 
00541     if (p == NULL)
00542         return gw_check_malloc(size, filename, lineno, function);
00543 
00544     gw_assert(initialized);
00545     gw_assert(size > 0);
00546 
00547     lock();
00548     area = find_area(p);
00549     if (!area) {
00550         unlock();
00551         panic(0, "Realloc called on non-allocated area");
00552     }
00553 
00554     if (size == area->area_size) {
00555         /* No changes */
00556     } else if (size <= area->max_size) {
00557         change_total_size(size - area->area_size);
00558         area->area_size = size;
00559         endmark(p, size);
00560     } else if (size > area->max_size) {
00561         /* The current block is not large enough for the reallocation.
00562          * We will allocate a new block, copy the data over, and free
00563          * the old block.  We round the size up to a power of two,
00564          * to prevent frequent reallocations. */
00565         struct area *new_area;
00566         size_t new_size;
00567         unsigned char *new_p;
00568 
00569         new_size = round_pow2(size + 2 * MARKER_SIZE);
00570         new_p = malloc(new_size);
00571         new_size -= 2 * MARKER_SIZE;
00572         new_p += MARKER_SIZE;
00573         memcpy(new_p, p, area->area_size);
00574         fill(new_p + area->area_size, size - area->area_size,
00575              NEW_AREA_PATTERN);
00576         new_area = record_allocation(new_p, size,
00577                                      area->allocator.filename,
00578                                      area->allocator.lineno,
00579                                      area->allocator.function);
00580         new_area->max_size = new_size;
00581         free_area(area);
00582 
00583         p = new_p;
00584         area = new_area;
00585     }
00586 
00587     area->reallocator.filename = filename;
00588     area->reallocator.lineno = lineno;
00589     area->reallocator.function = function;
00590     unlock();
00591     return p;
00592 }

Here is the call graph for this function:

void gw_check_shutdown void   ) 
 

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

References gwmem_lock, initialized, and mutex_destroy().

00508 {
00509     mutex_destroy(&gwmem_lock);
00510     initialized = 0;
00511 }

Here is the call graph for this function:

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

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

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

00616 {
00617     char *copy;
00618 
00619     gw_assert(initialized);
00620     gw_assert(str != NULL);
00621 
00622     copy = gw_check_malloc(strlen(str) + 1, filename, lineno, function);
00623     strcpy(copy, str);
00624     return copy;
00625 }

Here is the call graph for this function:

void gw_native_check_leaks void   ) 
 

void gw_native_free void *  ptr  ) 
 

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

References free.

00109 {
00110     free(ptr);
00111 }

void gw_native_init void   ) 
 

void* gw_native_malloc size_t  size  ) 
 

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

References gw_assert, malloc, panic, and size.

Referenced by gw_native_strdup().

00080 {
00081     void *ptr;
00082 
00083     /* ANSI C89 says malloc(0) is implementation-defined.  Avoid it. */
00084     gw_assert(size > 0);
00085 
00086     ptr = malloc(size);
00087     if (ptr == NULL)
00088         panic(errno, "Memory allocation failed");
00089 
00090     return ptr;
00091 }

void* gw_native_noop void *  ptr  ) 
 

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

00077 { return ptr; }

void* gw_native_realloc void *  ptr,
size_t  size
 

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

References gw_assert, panic, realloc, and size.

00095 {
00096     void *new_ptr;
00097 
00098     gw_assert(size > 0);
00099 
00100     new_ptr = realloc(ptr, size);
00101     if (new_ptr == NULL)
00102         panic(errno, "Memory re-allocation failed");
00103 
00104     return new_ptr;
00105 }

void gw_native_shutdown void   ) 
 

char* gw_native_strdup const char *  str  ) 
 

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

References gw_assert, and gw_native_malloc().

00115 {
00116     char *copy;
00117 
00118     gw_assert(str != NULL);
00119 
00120     copy = gw_native_malloc(strlen(str) + 1);
00121     strcpy(copy, str);
00122     return copy;
00123 }

Here is the call graph for this function:

See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.