malloc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. /*
  244. INTERNAL_SIZE_T is the word-size used for internal bookkeeping
  245. of chunk sizes. On a 64-bit machine, you can reduce malloc
  246. overhead by defining INTERNAL_SIZE_T to be a 32 bit `unsigned int'
  247. at the expense of not being able to handle requests greater than
  248. 2^31. This limitation is hardly ever a concern; you are encouraged
  249. to set this. However, the default version is the same as size_t.
  250. */
  251. #ifndef INTERNAL_SIZE_T
  252. #define INTERNAL_SIZE_T size_t
  253. #endif
  254. /*
  255. REALLOC_ZERO_BYTES_FREES should be set if a call to
  256. realloc with zero bytes should be the same as a call to free.
  257. Some people think it should. Otherwise, since this malloc
  258. returns a unique pointer for malloc(0), so does realloc(p, 0).
  259. */
  260. /* #define REALLOC_ZERO_BYTES_FREES */
  261. /*
  262. WIN32 causes an emulation of sbrk to be compiled in
  263. mmap-based options are not currently supported in WIN32.
  264. */
  265. /* #define WIN32 */
  266. #ifdef WIN32
  267. #define MORECORE wsbrk
  268. #define HAVE_MMAP 0
  269. #define LACKS_UNISTD_H
  270. #define LACKS_SYS_PARAM_H
  271. /*
  272. Include 'windows.h' to get the necessary declarations for the
  273. Microsoft Visual C++ data structures and routines used in the 'sbrk'
  274. emulation.
  275. Define WIN32_LEAN_AND_MEAN so that only the essential Microsoft
  276. Visual C++ header files are included.
  277. */
  278. #define WIN32_LEAN_AND_MEAN
  279. #include <windows.h>
  280. #endif
  281. /*
  282. HAVE_MEMCPY should be defined if you are not otherwise using
  283. ANSI STD C, but still have memcpy and memset in your C library
  284. and want to use them in calloc and realloc. Otherwise simple
  285. macro versions are defined here.
  286. USE_MEMCPY should be defined as 1 if you actually want to
  287. have memset and memcpy called. People report that the macro
  288. versions are often enough faster than libc versions on many
  289. systems that it is better to use them.
  290. */
  291. #define HAVE_MEMCPY
  292. #ifndef USE_MEMCPY
  293. #ifdef HAVE_MEMCPY
  294. #define USE_MEMCPY 1
  295. #else
  296. #define USE_MEMCPY 0
  297. #endif
  298. #endif
  299. #if (__STD_C || defined(HAVE_MEMCPY))
  300. #if __STD_C
  301. void* memset(void*, int, size_t);
  302. void* memcpy(void*, const void*, size_t);
  303. #else
  304. #ifdef WIN32
  305. /* On Win32 platforms, 'memset()' and 'memcpy()' are already declared in */
  306. /* 'windows.h' */
  307. #else
  308. Void_t* memset();
  309. Void_t* memcpy();
  310. #endif
  311. #endif
  312. #endif
  313. #if USE_MEMCPY
  314. /* The following macros are only invoked with (2n+1)-multiples of
  315. INTERNAL_SIZE_T units, with a positive integer n. This is exploited
  316. for fast inline execution when n is small. */
  317. #define MALLOC_ZERO(charp, nbytes) \
  318. do { \
  319. INTERNAL_SIZE_T mzsz = (nbytes); \
  320. if(mzsz <= 9*sizeof(mzsz)) { \
  321. INTERNAL_SIZE_T* mz = (INTERNAL_SIZE_T*) (charp); \
  322. if(mzsz >= 5*sizeof(mzsz)) { *mz++ = 0; \
  323. *mz++ = 0; \
  324. if(mzsz >= 7*sizeof(mzsz)) { *mz++ = 0; \
  325. *mz++ = 0; \
  326. if(mzsz >= 9*sizeof(mzsz)) { *mz++ = 0; \
  327. *mz++ = 0; }}} \
  328. *mz++ = 0; \
  329. *mz++ = 0; \
  330. *mz = 0; \
  331. } else memset((charp), 0, mzsz); \
  332. } while(0)
  333. #define MALLOC_COPY(dest,src,nbytes) \
  334. do { \
  335. INTERNAL_SIZE_T mcsz = (nbytes); \
  336. if(mcsz <= 9*sizeof(mcsz)) { \
  337. INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) (src); \
  338. INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) (dest); \
  339. if(mcsz >= 5*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  340. *mcdst++ = *mcsrc++; \
  341. if(mcsz >= 7*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  342. *mcdst++ = *mcsrc++; \
  343. if(mcsz >= 9*sizeof(mcsz)) { *mcdst++ = *mcsrc++; \
  344. *mcdst++ = *mcsrc++; }}} \
  345. *mcdst++ = *mcsrc++; \
  346. *mcdst++ = *mcsrc++; \
  347. *mcdst = *mcsrc ; \
  348. } else memcpy(dest, src, mcsz); \
  349. } while(0)
  350. #else /* !USE_MEMCPY */
  351. /* Use Duff's device for good zeroing/copying performance. */
  352. #define MALLOC_ZERO(charp, nbytes) \
  353. do { \
  354. INTERNAL_SIZE_T* mzp = (INTERNAL_SIZE_T*)(charp); \
  355. long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
  356. if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
  357. switch (mctmp) { \
  358. case 0: for(;;) { *mzp++ = 0; \
  359. case 7: *mzp++ = 0; \
  360. case 6: *mzp++ = 0; \
  361. case 5: *mzp++ = 0; \
  362. case 4: *mzp++ = 0; \
  363. case 3: *mzp++ = 0; \
  364. case 2: *mzp++ = 0; \
  365. case 1: *mzp++ = 0; if(mcn <= 0) break; mcn--; } \
  366. } \
  367. } while(0)
  368. #define MALLOC_COPY(dest,src,nbytes) \
  369. do { \
  370. INTERNAL_SIZE_T* mcsrc = (INTERNAL_SIZE_T*) src; \
  371. INTERNAL_SIZE_T* mcdst = (INTERNAL_SIZE_T*) dest; \
  372. long mctmp = (nbytes)/sizeof(INTERNAL_SIZE_T), mcn; \
  373. if (mctmp < 8) mcn = 0; else { mcn = (mctmp-1)/8; mctmp %= 8; } \
  374. switch (mctmp) { \
  375. case 0: for(;;) { *mcdst++ = *mcsrc++; \
  376. case 7: *mcdst++ = *mcsrc++; \
  377. case 6: *mcdst++ = *mcsrc++; \
  378. case 5: *mcdst++ = *mcsrc++; \
  379. case 4: *mcdst++ = *mcsrc++; \
  380. case 3: *mcdst++ = *mcsrc++; \
  381. case 2: *mcdst++ = *mcsrc++; \
  382. case 1: *mcdst++ = *mcsrc++; if(mcn <= 0) break; mcn--; } \
  383. } \
  384. } while(0)
  385. #endif
  386. /*
  387. Define HAVE_MMAP to optionally make malloc() use mmap() to
  388. allocate very large blocks. These will be returned to the
  389. operating system immediately after a free().
  390. */
  391. /***
  392. #ifndef HAVE_MMAP
  393. #define HAVE_MMAP 1
  394. #endif
  395. ***/
  396. #undef HAVE_MMAP /* Not available for U-Boot */
  397. /*
  398. Define HAVE_MREMAP to make realloc() use mremap() to re-allocate
  399. large blocks. This is currently only possible on Linux with
  400. kernel versions newer than 1.3.77.
  401. */
  402. /***
  403. #ifndef HAVE_MREMAP
  404. #ifdef INTERNAL_LINUX_C_LIB
  405. #define HAVE_MREMAP 1
  406. #else
  407. #define HAVE_MREMAP 0
  408. #endif
  409. #endif
  410. ***/
  411. #undef HAVE_MREMAP /* Not available for U-Boot */
  412. #ifdef HAVE_MMAP
  413. #include <unistd.h>
  414. #include <fcntl.h>
  415. #include <sys/mman.h>
  416. #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
  417. #define MAP_ANONYMOUS MAP_ANON
  418. #endif
  419. #endif /* HAVE_MMAP */
  420. /*
  421. Access to system page size. To the extent possible, this malloc
  422. manages memory from the system in page-size units.
  423. The following mechanics for getpagesize were adapted from
  424. bsd/gnu getpagesize.h
  425. */
  426. #define LACKS_UNISTD_H /* Shortcut for U-Boot */
  427. #define malloc_getpagesize 4096
  428. #ifndef LACKS_UNISTD_H
  429. # include <unistd.h>
  430. #endif
  431. #ifndef malloc_getpagesize
  432. # ifdef _SC_PAGESIZE /* some SVR4 systems omit an underscore */
  433. # ifndef _SC_PAGE_SIZE
  434. # define _SC_PAGE_SIZE _SC_PAGESIZE
  435. # endif
  436. # endif
  437. # ifdef _SC_PAGE_SIZE
  438. # define malloc_getpagesize sysconf(_SC_PAGE_SIZE)
  439. # else
  440. # if defined(BSD) || defined(DGUX) || defined(HAVE_GETPAGESIZE)
  441. extern size_t getpagesize();
  442. # define malloc_getpagesize getpagesize()
  443. # else
  444. # ifdef WIN32
  445. # define malloc_getpagesize (4096) /* TBD: Use 'GetSystemInfo' instead */
  446. # else
  447. # ifndef LACKS_SYS_PARAM_H
  448. # include <sys/param.h>
  449. # endif
  450. # ifdef EXEC_PAGESIZE
  451. # define malloc_getpagesize EXEC_PAGESIZE
  452. # else
  453. # ifdef NBPG
  454. # ifndef CLSIZE
  455. # define malloc_getpagesize NBPG
  456. # else
  457. # define malloc_getpagesize (NBPG * CLSIZE)
  458. # endif
  459. # else
  460. # ifdef NBPC
  461. # define malloc_getpagesize NBPC
  462. # else
  463. # ifdef PAGESIZE
  464. # define malloc_getpagesize PAGESIZE
  465. # else
  466. # define malloc_getpagesize (4096) /* just guess */
  467. # endif
  468. # endif
  469. # endif
  470. # endif
  471. # endif
  472. # endif
  473. # endif
  474. #endif
  475. /*
  476. This version of malloc supports the standard SVID/XPG mallinfo
  477. routine that returns a struct containing the same kind of
  478. information you can get from malloc_stats. It should work on
  479. any SVID/XPG compliant system that has a /usr/include/malloc.h
  480. defining struct mallinfo. (If you'd like to install such a thing
  481. yourself, cut out the preliminary declarations as described above
  482. and below and save them in a malloc.h file. But there's no
  483. compelling reason to bother to do this.)
  484. The main declaration needed is the mallinfo struct that is returned
  485. (by-copy) by mallinfo(). The SVID/XPG malloinfo struct contains a
  486. bunch of fields, most of which are not even meaningful in this
  487. version of malloc. Some of these fields are are instead filled by
  488. mallinfo() with other numbers that might possibly be of interest.
  489. HAVE_USR_INCLUDE_MALLOC_H should be set if you have a
  490. /usr/include/malloc.h file that includes a declaration of struct
  491. mallinfo. If so, it is included; else an SVID2/XPG2 compliant
  492. version is declared below. These must be precisely the same for
  493. mallinfo() to work.
  494. */
  495. /* #define HAVE_USR_INCLUDE_MALLOC_H */
  496. #ifdef HAVE_USR_INCLUDE_MALLOC_H
  497. #include "/usr/include/malloc.h"
  498. #else
  499. /* SVID2/XPG mallinfo structure */
  500. struct mallinfo {
  501. int arena; /* total space allocated from system */
  502. int ordblks; /* number of non-inuse chunks */
  503. int smblks; /* unused -- always zero */
  504. int hblks; /* number of mmapped regions */
  505. int hblkhd; /* total space in mmapped regions */
  506. int usmblks; /* unused -- always zero */
  507. int fsmblks; /* unused -- always zero */
  508. int uordblks; /* total allocated space */
  509. int fordblks; /* total non-inuse space */
  510. int keepcost; /* top-most, releasable (via malloc_trim) space */
  511. };
  512. /* SVID2/XPG mallopt options */
  513. #define M_MXFAST 1 /* UNUSED in this malloc */
  514. #define M_NLBLKS 2 /* UNUSED in this malloc */
  515. #define M_GRAIN 3 /* UNUSED in this malloc */
  516. #define M_KEEP 4 /* UNUSED in this malloc */
  517. #endif
  518. /* mallopt options that actually do something */
  519. #define M_TRIM_THRESHOLD -1
  520. #define M_TOP_PAD -2
  521. #define M_MMAP_THRESHOLD -3
  522. #define M_MMAP_MAX -4
  523. #ifndef DEFAULT_TRIM_THRESHOLD
  524. #define DEFAULT_TRIM_THRESHOLD (128 * 1024)
  525. #endif
  526. /*
  527. M_TRIM_THRESHOLD is the maximum amount of unused top-most memory
  528. to keep before releasing via malloc_trim in free().
  529. Automatic trimming is mainly useful in long-lived programs.
  530. Because trimming via sbrk can be slow on some systems, and can
  531. sometimes be wasteful (in cases where programs immediately
  532. afterward allocate more large chunks) the value should be high
  533. enough so that your overall system performance would improve by
  534. releasing.
  535. The trim threshold and the mmap control parameters (see below)
  536. can be traded off with one another. Trimming and mmapping are
  537. two different ways of releasing unused memory back to the
  538. system. Between these two, it is often possible to keep
  539. system-level demands of a long-lived program down to a bare
  540. minimum. For example, in one test suite of sessions measuring
  541. the XF86 X server on Linux, using a trim threshold of 128K and a
  542. mmap threshold of 192K led to near-minimal long term resource
  543. consumption.
  544. If you are using this malloc in a long-lived program, it should
  545. pay to experiment with these values. As a rough guide, you
  546. might set to a value close to the average size of a process
  547. (program) running on your system. Releasing this much memory
  548. would allow such a process to run in memory. Generally, it's
  549. worth it to tune for trimming rather tham memory mapping when a
  550. program undergoes phases where several large chunks are
  551. allocated and released in ways that can reuse each other's
  552. storage, perhaps mixed with phases where there are no such
  553. chunks at all. And in well-behaved long-lived programs,
  554. controlling release of large blocks via trimming versus mapping
  555. is usually faster.
  556. However, in most programs, these parameters serve mainly as
  557. protection against the system-level effects of carrying around
  558. massive amounts of unneeded memory. Since frequent calls to
  559. sbrk, mmap, and munmap otherwise degrade performance, the default
  560. parameters are set to relatively high values that serve only as
  561. safeguards.
  562. The default trim value is high enough to cause trimming only in
  563. fairly extreme (by current memory consumption standards) cases.
  564. It must be greater than page size to have any useful effect. To
  565. disable trimming completely, you can set to (unsigned long)(-1);
  566. */
  567. #ifndef DEFAULT_TOP_PAD
  568. #define DEFAULT_TOP_PAD (0)
  569. #endif
  570. /*
  571. M_TOP_PAD is the amount of extra `padding' space to allocate or
  572. retain whenever sbrk is called. It is used in two ways internally:
  573. * When sbrk is called to extend the top of the arena to satisfy
  574. a new malloc request, this much padding is added to the sbrk
  575. request.
  576. * When malloc_trim is called automatically from free(),
  577. it is used as the `pad' argument.
  578. In both cases, the actual amount of padding is rounded
  579. so that the end of the arena is always a system page boundary.
  580. The main reason for using padding is to avoid calling sbrk so
  581. often. Having even a small pad greatly reduces the likelihood
  582. that nearly every malloc request during program start-up (or
  583. after trimming) will invoke sbrk, which needlessly wastes
  584. time.
  585. Automatic rounding-up to page-size units is normally sufficient
  586. to avoid measurable overhead, so the default is 0. However, in
  587. systems where sbrk is relatively slow, it can pay to increase
  588. this value, at the expense of carrying around more memory than
  589. the program needs.
  590. */
  591. #ifndef DEFAULT_MMAP_THRESHOLD
  592. #define DEFAULT_MMAP_THRESHOLD (128 * 1024)
  593. #endif
  594. /*
  595. M_MMAP_THRESHOLD is the request size threshold for using mmap()
  596. to service a request. Requests of at least this size that cannot
  597. be allocated using already-existing space will be serviced via mmap.
  598. (If enough normal freed space already exists it is used instead.)
  599. Using mmap segregates relatively large chunks of memory so that
  600. they can be individually obtained and released from the host
  601. system. A request serviced through mmap is never reused by any
  602. other request (at least not directly; the system may just so
  603. happen to remap successive requests to the same locations).
  604. Segregating space in this way has the benefit that mmapped space
  605. can ALWAYS be individually released back to the system, which
  606. helps keep the system level memory demands of a long-lived
  607. program low. Mapped memory can never become `locked' between
  608. other chunks, as can happen with normally allocated chunks, which
  609. menas that even trimming via malloc_trim would not release them.
  610. However, it has the disadvantages that:
  611. 1. The space cannot be reclaimed, consolidated, and then
  612. used to service later requests, as happens with normal chunks.
  613. 2. It can lead to more wastage because of mmap page alignment
  614. requirements
  615. 3. It causes malloc performance to be more dependent on host
  616. system memory management support routines which may vary in
  617. implementation quality and may impose arbitrary
  618. limitations. Generally, servicing a request via normal
  619. malloc steps is faster than going through a system's mmap.
  620. All together, these considerations should lead you to use mmap
  621. only for relatively large requests.
  622. */
  623. #ifndef DEFAULT_MMAP_MAX
  624. #ifdef HAVE_MMAP
  625. #define DEFAULT_MMAP_MAX (64)
  626. #else
  627. #define DEFAULT_MMAP_MAX (0)
  628. #endif
  629. #endif
  630. /*
  631. M_MMAP_MAX is the maximum number of requests to simultaneously
  632. service using mmap. This parameter exists because:
  633. 1. Some systems have a limited number of internal tables for
  634. use by mmap.
  635. 2. In most systems, overreliance on mmap can degrade overall
  636. performance.
  637. 3. If a program allocates many large regions, it is probably
  638. better off using normal sbrk-based allocation routines that
  639. can reclaim and reallocate normal heap memory. Using a
  640. small value allows transition into this mode after the
  641. first few allocations.
  642. Setting to 0 disables all use of mmap. If HAVE_MMAP is not set,
  643. the default value is 0, and attempts to set it to non-zero values
  644. in mallopt will fail.
  645. */
  646. /*
  647. USE_DL_PREFIX will prefix all public routines with the string 'dl'.
  648. Useful to quickly avoid procedure declaration conflicts and linker
  649. symbol conflicts with existing memory allocation routines.
  650. */
  651. /* #define USE_DL_PREFIX */
  652. /*
  653. Special defines for linux libc
  654. Except when compiled using these special defines for Linux libc
  655. using weak aliases, this malloc is NOT designed to work in
  656. multithreaded applications. No semaphores or other concurrency
  657. control are provided to ensure that multiple malloc or free calls
  658. don't run at the same time, which could be disasterous. A single
  659. semaphore could be used across malloc, realloc, and free (which is
  660. essentially the effect of the linux weak alias approach). It would
  661. be hard to obtain finer granularity.
  662. */
  663. #ifdef INTERNAL_LINUX_C_LIB
  664. #if __STD_C
  665. Void_t * __default_morecore_init (ptrdiff_t);
  666. Void_t *(*__morecore)(ptrdiff_t) = __default_morecore_init;
  667. #else
  668. Void_t * __default_morecore_init ();
  669. Void_t *(*__morecore)() = __default_morecore_init;
  670. #endif
  671. #define MORECORE (*__morecore)
  672. #define MORECORE_FAILURE 0
  673. #define MORECORE_CLEARS 1
  674. #else /* INTERNAL_LINUX_C_LIB */
  675. #if __STD_C
  676. extern Void_t* sbrk(ptrdiff_t);
  677. #else
  678. extern Void_t* sbrk();
  679. #endif
  680. #ifndef MORECORE
  681. #define MORECORE sbrk
  682. #endif
  683. #ifndef MORECORE_FAILURE
  684. #define MORECORE_FAILURE -1
  685. #endif
  686. #ifndef MORECORE_CLEARS
  687. #define MORECORE_CLEARS 1
  688. #endif
  689. #endif /* INTERNAL_LINUX_C_LIB */
  690. #if defined(INTERNAL_LINUX_C_LIB) && defined(__ELF__)
  691. #define cALLOc __libc_calloc
  692. #define fREe __libc_free
  693. #define mALLOc __libc_malloc
  694. #define mEMALIGn __libc_memalign
  695. #define rEALLOc __libc_realloc
  696. #define vALLOc __libc_valloc
  697. #define pvALLOc __libc_pvalloc
  698. #define mALLINFo __libc_mallinfo
  699. #define mALLOPt __libc_mallopt
  700. #pragma weak calloc = __libc_calloc
  701. #pragma weak free = __libc_free
  702. #pragma weak cfree = __libc_free
  703. #pragma weak malloc = __libc_malloc
  704. #pragma weak memalign = __libc_memalign
  705. #pragma weak realloc = __libc_realloc
  706. #pragma weak valloc = __libc_valloc
  707. #pragma weak pvalloc = __libc_pvalloc
  708. #pragma weak mallinfo = __libc_mallinfo
  709. #pragma weak mallopt = __libc_mallopt
  710. #else
  711. #if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE)
  712. #define malloc malloc_simple
  713. #define realloc realloc_simple
  714. #define memalign memalign_simple
  715. static inline void free(void *ptr) {}
  716. void *calloc(size_t nmemb, size_t size);
  717. void *realloc_simple(void *ptr, size_t size);
  718. void malloc_simple_info(void);
  719. #else
  720. # ifdef USE_DL_PREFIX
  721. # define cALLOc dlcalloc
  722. # define fREe dlfree
  723. # define mALLOc dlmalloc
  724. # define mEMALIGn dlmemalign
  725. # define rEALLOc dlrealloc
  726. # define vALLOc dlvalloc
  727. # define pvALLOc dlpvalloc
  728. # define mALLINFo dlmallinfo
  729. # define mALLOPt dlmallopt
  730. # else /* USE_DL_PREFIX */
  731. # define cALLOc calloc
  732. # define fREe free
  733. # define mALLOc malloc
  734. # define mEMALIGn memalign
  735. # define rEALLOc realloc
  736. # define vALLOc valloc
  737. # define pvALLOc pvalloc
  738. # define mALLINFo mallinfo
  739. # define mALLOPt mallopt
  740. # endif /* USE_DL_PREFIX */
  741. #endif
  742. /* Set up pre-relocation malloc() ready for use */
  743. int initf_malloc(void);
  744. /* Public routines */
  745. /* Simple versions which can be used when space is tight */
  746. void *malloc_simple(size_t size);
  747. void *memalign_simple(size_t alignment, size_t bytes);
  748. #pragma GCC visibility push(hidden)
  749. # if __STD_C
  750. Void_t* mALLOc(size_t);
  751. void fREe(Void_t*);
  752. Void_t* rEALLOc(Void_t*, size_t);
  753. Void_t* mEMALIGn(size_t, size_t);
  754. Void_t* vALLOc(size_t);
  755. Void_t* pvALLOc(size_t);
  756. Void_t* cALLOc(size_t, size_t);
  757. void cfree(Void_t*);
  758. int malloc_trim(size_t);
  759. size_t malloc_usable_size(Void_t*);
  760. void malloc_stats(void);
  761. int mALLOPt(int, int);
  762. struct mallinfo mALLINFo(void);
  763. # else
  764. Void_t* mALLOc();
  765. void fREe();
  766. Void_t* rEALLOc();
  767. Void_t* mEMALIGn();
  768. Void_t* vALLOc();
  769. Void_t* pvALLOc();
  770. Void_t* cALLOc();
  771. void cfree();
  772. int malloc_trim();
  773. size_t malloc_usable_size();
  774. void malloc_stats();
  775. int mALLOPt();
  776. struct mallinfo mALLINFo();
  777. # endif
  778. #endif
  779. #pragma GCC visibility pop
  780. /*
  781. * Begin and End of memory area for malloc(), and current "brk"
  782. */
  783. extern ulong mem_malloc_start;
  784. extern ulong mem_malloc_end;
  785. extern ulong mem_malloc_brk;
  786. void mem_malloc_init(ulong start, ulong size);
  787. #ifdef __cplusplus
  788. }; /* end of extern "C" */
  789. #endif
  790. #endif /* __MALLOC_H__ */