pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:150:    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->empty = a->first = a->last_free = NULL; +  a->last = 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:234:       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->empty = a->first = a->last_free = NULL; +  a->last = a->empty = a->first = a->last_free = NULL;    a->first_blk = 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);
pike.git/src/block_allocator.c:459:    }    b = &(*b)->hchain;    }   #ifdef DEBUG    ba_print_htable(a);    fprintf(stderr, "ba_htable_delete(%p, %u)\n", p, n);    BA_ERROR("did not find index to delete.\n")   #endif   }    - static INLINE ba_page ba_htable_lookup(const struct block_allocator * a, + static INLINE void ba_htable_lookup(struct block_allocator * a,    const void * ptr) {    ba_page p1, p2;    p1 = a->pages[hash1(a, ptr) & BA_HASH_MASK(a)];    p2 = a->pages[hash2(a, ptr) & BA_HASH_MASK(a)];       while (p1 || p2) {    if (p1) {    if (BA_CHECK_PTR(a, p1, ptr)) { -  return p1; +  a->last_free = p1; +  return;    }    p1 = p1->hchain;    }    if (p2) {    if (BA_CHECK_PTR(a, p2, ptr)) { -  return p2; +  a->last_free = p2; +  return;    }    p2 = p2->hchain;    }    } -  -  return NULL; +    }      #ifdef BA_DEBUG   PMOD_EXPORT INLINE void ba_check_allocator(struct block_allocator * a,    char *fun, char *file, int line) {    ba_page_t n;    int bad = 0;    ba_page p;       if (a->empty_pages > a->num_pages) {
pike.git/src/block_allocator.c:609: Inside #if defined(BA_DEBUG)
     #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) { -  if (likely(!p)) { -  ba_find_page(a, ptr); -  p = a->last_free; -  ((ba_block_header)ptr)->next = p->first; -  p->first = (ba_block_header)ptr; -  p->used--; -  } -  +     // 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;
pike.git/src/block_allocator.c:649:    p->prev->next = p->next;    p->prev = NULL;    }    a->last_free = NULL;    p->next = a->empty;    a->empty = p;    a->empty_pages ++;    }   }    - ATTRIBUTE((always_inline)) + 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; +  } +  }); + } +    PMOD_EXPORT INLINE void ba_find_page(struct block_allocator * a,    const void * ptr) { -  ba_page p; +  a->last_free = NULL;   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_low_free", __FILE__, __LINE__);   #endif   #ifdef BA_HASH_THLD    if (a->num_pages <= BA_HASH_THLD) {    INC(find_linear); -  PAGE_LOOP(a, { -  if (BA_CHECK_PTR(a, p, ptr)) { -  a->last_free = p; -  return; -  } -  }); +  ba_htable_linear_lookup(a, ptr);    } else {   #endif    INC(find_hash); -  p = ba_htable_lookup(a, ptr); -  if (p) { -  a->last_free = p; -  return; -  } +  ba_htable_lookup(a, ptr);   #ifdef BA_HASH_THLD    } -  +  +  if (a->last_free) return; +    #endif   #ifdef BA_DEBUG    fprintf(stderr, "magnitude: %u\n", a->magnitude);    fprintf(stderr, "allocated: %u\n", a->allocated);    fprintf(stderr, "did not find %p (%X[%X] | %X[%X])\n", ptr,    hash1(a, ptr), hash1(a, ptr) & BA_HASH_MASK(a),    hash2(a, ptr), hash2(a, ptr) & BA_HASH_MASK(a)    );    ba_print_htable(a);   #endif