pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:18:    && !defined(STATIC_MODULES))   #endif   PMOD_EXPORT char errbuf[8192];      static INLINE void ba_htable_insert(const struct block_allocator * a,    const void * ptr, const ba_page_t n);      #define BA_NBLOCK(a, p, ptr) ((uintptr_t)((char*)ptr - (char)(p+1))/(a)->block_size)      #define BA_DIVIDE(a, b) ((a) / (b) + (!!((a) & ((b)-1)))) - #define BA_PAGESIZE(a) ((a)->blocks * (a)->block_size) - #define BA_SPAGE_SIZE(a) (sizeof(struct ba_page) + (BA_DIVIDE((a)->blocks, sizeof(uintptr_t)*8) - 1)*sizeof(uintptr_t)) + #define BA_PAGESIZE(a) (sizeof(struct ba_page) + (a)->blocks * (a)->block_size)   #define BA_HASH_MASK(a) (((a->allocated)) - 1)      #ifdef BA_HASH_THLD   # define BA_BYTES(a) ( (sizeof(ba_page) + ((a->allocated > BA_HASH_THLD) ? sizeof(ba_page_t) : 0)) * ((a)->allocated) )   #else   # define BA_BYTES(a) ( (sizeof(ba_page) + sizeof(ba_page_t)) * ((a)->allocated) )   #endif      static INLINE void ba_realloc(struct block_allocator * a) {    unsigned int i;
pike.git/src/block_allocator.c:104:      PMOD_EXPORT INLINE void ba_init(struct block_allocator * a,    uint32_t block_size, ba_page_t blocks) {    uint32_t page_size;      #ifdef BA_ALIGNMENT    if (block_size & (BA_ALIGNMENT - 1))    block_size += (BA_ALIGNMENT - (block_size & (BA_ALIGNMENT - 1)));   #endif    -  page_size = block_size * blocks; -  a->offset = sizeof(struct ba_page) + page_size - block_size; +  page_size = block_size * blocks + BLOCK_HEADER_SIZE;       a->first_blk = NULL;    a->empty = a->first = a->last_free = NULL;    -  + #ifdef BA_DEBUG +  fprintf(stderr, "blocks: %u block_size: %u page_size: %u\n", +  blocks, block_size, page_size); + #endif +  +  // is not multiple of memory page size    if ((page_size & (page_size - 1))) {    page_size = round_up32(page_size); -  a->blocks = page_size/block_size; -  } else a->blocks = blocks; +  } +  a->blocks = (page_size - BLOCK_HEADER_SIZE)/block_size; +  a->offset = sizeof(struct ba_page) + (a->blocks - 1) * block_size; + #ifdef BA_DEBUG +  fprintf(stderr, "blocks: %u block_size: %u page_size: %u mallocing size: %u, next pages: %u\n", +  a->blocks, block_size, blocks * block_size, +  BA_PAGESIZE(a)+sizeof(struct ba_page), +  round_up32(BA_PAGESIZE(a)+sizeof(struct ba_page))); + #endif    -  +     a->magnitude = (uint16_t)ctz32(page_size);    a->block_size = block_size;    a->num_pages = 0;    a->empty_pages = 0;    a->max_empty_pages = BA_MAX_EMPTY;       // we start with management structures for 16 pages    a->allocated = BA_ALLOC_INITIAL;    a->pages = NULL;    ba_realloc(a);