0a3d34 | 2011-11-10 | Arne Goedeke | | #include <stdlib.h>
|
67795d | 2011-11-11 | Arne Goedeke | | #include <string.h>
|
385483 | 2011-11-18 | Arne Goedeke | | #include <stdint.h>
|
0a3d34 | 2011-11-10 | Arne Goedeke | |
|
de2d07 | 2011-12-25 | Arne Goedeke | | #include "post_modules/CritBit/bitvector.h"
|
e90c49 | 2011-11-27 | Arne Goedeke | | #define __PIKE__
#ifdef __PIKE__
# include "pike_error.h"
#endif
#define BA_SEGREGATE
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
struct ba_page {
char * data;
|
67795d | 2011-11-11 | Arne Goedeke | | uint16_t next, prev;
|
385483 | 2011-11-18 | Arne Goedeke | | uint16_t hchain;
#ifdef BA_SEGREGATE
uint16_t blocks_used, first;
#else
uint16_t blocks_used;
|
67795d | 2011-11-11 | Arne Goedeke | | uint16_t free_mask;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | uintptr_t mask[1];
|
385483 | 2011-11-18 | Arne Goedeke | | #endif
};
#ifdef BA_SEGREGATE
struct ba_block_header {
uint32_t next;
|
0a3d34 | 2011-11-10 | Arne Goedeke | | };
|
385483 | 2011-11-18 | Arne Goedeke | | #endif
|
0a3d34 | 2011-11-10 | Arne Goedeke | |
#include "block_allocator.h"
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | static inline void ba_htable_insert(const struct block_allocator * a,
const void * ptr, const uint16_t n);
|
385483 | 2011-11-18 | Arne Goedeke | | static inline void ba_remove_page(struct block_allocator * a,
uint16_t n);
|
67795d | 2011-11-11 | Arne Goedeke | |
|
e90c49 | 2011-11-27 | Arne Goedeke | | #define BA_BLOCKN(a, p, n) ((void*)(((p)->data) + (n)*((a)->block_size)))
#define BA_LASTBLOCK(a, p) BA_BLOCKN(a, p, (a)->blocks - 1)
#define BA_NBLOCK(a, p, ptr) ((uintptr_t)((char*)ptr - (p)->data)/(a)->block_size)
|
67795d | 2011-11-11 | Arne Goedeke | | #define BA_DIVIDE(a, b) ((a) / (b) + !!((a) & ((b)-1)))
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | #define BA_PAGESIZE(a) ((a)->blocks * (a)->block_size)
|
385483 | 2011-11-18 | Arne Goedeke | | #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)
#define BA_CHECK_PTR(a, p, ptr) ((char*)ptr >= p->data && BA_LASTBLOCK(a,p) >= ptr)
#ifdef BA_SEGREGATE
# define BA_PAGE(a, n) ((a)->pages + (n) - 1)
# define BA_BYTES(a) ( (sizeof(struct ba_page) + sizeof(uint16_t)) * ((a)->allocated) )
#else
# define BA_PAGE(a, n) ((ba_page)((char*)(a)->pages + ((n)-1) * ((a)->ba_page_size)))
# define BA_BYTES(a) (( (a)->ba_page_size + sizeof(uint16_t)) * ((a)->allocated))
# define BA_LAST_BITS(a) ((a)->blocks & (sizeof(uintptr_t)*8 - 1))
# define BA_LAST_MASK(a) (BA_LAST_BITS(a) ? TMASK(uintptr_t, BA_LAST_BITS(a)) : ~((uintptr_t)0))
# define BA_MASK_NUM(a) (((a)->ba_page_size - sizeof(struct ba_page) + sizeof(uintptr_t))/sizeof(uintptr_t))
#endif
|
67795d | 2011-11-11 | Arne Goedeke | |
|
e90c49 | 2011-11-27 | Arne Goedeke | | #ifndef __PIKE__
|
67795d | 2011-11-11 | Arne Goedeke | | #include <stdio.h>
#include <unistd.h>
void Pike_error(char * msg) {
fprintf(stderr, "ERROR: %s\n", msg);
_exit(1);
}
#endif
static inline void ba_realloc(struct block_allocator * a) {
|
475075 | 2011-11-19 | Arne Goedeke | | unsigned int i;
|
67795d | 2011-11-11 | Arne Goedeke | | a->pages = realloc(a->pages, BA_BYTES(a));
if (!a->pages) {
Pike_error("no mem");
}
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
memset((void*)BA_PAGE(a, a->num_pages+1), 0, BA_BYTES(a) - a->ba_page_size*a->num_pages);
#else
memset((void*)BA_PAGE(a, a->num_pages+1), 0, BA_BYTES(a) - sizeof(struct ba_page)*a->num_pages);
#endif
a->htable = (uint16_t*) BA_PAGE(a, (a->allocated)+1);
|
67795d | 2011-11-11 | Arne Goedeke | | for (i = 0; i < a->num_pages; i++) {
|
385483 | 2011-11-18 | Arne Goedeke | | ba_htable_insert(a, BA_LASTBLOCK(a, BA_PAGE(a, i+1)), i+1);
|
67795d | 2011-11-11 | Arne Goedeke | | }
}
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
void ba_init(struct block_allocator * a,
|
0a3d34 | 2011-11-10 | Arne Goedeke | | uint32_t block_size, uint32_t blocks) {
|
475075 | 2011-11-19 | Arne Goedeke | | uint32_t page_size = block_size * blocks;
|
385483 | 2011-11-18 | Arne Goedeke | |
if (blocks > 0xfffe) {
Pike_error("number of blocks cannot exceed 2^16-1\n");
}
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | a->first = a->last = 0;
a->last_free = 0;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
0a3d34 | 2011-11-10 | Arne Goedeke | | if ((page_size & (page_size - 1)) == 0)
|
67795d | 2011-11-11 | Arne Goedeke | | a->magnitude = (uint16_t)ctz32(page_size);
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | else
|
67795d | 2011-11-11 | Arne Goedeke | | a->magnitude = (uint16_t)ctz32(round_up32(page_size));
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
0a3d34 | 2011-11-10 | Arne Goedeke | | a->block_size = block_size;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | a->blocks = blocks;
a->num_pages = 0;
|
67795d | 2011-11-11 | Arne Goedeke | |
a->allocated = 16;
a->pages = NULL;
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
a->ba_page_size = BA_SPAGE_SIZE(a);
#endif
|
67795d | 2011-11-11 | Arne Goedeke | | ba_realloc(a);
}
void ba_free_all(struct block_allocator * a) {
|
475075 | 2011-11-19 | Arne Goedeke | | unsigned int i;
|
67795d | 2011-11-11 | Arne Goedeke | |
for (i = 0; i < a->num_pages; i++) {
|
475075 | 2011-11-19 | Arne Goedeke | | free(BA_PAGE(a, i+1)->data);
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | a->num_pages = 0;
a->first = a->last = 0;
}
|
385483 | 2011-11-18 | Arne Goedeke | | void ba_count_all(struct block_allocator * a, size_t *num, size_t *size) {
|
475075 | 2011-11-19 | Arne Goedeke | | unsigned int i;
|
385483 | 2011-11-18 | Arne Goedeke | | size_t n = 0;
|
e90c49 | 2011-11-27 | Arne Goedeke | | fprintf(stderr, "page_size: %u, pages: %u\n", BA_PAGESIZE(a), a->num_pages);
|
385483 | 2011-11-18 | Arne Goedeke | | *size = BA_BYTES(a) + a->num_pages * BA_PAGESIZE(a);
for (i = 0; i < a->num_pages; i++) {
|
475075 | 2011-11-19 | Arne Goedeke | | n += BA_PAGE(a, i+1)->blocks_used;
|
385483 | 2011-11-18 | Arne Goedeke | | }
*num = n;
}
|
67795d | 2011-11-11 | Arne Goedeke | | void ba_destroy(struct block_allocator * a) {
ba_free_all(a);
free(a->pages);
a->pages = NULL;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | static inline void ba_grow(struct block_allocator * a) {
|
385483 | 2011-11-18 | Arne Goedeke | | if (a->allocated) {
a->allocated *= 2;
ba_realloc(a);
} else {
ba_init(a, a->block_size, a->blocks);
}
|
67795d | 2011-11-11 | Arne Goedeke | | }
static inline void ba_shrink(struct block_allocator * a) {
a->allocated /= 2;
ba_realloc(a);
}
static inline unsigned int mix(uintptr_t t) {
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | t ^= (t >> 20) ^ (t >> 12);
|
67795d | 2011-11-11 | Arne Goedeke | | return (unsigned int)(t ^ (t >> 7) ^ (t >> 4));
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | static inline unsigned int hash1(const struct block_allocator * a,
const void * ptr) {
uintptr_t t = ((uintptr_t)ptr) >> a->magnitude;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | return mix(t);
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | static inline unsigned int hash2(const struct block_allocator * a,
const void * ptr) {
uintptr_t t = ((uintptr_t)ptr) >> a->magnitude;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | return mix(t+1);
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | static inline void ba_htable_insert(const struct block_allocator * a,
const void * ptr, const uint16_t n) {
|
385483 | 2011-11-18 | Arne Goedeke | | unsigned int hval = hash1(a, ptr);
uint16_t * b = a->htable + (hval & BA_HASH_MASK(a));
BA_PAGE(a, n)->hchain = *b;
*b = n;
}
static inline void ba_htable_replace(const struct block_allocator * a,
const void * ptr, const uint16_t n,
const uint16_t new) {
unsigned int hval = hash1(a, ptr);
uint16_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;
return;
}
b = &BA_PAGE(a, *b)->hchain;
}
}
static inline void ba_htable_delete(const struct block_allocator * a,
const void * ptr, const uint16_t n) {
unsigned int hval = hash1(a, ptr);
uint16_t * b = a->htable + (hval & BA_HASH_MASK(a));
while (*b) {
if (*b == n) {
*b = BA_PAGE(a, n)->hchain;
return;
}
b = &BA_PAGE(a, *b)->hchain;
}
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | void ba_print_htable(struct block_allocator * a) {
int i;
|
0a3d34 | 2011-11-10 | Arne Goedeke | | ba_page p;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | fprintf(stderr, "allocated: %u\n", a->allocated*2);
fprintf(stderr, "magnitude: %u\n", a->magnitude);
|
e90c49 | 2011-11-27 | Arne Goedeke | | fprintf(stderr, "block_size: %u\n", a->block_size);
|
67795d | 2011-11-11 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | for (i = 0; i < a->allocated; i++) {
|
67795d | 2011-11-11 | Arne Goedeke | | uint16_t n = a->htable[i];
unsigned int hval;
void * ptr;
|
e90c49 | 2011-11-27 | Arne Goedeke | | while (n) {
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, n);
|
e90c49 | 2011-11-27 | Arne Goedeke | | ptr = BA_LASTBLOCK(a, p);
|
67795d | 2011-11-11 | Arne Goedeke | | hval = hash1(a, ptr);
|
e90c49 | 2011-11-27 | Arne Goedeke | | fprintf(stderr, "%u\t%X\t%p-%p\t%X\n", i, hval, p->data, ptr, ((uintptr_t)ptr >> a->magnitude));
n = p->hchain;
|
67795d | 2011-11-11 | Arne Goedeke | | }
}
}
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | #define likely(x) (__builtin_expect((x), 1))
#define unlikely(x) (__builtin_expect((x), 0))
static inline uint16_t ba_htable_lookup(const struct block_allocator * a,
const void * ptr) {
|
385483 | 2011-11-18 | Arne Goedeke | | uint16_t n1, n2;
int c = 0;
|
67795d | 2011-11-11 | Arne Goedeke | | ba_page p;
|
385483 | 2011-11-18 | Arne Goedeke | | n1 = a->htable[hash1(a, ptr) & BA_HASH_MASK(a)];
n2 = a->htable[hash2(a, ptr) & BA_HASH_MASK(a)];
|
67795d | 2011-11-11 | Arne Goedeke | |
while (n1 || n2) {
|
385483 | 2011-11-18 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | if (n1) {
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, n1);
if (BA_CHECK_PTR(a, p, ptr)) {
return n1;
|
67795d | 2011-11-11 | Arne Goedeke | | }
|
385483 | 2011-11-18 | Arne Goedeke | | n1 = p->hchain;
|
67795d | 2011-11-11 | Arne Goedeke | | }
if (n2) {
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, n2);
if (BA_CHECK_PTR(a, p, ptr)) {
return n2;
|
67795d | 2011-11-11 | Arne Goedeke | | }
|
385483 | 2011-11-18 | Arne Goedeke | | n2 = p->hchain;
|
67795d | 2011-11-11 | Arne Goedeke | | }
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
return 0;
}
void * ba_alloc(struct block_allocator * a) {
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | ba_page p;
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | size_t i, j;
|
385483 | 2011-11-18 | Arne Goedeke | | #endif
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | if (likely(a->first)) {
#ifndef BA_SEGREGATE
|
67795d | 2011-11-11 | Arne Goedeke | | uintptr_t m;
|
385483 | 2011-11-18 | Arne Goedeke | | #else
void * ptr;
#endif
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, a->first);
|
67795d | 2011-11-11 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
|
67795d | 2011-11-11 | Arne Goedeke | | i = p->free_mask;
|
385483 | 2011-11-18 | Arne Goedeke | | if (i >= BA_MASK_NUM(a)) {
Pike_error("free mask is out of range!\n");
}
|
67795d | 2011-11-11 | Arne Goedeke | | m = p->mask[i];
if (!m) {
|
f25b0b | 2011-11-10 | Arne Goedeke | | Pike_error("This should not happen!\n");
}
|
67795d | 2011-11-11 | Arne Goedeke | |
#if SIZEOF_CHAR_P == 8
j = ctz64(m);
#else
j = ctz32(m);
#endif
m ^= TBITMASK(uintptr_t, j);
|
385483 | 2011-11-18 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | p->mask[i] = m;
|
385483 | 2011-11-18 | Arne Goedeke | |
p->blocks_used ++;
|
e90c49 | 2011-11-27 | Arne Goedeke | | if (m) {
return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));
} else {
|
67795d | 2011-11-11 | Arne Goedeke | | for (m = i+1; m < BA_MASK_NUM(a); m++) {
if (p->mask[m]) {
p->free_mask = m;
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));
}
}
|
385483 | 2011-11-18 | Arne Goedeke | | 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");
}
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | 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);
|
e90c49 | 2011-11-27 | Arne Goedeke | | return BA_BLOCKN(a, p, (i*sizeof(uintptr_t)*8 + j));
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
385483 | 2011-11-18 | Arne Goedeke | | #else
if (p->blocks_used == a->blocks) Pike_error("baaad!\n");
p->blocks_used ++;
ptr = BA_BLOCKN(a, p, p->first-1);
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | |
if (unlikely(p->blocks_used == a->blocks)) {
a->first = p->next;
|
de2d07 | 2011-12-25 | Arne Goedeke | | BA_PAGE(a, a->first)->prev = 0;
|
385483 | 2011-11-18 | Arne Goedeke | | if (!a->first) {
a->last = 0;
}
p->next = 0;
} else {
|
e90c49 | 2011-11-27 | Arne Goedeke | |
if (((struct ba_block_header*)ptr)->next)
p->first = ((struct ba_block_header*)ptr)->next;
else p->first++;
|
385483 | 2011-11-18 | Arne Goedeke | | }
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | |
return ptr;
#endif
}
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | if (unlikely(a->num_pages == a->allocated))
ba_grow(a);
p = BA_PAGE(a, ++a->num_pages);
p->data = malloc(BA_PAGESIZE(a));
if (!p->data) {
Pike_error("no mem");
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
385483 | 2011-11-18 | Arne Goedeke | | 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
|
e90c49 | 2011-11-27 | Arne Goedeke | | if (BA_MASK_NUM(a) > 1)
memset((void*)p->mask, 0xff, (BA_MASK_NUM(a)-1)*sizeof(uintptr_t));
p->mask[BA_MASK_NUM(a)-1] = BA_LAST_MASK(a);
p->mask[0] ^= TBITMASK(uintptr_t, 0);
|
385483 | 2011-11-18 | Arne Goedeke | | p->free_mask = 0;
p->blocks_used = 1;
#else
p->blocks_used = 1;
p->first = 2;
memset(p->data, 0x00, BA_PAGESIZE(a));
#endif
return p->data;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
void ba_free(struct block_allocator * a, void * ptr) {
|
0a3d34 | 2011-11-10 | Arne Goedeke | | uintptr_t t;
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
|
0a3d34 | 2011-11-10 | Arne Goedeke | | unsigned int mask, bit;
|
385483 | 2011-11-18 | Arne Goedeke | | #endif
|
0a3d34 | 2011-11-10 | Arne Goedeke | | ba_page p;
|
67795d | 2011-11-11 | Arne Goedeke | | unsigned int n;
n = a->last_free;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | if (n) {
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, n);
|
67795d | 2011-11-11 | Arne Goedeke | | if (!BA_CHECK_PTR(a, p, ptr)) n = 0;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
e90c49 | 2011-11-27 | Arne Goedeke | | if (unlikely(!n)) {
|
67795d | 2011-11-11 | Arne Goedeke | | a->last_free = n = ba_htable_lookup(a, ptr);
|
e90c49 | 2011-11-27 | Arne Goedeke | | if (unlikely(!n)) {
#define BA_DEBUG
#ifdef BA_DEBUG
fprintf(stderr, "magnitude: %u\n", a->magnitude);
|
385483 | 2011-11-18 | Arne Goedeke | | 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)
);
|
e90c49 | 2011-11-27 | Arne Goedeke | | ba_print_htable(a);
#endif
|
67795d | 2011-11-11 | Arne Goedeke | | Pike_error("Unknown pointer \n");
}
|
385483 | 2011-11-18 | Arne Goedeke | | p = BA_PAGE(a, n);
|
67795d | 2011-11-11 | Arne Goedeke | | }
|
0a3d34 | 2011-11-10 | Arne Goedeke | | t = (uintptr_t)((char*)ptr - p->data)/a->block_size;
|
385483 | 2011-11-18 | Arne Goedeke | | #ifndef BA_SEGREGATE
|
0a3d34 | 2011-11-10 | Arne Goedeke | | mask = t / (sizeof(uintptr_t) * 8);
bit = t & ((sizeof(uintptr_t)*8) - 1);
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
67795d | 2011-11-11 | Arne Goedeke | | t = p->mask[mask];
if (t & TBITMASK(uintptr_t, bit)) {
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | Pike_error("double free!");
}
|
67795d | 2011-11-11 | Arne Goedeke | | if (mask < p->free_mask)
p->free_mask = mask;
|
385483 | 2011-11-18 | Arne Goedeke | |
p->blocks_used --;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | if (unlikely(t == 0)) {
|
e90c49 | 2011-11-27 | Arne Goedeke | | unsigned int i;
|
67795d | 2011-11-11 | Arne Goedeke | | p->free_mask = mask;
|
e90c49 | 2011-11-27 | Arne Goedeke | | for (i = 0; i < BA_MASK_NUM(a); i++) if (p->mask[i]) {
p->mask[mask] = TBITMASK(uintptr_t, bit);
return;
}
p->mask[mask] = TBITMASK(uintptr_t, bit);
|
385483 | 2011-11-18 | Arne Goedeke | | if (p->blocks_used != a->blocks-1) {
fprintf(stderr, "wrong block count detected: %u vs %u\n", p->blocks_used+1, a->blocks);
Pike_error("croak\n");
}
if (a->first == 0) {
a->first = a->last = n;
p->next = p->prev = 0;
} else {
p->next = a->first;
BA_PAGE(a, a->first)->prev = n;
a->first = n;
}
return;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
67795d | 2011-11-11 | Arne Goedeke | | t |= TBITMASK(uintptr_t, bit);
p->mask[mask] = t;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | if (unlikely(~t == 0)) {
for (bit = 0; bit < BA_MASK_NUM(a)-1; bit++) if (~p->mask[bit]) return;
if (~(p->mask[BA_MASK_NUM(a)-1]|~BA_LAST_MASK(a))) return;
if (p->blocks_used != 0) {
fprintf(stderr, "wrong block count detected: %u vs %u\n", p->blocks_used+1, 1);
Pike_error("croak\n");
}
ba_remove_page(a, n);
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | |
} else if (p->blocks_used == 0 || p->blocks_used == a->blocks - 1) {
fprintf(stderr, "wrong number of used blocks: %u\n", p->blocks_used);
Pike_error("bad croak\n");
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|
385483 | 2011-11-18 | Arne Goedeke | | #else
if (p->blocks_used == a->blocks) {
|
67795d | 2011-11-11 | Arne Goedeke | | if (a->first == 0) {
a->first = a->last = n;
p->next = p->prev = 0;
} else {
p->next = a->first;
BA_PAGE(a, a->first)->prev = n;
a->first = n;
}
|
385483 | 2011-11-18 | Arne Goedeke | | } else if (p->blocks_used == 1) {
ba_remove_page(a, n);
return;
}
p->blocks_used --;
((struct ba_block_header*)ptr)->next = p->first;
|
e90c49 | 2011-11-27 | Arne Goedeke | |
|
385483 | 2011-11-18 | Arne Goedeke | | p->first = t+1;
#endif
}
static inline void ba_remove_page(struct block_allocator * a,
uint16_t n) {
ba_page tmp, p;
p = BA_PAGE(a, n);
|
475075 | 2011-11-19 | Arne Goedeke | | |
385483 | 2011-11-18 | Arne Goedeke | | fprintf(stderr, "removing page %4u\t(%p .. %p) -> %X (%X) (of %u).\n", n,
p->data, BA_LASTBLOCK(a, p), hash1(a, BA_LASTBLOCK(a,p)),
hash1(a, BA_LASTBLOCK(a,p)) & BA_HASH_MASK(a),
a->num_pages);
|
475075 | 2011-11-19 | Arne Goedeke | | */
|
385483 | 2011-11-18 | Arne Goedeke | |
ba_htable_delete(a, BA_LASTBLOCK(a, p), n);
free(p->data);
p->data = NULL;
if (a->last_free == n)
a->last_free = 0;
else if (a->last_free == a->num_pages)
a->last_free = n;
if (p->prev) {
tmp = BA_PAGE(a, p->prev);
tmp->next = p->next;
} else {
a->first = p->next;
}
if (p->next) {
tmp = BA_PAGE(a, p->next);
tmp->prev = p->prev;
} else {
a->last = p->prev;
}
|
de2d07 | 2011-12-25 | Arne Goedeke | | if (a->num_pages != n) {
tmp = BA_PAGE(a, a->num_pages);
ba_htable_replace(a, BA_LASTBLOCK(a, tmp), a->num_pages, n);
*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;
}
}
memset(BA_PAGE(a, a->num_pages), 0, sizeof(struct ba_page));
|
385483 | 2011-11-18 | Arne Goedeke | | a->num_pages--;
|
a5e1b8 | 2011-11-09 | Arne Goedeke | | }
|