Last active
November 5, 2019 10:06
-
-
Save tesuji/6e92d11c5c5400f0033ef4caaf7b99b2 to your computer and use it in GitHub Desktop.
glibc 2.29
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! from glibc-2.29 | |
#include <stdio.h> | |
#include <stdlib.h> | |
#define INTERNAL_SIZE_T size_t | |
#define SIZE_SZ sizeof(INTERNAL_SIZE_T) | |
struct malloc_chunk { | |
INTERNAL_SIZE_T mchunk_prev_size; /* Size of previous chunk (if free). */ | |
INTERNAL_SIZE_T mchunk_size; /* Size in bytes, including overhead. */ | |
struct malloc_chunk* fd; /* double links -- used only if free. */ | |
struct malloc_chunk* bk; | |
}; | |
/* The smallest possible chunk */ | |
#define MIN_CHUNK_SIZE sizeof(struct malloc_chunk) | |
#define MALLOC_ALIGNMENT (2 * SIZE_SZ) | |
#define MALLOC_ALIGN_MASK ( MALLOC_ALIGNMENT - 1 ) | |
#define MINSIZE \ | |
(unsigned long)(((MIN_CHUNK_SIZE+MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)) | |
#define request2size(req) \ | |
(((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE) ? \ | |
MINSIZE : \ | |
((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK) | |
#define NBINS 128 | |
#define NSMALLBINS 64 | |
#define SMALLBIN_WIDTH MALLOC_ALIGNMENT | |
#define SMALLBIN_CORRECTION (MALLOC_ALIGNMENT > 2 * SIZE_SZ) | |
#define MIN_LARGE_SIZE ((NSMALLBINS - SMALLBIN_CORRECTION) * SMALLBIN_WIDTH) | |
#define in_smallbin_range(sz) \ | |
((unsigned long) (sz) < (unsigned long) MIN_LARGE_SIZE) | |
#define smallbin_index(sz) \ | |
((SMALLBIN_WIDTH == 16 ? (((unsigned) (sz)) >> 4) : (((unsigned) (sz)) >> 3))\ | |
+ SMALLBIN_CORRECTION) | |
#define largebin_index_32(sz) \ | |
(((((unsigned long) (sz)) >> 6) <= 38) ? 56 + (((unsigned long) (sz)) >> 6) :\ | |
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\ | |
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | |
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | |
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | |
126) | |
#define largebin_index_32_big(sz) \ | |
(((((unsigned long) (sz)) >> 6) <= 45) ? 49 + (((unsigned long) (sz)) >> 6) :\ | |
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\ | |
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | |
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | |
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | |
126) | |
// XXX It remains to be seen whether it is good to keep the widths of | |
// XXX the buckets the same or whether it should be scaled by a factor | |
// XXX of two as well. | |
#define largebin_index_64(sz) \ | |
(((((unsigned long) (sz)) >> 6) <= 48) ? 48 + (((unsigned long) (sz)) >> 6) :\ | |
((((unsigned long) (sz)) >> 9) <= 20) ? 91 + (((unsigned long) (sz)) >> 9) :\ | |
((((unsigned long) (sz)) >> 12) <= 10) ? 110 + (((unsigned long) (sz)) >> 12) :\ | |
((((unsigned long) (sz)) >> 15) <= 4) ? 119 + (((unsigned long) (sz)) >> 15) :\ | |
((((unsigned long) (sz)) >> 18) <= 2) ? 124 + (((unsigned long) (sz)) >> 18) :\ | |
126) | |
#define largebin_index(sz) \ | |
(SIZE_SZ == 8 ? largebin_index_64 (sz) \ | |
: MALLOC_ALIGNMENT == 16 ? largebin_index_32_big (sz) \ | |
: largebin_index_32 (sz)) | |
#define bin_index(sz) \ | |
((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz)) | |
int main(int argc, char **argv) { | |
if (argc != 2) { | |
printf("Usage: %s <size>\n", argv[0]); | |
return 1; | |
} | |
unsigned long req = atoi(argv[1]); | |
unsigned long size = request2size(req); | |
unsigned long idx = bin_index(req); | |
printf( | |
"request: %lu\n" | |
"size: %lu\n" | |
"bin index: %lu\n", | |
req, size, idx | |
); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment