OLD | NEW |
1 /* | 1 /* |
2 * xmlmemory.c: libxml memory allocator wrapper. | 2 * xmlmemory.c: libxml memory allocator wrapper. |
3 * | 3 * |
4 * daniel@veillard.com | 4 * daniel@veillard.com |
5 */ | 5 */ |
6 | 6 |
7 #define IN_LIBXML | 7 #define IN_LIBXML |
8 #include "libxml.h" | 8 #include "libxml.h" |
9 | 9 |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 #ifdef SUN4 | 103 #ifdef SUN4 |
104 #define ALIGN_SIZE 16 | 104 #define ALIGN_SIZE 16 |
105 #else | 105 #else |
106 #define ALIGN_SIZE sizeof(double) | 106 #define ALIGN_SIZE sizeof(double) |
107 #endif | 107 #endif |
108 #define HDR_SIZE sizeof(MEMHDR) | 108 #define HDR_SIZE sizeof(MEMHDR) |
109 #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \ | 109 #define RESERVE_SIZE (((HDR_SIZE + (ALIGN_SIZE-1)) \ |
110 / ALIGN_SIZE ) * ALIGN_SIZE) | 110 / ALIGN_SIZE ) * ALIGN_SIZE) |
111 | 111 |
| 112 #define MAX_SIZE_T ((size_t)-1) |
112 | 113 |
113 #define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE)) | 114 #define CLIENT_2_HDR(a) ((MEMHDR *) (((char *) (a)) - RESERVE_SIZE)) |
114 #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE)) | 115 #define HDR_2_CLIENT(a) ((void *) (((char *) (a)) + RESERVE_SIZE)) |
115 | 116 |
116 | 117 |
117 static unsigned int block=0; | 118 static unsigned int block=0; |
118 static unsigned int xmlMemStopAtBlock = 0; | 119 static unsigned int xmlMemStopAtBlock = 0; |
119 static void *xmlMemTraceBlockAt = NULL; | 120 static void *xmlMemTraceBlockAt = NULL; |
120 #ifdef MEM_LIST | 121 #ifdef MEM_LIST |
121 static MEMHDR *memlist = NULL; | 122 static MEMHDR *memlist = NULL; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 xmlMallocBreakpoint(); | 211 xmlMallocBreakpoint(); |
211 } | 212 } |
212 | 213 |
213 TEST_POINT | 214 TEST_POINT |
214 | 215 |
215 return(ret); | 216 return(ret); |
216 } | 217 } |
217 | 218 |
218 /** | 219 /** |
219 * xmlMallocAtomicLoc: | 220 * xmlMallocAtomicLoc: |
220 * @size: an int specifying the size in byte to allocate. | 221 * @size: an unsigned int specifying the size in byte to allocate. |
221 * @file: the file name or NULL | 222 * @file: the file name or NULL |
222 * @line: the line number | 223 * @line: the line number |
223 * | 224 * |
224 * a malloc() equivalent, with logging of the allocation info. | 225 * a malloc() equivalent, with logging of the allocation info. |
225 * | 226 * |
226 * Returns a pointer to the allocated area or NULL in case of lack of memory. | 227 * Returns a pointer to the allocated area or NULL in case of lack of memory. |
227 */ | 228 */ |
228 | 229 |
229 void * | 230 void * |
230 xmlMallocAtomicLoc(size_t size, const char * file, int line) | 231 xmlMallocAtomicLoc(size_t size, const char * file, int line) |
231 { | 232 { |
232 MEMHDR *p; | 233 MEMHDR *p; |
233 void *ret; | 234 void *ret; |
234 | 235 |
235 if (!xmlMemInitialized) xmlInitMemory(); | 236 if (!xmlMemInitialized) xmlInitMemory(); |
236 #ifdef DEBUG_MEMORY | 237 #ifdef DEBUG_MEMORY |
237 xmlGenericError(xmlGenericErrorContext, | 238 xmlGenericError(xmlGenericErrorContext, |
238 "Malloc(%d)\n",size); | 239 "Malloc(%d)\n",size); |
239 #endif | 240 #endif |
240 | 241 |
241 TEST_POINT | 242 TEST_POINT |
242 | 243 |
| 244 if (size > (MAX_SIZE_T - RESERVE_SIZE)) { |
| 245 xmlGenericError(xmlGenericErrorContext, |
| 246 "xmlMallocAtomicLoc : Unsigned overflow prevented\n"); |
| 247 xmlMemoryDump(); |
| 248 return(NULL); |
| 249 } |
| 250 |
243 p = (MEMHDR *) malloc(RESERVE_SIZE+size); | 251 p = (MEMHDR *) malloc(RESERVE_SIZE+size); |
244 | 252 |
245 if (!p) { | 253 if (!p) { |
246 xmlGenericError(xmlGenericErrorContext, | 254 xmlGenericError(xmlGenericErrorContext, |
247 » » "xmlMallocLoc : Out of free space\n"); | 255 » » "xmlMallocAtomicLoc : Out of free space\n"); |
248 xmlMemoryDump(); | 256 xmlMemoryDump(); |
249 return(NULL); | 257 return(NULL); |
250 } | 258 } |
251 p->mh_tag = MEMTAG; | 259 p->mh_tag = MEMTAG; |
252 p->mh_size = size; | 260 p->mh_size = size; |
253 p->mh_type = MALLOC_ATOMIC_TYPE; | 261 p->mh_type = MALLOC_ATOMIC_TYPE; |
254 p->mh_file = file; | 262 p->mh_file = file; |
255 p->mh_line = line; | 263 p->mh_line = line; |
256 xmlMutexLock(xmlMemMutex); | 264 xmlMutexLock(xmlMemMutex); |
257 p->mh_number = ++block; | 265 p->mh_number = ++block; |
(...skipping 867 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 if (freeFunc != NULL) *freeFunc = xmlFree; | 1133 if (freeFunc != NULL) *freeFunc = xmlFree; |
1126 if (mallocFunc != NULL) *mallocFunc = xmlMalloc; | 1134 if (mallocFunc != NULL) *mallocFunc = xmlMalloc; |
1127 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; | 1135 if (mallocAtomicFunc != NULL) *mallocAtomicFunc = xmlMallocAtomic; |
1128 if (reallocFunc != NULL) *reallocFunc = xmlRealloc; | 1136 if (reallocFunc != NULL) *reallocFunc = xmlRealloc; |
1129 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; | 1137 if (strdupFunc != NULL) *strdupFunc = xmlMemStrdup; |
1130 return(0); | 1138 return(0); |
1131 } | 1139 } |
1132 | 1140 |
1133 #define bottom_xmlmemory | 1141 #define bottom_xmlmemory |
1134 #include "elfgcchack.h" | 1142 #include "elfgcchack.h" |
OLD | NEW |