Mercurial > hg > xemacs-beta
comparison src/gmalloc.c @ 185:3d6bfa290dbd r20-3b19
Import from CVS: tag r20-3b19
author | cvs |
---|---|
date | Mon, 13 Aug 2007 09:55:28 +0200 |
parents | 4be1180a9e89 |
children | 51092a27c943 |
comparison
equal
deleted
inserted
replaced
184:bcd2674570bf | 185:3d6bfa290dbd |
---|---|
216 /* Limit of valid info table indices. */ | 216 /* Limit of valid info table indices. */ |
217 extern __malloc_size_t _heaplimit; | 217 extern __malloc_size_t _heaplimit; |
218 | 218 |
219 /* Doubly linked lists of free fragments. */ | 219 /* Doubly linked lists of free fragments. */ |
220 struct list | 220 struct list |
221 { | 221 { |
222 struct list *next; | 222 struct list *next; |
223 struct list *prev; | 223 struct list *prev; |
224 }; | 224 }; |
225 | 225 |
226 /* Free list headers for each fragment size. */ | 226 /* Free list headers for each fragment size. */ |
227 extern struct list _fraghead[]; | 227 extern struct list _fraghead[]; |
228 | 228 |
229 /* List of blocks allocated with `memalign' (or `valloc'). */ | 229 /* List of blocks allocated with `memalign' (or `valloc'). */ |
230 struct alignlist | 230 struct alignlist |
231 { | 231 { |
232 struct alignlist *next; | 232 struct alignlist *next; |
233 __ptr_t aligned; /* The address that memaligned returned. */ | 233 __ptr_t aligned; /* The address that memaligned returned. */ |
234 __ptr_t exact; /* The address that malloc returned. */ | 234 __ptr_t exact; /* The address that malloc returned. */ |
235 }; | 235 }; |
236 extern struct alignlist *_aligned_blocks; | 236 extern struct alignlist *_aligned_blocks; |
237 | 237 |
238 /* Instrumentation. */ | 238 /* Instrumentation. */ |
239 extern __malloc_size_t _chunks_used; | 239 extern __malloc_size_t _chunks_used; |
240 extern __malloc_size_t _bytes_used; | 240 extern __malloc_size_t _bytes_used; |
266 extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size)); | 266 extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, size_t __size)); |
267 | 267 |
268 /* Return values for `mprobe': these are the kinds of inconsistencies that | 268 /* Return values for `mprobe': these are the kinds of inconsistencies that |
269 `mcheck' enables detection of. */ | 269 `mcheck' enables detection of. */ |
270 enum mcheck_status | 270 enum mcheck_status |
271 { | 271 { |
272 MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */ | 272 MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */ |
273 MCHECK_OK, /* Block is fine. */ | 273 MCHECK_OK, /* Block is fine. */ |
274 MCHECK_FREE, /* Block freed twice. */ | 274 MCHECK_FREE, /* Block freed twice. */ |
275 MCHECK_HEAD, /* Memory before the block was clobbered. */ | 275 MCHECK_HEAD, /* Memory before the block was clobbered. */ |
276 MCHECK_TAIL /* Memory after the block was clobbered. */ | 276 MCHECK_TAIL /* Memory after the block was clobbered. */ |
277 }; | 277 }; |
278 | 278 |
279 /* Activate a standard collection of debugging hooks. This must be called | 279 /* Activate a standard collection of debugging hooks. This must be called |
280 before `malloc' is ever called. ABORTFUNC is called with an error code | 280 before `malloc' is ever called. ABORTFUNC is called with an error code |
281 (see enum above) when an inconsistency is detected. If ABORTFUNC is | 281 (see enum above) when an inconsistency is detected. If ABORTFUNC is |
282 null, the standard function prints on stderr and then calls `abort'. */ | 282 null, the standard function prints on stderr and then calls `abort'. */ |
291 extern void mtrace __P ((void)); | 291 extern void mtrace __P ((void)); |
292 extern void muntrace __P ((void)); | 292 extern void muntrace __P ((void)); |
293 | 293 |
294 /* Statistics available to the user. */ | 294 /* Statistics available to the user. */ |
295 struct mstats | 295 struct mstats |
296 { | 296 { |
297 __malloc_size_t bytes_total; /* Total size of the heap. */ | 297 __malloc_size_t bytes_total; /* Total size of the heap. */ |
298 __malloc_size_t chunks_used; /* Chunks allocated by the user. */ | 298 __malloc_size_t chunks_used; /* Chunks allocated by the user. */ |
299 __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */ | 299 __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */ |
300 __malloc_size_t chunks_free; /* Chunks in the free list. */ | 300 __malloc_size_t chunks_free; /* Chunks in the free list. */ |
301 __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */ | 301 __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */ |
302 }; | 302 }; |
303 | 303 |
304 /* Pick up the current statistics. */ | 304 /* Pick up the current statistics. */ |
305 extern struct mstats mstats __P ((void)); | 305 extern struct mstats mstats __P ((void)); |
306 | 306 |
307 /* Call WARNFUN with a warning message when memory usage is high. */ | 307 /* Call WARNFUN with a warning message when memory usage is high. */ |
365 #endif | 365 #endif |
366 | 366 |
367 static __malloc_size_t pagesize; | 367 static __malloc_size_t pagesize; |
368 | 368 |
369 __ptr_t | 369 __ptr_t |
370 valloc (size) | 370 valloc (__malloc_size_t size) |
371 __malloc_size_t size; | |
372 { | 371 { |
373 if (pagesize == 0) | 372 if (pagesize == 0) |
374 pagesize = __getpagesize (); | 373 pagesize = __getpagesize (); |
375 | 374 |
376 return memalign (pagesize, size); | 375 return memalign (pagesize, size); |
438 void (*__after_morecore_hook) __P ((void)); | 437 void (*__after_morecore_hook) __P ((void)); |
439 | 438 |
440 /* Aligned allocation. */ | 439 /* Aligned allocation. */ |
441 static __ptr_t align __P ((__malloc_size_t)); | 440 static __ptr_t align __P ((__malloc_size_t)); |
442 static __ptr_t | 441 static __ptr_t |
443 align (size) | 442 align (__malloc_size_t size) |
444 __malloc_size_t size; | |
445 { | 443 { |
446 __ptr_t result; | 444 __ptr_t result; |
447 unsigned long int adj; | 445 unsigned long int adj; |
448 | 446 |
449 result = (*__morecore) (size); | 447 result = (*__morecore) (size); |
487 | 485 |
488 /* Get neatly aligned memory, initializing or | 486 /* Get neatly aligned memory, initializing or |
489 growing the heap info table as necessary. */ | 487 growing the heap info table as necessary. */ |
490 static __ptr_t morecore __P ((__malloc_size_t)); | 488 static __ptr_t morecore __P ((__malloc_size_t)); |
491 static __ptr_t | 489 static __ptr_t |
492 morecore (size) | 490 morecore (__malloc_size_t size) |
493 __malloc_size_t size; | |
494 { | 491 { |
495 __ptr_t result; | 492 __ptr_t result; |
496 malloc_info *newinfo, *oldinfo; | 493 malloc_info *newinfo, *oldinfo; |
497 __malloc_size_t newsize; | 494 __malloc_size_t newsize; |
498 | 495 |
531 return result; | 528 return result; |
532 } | 529 } |
533 | 530 |
534 /* Allocate memory from the heap. */ | 531 /* Allocate memory from the heap. */ |
535 __ptr_t | 532 __ptr_t |
536 malloc (size) | 533 malloc (__malloc_size_t size) |
537 __malloc_size_t size; | |
538 { | 534 { |
539 __ptr_t result; | 535 __ptr_t result; |
540 __malloc_size_t block, blocks, lastblocks, start; | 536 __malloc_size_t block, blocks, lastblocks, start; |
541 __malloc_size_t i; | 537 __malloc_size_t i; |
542 struct list *next; | 538 struct list *next; |
779 struct alignlist *_aligned_blocks = NULL; | 775 struct alignlist *_aligned_blocks = NULL; |
780 | 776 |
781 /* Return memory to the heap. | 777 /* Return memory to the heap. |
782 Like `free' but don't call a __free_hook if there is one. */ | 778 Like `free' but don't call a __free_hook if there is one. */ |
783 void | 779 void |
784 _free_internal (ptr) | 780 _free_internal (__ptr_t ptr) |
785 __ptr_t ptr; | |
786 { | 781 { |
787 int type; | 782 int type; |
788 __malloc_size_t block, blocks; | 783 __malloc_size_t block, blocks; |
789 __malloc_size_t i; | 784 __malloc_size_t i; |
790 struct list *prev, *next; | 785 struct list *prev, *next; |
932 } | 927 } |
933 } | 928 } |
934 | 929 |
935 /* Return memory to the heap. */ | 930 /* Return memory to the heap. */ |
936 __free_ret_t | 931 __free_ret_t |
937 free (ptr) | 932 free (__ptr_t ptr) |
938 __ptr_t ptr; | |
939 { | 933 { |
940 struct alignlist *l; | 934 struct alignlist *l; |
941 | 935 |
942 if (ptr == NULL) | 936 if (ptr == NULL) |
943 return; | 937 return; |
1038 #ifndef emacs | 1032 #ifndef emacs |
1039 | 1033 |
1040 /* Like bcopy except never gets confused by overlap. */ | 1034 /* Like bcopy except never gets confused by overlap. */ |
1041 | 1035 |
1042 static void | 1036 static void |
1043 safe_bcopy (from, to, size) | 1037 safe_bcopy (char *from, char *to, int size) |
1044 char *from, *to; | |
1045 int size; | |
1046 { | 1038 { |
1047 if (size <= 0 || from == to) | 1039 if (size <= 0 || from == to) |
1048 return; | 1040 return; |
1049 | 1041 |
1050 /* If the source and destination don't overlap, then bcopy can | 1042 /* If the source and destination don't overlap, then bcopy can |
1088 little left over. The amount left over is | 1080 little left over. The amount left over is |
1089 (endt + (to - from)) - to, which is endt - from. */ | 1081 (endt + (to - from)) - to, which is endt - from. */ |
1090 bcopy (from, to, endt - from); | 1082 bcopy (from, to, endt - from); |
1091 } | 1083 } |
1092 } | 1084 } |
1093 } | 1085 } |
1094 #endif /* Not emacs. */ | 1086 #endif /* Not emacs. */ |
1095 | 1087 |
1096 #define memmove(to, from, size) safe_bcopy ((from), (to), (size)) | 1088 #define memmove(to, from, size) safe_bcopy ((from), (to), (size)) |
1097 | 1089 |
1098 #endif | 1090 #endif |
1112 some benchmarks seem to indicate that greater compactness is | 1104 some benchmarks seem to indicate that greater compactness is |
1113 achieved by unconditionally allocating and copying to a | 1105 achieved by unconditionally allocating and copying to a |
1114 new region. This module has incestuous knowledge of the | 1106 new region. This module has incestuous knowledge of the |
1115 internals of both free and malloc. */ | 1107 internals of both free and malloc. */ |
1116 __ptr_t | 1108 __ptr_t |
1117 realloc (ptr, size) | 1109 realloc (__ptr_t ptr, __malloc_size_t size) |
1118 __ptr_t ptr; | |
1119 __malloc_size_t size; | |
1120 { | 1110 { |
1121 __ptr_t result; | 1111 __ptr_t result; |
1122 int type; | 1112 int type; |
1123 __malloc_size_t block, blocks, oldlimit; | 1113 __malloc_size_t block, blocks, oldlimit; |
1124 | 1114 |
1252 #endif | 1242 #endif |
1253 | 1243 |
1254 /* Allocate an array of NMEMB elements each SIZE bytes long. | 1244 /* Allocate an array of NMEMB elements each SIZE bytes long. |
1255 The entire array is initialized to zeros. */ | 1245 The entire array is initialized to zeros. */ |
1256 __ptr_t | 1246 __ptr_t |
1257 calloc (nmemb, size) | 1247 calloc (__malloc_size_t nmemb, __malloc_size_t size) |
1258 __malloc_size_t nmemb; | |
1259 __malloc_size_t size; | |
1260 { | 1248 { |
1261 __ptr_t result = malloc (nmemb * size); | 1249 __ptr_t result = malloc (nmemb * size); |
1262 | 1250 |
1263 if (result != NULL) | 1251 if (result != NULL) |
1264 (void) memset (result, 0, nmemb * size); | 1252 (void) memset (result, 0, nmemb * size); |
1311 | 1299 |
1312 /* Allocate INCREMENT more bytes of data space, | 1300 /* Allocate INCREMENT more bytes of data space, |
1313 and return the start of data space, or NULL on errors. | 1301 and return the start of data space, or NULL on errors. |
1314 If INCREMENT is negative, shrink data space. */ | 1302 If INCREMENT is negative, shrink data space. */ |
1315 __ptr_t | 1303 __ptr_t |
1316 __default_morecore (increment) | 1304 __default_morecore ( |
1317 #ifdef __STDC__ | 1305 #ifdef __STDC__ |
1318 ptrdiff_t increment; | 1306 ptrdiff_t increment |
1319 #else | 1307 #else |
1320 #ifdef OSF1 | 1308 #ifdef OSF1 |
1321 long increment; | 1309 long increment |
1322 #else | 1310 #else |
1323 int increment; | 1311 int increment |
1324 #endif | 1312 #endif |
1325 #endif | 1313 #endif |
1314 ) | |
1326 { | 1315 { |
1327 #ifdef OSF1 | 1316 #ifdef OSF1 |
1328 __ptr_t result = (__ptr_t) __sbrk ((ssize_t) increment); | 1317 __ptr_t result = (__ptr_t) __sbrk ((ssize_t) increment); |
1329 #else | 1318 #else |
1330 __ptr_t result = (__ptr_t) __sbrk ((int) increment); | 1319 __ptr_t result = (__ptr_t) __sbrk ((int) increment); |
1354 #define _MALLOC_INTERNAL | 1343 #define _MALLOC_INTERNAL |
1355 #include <malloc.h> | 1344 #include <malloc.h> |
1356 #endif | 1345 #endif |
1357 | 1346 |
1358 __ptr_t | 1347 __ptr_t |
1359 memalign (alignment, size) | 1348 memalign (__malloc_size_t alignment, __malloc_size_t size) |
1360 __malloc_size_t alignment; | |
1361 __malloc_size_t size; | |
1362 { | 1349 { |
1363 __ptr_t result; | 1350 __ptr_t result; |
1364 unsigned long int adj; | 1351 unsigned long int adj; |
1365 | 1352 |
1366 size = ((size + alignment - 1) / alignment) * alignment; | 1353 size = ((size + alignment - 1) / alignment) * alignment; |