pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:39:   static INLINE struct ba_layout ba_get_layout(const struct block_allocator * a, int i) {    struct ba_layout l = a->l;    ba_inc_layout(&l, i);    return l;   }      struct ba_block_header {    struct ba_block_header * next;   };    - #ifdef HAVE_VALGRIND_MACROS - # ifndef VALGRIND_CREATE_MEMPOOL - # define VALGRIND_CREATE_MEMPOOL(a, b, c) - # define VALGRIND_MEMPOOL_ALLOC(a, p, l) VALGRIND_MAKE_MEM_UNDEFINED((p), (l)) - # define VALGRIND_MEMPOOL_FREE(a, p) VALGRIND_MAKE_MEM_NOACCESS((p), (a)->l.block_size) - # endif - #endif -  -  +    static struct ba_page * ba_alloc_page(struct block_allocator * a, int i) {    struct ba_layout l = ba_get_layout(a, i);    size_t n = l.offset + l.block_size + l.doffset;    struct ba_page * p;    if (l.alignment) {    p = (struct ba_page*)aligned_alloc(n, l.alignment);    } else {   #ifdef DEBUG_MALLOC    /* In debug malloc mode, calling xalloc from the block alloc may result    * in a deadlock, since xalloc will call ba_alloc, which in turn may call xalloc.
pike.git/src/block_allocator.c:71: Inside #if defined(DEBUG_MALLOC)
   fprintf(stderr, "Fatal: Out of memory.\n");    exit(17);    }   #else    p = (struct ba_page*)xalloc(n);   #endif    }    p->h.first = BA_BLOCKN(a->l, p, 0);    p->h.first->next = BA_ONE;    p->h.used = 0; - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MAKE_MEM_NOACCESS((char*)p + l.doffset, n - l.doffset); - #endif +  PIKE_MEM_NA_RANGE((char*)p + l.doffset, n - l.doffset);    return p;   }         PMOD_EXPORT void ba_init_aligned(struct block_allocator * a, unsigned INT32 block_size,    unsigned INT32 blocks, unsigned INT32 alignment) {    block_size = MAXIMUM(block_size, sizeof(struct ba_block_header));    if (alignment) {    if (alignment & (alignment - 1))    Pike_fatal("Block allocator alignment is not a power of 2.\n");
pike.git/src/block_allocator.c:100:       blocks = round_up32(blocks);    a->alloc = a->last_free = 0;    a->size = 1;    a->l.block_size = block_size;    a->l.blocks = blocks;    a->l.offset = block_size * (blocks-1);    a->l.alignment = alignment;    memset(a->pages, 0, sizeof(a->pages));    a->pages[0] = ba_alloc_page(a, 0); - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_CREATE_MEMPOOL(a, 0, 0); - #endif +  PIKE_MEMPOOL_CREATE(a);   }      PMOD_EXPORT void ba_destroy(struct block_allocator * a) {    int i;    for (i = 0; i < a->size; i++) {    if (a->pages[i]) {   #ifdef DEBUG_MALLOC    system_free(a->pages[i]);   #else    free(a->pages[i]);
pike.git/src/block_allocator.c:176:   PMOD_EXPORT void * ba_alloc(struct block_allocator * a) {    struct ba_page * p = a->pages[a->alloc];    struct ba_block_header * ptr;       if (!p || !p->h.first) {    ba_low_alloc(a);    p = a->pages[a->alloc];    }       ptr = p->h.first; - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MEMPOOL_ALLOC(a, ptr, a->l.block_size); -  VALGRIND_MAKE_MEM_DEFINED(ptr, sizeof(struct ba_block_header)); - #endif +  PIKE_MEMPOOL_ALLOC(a, ptr, a->l.block_size); +  PIKE_MEM_RW_RANGE(ptr, sizeof(struct ba_block_header));       p->h.used++;      #ifdef PIKE_DEBUG    {    struct ba_layout l = ba_get_layout(a, a->alloc);    if (!BA_CHECK_PTR(l, p, ptr)) {    print_allocator(a);    Pike_fatal("about to return pointer from hell: %p\n", ptr);    }    }   #endif       if (ptr->next == BA_ONE) {    struct ba_layout l = ba_get_layout(a, a->alloc);    p->h.first = (struct ba_block_header*)((char*)ptr + a->l.block_size); - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MEMPOOL_ALLOC(a, p->h.first, a->l.block_size); - #endif +  PIKE_MEMPOOL_ALLOC(a, p->h.first, a->l.block_size);    p->h.first->next = (struct ba_block_header*)(ptrdiff_t)!(p->h.first == BA_LASTBLOCK(l, p)); - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MEMPOOL_FREE(a, p->h.first); - #endif +  PIKE_MEMPOOL_FREE(a, p->h.first, a->l.block_size);    } else {    p->h.first = ptr->next;    } - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MAKE_MEM_UNDEFINED(ptr, sizeof(struct ba_block_header)); - #endif +  PIKE_MEM_WO_RANGE(ptr, sizeof(struct ba_block_header));      #if PIKE_DEBUG    if (a->l.alignment && (size_t)ptr & (a->l.alignment - 1)) {    print_allocator(a);    Pike_fatal("Returning unaligned pointer.\n");    }   #endif       return ptr;   }
pike.git/src/block_allocator.c:281:    a->size = i+1;    a->alloc = a->last_free = MAXIMUM(0, i);    }    }   #ifdef PIKE_DEBUG    } else {    print_allocator(a);    Pike_fatal("ptr %p not in any page.\n", ptr);    }   #endif - #ifdef HAVE_VALGRIND_MACROS -  VALGRIND_MEMPOOL_FREE(a, ptr); - #endif +  PIKE_MEMPOOL_FREE(a, ptr, a->l.block_size);   }      static void print_allocator(const struct block_allocator * a) {    int i;    struct ba_layout l;       for (i = a->size-1, l = ba_get_layout(a, i); i >= 0; ba_half_layout(&l), i--) {    struct ba_page * p = a->pages[i];    fprintf(stderr, "page: %p used: %u/%u last: %p p+offset: %p\n", a->pages[i],    p->h.used, l.blocks,    BA_BLOCKN(l, p, l.blocks-1), BA_LASTBLOCK(l, p));    }   }