| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#include <sys/types.h> |
|---|
| 23 |
#include <stdio.h> |
|---|
| 24 |
#include <stdlib.h> |
|---|
| 25 |
#include <string.h> |
|---|
| 26 |
#include <libxml/xmlstring.h> |
|---|
| 27 |
#include <err.h> |
|---|
| 28 |
|
|---|
| 29 |
#include "libswish3.h" |
|---|
| 30 |
|
|---|
| 31 |
extern int SWISH_DEBUG; |
|---|
| 32 |
|
|---|
| 33 |
static long int memcount = 0; |
|---|
| 34 |
|
|---|
| 35 |
void |
|---|
| 36 |
swish_init_memory( |
|---|
| 37 |
) |
|---|
| 38 |
{ |
|---|
| 39 |
memcount = 0; |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
long int |
|---|
| 43 |
swish_get_memcount( |
|---|
| 44 |
) |
|---|
| 45 |
{ |
|---|
| 46 |
return memcount; |
|---|
| 47 |
} |
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
void * |
|---|
| 52 |
swish_xrealloc( |
|---|
| 53 |
void *ptr, |
|---|
| 54 |
size_t size |
|---|
| 55 |
) |
|---|
| 56 |
{ |
|---|
| 57 |
void *new_ptr = realloc(ptr, size); |
|---|
| 58 |
|
|---|
| 59 |
if (new_ptr == NULL) |
|---|
| 60 |
SWISH_CROAK("Out of memory (could not reallocate %lu more bytes)!", |
|---|
| 61 |
(unsigned long)size); |
|---|
| 62 |
|
|---|
| 63 |
return new_ptr; |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
void * |
|---|
| 68 |
swish_xmalloc( |
|---|
| 69 |
size_t size |
|---|
| 70 |
) |
|---|
| 71 |
{ |
|---|
| 72 |
void *ptr = malloc(size); |
|---|
| 73 |
|
|---|
| 74 |
if (ptr == NULL) |
|---|
| 75 |
SWISH_CROAK("Out of memory! Can't malloc %lu bytes", |
|---|
| 76 |
(unsigned long)size); |
|---|
| 77 |
|
|---|
| 78 |
memcount++; |
|---|
| 79 |
if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) { |
|---|
| 80 |
SWISH_DEBUG_MSG("memcount = %ld", memcount); |
|---|
| 81 |
SWISH_DEBUG_MSG("xmalloc address: 0x%x", (int)ptr); |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
return ptr; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
xmlChar * |
|---|
| 88 |
swish_xstrdup( |
|---|
| 89 |
const xmlChar *ptr |
|---|
| 90 |
) |
|---|
| 91 |
{ |
|---|
| 92 |
xmlChar *copy; |
|---|
| 93 |
memcount++; |
|---|
| 94 |
if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) |
|---|
| 95 |
SWISH_DEBUG_MSG("memcount = %ld", memcount); |
|---|
| 96 |
copy = xmlStrdup(ptr); |
|---|
| 97 |
if (copy == NULL) |
|---|
| 98 |
SWISH_CROAK("strdup returned NULL for %s", ptr); |
|---|
| 99 |
|
|---|
| 100 |
return copy; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
xmlChar * |
|---|
| 104 |
swish_xstrndup( |
|---|
| 105 |
const xmlChar *ptr, |
|---|
| 106 |
int len |
|---|
| 107 |
) |
|---|
| 108 |
{ |
|---|
| 109 |
memcount++; |
|---|
| 110 |
if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) |
|---|
| 111 |
SWISH_DEBUG_MSG("memcount = %ld", memcount); |
|---|
| 112 |
return (xmlStrndup(ptr, len)); |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
void |
|---|
| 116 |
swish_xfree( |
|---|
| 117 |
void *ptr |
|---|
| 118 |
) |
|---|
| 119 |
{ |
|---|
| 120 |
if (ptr == NULL) { |
|---|
| 121 |
SWISH_WARN |
|---|
| 122 |
(" >>>>>>>>>>>>>> attempt to free NULL pointer <<<<<<<<<<<<<<"); |
|---|
| 123 |
return; |
|---|
| 124 |
} |
|---|
| 125 |
|
|---|
| 126 |
if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) |
|---|
| 127 |
SWISH_DEBUG_MSG("freeing %s 0x%x", (char*)ptr, (int)ptr); |
|---|
| 128 |
|
|---|
| 129 |
xmlFree(ptr); |
|---|
| 130 |
|
|---|
| 131 |
memcount--; |
|---|
| 132 |
|
|---|
| 133 |
if (SWISH_DEBUG & SWISH_DEBUG_MEMORY) |
|---|
| 134 |
SWISH_DEBUG_MSG("memcount = %ld", memcount); |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
void |
|---|
| 138 |
swish_mem_debug( |
|---|
| 139 |
) |
|---|
| 140 |
{ |
|---|
| 141 |
|
|---|
| 142 |
if (memcount > 0) |
|---|
| 143 |
SWISH_WARN |
|---|
| 144 |
("%ld more swish_xmalloc()s or swish_xstrdup()s than swish_xfree()s", |
|---|
| 145 |
memcount); |
|---|
| 146 |
|
|---|
| 147 |
if (memcount < 0) |
|---|
| 148 |
SWISH_WARN("too many swish_xfree()s %d", memcount); |
|---|
| 149 |
} |
|---|