pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:12: Inside #if undefined(PMOD_EXPORT)
     #ifndef PMOD_EXPORT   # warning WTF   #endif      #if defined(BA_DEBUG) || (!defined(PIKE_CORE) && !defined(DYNAMIC_MODULE) \    && !defined(STATIC_MODULES))   #endif   PMOD_EXPORT char errbuf[8192];    - static inline void ba_htable_insert(const struct block_allocator * a, + static INLINE void ba_htable_insert(const struct block_allocator * a,    const void * ptr, const ba_page_t n);      #define BA_NBLOCK(a, p, ptr) ((uintptr_t)((char*)ptr - (p)->data)/(a)->block_size)      #define BA_DIVIDE(a, b) ((a) / (b) + (!!((a) & ((b)-1))))   #define BA_PAGESIZE(a) ((a)->blocks * (a)->block_size)   #define BA_SPAGE_SIZE(a) (sizeof(struct ba_page) + (BA_DIVIDE((a)->blocks, sizeof(uintptr_t)*8) - 1)*sizeof(uintptr_t))   #define BA_HASH_MASK(a) (((a->allocated)) - 1)      #ifdef BA_HASH_THLD   # define BA_BYTES(a) ( (sizeof(struct ba_page) + ((a->allocated > BA_HASH_THLD) ? sizeof(ba_page_t) : 0)) * ((a)->allocated) )   #else   # define BA_BYTES(a) ( (sizeof(struct ba_page) + sizeof(ba_page_t)) * ((a)->allocated) )   #endif      #ifndef PIKE_MEM_RW_RANGE   # define PIKE_MEM_RW_RANGE(x, y)   #endif    - static inline void ba_realloc(struct block_allocator * a) { + static INLINE void ba_realloc(struct block_allocator * a) {    unsigned int i;    ba_page old;    ba_page p;    a->pages = realloc(old = a->pages, BA_BYTES(a));       if (unlikely(!a->pages)) {    //a->pages = old;    BA_ERROR("alloc of %lu bytes failed.", BA_BYTES(a));    }   
