Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stddef.h>
- #include <stdio.h>
- #include <stdlib.h>
- #ifndef TEST_SIZE
- #define TEST_SIZE 16
- #endif
- char* arenas[20];
- size_t numArenas;
- size_t newArenaGap = 8192;
- size_t nearestArena(char* ptr)
- {
- size_t bestGap = newArenaGap;
- size_t best;
- for (int i = 0; i < numArenas; ++i) {
- size_t gap = labs(ptr-arenas[i]);
- if (gap < bestGap) {
- best = i;
- bestGap = gap;
- }
- }
- if (bestGap < newArenaGap)
- return best;
- arenas[numArenas] = ptr;
- return numArenas++;
- }
- void doMalloc(size_t size) {
- char* mem = (char*) malloc(size);
- if (newArenaGap < size*2)
- newArenaGap = size*2;
- size_t i = nearestArena(mem);
- if (arenas[i] != mem) {
- ptrdiff_t delta = (char*)mem - (char*)arenas[i];
- printf("malloc(%zu)\t= %p (%c%+zd)\n", size, mem, 'p'+i, delta);
- } else {
- printf("malloc(%zu)\t= %p (%c)\n", size, mem,'p'+i);
- }
- }
- int main()
- {
- for(int i = 0; i < 16; ++i) {
- doMalloc(TEST_SIZE);
- doMalloc(TEST_SIZE*(2<<i));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement