pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:48:   # 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 + sizeof(struct ba_page); -  struct ba_page * p = (struct ba_page*)xalloc(n); +  struct ba_page * p; + #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. +  */ +  p = (struct ba_page*)system_malloc(n); +  if (!p) { +  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(p+1, n - sizeof(struct ba_page));   #endif    return p;   }      PMOD_EXPORT void ba_init(struct block_allocator * a, unsigned INT32 block_size, unsigned INT32 blocks) {
pike.git/src/block_allocator.c:77: Inside #if defined(HAVE_VALGRIND_MACROS)
   a->pages[0] = ba_alloc_page(a, 0);   #ifdef HAVE_VALGRIND_MACROS    VALGRIND_CREATE_MEMPOOL(a, 0, 0);   #endif   }      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]); -  + #endif    a->pages[i] = NULL;    }    }    a->size = 0;    a->alloc = 0;    a->last_free = 0;   }      PMOD_EXPORT size_t ba_count(const struct block_allocator * a) {    size_t c = 0;
pike.git/src/block_allocator.c:200:    b->next = p->h.first;    p->h.first = b;   #ifdef PIKE_DEBUG    if (!p->h.used) {    fprintf(stderr, "freeing from empty page %p\n", p);    goto ERR;    }   #endif    if (!(--p->h.used) && i+1 == a->size) {    while (i >= 0 && !(p->h.used)) { + #ifdef DEBUG_MALLOC +  system_free(p); + #else    free(p); -  + #endif    a->pages[i] = NULL;       p = a->pages[--i];    }    a->size = i+1;    a->alloc = a->last_free = MAXIMUM(0, i);    }    }   #ifdef PIKE_DEBUG    } else {