pike.git/src/block_allocator.c:82:    fprintf(stderr, "%d\t%f\t(%u %d) --> (prev: %d[%p] | next: %d[%p])\n",    i, p->blocks_used/(double)a->blocks * 100,    p->blocks_used,    p->blocks_used,    p->prev, p->prev ? BA_PAGE(a, p->prev) : NULL,    p->next, p->next ? BA_PAGE(a, p->next) : NULL);       }   }    - PMOD_EXPORT inline void ba_init(struct block_allocator * a, + PMOD_EXPORT INLINE void ba_init(struct block_allocator * a,    uint32_t block_size, ba_page_t blocks) {    uint32_t page_size = block_size * blocks;       a->first = NULL;    a->last_free = NULL;       if ((page_size & (page_size - 1)) == 0)    a->magnitude = (uint16_t)ctz32(page_size);    else    a->magnitude = (uint16_t)ctz32(round_up32(page_size));
pike.git/src/block_allocator.c:106:    a->num_pages = 0;    a->empty_pages = 0;    a->max_empty_pages = BA_MAX_EMPTY;       // we start with management structures for 16 pages    a->allocated = BA_ALLOC_INITIAL;    a->pages = NULL;    ba_realloc(a);   }    - PMOD_EXPORT inline void ba_free_all(struct block_allocator * a) { + PMOD_EXPORT INLINE void ba_free_all(struct block_allocator * a) {    unsigned int i;       if (!a->allocated) return;       for (i = 0; i < a->num_pages; i++) {    PIKE_MEM_RW_RANGE(BA_PAGE(a, i+1)->data, BA_PAGESIZE(a));    free(BA_PAGE(a, i+1)->data);    BA_PAGE(a, i+1)->data = NULL;    }    a->num_pages = 0;    a->empty_pages = 0;    a->first = NULL;    a->last_free = NULL;    a->allocated = BA_ALLOC_INITIAL;    ba_realloc(a);    IF_HASH(    memset(a->htable, 0, sizeof(*a->htable)*a->allocated);    );   }    - PMOD_EXPORT inline void ba_count_all(struct block_allocator * a, size_t *num, size_t *size) { + 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);    for (i = 0; i < a->num_pages; i++) {    n += BA_PAGE(a, i+1)->blocks_used;    }       *num = n;   }    - PMOD_EXPORT inline void ba_destroy(struct block_allocator * a) { + PMOD_EXPORT INLINE void ba_destroy(struct block_allocator * a) {    ba_free_all(a);    PIKE_MEM_RW_RANGE(a->pages, BA_BYTES(a));    free(a->pages);    a->allocated = 0;    a->htable = NULL;    a->pages = NULL;   }    - static inline void ba_grow(struct block_allocator * a) { + static INLINE void ba_grow(struct block_allocator * a) {    if (a->allocated) {    // try to detect 32bit overrun?    if (a->allocated >= ((ba_page_t)1 << (sizeof(ba_page_t)*8-1))) {    Pike_error("too many pages.\n");    }    a->allocated *= 2;    ba_realloc(a);    } else {    ba_init(a, a->block_size, a->blocks);    }   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_grow", __FILE__, __LINE__);   #endif   }    - static inline void ba_shrink(struct block_allocator * a) { + static INLINE void ba_shrink(struct block_allocator * a) {    a->allocated /= 2;    ba_realloc(a);   }      #define MIX(t) do { \    t ^= (t >> 20) ^ (t >> 12); \    t ^= (t >> 7) ^ (t >> 4); \   } while(0)    - static inline ba_page_t hash1(const struct block_allocator * a, + static INLINE ba_page_t hash1(const struct block_allocator * a,    const void * ptr) {    uintptr_t t = ((uintptr_t)ptr) >> a->magnitude;       MIX(t);       return (ba_page_t) t;   }    - static inline ba_page_t hash2(const struct block_allocator * a, + static INLINE ba_page_t hash2(const struct block_allocator * a,    const void * ptr) {    uintptr_t t = ((uintptr_t)ptr) >> a->magnitude;       t++;    MIX(t);       return (ba_page_t) t;   }      #ifdef BA_DEBUG
pike.git/src/block_allocator.c:227: Inside #if defined(BA_DEBUG)
   }    }    );   }   #endif      /*    * insert the pointer to an allocated page into the    * hashtable. uses linear probing and open allocation.    */ - static inline void ba_htable_insert(const struct block_allocator * a, + static INLINE void ba_htable_insert(const struct block_allocator * a,    const void * ptr, const ba_page_t n) {    ba_page_t hval = hash1(a, ptr);    ba_page_t * b = a->htable + (hval & BA_HASH_MASK(a));         #ifdef BA_DEBUG    while (*b) {    if (*b == n) {    fprintf(stderr, "inserting (%p, %d) twice\n", ptr, n);    fprintf(stderr, "is (%p, %d)\n", BA_PAGE(a, n)->data, n);
pike.git/src/block_allocator.c:256: Inside #if defined(BA_DEBUG)
  #ifdef BA_DEBUG    fprintf(stderr, "replacing bucket %u with page %u by %u\n",    hval & BA_HASH_MASK(a), *b, n);   #endif       BA_PAGE(a, n)->hchain = *b;    *b = n;   }       - static inline void ba_htable_replace(const struct block_allocator * a, + static INLINE void ba_htable_replace(const struct block_allocator * a,    const void * ptr, const ba_page_t n,    const ba_page_t new) {    ba_page_t hval = hash1(a, ptr);    ba_page_t * b = a->htable + (hval & BA_HASH_MASK(a));       while (*b) {    if (*b == n) {    *b = new;    BA_PAGE(a, new)->hchain = BA_PAGE(a, n)->hchain;    BA_PAGE(a, n)->hchain = 0;
