- Timestamp:
- Dec 16, 2014, 3:43:21 PM (9 years ago)
- Branches:
- (u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
- Children:
- fce947c9e6c3e8c6d5a622c7f6b0d724580993cc
- Parents:
- a2e4470c6e9a666de8ab7b706370c15e13092f76
- Location:
- omalloc
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
omalloc/Makefile.am
ra2e447 r75f460 27 27 EXTRA_DIST = omalloc.c omtTestAlloc.c omtTest.h omMmap.c 28 28 29 AM_CPPFLAGS =-I${top_srcdir}/.. -I${top_builddir}/.. 29 AM_CPPFLAGS =-I${top_srcdir}/.. -I${top_builddir}/.. 30 30 31 31 libomalloc_la_SOURCES=$(SOURCES) $(noinst_HEADERS) -
omalloc/Misc/dlmalloc/Makefile
ra2e447 r75f460 4 4 RM = rm -f 5 5 DISTFILES = COPYRIGHT Makefile malloc-trace.c malloc-trace.h \ 6 print-trace.c trace-test.c 6 print-trace.c trace-test.c 7 7 8 8 .c.o: -
omalloc/Misc/dlmalloc/malloc.c
ra2e447 r75f460 1 1 /* ---------- To make a malloc.h, start cutting here ------------ */ 2 2 3 /* 4 A version of malloc/free/realloc written by Doug Lea and released to the 3 /* 4 A version of malloc/free/realloc written by Doug Lea and released to the 5 5 public domain. Send questions/comments/complaints/performance data 6 6 to dl@cs.oswego.edu 7 7 8 8 * VERSION 2.6.5 Wed Jun 17 15:55:16 1998 Doug Lea (dl at gee) 9 9 10 10 Note: There may be an updated version of this malloc obtainable at 11 11 ftp://g.oswego.edu/pub/misc/malloc.c … … 22 22 most tunable malloc ever written. However it is among the fastest 23 23 while also being among the most space-conserving, portable and tunable. 24 Consistent balance across these factors results in a good general-purpose 25 allocator. For a high-level description, see 24 Consistent balance across these factors results in a good general-purpose 25 allocator. For a high-level description, see 26 26 http://g.oswego.edu/dl/html/malloc.html 27 27 … … 59 59 Equivalent to free(p). 60 60 malloc_trim(size_t pad); 61 Release all but pad bytes of freed top-most memory back 61 Release all but pad bytes of freed top-most memory back 62 62 to the system. Return 1 if successful, else 0. 63 63 malloc_usable_size(Void_t* p); … … 85 85 86 86 Assumed size_t representation: 4 or 8 bytes 87 Note that size_t is allowed to be 4 bytes even if pointers are 8. 87 Note that size_t is allowed to be 4 bytes even if pointers are 8. 88 88 89 89 Minimum overhead per allocated chunk: 4 or 8 bytes 90 90 Each malloced chunk has a hidden overhead of 4 bytes holding size 91 and status information. 91 and status information. 92 92 93 93 Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead) 94 94 8-byte ptrs: 24/32 bytes (including, 4/8 overhead) 95 95 96 96 When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte 97 ptrs but 4 byte size) or 24 (for 8/8) additional bytes are 97 ptrs but 4 byte size) or 24 (for 8/8) additional bytes are 98 98 needed; 4 (8) for a trailing size field 99 99 and 8 (16) bytes for free list pointers. Thus, the minimum … … 110 110 that `size_t' may be defined on a system as either a signed or 111 111 an unsigned type. To be conservative, values that would appear 112 as negative numbers are avoided. 112 as negative numbers are avoided. 113 113 Requests for sizes with a negative sign bit will return a 114 114 minimum-sized chunk. … … 118 118 Alignnment demands, plus the minimum allocatable size restriction 119 119 make the normal worst-case wastage 15 bytes (i.e., up to 15 120 more bytes will be allocated than were requested in malloc), with 120 more bytes will be allocated than were requested in malloc), with 121 121 two exceptions: 122 122 1. Because requests for zero bytes allocate non-zero space, … … 154 154 a C compiler sufficiently close to ANSI to get away with it. 155 155 DEBUG (default: NOT defined) 156 Define to enable debugging. Adds fairly extensive assertion-based 156 Define to enable debugging. Adds fairly extensive assertion-based 157 157 checking to help track down memory errors, but noticeably slows down 158 158 execution. 159 REALLOC_ZERO_BYTES_FREES (default: NOT defined) 159 REALLOC_ZERO_BYTES_FREES (default: NOT defined) 160 160 Define this if you think that realloc(p, 0) should be equivalent 161 161 to free(p). Otherwise, since malloc returns a unique pointer for 162 162 malloc(0), so does realloc(p, 0). 163 163 HAVE_MEMCPY (default: defined) 164 Define if you are not otherwise using ANSI STD C, but still 164 Define if you are not otherwise using ANSI STD C, but still 165 165 have memcpy and memset in your C library and want to use them. 166 166 Otherwise, simple internal versions are supplied. 167 167 USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise) 168 168 Define as 1 if you want the C library versions of memset and 169 memcpy called in realloc and calloc (otherwise macro versions are used). 169 memcpy called in realloc and calloc (otherwise macro versions are used). 170 170 At least on some platforms, the simple macro versions usually 171 171 outperform libc versions. 172 172 HAVE_MMAP (default: defined as 1) 173 173 Define to non-zero to optionally make malloc() use mmap() to 174 allocate very large blocks. 174 allocate very large blocks. 175 175 HAVE_MREMAP (default: defined as 0 unless Linux libc set) 176 176 Define to non-zero to optionally make realloc() use mremap() to 177 reallocate very large blocks. 177 reallocate very large blocks. 178 178 malloc_getpagesize (default: derived from system #includes) 179 179 Either a constant or routine call returning the system page size. 180 HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined) 180 HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined) 181 181 Optionally define if you are on a system with a /usr/include/malloc.h 182 182 that declares struct mallinfo. It is not at all necessary to 183 183 define this even if you do, but will ensure consistency. 184 184 INTERNAL_SIZE_T (default: size_t) 185 Define to a 32-bit type (probably `unsigned int') if you are on a 186 64-bit machine, yet do not want or need to allow malloc requests of 185 Define to a 32-bit type (probably `unsigned int') if you are on a 186 64-bit machine, yet do not want or need to allow malloc requests of 187 187 greater than 2^31 to be handled. This saves space, especially for 188 188 very small chunks. … … 205 205 holds for sbrk). 206 206 DEFAULT_TRIM_THRESHOLD 207 DEFAULT_TOP_PAD 207 DEFAULT_TOP_PAD 208 208 DEFAULT_MMAP_THRESHOLD 209 DEFAULT_MMAP_MAX 209 DEFAULT_MMAP_MAX 210 210 Default values of tunable parameters (described in detail below) 211 211 controlling interaction with host system routines (sbrk, mmap, etc). … … 279 279 cannot be checked very much automatically.) 280 280 281 Setting DEBUG may also be helpful if you are trying to modify 282 this code. The assertions in the check routines spell out in more 281 Setting DEBUG may also be helpful if you are trying to modify 282 this code. The assertions in the check routines spell out in more 283 283 detail the assumptions and invariants underlying the algorithms. 284 284 285 285 */ 286 286 287 #if DEBUG 287 #if DEBUG 288 288 #include <assert.h> 289 289 #else … … 309 309 realloc with zero bytes should be the same as a call to free. 310 310 Some people think it should. Otherwise, since this malloc 311 returns a unique pointer for malloc(0), so does realloc(p, 0). 311 returns a unique pointer for malloc(0), so does realloc(p, 0). 312 312 */ 313 313 … … 316 316 317 317 318 /* 318 /* 319 319 WIN32 causes an emulation of sbrk to be compiled in 320 320 mmap-based options are not currently supported in WIN32. … … 337 337 have memset and memcpy called. People report that the macro 338 338 versions are often enough faster than libc versions on many 339 systems that it is better to use them. 340 341 */ 342 343 #define HAVE_MEMCPY 339 systems that it is better to use them. 340 341 */ 342 343 #define HAVE_MEMCPY 344 344 345 345 #ifndef USE_MEMCPY … … 351 351 #endif 352 352 353 #if (__STD_C || defined(HAVE_MEMCPY)) 353 #if (__STD_C || defined(HAVE_MEMCPY)) 354 354 355 355 #if __STD_C … … 484 484 Access to system page size. To the extent possible, this malloc 485 485 manages memory from the system in page-size units. 486 487 The following mechanics for getpagesize were adapted from 488 bsd/gnu getpagesize.h 486 487 The following mechanics for getpagesize were adapted from 488 bsd/gnu getpagesize.h 489 489 */ 490 490 … … 516 516 # define malloc_getpagesize (NBPG * CLSIZE) 517 517 # endif 518 # else 518 # else 519 519 # ifdef NBPC 520 520 # define malloc_getpagesize NBPC … … 526 526 # endif 527 527 # endif 528 # endif 528 # endif 529 529 # endif 530 # endif 530 # endif 531 531 # endif 532 532 #endif … … 578 578 int fordblks; /* total non-inuse space */ 579 579 int keepcost; /* top-most, releasable (via malloc_trim) space */ 580 }; 580 }; 581 581 582 582 /* SVID2/XPG mallopt options */ … … 603 603 604 604 /* 605 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory 605 M_TRIM_THRESHOLD is the maximum amount of unused top-most memory 606 606 to keep before releasing via malloc_trim in free(). 607 607 … … 611 611 afterward allocate more large chunks) the value should be high 612 612 enough so that your overall system performance would improve by 613 releasing. 613 releasing. 614 614 615 615 The trim threshold and the mmap control parameters (see below) … … 621 621 the XF86 X server on Linux, using a trim threshold of 128K and a 622 622 mmap threshold of 192K led to near-minimal long term resource 623 consumption. 623 consumption. 624 624 625 625 If you are using this malloc in a long-lived program, it should … … 657 657 658 658 /* 659 M_TOP_PAD is the amount of extra `padding' space to allocate or 659 M_TOP_PAD is the amount of extra `padding' space to allocate or 660 660 retain whenever sbrk is called. It is used in two ways internally: 661 661 … … 667 667 it is used as the `pad' argument. 668 668 669 In both cases, the actual amount of padding is rounded 669 In both cases, the actual amount of padding is rounded 670 670 so that the end of the arena is always a system page boundary. 671 671 … … 674 674 that nearly every malloc request during program start-up (or 675 675 after trimming) will invoke sbrk, which needlessly wastes 676 time. 676 time. 677 677 678 678 Automatic rounding-up to page-size units is normally sufficient 679 679 to avoid measurable overhead, so the default is 0. However, in 680 680 systems where sbrk is relatively slow, it can pay to increase 681 this value, at the expense of carrying around more memory than 681 this value, at the expense of carrying around more memory than 682 682 the program needs. 683 683 … … 691 691 /* 692 692 693 M_MMAP_THRESHOLD is the request size threshold for using mmap() 694 to service a request. Requests of at least this size that cannot 695 be allocated using already-existing space will be serviced via mmap. 693 M_MMAP_THRESHOLD is the request size threshold for using mmap() 694 to service a request. Requests of at least this size that cannot 695 be allocated using already-existing space will be serviced via mmap. 696 696 (If enough normal freed space already exists it is used instead.) 697 697 … … 712 712 713 713 1. The space cannot be reclaimed, consolidated, and then 714 used to service later requests, as happens with normal chunks. 714 used to service later requests, as happens with normal chunks. 715 715 2. It can lead to more wastage because of mmap page alignment 716 716 requirements … … 722 722 723 723 All together, these considerations should lead you to use mmap 724 only for relatively large requests. 724 only for relatively large requests. 725 725 726 726 … … 738 738 739 739 /* 740 M_MMAP_MAX is the maximum number of requests to simultaneously 740 M_MMAP_MAX is the maximum number of requests to simultaneously 741 741 service using mmap. This parameter exists because: 742 742 … … 759 759 760 760 761 /* 761 /* 762 762 763 763 Special defines for linux libc … … 791 791 #define MORECORE (*__morecore) 792 792 #define MORECORE_FAILURE 0 793 #define MORECORE_CLEARS 1 793 #define MORECORE_CLEARS 1 794 794 795 795 #else /* INTERNAL_LINUX_C_LIB */ … … 894 894 895 895 896 /* 896 /* 897 897 Emulation of sbrk for WIN32 898 898 All code within the ifdef WIN32 is untested by me. … … 905 905 ~(malloc_getpagesize-1)) 906 906 907 /* resrve 64MB to insure large contiguous space */ 907 /* resrve 64MB to insure large contiguous space */ 908 908 #define RESERVED_SIZE (1024*1024*64) 909 909 #define NEXT_SIZE (2048*1024) … … 913 913 typedef struct GmListElement GmListElement; 914 914 915 struct GmListElement 915 struct GmListElement 916 916 { 917 917 GmListElement* next; … … 945 945 if (gAddressBase && (gNextAddress - gAddressBase)) 946 946 { 947 rval = VirtualFree ((void*)gAddressBase, 948 gNextAddress - gAddressBase, 947 rval = VirtualFree ((void*)gAddressBase, 948 gNextAddress - gAddressBase, 949 949 MEM_DECOMMIT); 950 950 ASSERT (rval); … … 959 959 } 960 960 } 961 961 962 962 static 963 963 void* findRegion (void* start_address, unsigned long size) … … 972 972 return start_address; 973 973 else 974 start_address = (char*)info.BaseAddress + info.RegionSize; 974 start_address = (char*)info.BaseAddress + info.RegionSize; 975 975 } 976 976 return NULL; 977 977 978 978 } 979 979 … … 987 987 { 988 988 gAllocatedSize = max (RESERVED_SIZE, AlignPage (size)); 989 gNextAddress = gAddressBase = 990 (unsigned int)VirtualAlloc (NULL, gAllocatedSize, 989 gNextAddress = gAddressBase = 990 (unsigned int)VirtualAlloc (NULL, gAllocatedSize, 991 991 MEM_RESERVE, PAGE_NOACCESS); 992 992 } else if (AlignPage (gNextAddress + size) > (gAddressBase + … … 995 995 long new_size = max (NEXT_SIZE, AlignPage (size)); 996 996 void* new_address = (void*)(gAddressBase+gAllocatedSize); 997 do 997 do 998 998 { 999 999 new_address = findRegion (new_address, new_size); 1000 1000 1001 1001 if (new_address == 0) 1002 1002 return (void*)-1; … … 1006 1006 MEM_RESERVE, PAGE_NOACCESS); 1007 1007 // repeat in case of race condition 1008 // The region that we found has been snagged 1008 // The region that we found has been snagged 1009 1009 // by another thread 1010 1010 } … … 1022 1022 void* res; 1023 1023 res = VirtualAlloc ((void*)AlignPage (gNextAddress), 1024 (size + gNextAddress - 1025 AlignPage (gNextAddress)), 1024 (size + gNextAddress - 1025 AlignPage (gNextAddress)), 1026 1026 MEM_COMMIT, PAGE_READWRITE); 1027 1027 if (res == 0) … … 1038 1038 if (alignedGoal >= gAddressBase) 1039 1039 { 1040 VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal, 1040 VirtualFree ((void*)alignedGoal, gNextAddress - alignedGoal, 1041 1041 MEM_DECOMMIT); 1042 1042 gNextAddress = gNextAddress + size; 1043 1043 return (void*)gNextAddress; 1044 1044 } 1045 else 1045 else 1046 1046 { 1047 1047 VirtualFree ((void*)gAddressBase, gNextAddress - gAddressBase, … … 1092 1092 in use. 1093 1093 1094 An allocated chunk looks like this: 1094 An allocated chunk looks like this: 1095 1095 1096 1096 … … 1147 1147 deal with alignments etc). 1148 1148 1149 The two exceptions to all this are 1150 1151 1. The special chunk `top', which doesn't bother using the 1149 The two exceptions to all this are 1150 1151 1. The special chunk `top', which doesn't bother using the 1152 1152 trailing size field since there is no 1153 1153 next contiguous chunk that would have to index off it. (After … … 1182 1182 order, which tends to give each chunk an equal opportunity to be 1183 1183 consolidated with adjacent freed chunks, resulting in larger free 1184 chunks and less fragmentation. 1184 chunks and less fragmentation. 1185 1185 1186 1186 * `top': The top-most available chunk (i.e., the one bordering the … … 1193 1193 most recently split (non-top) chunk. This bin is checked 1194 1194 before other non-fitting chunks, so as to provide better 1195 locality for runs of sequentially allocated chunks. 1195 locality for runs of sequentially allocated chunks. 1196 1196 1197 1197 * Implicitly, through the host system's memory mapping tables. 1198 If supported, requests greater than a threshold are usually 1198 If supported, requests greater than a threshold are usually 1199 1199 serviced via calls to mmap, and then later released via munmap. 1200 1200 … … 1234 1234 1235 1235 1236 /* 1237 Physical chunk operations 1236 /* 1237 Physical chunk operations 1238 1238 */ 1239 1239 … … 1241 1241 /* size field is or'ed with PREV_INUSE when previous adjacent chunk in use */ 1242 1242 1243 #define PREV_INUSE 0x1 1243 #define PREV_INUSE 0x1 1244 1244 1245 1245 /* size field is or'ed with IS_MMAPPED if the chunk was obtained with mmap() */ … … 1270 1270 1271 1271 1272 /* 1273 Dealing with use bits 1272 /* 1273 Dealing with use bits 1274 1274 */ 1275 1275 … … 1310 1310 1311 1311 1312 /* 1313 Dealing with size fields 1312 /* 1313 Dealing with size fields 1314 1314 */ 1315 1315 … … 1391 1391 /* 1392 1392 Because top initially points to its own bin with initial 1393 zero size, thus forcing extension on the first malloc request, 1394 we avoid having any special code in malloc to check whether 1393 zero size, thus forcing extension on the first malloc request, 1394 we avoid having any special code in malloc to check whether 1395 1395 it even exists yet. But we still need to in malloc_extend_top. 1396 1396 */ … … 1430 1430 #define last(b) ((b)->bk) 1431 1431 1432 /* 1432 /* 1433 1433 Indexing into bins 1434 1434 */ … … 1441 1441 ((((unsigned long)(sz)) >> 9) <= 340) ? 119 + (((unsigned long)(sz)) >> 15): \ 1442 1442 ((((unsigned long)(sz)) >> 9) <= 1364) ? 124 + (((unsigned long)(sz)) >> 18): \ 1443 126) 1444 /* 1443 126) 1444 /* 1445 1445 bins for chunks < 512 are all spaced 8 bytes apart, and hold 1446 1446 identically sized chunks. This is exploited in malloc. … … 1453 1453 #define smallbin_index(sz) (((unsigned long)(sz)) >> 3) 1454 1454 1455 /* 1455 /* 1456 1456 Requests are `small' if both the corresponding and the next bin are small 1457 1457 */ … … 1500 1500 1501 1501 /* The maximum memory obtained from system via sbrk */ 1502 static unsigned long max_sbrked_mem = 0; 1502 static unsigned long max_sbrked_mem = 0; 1503 1503 1504 1504 /* The maximum via either sbrk or mmap */ 1505 static unsigned long max_total_mem = 0; 1505 static unsigned long max_total_mem = 0; 1506 1506 1507 1507 /* internal working copy of mallinfo */ … … 1521 1521 1522 1522 1523 /* 1524 Debugging support 1523 /* 1524 Debugging support 1525 1525 */ 1526 1526 … … 1537 1537 1538 1538 #if __STD_C 1539 static void do_check_chunk(mchunkptr p) 1539 static void do_check_chunk(mchunkptr p) 1540 1540 #else 1541 1541 static void do_check_chunk(p) mchunkptr p; 1542 1542 #endif 1543 { 1543 { 1544 1544 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; 1545 1545 … … 1549 1549 /* Check for legal address ... */ 1550 1550 assert((char*)p >= sbrk_base); 1551 if (p != top) 1551 if (p != top) 1552 1552 assert((char*)p + sz <= (char*)top); 1553 1553 else … … 1558 1558 1559 1559 #if __STD_C 1560 static void do_check_free_chunk(mchunkptr p) 1560 static void do_check_free_chunk(mchunkptr p) 1561 1561 #else 1562 1562 static void do_check_free_chunk(p) mchunkptr p; 1563 1563 #endif 1564 { 1564 { 1565 1565 INTERNAL_SIZE_T sz = p->size & ~PREV_INUSE; 1566 1566 mchunkptr next = chunk_at_offset(p, sz); … … 1581 1581 assert(prev_inuse(p)); 1582 1582 assert (next == top || inuse(next)); 1583 1583 1584 1584 /* ... and has minimally sane links */ 1585 1585 assert(p->fd->bk == p); … … 1587 1587 } 1588 1588 else /* markers are always of size SIZE_SZ */ 1589 assert(sz == SIZE_SZ); 1589 assert(sz == SIZE_SZ); 1590 1590 } 1591 1591 1592 1592 #if __STD_C 1593 static void do_check_inuse_chunk(mchunkptr p) 1593 static void do_check_inuse_chunk(mchunkptr p) 1594 1594 #else 1595 1595 static void do_check_inuse_chunk(p) mchunkptr p; 1596 1596 #endif 1597 { 1597 { 1598 1598 mchunkptr next = next_chunk(p); 1599 1599 do_check_chunk(p); … … 1606 1606 if an inuse chunk borders them and debug is on, it's worth doing them. 1607 1607 */ 1608 if (!prev_inuse(p)) 1608 if (!prev_inuse(p)) 1609 1609 { 1610 1610 mchunkptr prv = prev_chunk(p); … … 1623 1623 1624 1624 #if __STD_C 1625 static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s) 1625 static void do_check_malloced_chunk(mchunkptr p, INTERNAL_SIZE_T s) 1626 1626 #else 1627 1627 static void do_check_malloced_chunk(p, s) mchunkptr p; INTERNAL_SIZE_T s; … … 1654 1654 #define check_malloced_chunk(P,N) do_check_malloced_chunk(P,N) 1655 1655 #else 1656 #define check_free_chunk(P) 1656 #define check_free_chunk(P) 1657 1657 #define check_inuse_chunk(P) 1658 1658 #define check_chunk(P) … … 1663 1663 1664 1664 1665 /* 1665 /* 1666 1666 Macro-based internal utilities 1667 1667 */ 1668 1668 1669 1669 1670 /* 1670 /* 1671 1671 Linking chunks in bin lists. 1672 1672 Call these only with variables, not arbitrary expressions, as arguments. 1673 1673 */ 1674 1674 1675 /* 1675 /* 1676 1676 Place chunk p of size s in its bin, in size order, 1677 1677 putting it ahead of others of same size. … … 1766 1766 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 1767 1767 #else /* !MAP_ANONYMOUS */ 1768 if (fd < 0) 1768 if (fd < 0) 1769 1769 { 1770 1770 fd = open("/dev/zero", O_RDWR); … … 1778 1778 n_mmaps++; 1779 1779 if (n_mmaps > max_n_mmaps) max_n_mmaps = n_mmaps; 1780 1780 1781 1781 /* We demand that eight bytes into a page must be 8-byte aligned. */ 1782 1782 assert(aligned_OK(chunk2mem(p))); … … 1788 1788 p->prev_size = 0; 1789 1789 set_head(p, size|IS_MMAPPED); 1790 1790 1791 1791 mmapped_mem += size; 1792 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) 1792 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) 1793 1793 max_mmapped_mem = mmapped_mem; 1794 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) 1794 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) 1795 1795 max_total_mem = mmapped_mem + sbrked_mem; 1796 1796 return p; … … 1854 1854 mmapped_mem -= size + offset; 1855 1855 mmapped_mem += new_size; 1856 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) 1856 if ((unsigned long)mmapped_mem > (unsigned long)max_mmapped_mem) 1857 1857 max_mmapped_mem = mmapped_mem; 1858 1858 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) … … 1869 1869 1870 1870 1871 /* 1871 /* 1872 1872 Extend the top-most chunk by obtaining memory from system. 1873 1873 Main interface to sbrk (but see also malloc_trim). … … 1891 1891 1892 1892 /* Pad request with top_pad plus minimal overhead */ 1893 1893 1894 1894 INTERNAL_SIZE_T sbrk_size = nb + top_pad + MINSIZE; 1895 1895 unsigned long pagesz = malloc_getpagesize; … … 1905 1905 1906 1906 /* Fail if sbrk failed or if a foreign sbrk call killed our space */ 1907 if (brk == (char*)(MORECORE_FAILURE) || 1907 if (brk == (char*)(MORECORE_FAILURE) || 1908 1908 (brk < old_end && old_top != initial_top)) 1909 return; 1909 return; 1910 1910 1911 1911 sbrked_mem += sbrk_size; … … 1925 1925 /* Guarantee alignment of first new chunk made from this space */ 1926 1926 front_misalign = (unsigned long)chunk2mem(brk) & MALLOC_ALIGN_MASK; 1927 if (front_misalign > 0) 1927 if (front_misalign > 0) 1928 1928 { 1929 1929 correction = (MALLOC_ALIGNMENT) - front_misalign; … … 1938 1938 /* Allocate correction */ 1939 1939 new_brk = (char*)(MORECORE (correction)); 1940 if (new_brk == (char*)(MORECORE_FAILURE)) return; 1940 if (new_brk == (char*)(MORECORE_FAILURE)) return; 1941 1941 1942 1942 sbrked_mem += correction; … … 1953 1953 1954 1954 /* If not enough space to do this, then user did something very wrong */ 1955 if (old_top_size < MINSIZE) 1955 if (old_top_size < MINSIZE) 1956 1956 { 1957 1957 set_head(top, PREV_INUSE); /* will force null return from malloc */ … … 1967 1967 SIZE_SZ|PREV_INUSE; 1968 1968 /* If possible, release the rest. */ 1969 if (old_top_size >= MINSIZE) 1969 if (old_top_size >= MINSIZE) 1970 1970 fREe(chunk2mem(old_top)); 1971 1971 } 1972 1972 } 1973 1973 1974 if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem) 1974 if ((unsigned long)sbrked_mem > (unsigned long)max_sbrked_mem) 1975 1975 max_sbrked_mem = sbrked_mem; 1976 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) 1976 if ((unsigned long)(mmapped_mem + sbrked_mem) > (unsigned long)max_total_mem) 1977 1977 max_total_mem = mmapped_mem + sbrked_mem; 1978 1978 … … 2072 2072 if (is_small_request(nb)) /* Faster version for small requests */ 2073 2073 { 2074 idx = smallbin_index(nb); 2074 idx = smallbin_index(nb); 2075 2075 2076 2076 /* No traversal or size check necessary for small bins. */ … … 2106 2106 victim_size = chunksize(victim); 2107 2107 remainder_size = victim_size - nb; 2108 2108 2109 2109 if (remainder_size >= (long)MINSIZE) /* too big */ 2110 2110 { 2111 2111 --idx; /* adjust to rescan below after checking last remainder */ 2112 break; 2112 break; 2113 2113 } 2114 2114 … … 2122 2122 } 2123 2123 2124 ++idx; 2124 ++idx; 2125 2125 2126 2126 } … … 2158 2158 } 2159 2159 2160 /* 2161 If there are any possibly nonempty big-enough blocks, 2160 /* 2161 If there are any possibly nonempty big-enough blocks, 2162 2162 search for best fitting chunk by scanning bins in blockwidth units. 2163 2163 */ 2164 2164 2165 if ( (block = idx2binblock(idx)) <= binblocks) 2165 if ( (block = idx2binblock(idx)) <= binblocks) 2166 2166 { 2167 2167 2168 2168 /* Get to the first marked block */ 2169 2169 2170 if ( (block & binblocks) == 0) 2170 if ( (block & binblocks) == 0) 2171 2171 { 2172 2172 /* force to an even block boundary */ … … 2179 2179 } 2180 2180 } 2181 2181 2182 2182 /* For each possibly nonempty block ... */ 2183 for (;;) 2183 for (;;) 2184 2184 { 2185 2185 startidx = idx; /* (track incomplete blocks) */ … … 2237 2237 /* Get to the next possibly nonempty block */ 2238 2238 2239 if ( (block <<= 1) <= binblocks && (block != 0) ) 2239 if ( (block <<= 1) <= binblocks && (block != 0) ) 2240 2240 { 2241 2241 while ((block & binblocks) == 0) … … 2289 2289 cases: 2290 2290 2291 1. free(0) has no effect. 2291 1. free(0) has no effect. 2292 2292 2293 2293 2. If the chunk was allocated via mmap, it is release via munmap(). … … 2335 2335 } 2336 2336 #endif 2337 2337 2338 2338 check_inuse_chunk(p); 2339 2339 2340 2340 sz = hd & ~PREV_INUSE; 2341 2341 next = chunk_at_offset(p, sz); 2342 2342 nextsz = chunksize(next); 2343 2343 2344 2344 if (next == top) /* merge with top */ 2345 2345 { … … 2356 2356 set_head(p, sz | PREV_INUSE); 2357 2357 top = p; 2358 if ((unsigned long)(sz) >= (unsigned long)trim_threshold) 2359 malloc_trim(top_pad); 2358 if ((unsigned long)(sz) >= (unsigned long)trim_threshold) 2359 malloc_trim(top_pad); 2360 2360 return; 2361 2361 } … … 2370 2370 p = chunk_at_offset(p, -prevsz); 2371 2371 sz += prevsz; 2372 2372 2373 2373 if (p->fd == last_remainder) /* keep as last_remainder */ 2374 2374 islr = 1; … … 2376 2376 unlink(p, bck, fwd); 2377 2377 } 2378 2378 2379 2379 if (!(inuse_bit_at_offset(next, nextsz))) /* consolidate forward */ 2380 2380 { 2381 2381 sz += nextsz; 2382 2382 2383 2383 if (!islr && next->fd == last_remainder) /* re-insert last_remainder */ 2384 2384 { 2385 2385 islr = 1; 2386 link_last_remainder(p); 2386 link_last_remainder(p); 2387 2387 } 2388 2388 else … … 2394 2394 set_foot(p, sz); 2395 2395 if (!islr) 2396 frontlink(p, sz, idx, bck, fwd); 2396 frontlink(p, sz, idx, bck, fwd); 2397 2397 } 2398 2398 … … 2431 2431 to be used as an argument to realloc is no longer supported. 2432 2432 I don't know of any programs still relying on this feature, 2433 and allowing it would also allow too many other incorrect 2433 and allowing it would also allow too many other incorrect 2434 2434 usages of realloc to be sensible. 2435 2435 … … 2480 2480 2481 2481 #if HAVE_MMAP 2482 if (chunk_is_mmapped(oldp)) 2482 if (chunk_is_mmapped(oldp)) 2483 2483 { 2484 2484 #if HAVE_MREMAP … … 2499 2499 check_inuse_chunk(oldp); 2500 2500 2501 if ((long)(oldsize) < (long)(nb)) 2501 if ((long)(oldsize) < (long)(nb)) 2502 2502 { 2503 2503 … … 2505 2505 2506 2506 next = chunk_at_offset(oldp, oldsize); 2507 if (next == top || !inuse(next)) 2507 if (next == top || !inuse(next)) 2508 2508 { 2509 2509 nextsize = chunksize(next); … … 2524 2524 /* Forward into next chunk */ 2525 2525 else if (((long)(nextsize + newsize) >= (long)(nb))) 2526 { 2526 { 2527 2527 unlink(next, bck, fwd); 2528 2528 newsize += nextsize; … … 2576 2576 } 2577 2577 } 2578 2578 2579 2579 /* backward only */ 2580 if (prev != 0 && (long)(prevsize + newsize) >= (long)nb) 2580 if (prev != 0 && (long)(prevsize + newsize) >= (long)nb) 2581 2581 { 2582 2582 unlink(prev, bck, fwd); … … 2594 2594 2595 2595 if (newmem == 0) /* propagate failure */ 2596 return 0; 2596 return 0; 2597 2597 2598 2598 /* Avoid copy if newp is next chunk after oldp. */ 2599 2599 /* (This can only happen when new chunk is sbrk'ed.) */ 2600 2600 2601 if ( (newp = mem2chunk(newmem)) == next_chunk(oldp)) 2601 if ( (newp = mem2chunk(newmem)) == next_chunk(oldp)) 2602 2602 { 2603 2603 newsize += chunksize(newp); … … 2644 2644 memalign requests more than enough space from malloc, finds a spot 2645 2645 within that chunk that meets the alignment request, and then 2646 possibly frees the leading and trailing space. 2646 possibly frees the leading and trailing space. 2647 2647 2648 2648 The alignment argument must be a power of two. This property is not … … 2678 2678 2679 2679 /* Otherwise, ensure that it is at least a minimum chunk size */ 2680 2680 2681 2681 if (alignment < MINSIZE) alignment = MINSIZE; 2682 2682 … … 2699 2699 else /* misaligned */ 2700 2700 { 2701 /* 2701 /* 2702 2702 Find an aligned spot inside chunk. 2703 Since we need to give back leading space in a chunk of at 2703 Since we need to give back leading space in a chunk of at 2704 2704 least MINSIZE, if the first calculation places us at 2705 2705 a spot with less than MINSIZE leader, we can move to the … … 2716 2716 2717 2717 #if HAVE_MMAP 2718 if(chunk_is_mmapped(p)) 2718 if(chunk_is_mmapped(p)) 2719 2719 { 2720 2720 newp->prev_size = p->prev_size + leadsize; … … 2771 2771 } 2772 2772 2773 /* 2773 /* 2774 2774 pvalloc just invokes valloc for the nearest pagesize 2775 2775 that will accommodate request … … 2811 2811 Void_t* mem = mALLOc (sz); 2812 2812 2813 if (mem == 0) 2813 if (mem == 0) 2814 2814 return 0; 2815 2815 else … … 2827 2827 2828 2828 #if MORECORE_CLEARS 2829 if (p == oldtop && csz > oldtopsize) 2829 if (p == oldtop && csz > oldtopsize) 2830 2830 { 2831 2831 /* clear only the bytes from non-freshly-sbrked memory */ … … 2840 2840 2841 2841 /* 2842 2842 2843 2843 cfree just calls free. It is needed/defined on some systems 2844 2844 that pair it with calloc, presumably for odd historical reasons. … … 2912 2912 { 2913 2913 new_brk = (char*)(MORECORE (-extra)); 2914 2914 2915 2915 if (new_brk == (char*)(MORECORE_FAILURE)) /* sbrk failed? */ 2916 2916 { … … 2924 2924 } 2925 2925 check_chunk(top); 2926 return 0; 2926 return 0; 2927 2927 } 2928 2928 … … 2981 2981 /* Utility to update current_mallinfo for malloc_stats and mallinfo() */ 2982 2982 2983 static void malloc_update_mallinfo() 2983 static void malloc_update_mallinfo() 2984 2984 { 2985 2985 int i; … … 2996 2996 { 2997 2997 b = bin_at(i); 2998 for (p = last(b); p != b; p = p->bk) 2998 for (p = last(b); p != b; p = p->bk) 2999 2999 { 3000 3000 #if DEBUG 3001 3001 check_free_chunk(p); 3002 for (q = next_chunk(p); 3003 q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE; 3002 for (q = next_chunk(p); 3003 q < top && inuse(q) && (long)(chunksize(q)) >= (long)MINSIZE; 3004 3004 q = next_chunk(q)) 3005 3005 check_inuse_chunk(q); … … 3040 3040 { 3041 3041 malloc_update_mallinfo(); 3042 fprintf(stderr, "max system bytes = %10u\n", 3042 fprintf(stderr, "max system bytes = %10u\n", 3043 3043 (unsigned int)(max_total_mem)); 3044 fprintf(stderr, "system bytes = %10u\n", 3044 fprintf(stderr, "system bytes = %10u\n", 3045 3045 (unsigned int)(sbrked_mem + mmapped_mem)); 3046 fprintf(stderr, "in use bytes = %10u\n", 3046 fprintf(stderr, "in use bytes = %10u\n", 3047 3047 (unsigned int)(current_mallinfo.uordblks + mmapped_mem)); 3048 3048 #if HAVE_MMAP 3049 fprintf(stderr, "max mmap regions = %10u\n", 3049 fprintf(stderr, "max mmap regions = %10u\n", 3050 3050 (unsigned int)max_n_mmaps); 3051 3051 #endif … … 3085 3085 #endif 3086 3086 { 3087 switch(param_number) 3087 switch(param_number) 3088 3088 { 3089 3089 case M_TRIM_THRESHOLD: 3090 trim_threshold = value; return 1; 3090 trim_threshold = value; return 1; 3091 3091 case M_TOP_PAD: 3092 top_pad = value; return 1; 3092 top_pad = value; return 1; 3093 3093 case M_MMAP_THRESHOLD: 3094 3094 mmap_threshold = value; return 1; … … 3120 3120 foreign sbrks 3121 3121 * Add linux mremap support code from HJ Liu 3122 3122 3123 3123 V2.6.2 Tue Dec 5 06:52:55 1995 Doug Lea (dl at gee) 3124 3124 * Integrated most documentation with the code. 3125 * Add support for mmap, with help from 3125 * Add support for mmap, with help from 3126 3126 Wolfram Gloger (Gloger@lrz.uni-muenchen.de). 3127 3127 * Use last_remainder in more cases. … … 3130 3130 * Eliminate block-local decls to simplify tracing and debugging. 3131 3131 * Support another case of realloc via move into top 3132 * Fix error occuring when initial sbrk_base not word-aligned. 3132 * Fix error occuring when initial sbrk_base not word-aligned. 3133 3133 * Rely on page size for units instead of SBRK_UNIT to 3134 3134 avoid surprises about sbrk alignment conventions. 3135 3135 * Add mallinfo, mallopt. Thanks to Raymond Nijssen 3136 (raymond@es.ele.tue.nl) for the suggestion. 3136 (raymond@es.ele.tue.nl) for the suggestion. 3137 3137 * Add `pad' argument to malloc_trim and top_pad mallopt parameter. 3138 3138 * More precautions for cases where other routines call sbrk, … … 3158 3158 3159 3159 V2.5.4 Wed Nov 1 07:54:51 1995 Doug Lea (dl at gee) 3160 * Added malloc_trim, with help from Wolfram Gloger 3160 * Added malloc_trim, with help from Wolfram Gloger 3161 3161 (wmglo@Dent.MED.Uni-Muenchen.DE). 3162 3162 … … 3178 3178 * Scan 2 returns chunks (not just 1) 3179 3179 * Propagate failure in realloc if malloc returns 0 3180 * Add stuff to allow compilation on non-ANSI compilers 3180 * Add stuff to allow compilation on non-ANSI compilers 3181 3181 from kpv@research.att.com 3182 3182 3183 3183 V2.5 Sat Aug 7 07:41:59 1993 Doug Lea (dl at g.oswego.edu) 3184 3184 * removed potential for odd address access in prev_chunk … … 3186 3186 * misc cosmetics and a bit more internal documentation 3187 3187 * anticosmetics: mangled names in macros to evade debugger strangeness 3188 * tested on sparc, hp-700, dec-mips, rs6000 3188 * tested on sparc, hp-700, dec-mips, rs6000 3189 3189 with gcc & native cc (hp, dec only) allowing 3190 3190 Detlefs & Zorn comparison study (in SIGPLAN Notices.) 3191 3191 3192 3192 Trial version Fri Aug 28 13:14:29 1992 Doug Lea (dl at g.oswego.edu) 3193 * Based loosely on libg++-1.2X malloc. (It retains some of the overall 3193 * Based loosely on libg++-1.2X malloc. (It retains some of the overall 3194 3194 structure of old version, but most details differ.) 3195 3195 -
omalloc/Misc/dlmalloc/trace-test.c
ra2e447 r75f460 75 75 76 76 unsigned long reqs[32] = { 77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 77 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 78 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 79 79 }; 80 80 … … 95 95 printf("# bin N\n"); 96 96 for (i = 0; i < 32; ++i) { 97 if (reqs[i] != 0) 97 if (reqs[i] != 0) 98 98 printf(" %10ld%10ld\n", b, reqs[i]); 99 99 b <<= 1; -
omalloc/configure.ac
ra2e447 r75f460 307 307 { 308 308 void* addr = OM_MALLOC_MALLOC(512); 309 #ifdef OM_MALLOC_SIZEOF_ADDR 309 #ifdef OM_MALLOC_SIZEOF_ADDR 310 310 if (OM_MALLOC_SIZEOF_ADDR(addr) < 512) 311 311 exit(1); -
omalloc/omDebugTrack.c
ra2e447 r75f460 676 676 omTrackAddr d_addr = (omTrackAddr) addr; 677 677 if (!omCheckPtr(addr, omError_MaxError, OM_FLR)) 678 { 678 { 679 679 omAssume(omIsTrackAddr(addr) && omOutAddr_2_TrackAddr(addr) == d_addr); 680 680 d_addr->flags |= OM_FSTATIC; 681 } 681 } 682 682 } 683 683 -
omalloc/omGetBackTrace.c
ra2e447 r75f460 50 50 OM_GET_BACK_TRACE(2) 51 51 /* the following fails on Mac OsX, but the debugging 52 * support it provides is too useful to disable it 52 * support it provides is too useful to disable it 53 53 */ 54 54 #ifdef __linux … … 68 68 OM_GET_BACK_TRACE(15) 69 69 OM_GET_BACK_TRACE(16) 70 OM_GET_BACK_TRACE(17) 70 OM_GET_BACK_TRACE(17) 71 71 #endif 72 72 #endif -
omalloc/omRet2Info.c
ra2e447 r75f460 39 39 { 40 40 strncpy(om_this_prog, argv0, MAXPATHLEN); // // buf); 41 om_this_prog[MAXPATHLEN - 1]= '\0'; 41 om_this_prog[MAXPATHLEN - 1]= '\0'; 42 42 } 43 43 } -
omalloc/omalloc.dox
ra2e447 r75f460 6 6 handle the allocation and de-allocation of memory blocks of small 7 7 size as efficient as it is possible (a few machine instructions in most cases). 8 9 Short introduction to omalloc 8 9 Short introduction to omalloc 10 10 <A HREF="http://www.mathematik.uni-kl.de/~motsak/talks/SICSA2011_Motsak_omalloc.pdf">short talk</A>. 11 11 For more details see 12 12 <A HREF="ftp://www.mathematik.uni-kl.de/pub/Math/Singular/doc/OMALLOC.ps.gz">Detailed manual (OMALLOC.ps.gz)</A>. 13 13 14 Note that this package has no further dependencies, while it is used by the rest of Singular packages 15 - \ref factory_page 14 Note that this package has no further dependencies, while it is used by the rest of Singular packages 15 - \ref factory_page 16 16 - \ref libpolys_page 17 17 - \ref kernel_page 18 18 - \ref singular_page 19 19 20 20 */ -
omalloc/omalloc.pc.in
ra2e447 r75f460 10 10 11 11 # Requires: 12 # Conflicts: 12 # Conflicts: 13 13 14 14 Cflags: -I${includedir} -DOM_NDEBUG 15 15 Libs: -L${libdir} -l@PACKAGE@ 16 # Libs.private: 16 # Libs.private: 17 17
Note: See TracChangeset
for help on using the changeset viewer.