You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

562 lines
20 KiB

  1. /*
  2. Default header file for malloc-2.8.x, written by Doug Lea
  3. and released to the public domain, as explained at
  4. http://creativecommons.org/licenses/publicdomain.
  5. last update: Wed May 27 14:25:17 2009 Doug Lea (dl at gee)
  6. This header is for ANSI C/C++ only. You can set any of
  7. the following #defines before including:
  8. * If USE_DL_PREFIX is defined, it is assumed that malloc.c
  9. was also compiled with this option, so all routines
  10. have names starting with "dl".
  11. * If HAVE_USR_INCLUDE_MALLOC_H is defined, it is assumed that this
  12. file will be #included AFTER <malloc.h>. This is needed only if
  13. your system defines a struct mallinfo that is incompatible with the
  14. standard one declared here. Otherwise, you can include this file
  15. INSTEAD of your system system <malloc.h>. At least on ANSI, all
  16. declarations should be compatible with system versions
  17. * If MSPACES is defined, declarations for mspace versions are included.
  18. */
  19. #ifndef MALLOC_280_H
  20. #define MALLOC_280_H
  21. #define USE_DL_PREFIX
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <stddef.h> /* for size_t */
  26. #ifndef ONLY_MSPACES
  27. #define ONLY_MSPACES 0 /* define to a value */
  28. #endif /* ONLY_MSPACES */
  29. #ifndef NO_MALLINFO
  30. #define NO_MALLINFO 0
  31. #endif /* NO_MALLINFO */
  32. #if !ONLY_MSPACES
  33. #ifndef USE_DL_PREFIX
  34. #define dlcalloc calloc
  35. #define dlfree free
  36. #define dlmalloc malloc
  37. #define dlmemalign memalign
  38. #define dlrealloc realloc
  39. #define dlvalloc valloc
  40. #define dlpvalloc pvalloc
  41. #define dlmallinfo mallinfo
  42. #define dlmallopt mallopt
  43. #define dlmalloc_trim malloc_trim
  44. #define dlmalloc_stats malloc_stats
  45. #define dlmalloc_usable_size malloc_usable_size
  46. #define dlmalloc_footprint malloc_footprint
  47. #define dlindependent_calloc independent_calloc
  48. #define dlindependent_comalloc independent_comalloc
  49. #endif /* USE_DL_PREFIX */
  50. #if !NO_MALLINFO
  51. #ifndef HAVE_USR_INCLUDE_MALLOC_H
  52. #ifndef _MALLOC_H
  53. #ifndef MALLINFO_FIELD_TYPE
  54. #define MALLINFO_FIELD_TYPE size_t
  55. #endif /* MALLINFO_FIELD_TYPE */
  56. #ifndef STRUCT_MALLINFO_DECLARED
  57. #define STRUCT_MALLINFO_DECLARED 1
  58. struct mallinfo {
  59. MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */
  60. MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */
  61. MALLINFO_FIELD_TYPE smblks; /* always 0 */
  62. MALLINFO_FIELD_TYPE hblks; /* always 0 */
  63. MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */
  64. MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */
  65. MALLINFO_FIELD_TYPE fsmblks; /* always 0 */
  66. MALLINFO_FIELD_TYPE uordblks; /* total allocated space */
  67. MALLINFO_FIELD_TYPE fordblks; /* total free space */
  68. MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */
  69. };
  70. #endif /* STRUCT_MALLINFO_DECLARED */
  71. #endif /* _MALLOC_H */
  72. #endif /* HAVE_USR_INCLUDE_MALLOC_H */
  73. #endif /* !NO_MALLINFO */
  74. /*
  75. malloc(size_t n)
  76. Returns a pointer to a newly allocated chunk of at least n bytes, or
  77. null if no space is available, in which case errno is set to ENOMEM
  78. on ANSI C systems.
  79. If n is zero, malloc returns a minimum-sized chunk. (The minimum
  80. size is 16 bytes on most 32bit systems, and 32 bytes on 64bit
  81. systems.) Note that size_t is an unsigned type, so calls with
  82. arguments that would be negative if signed are interpreted as
  83. requests for huge amounts of space, which will often fail. The
  84. maximum supported value of n differs across systems, but is in all
  85. cases less than the maximum representable value of a size_t.
  86. */
  87. void* dlmalloc(size_t);
  88. /*
  89. free(void* p)
  90. Releases the chunk of memory pointed to by p, that had been previously
  91. allocated using malloc or a related routine such as realloc.
  92. It has no effect if p is null. If p was not malloced or already
  93. freed, free(p) will by default cuase the current program to abort.
  94. */
  95. void dlfree(void*);
  96. /*
  97. calloc(size_t n_elements, size_t element_size);
  98. Returns a pointer to n_elements * element_size bytes, with all locations
  99. set to zero.
  100. */
  101. void* dlcalloc(size_t, size_t);
  102. /*
  103. realloc(void* p, size_t n)
  104. Returns a pointer to a chunk of size n that contains the same data
  105. as does chunk p up to the minimum of (n, p's size) bytes, or null
  106. if no space is available.
  107. The returned pointer may or may not be the same as p. The algorithm
  108. prefers extending p in most cases when possible, otherwise it
  109. employs the equivalent of a malloc-copy-free sequence.
  110. If p is null, realloc is equivalent to malloc.
  111. If space is not available, realloc returns null, errno is set (if on
  112. ANSI) and p is NOT freed.
  113. if n is for fewer bytes than already held by p, the newly unused
  114. space is lopped off and freed if possible. realloc with a size
  115. argument of zero (re)allocates a minimum-sized chunk.
  116. The old unix realloc convention of allowing the last-free'd chunk
  117. to be used as an argument to realloc is not supported.
  118. */
  119. void* dlrealloc(void*, size_t);
  120. /*
  121. memalign(size_t alignment, size_t n);
  122. Returns a pointer to a newly allocated chunk of n bytes, aligned
  123. in accord with the alignment argument.
  124. The alignment argument should be a power of two. If the argument is
  125. not a power of two, the nearest greater power is used.
  126. 8-byte alignment is guaranteed by normal malloc calls, so don't
  127. bother calling memalign with an argument of 8 or less.
  128. Overreliance on memalign is a sure way to fragment space.
  129. */
  130. void* dlmemalign(size_t, size_t);
  131. /*
  132. valloc(size_t n);
  133. Equivalent to memalign(pagesize, n), where pagesize is the page
  134. size of the system. If the pagesize is unknown, 4096 is used.
  135. */
  136. void* dlvalloc(size_t);
  137. /*
  138. mallopt(int parameter_number, int parameter_value)
  139. Sets tunable parameters The format is to provide a
  140. (parameter-number, parameter-value) pair. mallopt then sets the
  141. corresponding parameter to the argument value if it can (i.e., so
  142. long as the value is meaningful), and returns 1 if successful else
  143. 0. SVID/XPG/ANSI defines four standard param numbers for mallopt,
  144. normally defined in malloc.h. None of these are use in this malloc,
  145. so setting them has no effect. But this malloc also supports other
  146. options in mallopt:
  147. Symbol param # default allowed param values
  148. M_TRIM_THRESHOLD -1 2*1024*1024 any (-1U disables trimming)
  149. M_GRANULARITY -2 page size any power of 2 >= page size
  150. M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support)
  151. */
  152. int dlmallopt(int, int);
  153. #define M_TRIM_THRESHOLD (-1)
  154. #define M_GRANULARITY (-2)
  155. #define M_MMAP_THRESHOLD (-3)
  156. /*
  157. malloc_footprint();
  158. Returns the number of bytes obtained from the system. The total
  159. number of bytes allocated by malloc, realloc etc., is less than this
  160. value. Unlike mallinfo, this function returns only a precomputed
  161. result, so can be called frequently to monitor memory consumption.
  162. Even if locks are otherwise defined, this function does not use them,
  163. so results might not be up to date.
  164. */
  165. size_t dlmalloc_footprint();
  166. #if !NO_MALLINFO
  167. /*
  168. mallinfo()
  169. Returns (by copy) a struct containing various summary statistics:
  170. arena: current total non-mmapped bytes allocated from system
  171. ordblks: the number of free chunks
  172. smblks: always zero.
  173. hblks: current number of mmapped regions
  174. hblkhd: total bytes held in mmapped regions
  175. usmblks: the maximum total allocated space. This will be greater
  176. than current total if trimming has occurred.
  177. fsmblks: always zero
  178. uordblks: current total allocated space (normal or mmapped)
  179. fordblks: total free space
  180. keepcost: the maximum number of bytes that could ideally be released
  181. back to system via malloc_trim. ("ideally" means that
  182. it ignores page restrictions etc.)
  183. Because these fields are ints, but internal bookkeeping may
  184. be kept as longs, the reported values may wrap around zero and
  185. thus be inaccurate.
  186. */
  187. struct mallinfo dlmallinfo(void);
  188. #endif /* NO_MALLINFO */
  189. /*
  190. independent_calloc(size_t n_elements, size_t element_size, void* chunks[]);
  191. independent_calloc is similar to calloc, but instead of returning a
  192. single cleared space, it returns an array of pointers to n_elements
  193. independent elements that can hold contents of size elem_size, each
  194. of which starts out cleared, and can be independently freed,
  195. realloc'ed etc. The elements are guaranteed to be adjacently
  196. allocated (this is not guaranteed to occur with multiple callocs or
  197. mallocs), which may also improve cache locality in some
  198. applications.
  199. The "chunks" argument is optional (i.e., may be null, which is
  200. probably the most typical usage). If it is null, the returned array
  201. is itself dynamically allocated and should also be freed when it is
  202. no longer needed. Otherwise, the chunks array must be of at least
  203. n_elements in length. It is filled in with the pointers to the
  204. chunks.
  205. In either case, independent_calloc returns this pointer array, or
  206. null if the allocation failed. If n_elements is zero and "chunks"
  207. is null, it returns a chunk representing an array with zero elements
  208. (which should be freed if not wanted).
  209. Each element must be individually freed when it is no longer
  210. needed. If you'd like to instead be able to free all at once, you
  211. should instead use regular calloc and assign pointers into this
  212. space to represent elements. (In this case though, you cannot
  213. independently free elements.)
  214. independent_calloc simplifies and speeds up implementations of many
  215. kinds of pools. It may also be useful when constructing large data
  216. structures that initially have a fixed number of fixed-sized nodes,
  217. but the number is not known at compile time, and some of the nodes
  218. may later need to be freed. For example:
  219. struct Node { int item; struct Node* next; };
  220. struct Node* build_list() {
  221. struct Node** pool;
  222. int n = read_number_of_nodes_needed();
  223. if (n <= 0) return 0;
  224. pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0);
  225. if (pool == 0) die();
  226. // organize into a linked list...
  227. struct Node* first = pool[0];
  228. for (i = 0; i < n-1; ++i)
  229. pool[i]->next = pool[i+1];
  230. free(pool); // Can now free the array (or not, if it is needed later)
  231. return first;
  232. }
  233. */
  234. void** dlindependent_calloc(size_t, size_t, void**);
  235. /*
  236. independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]);
  237. independent_comalloc allocates, all at once, a set of n_elements
  238. chunks with sizes indicated in the "sizes" array. It returns
  239. an array of pointers to these elements, each of which can be
  240. independently freed, realloc'ed etc. The elements are guaranteed to
  241. be adjacently allocated (this is not guaranteed to occur with
  242. multiple callocs or mallocs), which may also improve cache locality
  243. in some applications.
  244. The "chunks" argument is optional (i.e., may be null). If it is null
  245. the returned array is itself dynamically allocated and should also
  246. be freed when it is no longer needed. Otherwise, the chunks array
  247. must be of at least n_elements in length. It is filled in with the
  248. pointers to the chunks.
  249. In either case, independent_comalloc returns this pointer array, or
  250. null if the allocation failed. If n_elements is zero and chunks is
  251. null, it returns a chunk representing an array with zero elements
  252. (which should be freed if not wanted).
  253. Each element must be individually freed when it is no longer
  254. needed. If you'd like to instead be able to free all at once, you
  255. should instead use a single regular malloc, and assign pointers at
  256. particular offsets in the aggregate space. (In this case though, you
  257. cannot independently free elements.)
  258. independent_comallac differs from independent_calloc in that each
  259. element may have a different size, and also that it does not
  260. automatically clear elements.
  261. independent_comalloc can be used to speed up allocation in cases
  262. where several structs or objects must always be allocated at the
  263. same time. For example:
  264. struct Head { ... }
  265. struct Foot { ... }
  266. void send_message(char* msg) {
  267. int msglen = strlen(msg);
  268. size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) };
  269. void* chunks[3];
  270. if (independent_comalloc(3, sizes, chunks) == 0)
  271. die();
  272. struct Head* head = (struct Head*)(chunks[0]);
  273. char* body = (char*)(chunks[1]);
  274. struct Foot* foot = (struct Foot*)(chunks[2]);
  275. // ...
  276. }
  277. In general though, independent_comalloc is worth using only for
  278. larger values of n_elements. For small values, you probably won't
  279. detect enough difference from series of malloc calls to bother.
  280. Overuse of independent_comalloc can increase overall memory usage,
  281. since it cannot reuse existing noncontiguous small chunks that
  282. might be available for some of the elements.
  283. */
  284. void** dlindependent_comalloc(size_t, size_t*, void**);
  285. /*
  286. pvalloc(size_t n);
  287. Equivalent to valloc(minimum-page-that-holds(n)), that is,
  288. round up n to nearest pagesize.
  289. */
  290. void* dlpvalloc(size_t);
  291. /*
  292. malloc_trim(size_t pad);
  293. If possible, gives memory back to the system (via negative arguments
  294. to sbrk) if there is unused memory at the `high' end of the malloc
  295. pool or in unused MMAP segments. You can call this after freeing
  296. large blocks of memory to potentially reduce the system-level memory
  297. requirements of a program. However, it cannot guarantee to reduce
  298. memory. Under some allocation patterns, some large free blocks of
  299. memory will be locked between two used chunks, so they cannot be
  300. given back to the system.
  301. The `pad' argument to malloc_trim represents the amount of free
  302. trailing space to leave untrimmed. If this argument is zero, only
  303. the minimum amount of memory to maintain internal data structures
  304. will be left. Non-zero arguments can be supplied to maintain enough
  305. trailing space to service future expected allocations without having
  306. to re-obtain memory from the system.
  307. Malloc_trim returns 1 if it actually released any memory, else 0.
  308. */
  309. int dlmalloc_trim(size_t);
  310. /*
  311. malloc_stats();
  312. Prints on stderr the amount of space obtained from the system (both
  313. via sbrk and mmap), the maximum amount (which may be more than
  314. current if malloc_trim and/or munmap got called), and the current
  315. number of bytes allocated via malloc (or realloc, etc) but not yet
  316. freed. Note that this is the number of bytes allocated, not the
  317. number requested. It will be larger than the number requested
  318. because of alignment and bookkeeping overhead. Because it includes
  319. alignment wastage as being in use, this figure may be greater than
  320. zero even when no user-level chunks are allocated.
  321. The reported current and maximum system memory can be inaccurate if
  322. a program makes other calls to system memory allocation functions
  323. (normally sbrk) outside of malloc.
  324. malloc_stats prints only the most commonly interesting statistics.
  325. More information can be obtained by calling mallinfo.
  326. */
  327. void dlmalloc_stats();
  328. #endif /* !ONLY_MSPACES */
  329. /*
  330. malloc_usable_size(void* p);
  331. Returns the number of bytes you can actually use in
  332. an allocated chunk, which may be more than you requested (although
  333. often not) due to alignment and minimum size constraints.
  334. You can use this many bytes without worrying about
  335. overwriting other allocated objects. This is not a particularly great
  336. programming practice. malloc_usable_size can be more useful in
  337. debugging and assertions, for example:
  338. p = malloc(n);
  339. assert(malloc_usable_size(p) >= 256);
  340. */
  341. size_t dlmalloc_usable_size(void*);
  342. #if MSPACES
  343. /*
  344. mspace is an opaque type representing an independent
  345. region of space that supports mspace_malloc, etc.
  346. */
  347. typedef void* mspace;
  348. /*
  349. create_mspace creates and returns a new independent space with the
  350. given initial capacity, or, if 0, the default granularity size. It
  351. returns null if there is no system memory available to create the
  352. space. If argument locked is non-zero, the space uses a separate
  353. lock to control access. The capacity of the space will grow
  354. dynamically as needed to service mspace_malloc requests. You can
  355. control the sizes of incremental increases of this space by
  356. compiling with a different DEFAULT_GRANULARITY or dynamically
  357. setting with mallopt(M_GRANULARITY, value).
  358. */
  359. mspace create_mspace(size_t capacity, int locked);
  360. /*
  361. destroy_mspace destroys the given space, and attempts to return all
  362. of its memory back to the system, returning the total number of
  363. bytes freed. After destruction, the results of access to all memory
  364. used by the space become undefined.
  365. */
  366. size_t destroy_mspace(mspace msp);
  367. /*
  368. create_mspace_with_base uses the memory supplied as the initial base
  369. of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this
  370. space is used for bookkeeping, so the capacity must be at least this
  371. large. (Otherwise 0 is returned.) When this initial space is
  372. exhausted, additional memory will be obtained from the system.
  373. Destroying this space will deallocate all additionally allocated
  374. space (if possible) but not the initial base.
  375. */
  376. mspace create_mspace_with_base(void* base, size_t capacity, int locked);
  377. /*
  378. mspace_track_large_chunks controls whether requests for large chunks
  379. are allocated in their own untracked mmapped regions, separate from
  380. others in this mspace. By default large chunks are not tracked,
  381. which reduces fragmentation. However, such chunks are not
  382. necessarily released to the system upon destroy_mspace. Enabling
  383. tracking by setting to true may increase fragmentation, but avoids
  384. leakage when relying on destroy_mspace to release all memory
  385. allocated using this space. The function returns the previous
  386. setting.
  387. */
  388. int mspace_track_large_chunks(mspace msp, int enable);
  389. /*
  390. mspace_malloc behaves as malloc, but operates within
  391. the given space.
  392. */
  393. void* mspace_malloc(mspace msp, size_t bytes);
  394. /*
  395. mspace_free behaves as free, but operates within
  396. the given space.
  397. If compiled with FOOTERS==1, mspace_free is not actually needed.
  398. free may be called instead of mspace_free because freed chunks from
  399. any space are handled by their originating spaces.
  400. */
  401. void mspace_free(mspace msp, void* mem);
  402. /*
  403. mspace_realloc behaves as realloc, but operates within
  404. the given space.
  405. If compiled with FOOTERS==1, mspace_realloc is not actually
  406. needed. realloc may be called instead of mspace_realloc because
  407. realloced chunks from any space are handled by their originating
  408. spaces.
  409. */
  410. void* mspace_realloc(mspace msp, void* mem, size_t newsize);
  411. /*
  412. mspace_calloc behaves as calloc, but operates within
  413. the given space.
  414. */
  415. void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size);
  416. /*
  417. mspace_memalign behaves as memalign, but operates within
  418. the given space.
  419. */
  420. void* mspace_memalign(mspace msp, size_t alignment, size_t bytes);
  421. /*
  422. mspace_independent_calloc behaves as independent_calloc, but
  423. operates within the given space.
  424. */
  425. void** mspace_independent_calloc(mspace msp, size_t n_elements,
  426. size_t elem_size, void* chunks[]);
  427. /*
  428. mspace_independent_comalloc behaves as independent_comalloc, but
  429. operates within the given space.
  430. */
  431. void** mspace_independent_comalloc(mspace msp, size_t n_elements,
  432. size_t sizes[], void* chunks[]);
  433. /*
  434. mspace_footprint() returns the number of bytes obtained from the
  435. system for this space.
  436. */
  437. size_t mspace_footprint(mspace msp);
  438. #if !NO_MALLINFO
  439. /*
  440. mspace_mallinfo behaves as mallinfo, but reports properties of
  441. the given space.
  442. */
  443. struct mallinfo mspace_mallinfo(mspace msp);
  444. #endif /* NO_MALLINFO */
  445. /*
  446. malloc_usable_size(void* p) behaves the same as malloc_usable_size;
  447. */
  448. size_t mspace_usable_size(void* mem);
  449. /*
  450. mspace_malloc_stats behaves as malloc_stats, but reports
  451. properties of the given space.
  452. */
  453. void mspace_malloc_stats(mspace msp);
  454. /*
  455. mspace_trim behaves as malloc_trim, but
  456. operates within the given space.
  457. */
  458. int mspace_trim(mspace msp, size_t pad);
  459. /*
  460. An alias for mallopt.
  461. */
  462. int mspace_mallopt(int, int);
  463. #endif /* MSPACES */
  464. #ifdef __cplusplus
  465. }; /* end of extern "C" */
  466. #endif
  467. #endif /* MALLOC_280_H */