pike.git/src/block_allocator.c:280: Inside #if defined(DEBUG)
   }   #ifdef DEBUG       fprintf(stderr, "ba_htable_replace(%p, %u, %u)\n", ptr, n, new);    fprintf(stderr, "hval: %u, %u, %u\n", hval, hval & BA_HASH_MASK(a), BA_HASH_MASK(a));    ba_print_htable(a);    Pike_error("did not find index to replace.\n")   #endif   }    - static inline void ba_htable_delete(const struct block_allocator * a, + static INLINE void ba_htable_delete(const struct block_allocator * a,    const void * ptr, const ba_page_t n) {    ba_page_t hval = hash1(a, ptr);    ba_page_t * b = a->htable + (hval & BA_HASH_MASK(a));       while (*b) {    if (*b == n) {    *b = BA_PAGE(a, n)->hchain;    BA_PAGE(a, n)->hchain = 0;    return;    }    b = &BA_PAGE(a, *b)->hchain;    }   #ifdef DEBUG    ba_print_htable(a);    fprintf(stderr, "ba_htable_delete(%p, %u)\n", ptr, n);    Pike_error("did not find index to delete.\n")   #endif   }    - static inline ba_page_t ba_htable_lookup(const struct block_allocator * a, + static INLINE ba_page_t ba_htable_lookup(const struct block_allocator * a,    const void * ptr) {   #ifdef BA_DEBUG    int c = 0;   #endif    ba_page_t n;    ba_page p;    n = a->htable[hash1(a, ptr) & BA_HASH_MASK(a)];    while (n) {    p = BA_PAGE(a, n);    if (BA_CHECK_PTR(a, p, ptr)) {
pike.git/src/block_allocator.c:333: Inside #if defined(BA_DEBUG)
   Pike_error("hash chain is infinite\n");    }   #endif    n = p->hchain;    }       return 0;   }      #ifdef BA_DEBUG - PMOD_EXPORT inline void ba_check_allocator(struct block_allocator * a, + PMOD_EXPORT INLINE void ba_check_allocator(struct block_allocator * a,    char *fun, char *file, int line) {    unsigned int i = 0;    int bad = 0;    ba_page p;       if (a->empty_pages > a->num_pages) {    fprintf(stderr, "too many empty pages.\n");    bad = 1;    }   
pike.git/src/block_allocator.c:403: Inside #if defined(BA_DEBUG)
   if (bad) {    ba_print_htable(a);    fprintf(stderr, "\nCalled from %s:%d:%s\n", fun, line, file);    fprintf(stderr, "pages: %u\n", a->num_pages);    Pike_error("bad");    }   }   #endif       - PMOD_EXPORT inline void * ba_low_alloc(struct block_allocator * a) { + PMOD_EXPORT INLINE void * ba_low_alloc(struct block_allocator * a) {    //fprintf(stderr, "ba_alloc(%p)\n", a);    ba_page p;   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_alloc top", __FILE__, __LINE__);   #endif    unsigned int i;       //fprintf(stderr, "allocating new page. was %p\n", p);    if (unlikely(a->num_pages == a->allocated)) {    ba_grow(a);
pike.git/src/block_allocator.c:466: Inside #if defined(BA_DEBUG)
  #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__);   #endif    return p->data;   }    - PMOD_EXPORT inline void ba_low_free(struct block_allocator * a, + PMOD_EXPORT INLINE void ba_low_free(struct block_allocator * a,    void * ptr, ba_page p) {   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_low_free", __FILE__, __LINE__);   #endif    if (!p) {   #ifdef BA_HASH_THLD    if (a->allocated <= BA_HASH_THLD)    BA_ERROR("ba_low_free(.., %p) should never be called: %d, %d\n",    ptr, a->allocated, BA_HASH_THLD);   #endif
pike.git/src/block_allocator.c:534: Inside #if defined(BA_DEBUG)
   }    //memset(ptr, 0x75, a->block_size);    ((ba_block_header)ptr)->magic = BA_MARK_FREE;   #endif    ((ba_block_header)ptr)->next = p->first;    //fprintf(stderr, "setting first to %u (%p vs %p) n: %u vs %u\n", t+1, BA_BLOCKN(a, p, t+1), ptr, BA_NBLOCK(a, p, BA_BLOCKN(a, p, t+1)), BA_NBLOCK(a, p, ptr));    p->first = ptr;   }       - PMOD_EXPORT inline void ba_remove_page(struct block_allocator * a, + PMOD_EXPORT INLINE void ba_remove_page(struct block_allocator * a,    ba_page_t n) {    ba_page tmp, p;   #ifdef BA_DEBUG    ba_check_allocator(a, "ba_remove_page", __FILE__, __LINE__);    if (a->empty_pages < a->max_empty_pages) {    Pike_error("strange things happening\n");    }   #endif       p = BA_PAGE(a, n);