Counter Strike : Global Offensive Source Code
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.

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