pike.git / src / block_allocator.c

version» Context lines:

pike.git/src/block_allocator.c:284:    size_t i, j;   #endif       if (likely(a->first)) {   #ifndef BA_SEGREGATE    uintptr_t m;   #else    void * ptr;   #endif    + #ifdef BA_DEBUG +  if (a->first > a->num_pages) { +  fprintf(stderr, "unused page set. %d > num_pages: %d.\n", a->first, a->num_pages); +  } + #endif +     p = BA_PAGE(a, a->first);    -  + #ifdef BA_DEBUG +  if (p->prev) { +  fprintf(stderr, "a->first has previous: %d\n", p->prev); +  } + #endif +    #ifndef BA_SEGREGATE    i = p->free_mask; -  + #ifdef BA_DEBUG    if (i >= BA_MASK_NUM(a)) {    Pike_error("free mask is out of range!\n");    } -  + #endif    m = p->mask[i]; -  + #ifdef BA_DEBUG    if (!m) {   // fprintf(stderr, "blk(%p)# n: %u\tmask: %04X\n", p, i, m);    Pike_error("This should not happen!\n");    } -  + #endif   // fprintf(stderr, "blk(%p)> n: %u\tmask: %04X\n", p, i, m);   #if SIZEOF_CHAR_P == 8    j = ctz64(m);   #else    j = ctz32(m);   #endif    m ^= TBITMASK(uintptr_t, j);    //fprintf(stderr, "setting bit %u -> %8X (using %u blocks) maks_num: %u\n", i*sizeof(uintptr_t)*8 + j, m, p->blocks_used, BA_MASK_NUM(a));    p->mask[i] = m;   // fprintf(stderr, "blk(%p)< n: %u\tmask: %04X\n", p, i, m);
pike.git/src/block_allocator.c:330:    p->free_mask = m;    /*    fprintf(stderr, "1 alloced pointer %p (%u/%u used %u)\n",    BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j)),    (i*sizeof(uintptr_t)*8 + j), a->blocks,    p->blocks_used);    */    return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));    }    } + #ifdef BA_DEBUG    if (p->blocks_used != a->blocks) {    fprintf(stderr, "wrong block count detected: %u vs %u\n", p->blocks_used, a->blocks);    Pike_error("croak\n");    } -  + #endif    //fprintf(stderr, "page is full now\n");    if (a->last == a->first) {    a->first = a->last = 0;    } else {    a->first = p->next;    }    p->next = 0;    p->free_mask = BA_MASK_NUM(a);    return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));    }   #else    if (p->blocks_used == a->blocks) Pike_error("baaad!\n");    p->blocks_used ++; -  + #ifdef BA_DEBUG +  if (p->first < 1 || p->first > a->blocks) { +  fprintf(stderr, "bad index: %d (should be [1..%d]\n", p->first-1, a->blocks); +  } + #endif    ptr = BA_BLOCKN(a, p, p->first-1); -  +  + #ifdef BA_DEBUG +  if (!p->data) { +  fprintf(stderr, "got null pointer from uninitialized page %d of block %d (num_pages %d).\n", a->first, p->first-1, a->num_pages); +  } + #endif    //fprintf(stderr, "alloced pointer %p (%u/%u used %u)\n",    //ptr, p->first-1, a->blocks, p->blocks_used);       if (unlikely(p->blocks_used == a->blocks)) {    a->first = p->next;    BA_PAGE(a, a->first)->prev = 0;    if (!a->first) {    a->last = 0;    }    p->next = 0;
pike.git/src/block_allocator.c:375:       return ptr;   #endif    }       //fprintf(stderr, "allocating new page. was %p\n", p);    if (unlikely(a->num_pages == a->allocated))    ba_grow(a);       p = BA_PAGE(a, ++a->num_pages); +  if (p->data) { +  fprintf(stderr, "reusing unfreed page\n"); +  }    p->data = malloc(BA_PAGESIZE(a));    if (!p->data) {    Pike_error("no mem");    }    p->next = p->prev = 0;    a->first = a->last = a->num_pages;    ba_htable_insert(a, BA_LASTBLOCK(a, p), a->num_pages);   #ifndef BA_SEGREGATE    if (BA_MASK_NUM(a) > 1)    memset((void*)p->mask, 0xff, (BA_MASK_NUM(a)-1)*sizeof(uintptr_t));
pike.git/src/block_allocator.c:418:       if (n) {    p = BA_PAGE(a, n);    if (!BA_CHECK_PTR(a, p, ptr)) n = 0;    }       if (unlikely(!n)) {    a->last_free = n = ba_htable_lookup(a, ptr);       if (unlikely(!n)) { - #define BA_DEBUG +    #ifdef BA_DEBUG    fprintf(stderr, "magnitude: %u\n", a->magnitude);    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    Pike_error("Unknown pointer \n");    }    -  + #ifdef BA_DEBUG +  if (n > a->num_pages) { +  fprintf(stderr, "freeing from unknown page %d (num_pages: %d).\n", +  n, a->num_pages); +  } + #endif +     p = BA_PAGE(a, n); -  +  + #ifdef BA_DEBUG +  if (!BA_CHECK_PTR(a, p, ptr)) { +  fprintf(stderr, "pointer %p in wrong page %p", ptr, p->data);    } -  + #endif +  }       t = (uintptr_t)((char*)ptr - p->data)/a->block_size;   #ifndef BA_SEGREGATE    mask = t / (sizeof(uintptr_t) * 8);    bit = t & ((sizeof(uintptr_t)*8) - 1);    /*    fprintf(stderr, "freeing pointer %p (%u/%u) used %u\n", ptr, t, a->blocks,    p->blocks_used);    */   
pike.git/src/block_allocator.c:569:    );    */    *p = *tmp;    if (p->next) BA_PAGE(a, p->next)->prev = n;    if (p->prev) BA_PAGE(a, p->prev)->next = n;    if (a->num_pages == a->first) {    a->first = n;    }    }    + #ifdef BA_DEBUG +  if (a->first == a->num_pages) { +  fprintf(stderr, "a->first will be old removed page %d\n", a->first); +  fprintf(stderr, "page %d was not moved and prev was %d\n", n, p->prev); +  } +     memset(BA_PAGE(a, a->num_pages), 0, sizeof(struct ba_page)); -  + #endif    -  +     a->num_pages--;   }