pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:24:   };      #ifdef BA_SEGREGATE   struct ba_block_header {    uint32_t next;   };   #endif      #include "block_allocator.h"    + size_t _malloc_counter = 0; +    static inline void ba_htable_insert(const struct block_allocator * a,    const void * ptr, const uint16_t n);   static inline void ba_remove_page(struct block_allocator * a,    uint16_t n);      #define BA_BLOCKN(a, p, n) ((void*)(((p)->data) + (n)*((a)->block_size)))   #define BA_LASTBLOCK(a, p) BA_BLOCKN(a, p, (a)->blocks - 1)   #define BA_NBLOCK(a, p, ptr) ((uintptr_t)((char*)ptr - (p)->data)/(a)->block_size)      #define BA_DIVIDE(a, b) ((a) / (b) + !!((a) & ((b)-1)))
pike.git/src/block_allocator.c:102:       if ((page_size & (page_size - 1)) == 0)    a->magnitude = (uint16_t)ctz32(page_size);    else    a->magnitude = (uint16_t)ctz32(round_up32(page_size));    //fprintf(stderr, "page_size: %u, magnitude: %u\n", page_size, a->magnitude);       a->block_size = block_size;    a->blocks = blocks;    a->num_pages = 0; +  a->empty_blocks = 0; +  a->max_empty_blocks = 3;       // we start with management structures for 16 pages    a->allocated = 16;    a->pages = NULL;   #ifndef BA_SEGREGATE    a->ba_page_size = BA_SPAGE_SIZE(a);   #endif    ba_realloc(a);   }   
pike.git/src/block_allocator.c:364:    a->first = a->last = 0;    } else {    a->first = p->next;    }    p->next = 0;    p->free_mask = BA_MASK_NUM(a);    return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));    }   #else    if (p->blocks_used == a->blocks) Pike_error("baaad!\n"); +  if (p->blocks_used == 0) { +  a->empty_pages--; +  }    p->blocks_used ++;   #ifdef BA_DEBUG    if (p->first < 1 || p->first > a->blocks) {    fprintf(stderr, "bad index: %d (should be [1..%d]\n", p->first-1, a->blocks);    }   #endif    ptr = BA_BLOCKN(a, p, p->first-1);      #ifdef BA_DEBUG    if (!p->data) {
pike.git/src/block_allocator.c:408:       //fprintf(stderr, "allocating new page. was %p\n", p);    if (unlikely(a->num_pages == a->allocated))    ba_grow(a);       p = BA_PAGE(a, ++a->num_pages);    if (p->data) {    fprintf(stderr, "reusing unfreed page\n");    }    p->data = malloc(BA_PAGESIZE(a)); +  _malloc_counter++;    if (!p->data) {    Pike_error("no mem");    }    p->next = p->prev = 0;    a->first = a->last = a->num_pages;    ba_htable_insert(a, BA_LASTBLOCK(a, p), a->num_pages);   #ifndef BA_SEGREGATE    if (BA_MASK_NUM(a) > 1)    memset((void*)p->mask, 0xff, (BA_MASK_NUM(a)-1)*sizeof(uintptr_t));    p->mask[BA_MASK_NUM(a)-1] = BA_LAST_MASK(a);
pike.git/src/block_allocator.c:547:    if (p->blocks_used == a->blocks) {    if (a->first == 0) {    a->first = a->last = n;    p->next = p->prev = 0;    } else {    p->next = a->first;    BA_PAGE(a, a->first)->prev = n;    a->first = n;    }    } else if (p->blocks_used == 1) { +  if (a->empty_pages == a->max_empty_pages) {    ba_remove_page(a, n); -  +  }    return;    }    p->blocks_used --;    ((struct ba_block_header*)ptr)->next = p->first;    //fprintf(stderr, "setting first to %u (%p vs %p) n: %u vs %u\n", t+1, BA_BLOCKN(a, p, t+1), ptr, BA_NBLOCK(a, p, BA_BLOCKN(a, p, t+1)), BA_NBLOCK(a, p, ptr));    p->first = t+1;   #endif   }      static inline void ba_remove_page(struct block_allocator * a,