malloc.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /*
  2. A version of malloc/free/realloc written by Doug Lea and released to the
  3. public domain. Send questions/comments/complaints/performance data
  4. to dl@cs.oswego.edu
  5. * VERSION 2.6.6 Sun Mar 5 19:10:03 2000 Doug Lea (dl at gee)
  6. Note: There may be an updated version of this malloc obtainable at
  7. ftp://g.oswego.edu/pub/misc/malloc.c
  8. Check before installing!
  9. * Why use this malloc?
  10. This is not the fastest, most space-conserving, most portable, or
  11. most tunable malloc ever written. However it is among the fastest
  12. while also being among the most space-conserving, portable and tunable.
  13. Consistent balance across these factors results in a good general-purpose
  14. allocator. For a high-level description, see
  15. http://g.oswego.edu/dl/html/malloc.html
  16. * Synopsis of public routines
  17. (Much fuller descriptions are contained in the program documentation below.)
  18. malloc(size_t n);
  19. Return a pointer to a newly allocated chunk of at least n bytes, or null
  20. if no space is available.
  21. free(Void_t* p);
  22. Release the chunk of memory pointed to by p, or no effect if p is null.
  23. realloc(Void_t* p, size_t n);
  24. Return a pointer to a chunk of size n that contains the same data
  25. as does chunk p up to the minimum of (n, p's size) bytes, or null
  26. if no space is available. The returned pointer may or may not be
  27. the same as p. If p is null, equivalent to malloc. Unless the
  28. #define REALLOC_ZERO_BYTES_FREES below is set, realloc with a
  29. size argument of zero (re)allocates a minimum-sized chunk.
  30. memalign(size_t alignment, size_t n);
  31. Return a pointer to a newly allocated chunk of n bytes, aligned
  32. in accord with the alignment argument, which must be a power of
  33. two.
  34. valloc(size_t n);
  35. Equivalent to memalign(pagesize, n), where pagesize is the page
  36. size of the system (or as near to this as can be figured out from
  37. all the includes/defines below.)
  38. pvalloc(size_t n);
  39. Equivalent to valloc(minimum-page-that-holds(n)), that is,
  40. round up n to nearest pagesize.
  41. calloc(size_t unit, size_t quantity);
  42. Returns a pointer to quantity * unit bytes, with all locations
  43. set to zero.
  44. cfree(Void_t* p);
  45. Equivalent to free(p).
  46. malloc_trim(size_t pad);
  47. Release all but pad bytes of freed top-most memory back
  48. to the system. Return 1 if successful, else 0.
  49. malloc_usable_size(Void_t* p);
  50. Report the number usable allocated bytes associated with allocated
  51. chunk p. This may or may not report more bytes than were requested,
  52. due to alignment and minimum size constraints.
  53. malloc_stats();
  54. Prints brief summary statistics on stderr.
  55. mallinfo()
  56. Returns (by copy) a struct containing various summary statistics.
  57. mallopt(int parameter_number, int parameter_value)
  58. Changes one of the tunable parameters described below. Returns
  59. 1 if successful in changing the parameter, else 0.
  60. * Vital statistics:
  61. Alignment: 8-byte
  62. 8 byte alignment is currently hardwired into the design. This
  63. seems to suffice for all current machines and C compilers.
  64. Assumed pointer representation: 4 or 8 bytes
  65. Code for 8-byte pointers is untested by me but has worked
  66. reliably by Wolfram Gloger, who contributed most of the
  67. changes supporting this.
  68. Assumed size_t representation: 4 or 8 bytes
  69. Note that size_t is allowed to be 4 bytes even if pointers are 8.
  70. Minimum overhead per allocated chunk: 4 or 8 bytes
  71. Each malloced chunk has a hidden overhead of 4 bytes holding size
  72. and status information.
  73. Minimum allocated size: 4-byte ptrs: 16 bytes (including 4 overhead)
  74. 8-byte ptrs: 24/32 bytes (including, 4/8 overhead)
  75. When a chunk is freed, 12 (for 4byte ptrs) or 20 (for 8 byte
  76. ptrs but 4 byte size) or 24 (for 8/8) additional bytes are
  77. needed; 4 (8) for a trailing size field
  78. and 8 (16) bytes for free list pointers. Thus, the minimum
  79. allocatable size is 16/24/32 bytes.
  80. Even a request for zero bytes (i.e., malloc(0)) returns a
  81. pointer to something of the minimum allocatable size.
  82. Maximum allocated size: 4-byte size_t: 2^31 - 8 bytes
  83. 8-byte size_t: 2^63 - 16 bytes
  84. It is assumed that (possibly signed) size_t bit values suffice to
  85. represent chunk sizes. `Possibly signed' is due to the fact
  86. that `size_t' may be defined on a system as either a signed or
  87. an unsigned type. To be conservative, values that would appear
  88. as negative numbers are avoided.
  89. Requests for sizes with a negative sign bit when the request
  90. size is treaded as a long will return null.
  91. Maximum overhead wastage per allocated chunk: normally 15 bytes
  92. Alignnment demands, plus the minimum allocatable size restriction
  93. make the normal worst-case wastage 15 bytes (i.e., up to 15
  94. more bytes will be allocated than were requested in malloc), with
  95. two exceptions:
  96. 1. Because requests for zero bytes allocate non-zero space,
  97. the worst case wastage for a request of zero bytes is 24 bytes.
  98. 2. For requests >= mmap_threshold that are serviced via
  99. mmap(), the worst case wastage is 8 bytes plus the remainder
  100. from a system page (the minimal mmap unit); typically 4096 bytes.
  101. * Limitations
  102. Here are some features that are NOT currently supported
  103. * No user-definable hooks for callbacks and the like.
  104. * No automated mechanism for fully checking that all accesses
  105. to malloced memory stay within their bounds.
  106. * No support for compaction.
  107. * Synopsis of compile-time options:
  108. People have reported using previous versions of this malloc on all
  109. versions of Unix, sometimes by tweaking some of the defines
  110. below. It has been tested most extensively on Solaris and
  111. Linux. It is also reported to work on WIN32 platforms.
  112. People have also reported adapting this malloc for use in
  113. stand-alone embedded systems.
  114. The implementation is in straight, hand-tuned ANSI C. Among other
  115. consequences, it uses a lot of macros. Because of this, to be at
  116. all usable, this code should be compiled using an optimizing compiler
  117. (for example gcc -O2) that can simplify expressions and control
  118. paths.
  119. __STD_C (default: derived from C compiler defines)
  120. Nonzero if using ANSI-standard C compiler, a C++ compiler, or
  121. a C compiler sufficiently close to ANSI to get away with it.
  122. DEBUG (default: NOT defined)
  123. Define to enable debugging. Adds fairly extensive assertion-based
  124. checking to help track down memory errors, but noticeably slows down
  125. execution.
  126. REALLOC_ZERO_BYTES_FREES (default: NOT defined)
  127. Define this if you think that realloc(p, 0) should be equivalent
  128. to free(p). Otherwise, since malloc returns a unique pointer for
  129. malloc(0), so does realloc(p, 0).
  130. HAVE_MEMCPY (default: defined)
  131. Define if you are not otherwise using ANSI STD C, but still
  132. have memcpy and memset in your C library and want to use them.
  133. Otherwise, simple internal versions are supplied.
  134. USE_MEMCPY (default: 1 if HAVE_MEMCPY is defined, 0 otherwise)
  135. Define as 1 if you want the C library versions of memset and
  136. memcpy called in realloc and calloc (otherwise macro versions are used).
  137. At least on some platforms, the simple macro versions usually
  138. outperform libc versions.
  139. HAVE_MMAP (default: defined as 1)
  140. Define to non-zero to optionally make malloc() use mmap() to
  141. allocate very large blocks.
  142. HAVE_MREMAP (default: defined as 0 unless Linux libc set)
  143. Define to non-zero to optionally make realloc() use mremap() to
  144. reallocate very large blocks.
  145. malloc_getpagesize (default: derived from system #includes)
  146. Either a constant or routine call returning the system page size.
  147. HAVE_USR_INCLUDE_MALLOC_H (default: NOT defined)
  148. Optionally define if you are on a system with a /usr/include/malloc.h
  149. that declares struct mallinfo. It is not at all necessary to
  150. define this even if you do, but will ensure consistency.
  151. INTERNAL_SIZE_T (default: size_t)
  152. Define to a 32-bit type (probably `unsigned int') if you are on a
  153. 64-bit machine, yet do not want or need to allow malloc requests of
  154. greater than 2^31 to be handled. This saves space, especially for
  155. very small chunks.
  156. INTERNAL_LINUX_C_LIB (default: NOT defined)
  157. Defined only when compiled as part of Linux libc.
  158. Also note that there is some odd internal name-mangling via defines
  159. (for example, internally, `malloc' is named `mALLOc') needed
  160. when compiling in this case. These look funny but don't otherwise
  161. affect anything.
  162. WIN32 (default: undefined)
  163. Define this on MS win (95, nt) platforms to compile in sbrk emulation.
  164. LACKS_UNISTD_H (default: undefined if not WIN32)
  165. Define this if your system does not have a <unistd.h>.
  166. LACKS_SYS_PARAM_H (default: undefined if not WIN32)
  167. Define this if your system does not have a <sys/param.h>.
  168. MORECORE (default: sbrk)
  169. The name of the routine to call to obtain more memory from the system.
  170. MORECORE_FAILURE (default: -1)
  171. The value returned upon failure of MORECORE.
  172. MORECORE_CLEARS (default 1)
  173. True (1) if the routine mapped to MORECORE zeroes out memory (which
  174. holds for sbrk).
  175. DEFAULT_TRIM_THRESHOLD
  176. DEFAULT_TOP_PAD
  177. DEFAULT_MMAP_THRESHOLD
  178. DEFAULT_MMAP_MAX
  179. Default values of tunable parameters (described in detail below)
  180. controlling interaction with host system routines (sbrk, mmap, etc).
  181. These values may also be changed dynamically via mallopt(). The
  182. preset defaults are those that give best performance for typical
  183. programs/systems.
  184. USE_DL_PREFIX (default: undefined)
  185. Prefix all public routines with the string 'dl'. Useful to
  186. quickly avoid procedure declaration conflicts and linker symbol
  187. conflicts with existing memory allocation routines.
  188. */
  189. #ifndef __MALLOC_H__
  190. #define __MALLOC_H__
  191. /* Preliminaries */
  192. #ifndef __STD_C
  193. #ifdef __STDC__
  194. #define __STD_C 1
  195. #else
  196. #if __cplusplus
  197. #define __STD_C 1
  198. #else
  199. #define __STD_C 0
  200. #endif /*__cplusplus*/
  201. #endif /*__STDC__*/
  202. #endif /*__STD_C*/
  203. #ifndef Void_t
  204. #if (__STD_C || defined(WIN32))
  205. #define Void_t void
  206. #else
  207. #define Void_t char
  208. #endif
  209. #endif /*Void_t*/
  210. #if __STD_C
  211. #include <linux/stddef.h> /* for size_t */
  212. #else
  213. #include <sys/types.h>
  214. #endif /* __STD_C */
  215. #ifdef __cplusplus
  216. extern "C" {
  217. #endif
  218. #if 0 /* not for U-Boot */
  219. #include <stdio.h> /* needed for malloc_stats */
  220. #endif
  221. /*
  222. Compile-time options
  223. */
  224. /*
  225. Debugging:
  226. Because freed chunks may be overwritten with link fields, this
  227. malloc will often die when freed memory is overwritten by user
  228. programs. This can be very effective (albeit in an annoying way)
  229. in helping track down dangling pointers.
  230. If you compile with -DDEBUG, a number of assertion checks are
  231. enabled that will catch more memory errors. You probably won't be
  232. able to make much sense of the actual assertion errors, but they
  233. should help you locate incorrectly overwritten memory. The
  234. checking is fairly extensive, and will slow down execution
  235. noticeably. Calling malloc_stats or mallinfo with DEBUG set will
  236. attempt to check every non-mmapped allocated and free chunk in the
  237. course of computing the summmaries. (By nature, mmapped regions
  238. cannot be checked very much automatically.)
  239. Setting DEBUG may also be helpful if you are trying to modify
  240. this code. The assertions in the check routines spell out in more
  241. detail the assumptions and invariants underlying the algorithms.
  242. */
  243. #ifdef DEBUG
  244. /* #include <assert.h> */
  245. #define assert(x) ((void)0)
  246. #else
  247. #define assert(x) ((void)0)
  248. #endif
  249. /*
  250. INTERNAL_SIZE_T is the word-size used for internal bookkeeping
  251. of chunk sizes. On a 64-bit machine, you can reduce malloc
  252. overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int'
  253. at the expense of not being able to handle requests greater than
  254. 2^31. This limitation is hardly ever a concern; you are encouraged
  255. to set this. However, the default version is the same as size_t.
  256. */
  257. #ifndef INTERNAL_SIZE_T
  258. #define INTERNAL_SIZE_T size_t
  259. #endif
  260. /*
  261. REALLOC_ZERO_BYTES_FREES should be set if a call to
  262. realloc with zero bytes should be the same as a call to free.
  263. Some people think it should. Otherwise, since this malloc
  264. returns a unique pointer for malloc(0), so does realloc(p, 0).
  265. */
  266. /* #define REALLOC_ZERO_BYTES_FREES */
  267. /*
  268. WIN32 causes an emulation of sbrk to be compiled in
  269. mmap-based options are not currently supported in WIN32.
  270. */
  271. /* #define WIN32 */
  272. #ifdef WIN32
  273. #define MORECORE wsbrk
  274. #define HAVE_MMAP 0
  275. #define LACKS_UNISTD_H
  276. #define LACKS_SYS_PARAM_H
  277. /*
  278. Include 'windows.h' to get the necessary declarations for the
  279. Microsoft Visual C++ data structures and routines used in the 'sbrk'
  280. emulation.
  281. Define WIN32_LEAN_AND_MEAN so that only the essential Microsoft
  282. Visual C++ header files are included.
  283. */
  284. #define WIN32_LEAN_AND_MEAN
  285. #include <windows.h>
  286. #endif
  287. /*
  288. HAVE_MEMCPY should be defined if you are not otherwise using
  289. ANSI STD C, but still have memcpy and memset in your C library
  290. and want to use them in calloc and realloc. Otherwise simple
  291. macro versions are defined here.
  292. USE_MEMCPY should be defined as 1 if you actually want to
  293. have memset and memcpy called. People report that the macro
  294. versions are often enough faster than libc versions on many
  295. systems that it is better to use them.
  296. */
  297. #define HAVE_MEMCPY
  298. #ifndef USE_MEMCPY
  299. #ifdef HAVE_MEMCPY
  300. #define USE_MEMCPY 1
  301. #else
  302. #define USE_MEMCPY 0
  303. #endif
  304. #endif
  305. #if (__STD_C || defined(HAVE_MEMCPY))
  306. #if __STD_C
  307. void* memset(void*, int, size_t);
  308. void* memcpy(void*, const void*, size_t);
  309. #else
  310. #ifdef WIN32
  311. /* On Win32 platforms, 'memset()' and 'memcpy()' are already declared in */
  312. /* 'windows.h' */
  313. #else
  314. Void_t* memset();
  315. Void_t* memcpy();
  316. #endif
  317. #endif
  318. #endif
  319. #if USE_MEMCPY
  320. /* The following macros are only invoked with (2n+1)-multiples of
  321. INTERNAL_SIZE_T units, with a positive integer n. This is exploited
  322. for fast inline execution when n is small. */
  323. #define MALLOC_ZERO(charp, nbytes) \
  324. do { \
  325. INTERNAL_SIZE_T mzsz = (nbytes); \
  326. if(mzsz <= 9*sizeof(mzsz)) { \
  327. INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \
  328. if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
  329. *mz++ = 0; \
  330. if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
  331. *mz++ = 0; \
  332. if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
  333. *mz++ = 0; }}} \
  334. *mz++ = 0; \
  335. *mz++ = 0; \
  336. *mz = 0; \
  337. } else memset((charp), 0, mzsz); \
  338. } while(0)
  339. #define MALLOC_COPY(dest,src,nbytes) \
  340. do { \
  341. INTERNAL_SIZE_T mcsz = (nbytes); \
  342. if(mcsz <= 9*sizeof(mcsz)) { \
  343. INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \
  344. INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \
  345. if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  346. *mcdst++ = *mcsrc++; \
  347. if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  348. *mcdst++ = *mcsrc++; \
  349. if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  350. *mcdst++ = *mcsrc++; }}} \
  351. *mcdst++ = *mcsrc++; \
  352. *mcdst++ = *mcsrc++; \
  353. *mcdst = *mcsrc ; \
  354. } else memcpy(dest, src, mcsz); \
  355. } while(0)
  356. #else /* !USE_MEMCPY */
  357. /* Use Duff's device for good zeroing/copying performance. */
  358. #define MALLOC_ZERO(charp, nbytes) \
  359. do { \
  360. INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \
  361. long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
  362. if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
  363. switch (mctmp) { \
  364. case 0: for(;;) { *mzp++ = 0; \
  365. case 7: *mzp++ = 0; \
  366. case 6: *mzp++ = 0; \
  367. case 5: *mzp++ = 0; \
  368. case 4: *mzp++ = 0; \
  369. case 3: *mzp++ = 0; \
  370. case 2: *mzp++ = 0; \
  371. case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
  372. } \
  373. } while(0)
  374. #define MALLOC_COPY(dest,src,nbytes) \
  375. do { \
  376. INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \
  377. INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \
  378. long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
  379. if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
  380. switch (mctmp) { \
  381. case 0: for(;;) { *mcdst++ = *mcsrc++; \
  382. case 7: *mcdst++ = *mcsrc++; \
  383. case 6: *mcdst++ = *mcsrc++; \
  384. case 5: *mcdst++ = *mcsrc++; \
  385. case 4: *mcdst++ = *mcsrc++; \
  386. case 3: *mcdst++ = *mcsrc++; \
  387. case 2: *mcdst++ = *mcsrc++; \
  388. case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
  389. } \
  390. } while(0)
  391. #endif
  392. /*
  393. Define HAVE_MMAP to optionally make malloc() use mmap() to
  394. allocate very large blocks. These will be returned to the
  395. operating system immediately after a free().
  396. */
  397. /***
  398. #ifndef HAVE_MMAP
  399. #define HAVE_MMAP 1
  400. #endif
  401. ***/
  402. #undef HAVE_MMAP /* Not available for U-Boot */
  403. /*
  404. Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
  405. large blocks. This is currently only possible on Linux with
  406. kernel versions newer than 1.3.77.
  407. */
  408. /***
  409. #ifndef HAVE_MREMAP
  410. #ifdef INTERNAL_LINUX_C_LIB
  411. #define HAVE_MREMAP 1
  412. #else
  413. #define HAVE_MREMAP 0
  414. #endif
  415. #endif
  416. ***/
  417. #undef HAVE_MREMAP /* Not available for U-Boot */
  418. #if HAVE_MMAP
  419. #include <unistd.h>
  420. #include <fcntl.h>
  421. #include <sys/mman.h>
  422. #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
  423. #define MAP_ANONYMOUS MAP_ANON
  424. #endif
  425. #endif /* HAVE_MMAP */
  426. /*
  427. Access to system page size. To the extent possible, this malloc
  428. manages memory from the system in page-size units.
  429. The following mechanics for getpagesize were adapted from
  430. bsd/gnu getpagesize.h
  431. */
  432. #define LACKS_UNISTD_H /* Shortcut for U-Boot */
  433. #define malloc_getpagesize 4096
  434. #ifndef LACKS_UNISTD_H
  435. # include <unistd.h>
  436. #endif
  437. #ifndef malloc_getpagesize
  438. # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
  439. # ifndef _SC_PAGE_SIZE
  440. # define _SC_PAGE_SIZE _SC_PAGESIZE
  441. # endif
  442. # endif
  443. # ifdef _SC_PAGE_SIZE
  444. # define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
  445. # else
  446. # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
  447. extern size_t getpagesize();
  448. # define malloc_getpagesize getpagesize()
  449. # else
  450. # ifdef WIN32
  451. # define malloc_getpagesize (4096) /* TBD: Use 'GetSystemInfo' instead */
  452. # else
  453. # ifndef LACKS_SYS_PARAM_H
  454. # include <sys/param.h>
  455. # endif
  456. # ifdef EXEC_PAGESIZE
  457. # define malloc_getpagesize EXEC_PAGESIZE
  458. # else
  459. # ifdef NBPG
  460. # ifndef CLSIZE
  461. # define malloc_getpagesize NBPG
  462. # else
  463. # define malloc_getpagesize (NBPG * CLSIZE)
  464. # endif
  465. # else
  466. # ifdef NBPC
  467. # define malloc_getpagesize NBPC
  468. # else
  469. # ifdef PAGESIZE
  470. # define malloc_getpagesize PAGESIZE
  471. # else
  472. # define malloc_getpagesize (4096) /* just guess */
  473. # endif
  474. # endif
  475. # endif
  476. # endif
  477. # endif
  478. # endif
  479. # endif
  480. #endif
  481. /*
  482. This version of malloc supports the standard SVID/XPG mallinfo
  483. routine that returns a struct containing the same kind of
  484. information you can get from malloc_stats. It should work on
  485. any SVID/XPG compliant system that has a /usr/include/malloc.h
  486. defining struct mallinfo. (If you'd like to install such a thing
  487. yourself, cut out the preliminary declarations as described above
  488. and below and save them in a malloc.h file. But there's no
  489. compelling reason to bother to do this.)
  490. The main declaration needed is the mallinfo struct that is returned
  491. (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
  492. bunch of fields, most of which are not even meaningful in this
  493. version of malloc. Some of these fields are are instead filled by
  494. mallinfo() with other numbers that might possibly be of interest.
  495. HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
  496. /usr/include/malloc.h file that includes a declaration of struct
  497. mallinfo. If so, it is included; else an SVID2/XPG2 compliant
  498. version is declared below. These must be precisely the same for
  499. mallinfo() to work.
  500. */
  501. /* #define HAVE_USR_INCLUDE_MALLOC_H */
  502. #if HAVE_USR_INCLUDE_MALLOC_H
  503. #include "/usr/include/malloc.h"
  504. #else
  505. /* SVID2/XPG mallinfo structure */
  506. struct mallinfo {
  507. int arena; /* total space allocated from system */
  508. int ordblks; /* number of non-inuse chunks */
  509. int smblks; /* unused -- always zero */
  510. int hblks; /* number of mmapped regions */
  511. int hblkhd; /* total space in mmapped regions */
  512. int usmblks; /* unused -- always zero */
  513. int fsmblks; /* unused -- always zero */
  514. int uordblks; /* total allocated space */
  515. int fordblks; /* total non-inuse space */
  516. int keepcost; /* top-most, releasable (via malloc_trim) space */
  517. };
  518. /* SVID2/XPG mallopt options */
  519. #define M_MXFAST 1 /* UNUSED in this malloc */
  520. #define M_NLBLKS 2 /* UNUSED in this malloc */
  521. #define M_GRAIN 3 /* UNUSED in this malloc */
  522. #define M_KEEP 4 /* UNUSED in this malloc */
  523. #endif
  524. /* mallopt options that actually do something */
  525. #define M_TRIM_THRESHOLD -1
  526. #define M_TOP_PAD -2
  527. #define M_MMAP_THRESHOLD -3
  528. #define M_MMAP_MAX -4
  529. #ifndef DEFAULT_TRIM_THRESHOLD
  530. #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
  531. #endif
  532. /*
  533. M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
  534. to keep before releasing via malloc_trim in free().
  535. Automatic trimming is mainly useful in long-lived programs.
  536. Because trimming via sbrk can be slow on some systems, and can
  537. sometimes be wasteful (in cases where programs immediately
  538. afterward allocate more large chunks) the value should be high
  539. enough so that your overall system performance would improve by
  540. releasing.
  541. The trim threshold and the mmap control parameters (see below)
  542. can be traded off with one another. Trimming and mmapping are
  543. two different ways of releasing unused memory back to the
  544. system. Between these two, it is often possible to keep
  545. system-level demands of a long-lived program down to a bare
  546. minimum. For example, in one test suite of sessions measuring
  547. the XF86 X server on Linux, using a trim threshold of 128K and a
  548. mmap threshold of 192K led to near-minimal long term resource
  549. consumption.
  550. If you are using this malloc in a long-lived program, it should
  551. pay to experiment with these values. As a rough guide, you
  552. might set to a value close to the average size of a process
  553. (program) running on your system. Releasing this much memory
  554. would allow such a process to run in memory. Generally, it's
  555. worth it to tune for trimming rather tham memory mapping when a
  556. program undergoes phases where several large chunks are
  557. allocated and released in ways that can reuse each other's
  558. storage, perhaps mixed with phases where there are no such
  559. chunks at all. And in well-behaved long-lived programs,
  560. controlling release of large blocks via trimming versus mapping
  561. is usually faster.
  562. However, in most programs, these parameters serve mainly as
  563. protection against the system-level effects of carrying around
  564. massive amounts of unneeded memory. Since frequent calls to
  565. sbrk, mmap, and munmap otherwise degrade performance, the default
  566. parameters are set to relatively high values that serve only as
  567. safeguards.
  568. The default trim value is high enough to cause trimming only in
  569. fairly extreme (by current memory consumption standards) cases.
  570. It must be greater than page size to have any useful effect. To
  571. disable trimming completely, you can set to (unsigned long)(-1);
  572. */
  573. #ifndef DEFAULT_TOP_PAD
  574. #define DEFAULT_TOP_PAD (0)
  575. #endif
  576. /*
  577. M_TOP_PAD is the amount of extra `padding' space to allocate or
  578. retain whenever sbrk is called. It is used in two ways internally:
  579. * When sbrk is called to extend the top of the arena to satisfy
  580. a new malloc request, this much padding is added to the sbrk
  581. request.
  582. * When malloc_trim is called automatically from free(),
  583. it is used as the `pad' argument.
  584. In both cases, the actual amount of padding is rounded
  585. so that the end of the arena is always a system page boundary.
  586. The main reason for using padding is to avoid calling sbrk so
  587. often. Having even a small pad greatly reduces the likelihood
  588. that nearly every malloc request during program start-up (or
  589. after trimming) will invoke sbrk, which needlessly wastes
  590. time.
  591. Automatic rounding-up to page-size units is normally sufficient
  592. to avoid measurable overhead, so the default is 0. However, in
  593. systems where sbrk is relatively slow, it can pay to increase
  594. this value, at the expense of carrying around more memory than
  595. the program needs.
  596. */
  597. #ifndef DEFAULT_MMAP_THRESHOLD
  598. #define DEFAULT_MMAP_THRESHOLD (128 * 1024)
  599. #endif
  600. /*
  601. M_MMAP_THRESHOLD is the request size threshold for using mmap()
  602. to service a request. Requests of at least this size that cannot
  603. be allocated using already-existing space will be serviced via mmap.
  604. (If enough normal freed space already exists it is used instead.)
  605. Using mmap segregates relatively large chunks of memory so that
  606. they can be individually obtained and released from the host
  607. system. A request serviced through mmap is never reused by any
  608. other request (at least not directly; the system may just so
  609. happen to remap successive requests to the same locations).
  610. Segregating space in this way has the benefit that mmapped space
  611. can ALWAYS be individually released back to the system, which
  612. helps keep the system level memory demands of a long-lived
  613. program low. Mapped memory can never become `locked' between
  614. other chunks, as can happen with normally allocated chunks, which
  615. menas that even trimming via malloc_trim would not release them.
  616. However, it has the disadvantages that:
  617. 1. The space cannot be reclaimed, consolidated, and then
  618. used to service later requests, as happens with normal chunks.
  619. 2. It can lead to more wastage because of mmap page alignment
  620. requirements
  621. 3. It causes malloc performance to be more dependent on host
  622. system memory management support routines which may vary in
  623. implementation quality and may impose arbitrary
  624. limitations. Generally, servicing a request via normal
  625. malloc steps is faster than going through a system's mmap.
  626. All together, these considerations should lead you to use mmap
  627. only for relatively large requests.
  628. */
  629. #ifndef DEFAULT_MMAP_MAX
  630. #if HAVE_MMAP
  631. #define DEFAULT_MMAP_MAX (64)
  632. #else
  633. #define DEFAULT_MMAP_MAX (0)
  634. #endif
  635. #endif
  636. /*
  637. M_MMAP_MAX is the maximum number of requests to simultaneously
  638. service using mmap. This parameter exists because:
  639. 1. Some systems have a limited number of internal tables for
  640. use by mmap.
  641. 2. In most systems, overreliance on mmap can degrade overall
  642. performance.
  643. 3. If a program allocates many large regions, it is probably
  644. better off using normal sbrk-based allocation routines that
  645. can reclaim and reallocate normal heap memory. Using a
  646. small value allows transition into this mode after the
  647. first few allocations.
  648. Setting to 0 disables all use of mmap. If HAVE_MMAP is not set,
  649. the default value is 0, and attempts to set it to non-zero values
  650. in mallopt will fail.
  651. */
  652. /*
  653. USE_DL_PREFIX will prefix all public routines with the string 'dl'.
  654. Useful to quickly avoid procedure declaration conflicts and linker
  655. symbol conflicts with existing memory allocation routines.
  656. */
  657. /* #define USE_DL_PREFIX */
  658. /*
  659. Special defines for linux libc
  660. Except when compiled using these special defines for Linux libc
  661. using weak aliases, this malloc is NOT designed to work in
  662. multithreaded applications. No semaphores or other concurrency
  663. control are provided to ensure that multiple malloc or free calls
  664. don't run at the same time, which could be disasterous. A single
  665. semaphore could be used across malloc, realloc, and free (which is
  666. essentially the effect of the linux weak alias approach). It would
  667. be hard to obtain finer granularity.
  668. */
  669. #ifdef INTERNAL_LINUX_C_LIB
  670. #if __STD_C
  671. Void_t * __default_morecore_init (ptrdiff_t);
  672. Void_t *(*__morecore)(ptrdiff_t) = __default_morecore_init;
  673. #else
  674. Void_t * __default_morecore_init ();
  675. Void_t *(*__morecore)() = __default_morecore_init;
  676. #endif
  677. #define MORECORE (*__morecore)
  678. #define MORECORE_FAILURE 0
  679. #define MORECORE_CLEARS 1
  680. #else /* INTERNAL_LINUX_C_LIB */
  681. #if __STD_C
  682. extern Void_t* sbrk(ptrdiff_t);
  683. #else
  684. extern Void_t* sbrk();
  685. #endif
  686. #ifndef MORECORE
  687. #define MORECORE sbrk
  688. #endif
  689. #ifndef MORECORE_FAILURE
  690. #define MORECORE_FAILURE -1
  691. #endif
  692. #ifndef MORECORE_CLEARS
  693. #define MORECORE_CLEARS 1
  694. #endif
  695. #endif /* INTERNAL_LINUX_C_LIB */
  696. #if defined(INTERNAL_LINUX_C_LIB) && defined(__ELF__)
  697. #define cALLOc __libc_calloc
  698. #define fREe __libc_free
  699. #define mALLOc __libc_malloc
  700. #define mEMALIGn __libc_memalign
  701. #define rEALLOc __libc_realloc
  702. #define vALLOc __libc_valloc
  703. #define pvALLOc __libc_pvalloc
  704. #define mALLINFo __libc_mallinfo
  705. #define mALLOPt __libc_mallopt
  706. #pragma weak calloc = __libc_calloc
  707. #pragma weak free = __libc_free
  708. #pragma weak cfree = __libc_free
  709. #pragma weak malloc = __libc_malloc
  710. #pragma weak memalign = __libc_memalign
  711. #pragma weak realloc = __libc_realloc
  712. #pragma weak valloc = __libc_valloc
  713. #pragma weak pvalloc = __libc_pvalloc
  714. #pragma weak mallinfo = __libc_mallinfo
  715. #pragma weak mallopt = __libc_mallopt
  716. #else
  717. #ifdef USE_DL_PREFIX
  718. #define cALLOc dlcalloc
  719. #define fREe dlfree
  720. #define mALLOc dlmalloc
  721. #define mEMALIGn dlmemalign
  722. #define rEALLOc dlrealloc
  723. #define vALLOc dlvalloc
  724. #define pvALLOc dlpvalloc
  725. #define mALLINFo dlmallinfo
  726. #define mALLOPt dlmallopt
  727. #else /* USE_DL_PREFIX */
  728. #define cALLOc calloc
  729. #define fREe free
  730. #define mALLOc malloc
  731. #define mEMALIGn memalign
  732. #define rEALLOc realloc
  733. #define vALLOc valloc
  734. #define pvALLOc pvalloc
  735. #define mALLINFo mallinfo
  736. #define mALLOPt mallopt
  737. #endif /* USE_DL_PREFIX */
  738. #endif
  739. /* Public routines */
  740. #if __STD_C
  741. Void_t* mALLOc(size_t);
  742. void fREe(Void_t*);
  743. Void_t* rEALLOc(Void_t*, size_t);
  744. Void_t* mEMALIGn(size_t, size_t);
  745. Void_t* vALLOc(size_t);
  746. Void_t* pvALLOc(size_t);
  747. Void_t* cALLOc(size_t, size_t);
  748. void cfree(Void_t*);
  749. int malloc_trim(size_t);
  750. size_t malloc_usable_size(Void_t*);
  751. void malloc_stats(void);
  752. int mALLOPt(int, int);
  753. struct mallinfo mALLINFo(void);
  754. #else
  755. Void_t* mALLOc();
  756. void fREe();
  757. Void_t* rEALLOc();
  758. Void_t* mEMALIGn();
  759. Void_t* vALLOc();
  760. Void_t* pvALLOc();
  761. Void_t* cALLOc();
  762. void cfree();
  763. int malloc_trim();
  764. size_t malloc_usable_size();
  765. void malloc_stats();
  766. int mALLOPt();
  767. struct mallinfo mALLINFo();
  768. #endif
  769. /*
  770. * Begin and End of memory area for malloc(), and current "brk"
  771. */
  772. extern ulong mem_malloc_start;
  773. extern ulong mem_malloc_end;
  774. extern ulong mem_malloc_brk;
  775. void mem_malloc_init(ulong start, ulong size);
  776. #ifdef __cplusplus
  777. }; /* end of extern "C" */
  778. #endif
  779. #endif /* __MALLOC_H__ */