pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:52:    fprintf(stderr, "num_pages: %u\n", a->num_pages);    fprintf(stderr, "max_empty_pages: %u\n", a->max_empty_pages);    fprintf(stderr, "empty_pages: %u\n", a->empty_pages);    fprintf(stderr, "magnitude: %u\n", a->magnitude);    PRINT_NODE(a, empty);    PRINT_NODE(a, first);    PRINT_NODE(a, last_free);       PAGE_LOOP(a, {    ba_block_t blocks_used; -  if (p == a->first) { -  blocks_used = 0; -  } else { +     blocks_used = p->used; -  } +     fprintf(stderr, "(%p)\t%f\t(%u %d) --> (prev: %p | next: %p)\n",    p, blocks_used/(double)a->blocks * 100,    blocks_used,    blocks_used,    p->prev, p->next);    });   }      #ifdef BA_STATS   # define PRCOUNT(X) fprintf(stat_file, #X " %5.1lf %% ", (a->stats.X/all)*100);
pike.git/src/block_allocator.c:133:    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 + BLOCK_HEADER_SIZE;    -  a->first_blk = NULL; -  a->last = a->empty = a->first = a->last_free = 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);    }
pike.git/src/block_allocator.c:218:       if (!a->allocated) return;       PAGE_LOOP(a, {    PIKE_MEM_RW_RANGE(p, BA_PAGESIZE(a));    free(p);    });       a->num_pages = 0;    a->empty_pages = 0; -  a->last = a->empty = a->first = a->last_free = NULL; -  a->first_blk = NULL; +  a->empty = a->first = a->last_free = NULL;    a->allocated = BA_ALLOC_INITIAL;    memset(a->pages, 0, a->allocated * sizeof(ba_page));   }      PMOD_EXPORT INLINE void ba_count_all(struct block_allocator * a, size_t *num, size_t *size) {    unsigned int i;    size_t n = 0;       //fprintf(stderr, "page_size: %u, pages: %u\n", BA_PAGESIZE(a), a->num_pages);    *size = BA_BYTES(a) + a->num_pages * BA_PAGESIZE(a);
pike.git/src/block_allocator.c:247:   PMOD_EXPORT INLINE void ba_destroy(struct block_allocator * a) {    unsigned int i;       PAGE_LOOP(a, {    PIKE_MEM_RW_RANGE(p, BA_PAGESIZE(a));    free(p);    });       PIKE_MEM_RW_RANGE(a->pages, BA_BYTES(a));    free(a->pages); -  a->first_blk = NULL; +     a->last_free = NULL; -  a->empty = a->first = a->last = NULL; +  a->empty = a->first = NULL;    a->pages = NULL;    a->empty_pages = 0;    a->allocated = 0;    a->num_pages = 0;   #ifdef BA_STATS    a->stats.st_max = a->stats.st_used = 0;   #endif   }      static INLINE void ba_grow(struct block_allocator * a) {
pike.git/src/block_allocator.c:530:       if (unlikely(a->num_pages == a->allocated)) {    ba_grow(a);    }       a->num_pages++;    p = (ba_page)malloc(BA_PAGESIZE(a));    if (!p) BA_ERROR("no mem. alloc returned zero.");    ba_free_page(a, p);    p->next = p->prev = NULL; -  a->last = a->first = p; +  a->first = p;    ba_htable_insert(a, p);   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_alloc after insert", __FILE__, __LINE__);   #endif   #ifdef BA_DEBUG    PIKE_MEM_RW(BA_LASTBLOCK(a, p)->magic);    BA_LASTBLOCK(a, p)->magic = BA_MARK_FREE;    PIKE_MEM_RW(BA_BLOCKN(a, p, 0)->magic);    BA_BLOCKN(a, p, 0)->magic = BA_MARK_ALLOC;    ba_check_allocator(a, "ba_alloc after insert", __FILE__, __LINE__);
pike.git/src/block_allocator.c:553:      PMOD_EXPORT void ba_low_alloc(struct block_allocator * a) {    //fprintf(stderr, "ba_alloc(%p)\n", a);   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_alloc top", __FILE__, __LINE__);   #endif       //fprintf(stderr, "allocating new page. was %p\n", p);    if (a->first) {    ba_page p = a->first; -  if (p->next) a->first = p->next; -  else a->first = a->last = NULL; -  p->prev = NULL; +  DOUBLE_SHIFT(a->first);    p->next = NULL;    p->first = NULL; -  p->used = a->blocks; +     }    -  if (a->first) { - #ifdef BA_DEBUG -  if (!a->first->first) { -  ba_show_pages(a); -  BA_ERROR("no free blk in page %lx\n", (long int)a->first); -  } - #endif -  a->first->prev = NULL; -  } else if (a->empty_pages) { -  a->last = a->first = a->empty; -  a->empty = a->empty->next; +  if (a->first) return; +  +  if (a->empty_pages) { +  a->first = a->empty; +  SINGLE_SHIFT(a->empty);    a->empty_pages--;    a->first->next = NULL;    } else ba_alloc_page(a);      #ifdef BA_DEBUG    if (!a->first->first) {    ba_show_pages(a);    BA_ERROR("a->first has no first block!\n");    }   #endif      #ifdef BA_DEBUG    ba_check_allocator(a, "ba_alloc after grow", __FILE__, __LINE__);   #endif -  -  a->first_blk = a->first->first; +    }      PMOD_EXPORT void ba_low_free(struct block_allocator * a, ba_page p,    ba_block_header ptr) {    // page was full    if (unlikely(!ptr->next)) {    INC(free_full); -  if (a->first) { -  p->prev = a->last; -  a->last->next = p; -  a->last = p; -  p->first = ptr; -  } else { -  p->prev = p->next = NULL; -  a->first = a->last = p; -  a->first_blk = ptr; -  } +  DOUBLE_LINK(a->first, p);    } else if (!p->used) {    INC(free_empty);    if (a->empty_pages == a->max_empty_pages) {    ba_remove_page(a, p);    return;    } -  if (p->next) p->next->prev = p->prev; -  else a->last = p->prev; -  if (p->prev) { -  p->prev->next = p->next; -  p->prev = NULL; -  } -  a->last_free = NULL; -  p->next = a->empty; -  a->empty = p; +  DOUBLE_UNLINK(a->first, p); +  SINGLE_LINK(a->empty, p);    a->empty_pages ++;    }   }      static INLINE void ba_htable_linear_lookup(struct block_allocator * a,    const void * ptr) {    PAGE_LOOP(a, {    if (BA_CHECK_PTR(a, p, ptr)) {    a->last_free = p;    return;
pike.git/src/block_allocator.c:695: Inside #if 0//def BA_DEBUG
   hash1(a, BA_LASTBLOCK(a,p)) & BA_HASH_MASK(a),    a->num_pages);   #endif       ba_htable_delete(a, p);    PIKE_MEM_RW_RANGE(*p, BA_PAGESIZE(a));       // we know that p == a->last_free    a->last_free = NULL;    -  if (p->prev) p->prev->next = p->next; -  if (p->next) p->next->prev = p->prev; -  else a->last = p->prev; +  DOUBLE_UNLINK(a->first, p);      #ifdef BA_DEBUG    memset(p, 0, sizeof(struct ba_page));    if (a->first == p) {    fprintf(stderr, "a->first will be old removed page %p\n", a->first);    }   #endif       a->num_pages--;    free(p);