00001 /* 00002 Default header file for malloc-2.8.x, written by Doug Lea 00003 and released to the public domain, as explained at 00004 http://creativecommons.org/licenses/publicdomain. 00005 00006 last update: Mon Aug 15 08:55:52 2005 Doug Lea (dl at gee) 00007 00008 This header is for ANSI C/C++ only. You can set any of 00009 the following #defines before including: 00010 00011 * If USE_DL_PREFIX is defined, it is assumed that malloc.c 00012 was also compiled with this option, so all routines 00013 have names starting with "dl". 00014 00015 * If HAVE_USR_INCLUDE_MALLOC_H is defined, it is assumed that this 00016 file will be #included AFTER <malloc.h>. This is needed only if 00017 your system defines a struct mallinfo that is incompatible with the 00018 standard one declared here. Otherwise, you can include this file 00019 INSTEAD of your system system <malloc.h>. At least on ANSI, all 00020 declarations should be compatible with system versions 00021 00022 * If MSPACES is defined, declarations for mspace versions are included. 00023 */ 00024 00025 #ifndef MALLOC_280_H 00026 #define MALLOC_280_H 00027 00028 #include "dl_config.h" 00029 #include "meminfo.h" 00030 00031 #ifdef __cplusplus 00032 extern "C" { 00033 #endif 00034 00035 #include <stddef.h> /* for size_t */ 00036 00037 #if !ONLY_MSPACES 00038 00039 #ifndef USE_DL_PREFIX 00040 #define dlcalloc calloc 00041 #define dlfree free 00042 #define dlmalloc malloc 00043 #define dlmemalign memalign 00044 #define dlrealloc realloc 00045 #define dlvalloc valloc 00046 #define dlpvalloc pvalloc 00047 #define dlmallinfo mallinfo 00048 #define dlmallopt mallopt 00049 #define dlmalloc_trim malloc_trim 00050 #define dlmalloc_stats malloc_stats 00051 #define dlmalloc_usable_size malloc_usable_size 00052 #define dlmalloc_footprint malloc_footprint 00053 #define dlindependent_calloc independent_calloc 00054 #define dlindependent_comalloc independent_comalloc 00055 #endif /* USE_DL_PREFIX */ 00056 00057 00058 /* 00059 malloc(size_t n) 00060 Returns a pointer to a newly allocated chunk of at least n bytes, or 00061 null if no space is available, in which case errno is set to ENOMEM 00062 on ANSI C systems. 00063 00064 If n is zero, malloc returns a minimum-sized chunk. (The minimum 00065 size is 16 bytes on most 32bit systems, and 32 bytes on 64bit 00066 systems.) Note that size_t is an unsigned type, so calls with 00067 arguments that would be negative if signed are interpreted as 00068 requests for huge amounts of space, which will often fail. The 00069 maximum supported value of n differs across systems, but is in all 00070 cases less than the maximum representable value of a size_t. 00071 */ 00072 void* dlmalloc(size_t); 00073 00074 /* 00075 free(void* p) 00076 Releases the chunk of memory pointed to by p, that had been previously 00077 allocated using malloc or a related routine such as realloc. 00078 It has no effect if p is null. If p was not malloced or already 00079 freed, free(p) will by default cuase the current program to abort. 00080 */ 00081 void dlfree(void*); 00082 00083 /* 00084 calloc(size_t n_elements, size_t element_size); 00085 Returns a pointer to n_elements * element_size bytes, with all locations 00086 set to zero. 00087 */ 00088 void* dlcalloc(size_t, size_t); 00089 00090 /* 00091 realloc(void* p, size_t n) 00092 Returns a pointer to a chunk of size n that contains the same data 00093 as does chunk p up to the minimum of (n, p's size) bytes, or null 00094 if no space is available. 00095 00096 The returned pointer may or may not be the same as p. The algorithm 00097 prefers extending p in most cases when possible, otherwise it 00098 employs the equivalent of a malloc-copy-free sequence. 00099 00100 If p is null, realloc is equivalent to malloc. 00101 00102 If space is not available, realloc returns null, errno is set (if on 00103 ANSI) and p is NOT freed. 00104 00105 if n is for fewer bytes than already held by p, the newly unused 00106 space is lopped off and freed if possible. realloc with a size 00107 argument of zero (re)allocates a minimum-sized chunk. 00108 00109 The old unix realloc convention of allowing the last-free'd chunk 00110 to be used as an argument to realloc is not supported. 00111 */ 00112 00113 void* dlrealloc(void*, size_t); 00114 00115 /* 00116 memalign(size_t alignment, size_t n); 00117 Returns a pointer to a newly allocated chunk of n bytes, aligned 00118 in accord with the alignment argument. 00119 00120 The alignment argument should be a power of two. If the argument is 00121 not a power of two, the nearest greater power is used. 00122 8-byte alignment is guaranteed by normal malloc calls, so don't 00123 bother calling memalign with an argument of 8 or less. 00124 00125 Overreliance on memalign is a sure way to fragment space. 00126 */ 00127 void* dlmemalign(size_t, size_t); 00128 00129 /* 00130 valloc(size_t n); 00131 Equivalent to memalign(pagesize, n), where pagesize is the page 00132 size of the system. If the pagesize is unknown, 4096 is used. 00133 */ 00134 void* dlvalloc(size_t); 00135 00136 /* 00137 mallopt(int parameter_number, int parameter_value) 00138 Sets tunable parameters The format is to provide a 00139 (parameter-number, parameter-value) pair. mallopt then sets the 00140 corresponding parameter to the argument value if it can (i.e., so 00141 long as the value is meaningful), and returns 1 if successful else 00142 0. SVID/XPG/ANSI defines four standard param numbers for mallopt, 00143 normally defined in malloc.h. None of these are use in this malloc, 00144 so setting them has no effect. But this malloc also supports other 00145 options in mallopt: 00146 00147 Symbol param # default allowed param values 00148 M_TRIM_THRESHOLD -1 2*1024*1024 any (-1U disables trimming) 00149 M_GRANULARITY -2 page size any power of 2 >= page size 00150 M_MMAP_THRESHOLD -3 256*1024 any (or 0 if no MMAP support) 00151 */ 00152 int dlmallopt(int, int); 00153 00154 #define M_TRIM_THRESHOLD (-1) 00155 #define M_GRANULARITY (-2) 00156 #define M_MMAP_THRESHOLD (-3) 00157 00158 00159 /* 00160 malloc_footprint(); 00161 Returns the number of bytes obtained from the system. The total 00162 number of bytes allocated by malloc, realloc etc., is less than this 00163 value. Unlike mallinfo, this function returns only a precomputed 00164 result, so can be called frequently to monitor memory consumption. 00165 Even if locks are otherwise defined, this function does not use them, 00166 so results might not be up to date. 00167 */ 00168 size_t dlmalloc_footprint(); 00169 00170 #if !NO_MALLINFO 00171 /* 00172 mallinfo() 00173 Returns (by copy) a struct containing various summary statistics: 00174 00175 arena: current total non-mmapped bytes allocated from system 00176 ordblks: the number of free chunks 00177 smblks: always zero. 00178 hblks: current number of mmapped regions 00179 hblkhd: total bytes held in mmapped regions 00180 usmblks: the maximum total allocated space. This will be greater 00181 than current total if trimming has occurred. 00182 fsmblks: always zero 00183 uordblks: current total allocated space (normal or mmapped) 00184 fordblks: total free space 00185 keepcost: the maximum number of bytes that could ideally be released 00186 back to system via malloc_trim. ("ideally" means that 00187 it ignores page restrictions etc.) 00188 00189 Because these fields are ints, but internal bookkeeping may 00190 be kept as longs, the reported values may wrap around zero and 00191 thus be inaccurate. 00192 */ 00193 #ifndef HAVE_USR_INCLUDE_MALLOC_H 00194 #ifndef _MALLOC_H 00195 #ifndef MALLINFO_FIELD_TYPE 00196 #define MALLINFO_FIELD_TYPE size_t 00197 #endif /* MALLINFO_FIELD_TYPE */ 00198 struct mallinfo { 00199 MALLINFO_FIELD_TYPE arena; /* non-mmapped space allocated from system */ 00200 MALLINFO_FIELD_TYPE ordblks; /* number of free chunks */ 00201 MALLINFO_FIELD_TYPE smblks; /* always 0 */ 00202 MALLINFO_FIELD_TYPE hblks; /* always 0 */ 00203 MALLINFO_FIELD_TYPE hblkhd; /* space in mmapped regions */ 00204 MALLINFO_FIELD_TYPE usmblks; /* maximum total allocated space */ 00205 MALLINFO_FIELD_TYPE fsmblks; /* always 0 */ 00206 MALLINFO_FIELD_TYPE uordblks; /* total allocated space */ 00207 MALLINFO_FIELD_TYPE fordblks; /* total free space */ 00208 MALLINFO_FIELD_TYPE keepcost; /* releasable (via malloc_trim) space */ 00209 }; 00210 #endif /* _MALLOC_H */ 00211 #endif /* HAVE_USR_INCLUDE_MALLOC_H */ 00212 00213 struct mallinfo dlmallinfo(void); 00214 #endif /* NO_MALLINFO */ 00215 00216 /* 00217 independent_calloc(size_t n_elements, size_t element_size, void* chunks[]); 00218 00219 independent_calloc is similar to calloc, but instead of returning a 00220 single cleared space, it returns an array of pointers to n_elements 00221 independent elements that can hold contents of size elem_size, each 00222 of which starts out cleared, and can be independently freed, 00223 realloc'ed etc. The elements are guaranteed to be adjacently 00224 allocated (this is not guaranteed to occur with multiple callocs or 00225 mallocs), which may also improve cache locality in some 00226 applications. 00227 00228 The "chunks" argument is optional (i.e., may be null, which is 00229 probably the most typical usage). If it is null, the returned array 00230 is itself dynamically allocated and should also be freed when it is 00231 no longer needed. Otherwise, the chunks array must be of at least 00232 n_elements in length. It is filled in with the pointers to the 00233 chunks. 00234 00235 In either case, independent_calloc returns this pointer array, or 00236 null if the allocation failed. If n_elements is zero and "chunks" 00237 is null, it returns a chunk representing an array with zero elements 00238 (which should be freed if not wanted). 00239 00240 Each element must be individually freed when it is no longer 00241 needed. If you'd like to instead be able to free all at once, you 00242 should instead use regular calloc and assign pointers into this 00243 space to represent elements. (In this case though, you cannot 00244 independently free elements.) 00245 00246 independent_calloc simplifies and speeds up implementations of many 00247 kinds of pools. It may also be useful when constructing large data 00248 structures that initially have a fixed number of fixed-sized nodes, 00249 but the number is not known at compile time, and some of the nodes 00250 may later need to be freed. For example: 00251 00252 struct Node { int item; struct Node* next; }; 00253 00254 struct Node* build_list() { 00255 struct Node** pool; 00256 int n = read_number_of_nodes_needed(); 00257 if (n <= 0) return 0; 00258 pool = (struct Node**)(independent_calloc(n, sizeof(struct Node), 0); 00259 if (pool == 0) die(); 00260 // organize into a linked list... 00261 struct Node* first = pool[0]; 00262 for (i = 0; i < n-1; ++i) 00263 pool[i]->next = pool[i+1]; 00264 free(pool); // Can now free the array (or not, if it is needed later) 00265 return first; 00266 } 00267 */ 00268 void** dlindependent_calloc(size_t, size_t, void**); 00269 00270 /* 00271 independent_comalloc(size_t n_elements, size_t sizes[], void* chunks[]); 00272 00273 independent_comalloc allocates, all at once, a set of n_elements 00274 chunks with sizes indicated in the "sizes" array. It returns 00275 an array of pointers to these elements, each of which can be 00276 independently freed, realloc'ed etc. The elements are guaranteed to 00277 be adjacently allocated (this is not guaranteed to occur with 00278 multiple callocs or mallocs), which may also improve cache locality 00279 in some applications. 00280 00281 The "chunks" argument is optional (i.e., may be null). If it is null 00282 the returned array is itself dynamically allocated and should also 00283 be freed when it is no longer needed. Otherwise, the chunks array 00284 must be of at least n_elements in length. It is filled in with the 00285 pointers to the chunks. 00286 00287 In either case, independent_comalloc returns this pointer array, or 00288 null if the allocation failed. If n_elements is zero and chunks is 00289 null, it returns a chunk representing an array with zero elements 00290 (which should be freed if not wanted). 00291 00292 Each element must be individually freed when it is no longer 00293 needed. If you'd like to instead be able to free all at once, you 00294 should instead use a single regular malloc, and assign pointers at 00295 particular offsets in the aggregate space. (In this case though, you 00296 cannot independently free elements.) 00297 00298 independent_comallac differs from independent_calloc in that each 00299 element may have a different size, and also that it does not 00300 automatically clear elements. 00301 00302 independent_comalloc can be used to speed up allocation in cases 00303 where several structs or objects must always be allocated at the 00304 same time. For example: 00305 00306 struct Head { ... } 00307 struct Foot { ... } 00308 00309 void send_message(char* msg) { 00310 int msglen = strlen(msg); 00311 size_t sizes[3] = { sizeof(struct Head), msglen, sizeof(struct Foot) }; 00312 void* chunks[3]; 00313 if (independent_comalloc(3, sizes, chunks) == 0) 00314 die(); 00315 struct Head* head = (struct Head*)(chunks[0]); 00316 char* body = (char*)(chunks[1]); 00317 struct Foot* foot = (struct Foot*)(chunks[2]); 00318 // ... 00319 } 00320 00321 In general though, independent_comalloc is worth using only for 00322 larger values of n_elements. For small values, you probably won't 00323 detect enough difference from series of malloc calls to bother. 00324 00325 Overuse of independent_comalloc can increase overall memory usage, 00326 since it cannot reuse existing noncontiguous small chunks that 00327 might be available for some of the elements. 00328 */ 00329 void** dlindependent_comalloc(size_t, size_t*, void**); 00330 00331 00332 /* 00333 pvalloc(size_t n); 00334 Equivalent to valloc(minimum-page-that-holds(n)), that is, 00335 round up n to nearest pagesize. 00336 */ 00337 void* dlpvalloc(size_t); 00338 00339 /* 00340 malloc_trim(size_t pad); 00341 00342 If possible, gives memory back to the system (via negative arguments 00343 to sbrk) if there is unused memory at the `high' end of the malloc 00344 pool or in unused MMAP segments. You can call this after freeing 00345 large blocks of memory to potentially reduce the system-level memory 00346 requirements of a program. However, it cannot guarantee to reduce 00347 memory. Under some allocation patterns, some large free blocks of 00348 memory will be locked between two used chunks, so they cannot be 00349 given back to the system. 00350 00351 The `pad' argument to malloc_trim represents the amount of free 00352 trailing space to leave untrimmed. If this argument is zero, only 00353 the minimum amount of memory to maintain internal data structures 00354 will be left. Non-zero arguments can be supplied to maintain enough 00355 trailing space to service future expected allocations without having 00356 to re-obtain memory from the system. 00357 00358 Malloc_trim returns 1 if it actually released any memory, else 0. 00359 */ 00360 int dlmalloc_trim(size_t); 00361 00362 /* 00363 malloc_usable_size(void* p); 00364 00365 Returns the number of bytes you can actually use in 00366 an allocated chunk, which may be more than you requested (although 00367 often not) due to alignment and minimum size constraints. 00368 You can use this many bytes without worrying about 00369 overwriting other allocated objects. This is not a particularly great 00370 programming practice. malloc_usable_size can be more useful in 00371 debugging and assertions, for example: 00372 00373 p = malloc(n); 00374 assert(malloc_usable_size(p) >= 256); 00375 */ 00376 size_t dlmalloc_usable_size(void*); 00377 00378 /* 00379 malloc_stats(); 00380 Prints on stderr the amount of space obtained from the system (both 00381 via sbrk and mmap), the maximum amount (which may be more than 00382 current if malloc_trim and/or munmap got called), and the current 00383 number of bytes allocated via malloc (or realloc, etc) but not yet 00384 freed. Note that this is the number of bytes allocated, not the 00385 number requested. It will be larger than the number requested 00386 because of alignment and bookkeeping overhead. Because it includes 00387 alignment wastage as being in use, this figure may be greater than 00388 zero even when no user-level chunks are allocated. 00389 00390 The reported current and maximum system memory can be inaccurate if 00391 a program makes other calls to system memory allocation functions 00392 (normally sbrk) outside of malloc. 00393 00394 malloc_stats prints only the most commonly interesting statistics. 00395 More information can be obtained by calling mallinfo. 00396 */ 00397 void dlmalloc_stats(); 00398 00399 #endif /* !ONLY_MSPACES */ 00400 00401 #if MSPACES 00402 00403 /* 00404 mspace is an opaque type representing an independent 00405 region of space that supports mspace_malloc, etc. 00406 */ 00407 typedef void* mspace; 00408 00409 /* 00410 create_mspace creates and returns a new independent space with the 00411 given initial capacity, or, if 0, the default granularity size. It 00412 returns null if there is no system memory available to create the 00413 space. If argument locked is non-zero, the space uses a separate 00414 lock to control access. The capacity of the space will grow 00415 dynamically as needed to service mspace_malloc requests. You can 00416 control the sizes of incremental increases of this space by 00417 compiling with a different DEFAULT_GRANULARITY or dynamically 00418 setting with mallopt(M_GRANULARITY, value). 00419 */ 00420 mspace create_mspace(size_t capacity, int locked); 00421 00422 /* 00423 destroy_mspace destroys the given space, and attempts to return all 00424 of its memory back to the system, returning the total number of 00425 bytes freed. After destruction, the results of access to all memory 00426 used by the space become undefined. 00427 */ 00428 size_t destroy_mspace(mspace msp); 00429 00430 /* 00431 create_mspace_with_base uses the memory supplied as the initial base 00432 of a new mspace. Part (less than 128*sizeof(size_t) bytes) of this 00433 space is used for bookkeeping, so the capacity must be at least this 00434 large. (Otherwise 0 is returned.) When this initial space is 00435 exhausted, additional memory will be obtained from the system. 00436 Destroying this space will deallocate all additionally allocated 00437 space (if possible) but not the initial base. 00438 */ 00439 mspace create_mspace_with_base(void* base, size_t capacity, int locked); 00440 00441 /* 00442 mspace_malloc behaves as malloc, but operates within 00443 the given space. 00444 */ 00445 void* mspace_malloc(mspace msp, size_t bytes); 00446 00447 /* 00448 mspace_free behaves as free, but operates within 00449 the given space. 00450 00451 If compiled with FOOTERS==1, mspace_free is not actually needed. 00452 free may be called instead of mspace_free because freed chunks from 00453 any space are handled by their originating spaces. 00454 */ 00455 void mspace_free(mspace msp, void* mem); 00456 00457 /* 00458 mspace_realloc behaves as realloc, but operates within 00459 the given space. 00460 00461 If compiled with FOOTERS==1, mspace_realloc is not actually 00462 needed. realloc may be called instead of mspace_realloc because 00463 realloced chunks from any space are handled by their originating 00464 spaces. 00465 */ 00466 void* mspace_realloc(mspace msp, void* mem, size_t newsize); 00467 00468 /* 00469 mspace_calloc behaves as calloc, but operates within 00470 the given space. 00471 */ 00472 void* mspace_calloc(mspace msp, size_t n_elements, size_t elem_size); 00473 00474 /* 00475 mspace_memalign behaves as memalign, but operates within 00476 the given space. 00477 */ 00478 void* mspace_memalign(mspace msp, size_t alignment, size_t bytes); 00479 00480 /* 00481 mspace_independent_calloc behaves as independent_calloc, but 00482 operates within the given space. 00483 */ 00484 void** mspace_independent_calloc(mspace msp, size_t n_elements, 00485 size_t elem_size, void* chunks[]); 00486 00487 /* 00488 mspace_independent_comalloc behaves as independent_comalloc, but 00489 operates within the given space. 00490 */ 00491 void** mspace_independent_comalloc(mspace msp, size_t n_elements, 00492 size_t sizes[], void* chunks[]); 00493 00494 /* 00495 mspace_footprint() returns the number of bytes obtained from the 00496 system for this space. 00497 */ 00498 size_t mspace_footprint(mspace msp); 00499 00500 00501 #if !NO_MALLINFO 00502 /* 00503 mspace_mallinfo behaves as mallinfo, but reports properties of 00504 the given space. 00505 */ 00506 struct mallinfo mspace_mallinfo(mspace msp); 00507 #endif /* NO_MALLINFO */ 00508 00509 /* 00510 mspace_malloc_stats behaves as malloc_stats, but reports 00511 properties of the given space. 00512 */ 00513 void mspace_malloc_stats(mspace msp); 00514 00515 /* 00516 mspace_trim behaves as malloc_trim, but 00517 operates within the given space. 00518 */ 00519 int mspace_trim(mspace msp, size_t pad); 00520 00521 /* 00522 An alias for mallopt. 00523 */ 00524 int mspace_mallopt(int, int); 00525 00526 void mspace_info(mspace ms, struct mem_info* info); 00527 00528 #endif /* MSPACES */ 00529 00530 #ifdef __cplusplus 00531 }; /* end of extern "C" */ 00532 #endif 00533 00534 #endif /* MALLOC_280_H */
1.7.1