pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:4:      #include "block_allocator.h"   #include "bitvector.h"      #include <stdlib.h>      #define BA_BLOCKN(l, p, n) ((struct ba_block_header *)((char*)(p) + (l).doffset + (n)*((l).block_size)))   #define BA_LASTBLOCK(l, p) ((struct ba_block_header*)((char*)(p) + (l).doffset + (l).offset))   #define BA_CHECK_PTR(l, p, ptr) ((size_t)((char*)(ptr) - (char*)(p)) <= (l).offset + (l).doffset)    + #define BA_PAGE_SIZE 4096 + #define BA_MALLOC_OVERHEAD sizeof(void*) +    #define BA_ONE ((struct ba_block_header *)1)   #define BA_FLAG_SORTED 1u      #ifdef PIKE_DEBUG   static void print_allocator(const struct block_allocator * a);   #endif      #ifdef PIKE_DEBUG   static void ba_check_ptr(struct block_allocator * a, int page, void * ptr, struct ba_block_header *loc,    int ln);
pike.git/src/block_allocator.c:118:    }       if (a->size != i+1) {    a->size = i+1;    a->alloc = a->last_free = MAXIMUM(0, i);    }   }      static void ba_low_init_aligned(struct block_allocator * a) {    unsigned INT32 block_size = MAXIMUM(a->l.block_size, sizeof(struct ba_block_header)); +  size_t bytes;       PIKE_MEMPOOL_CREATE(a);       if (a->l.alignment) {   #if PIKE_DEBUG    if (a->l.alignment & (a->l.alignment - 1))    Pike_fatal("Block allocator a->l.alignment is not a power of 2.\n");    if (block_size & (a->l.alignment-1))    Pike_fatal("Block allocator block size is not aligned.\n");   #endif    a->l.doffset = PIKE_ALIGNTO(sizeof(struct ba_page), a->l.alignment);    } else {    a->l.doffset = sizeof(struct ba_page);    }    -  if (a->l.blocks & (a->l.blocks - 1)) { -  unsigned INT32 tmp = round_up32(a->l.blocks); -  if (tmp) a->l.blocks = tmp; -  } else if (!a->l.blocks) a->l.blocks = 1; +  if (!a->l.blocks) a->l.blocks = 1; +  +  bytes = a->l.doffset + block_size * a->l.blocks; +  +  bytes = PIKE_ALIGNTO(bytes + BA_MALLOC_OVERHEAD, BA_PAGE_SIZE) - BA_MALLOC_OVERHEAD; +  +  a->l.blocks = (bytes - a->l.doffset) / block_size;    a->l.block_size = block_size;    a->l.offset = block_size * (a->l.blocks-1);   }      PMOD_EXPORT void ba_init_aligned(struct block_allocator * a, unsigned INT32 block_size,    unsigned INT32 blocks, unsigned INT32 alignment) {    a->l.blocks = blocks;    a->l.block_size = block_size;    a->l.alignment = alignment;    ba_low_init_aligned(a);