Révision | 1bc015efddaf7dc4ddd1b6b6831c43e7ad4582c0 (tree) |
---|---|
l'heure | 2018-02-01 03:48:29 |
Auteur | Waldemar Brodkorb <wbx@ucli...> |
Commiter | Waldemar Brodkorb |
malloc: add malloc_usable_size()
@@ -162,6 +162,8 @@ extern int malloc_trim(size_t pad); | ||
162 | 162 | /* Prints brief summary statistics on the stderr. */ |
163 | 163 | extern void malloc_stats(void); |
164 | 164 | |
165 | +extern size_t malloc_usable_size(void *); | |
166 | + | |
165 | 167 | /* SVID2/XPG mallopt options */ |
166 | 168 | #ifndef M_MXFAST |
167 | 169 | # define M_MXFAST 1 /* UNUSED in this malloc */ |
@@ -0,0 +1,14 @@ | ||
1 | +/* | |
2 | + malloc_usable_size - fully inspired by musl implementation | |
3 | +*/ | |
4 | + | |
5 | +#include "malloc.h" | |
6 | + | |
7 | +/* for malloc_usable_size */ | |
8 | +#define OVERHEAD (2*sizeof(size_t)) | |
9 | +#define CHUNK_SIZE(c) ((c)->size & -2) | |
10 | +#define MEM_TO_CHUNK(p) (struct malloc_chunk *)((char *)(p) - OVERHEAD) | |
11 | + | |
12 | +size_t malloc_usable_size(void *p) { | |
13 | + return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0; | |
14 | +} |