malloc.h 34 KB

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