pike.git/
src/
block_allocator.c
Branch:
Tag:
Non-build tags
All tags
No tags
2012-02-09
2012-02-09 13:29:46 by Arne Goedeke <el@laramies.com>
f25b0bb075b26e86385c78523d2c5586c8e023ac (
28
lines) (+
17
/-
11
)
[
Show
|
Annotate
]
Branch:
arne/block_alloc
fixed page indexing
17:
#define BA_SPAGE_SIZE(a) (sizeof(struct ba_page) + (BA_MASK_NUM(a) - 1)*sizeof(uintptr_t)) #define BA_HASH_MASK(a) ((1 << (a->allocated + 1)) - 1) #define BA_CHECK_PTR(a, p, ptr) ((char*)ptr >= p->data && ((uintptr_t)(p->data) + BA_PAGESIZE(a) > (uintptr_t)ptr))
+
#define BA_PAGE(a, n) ((ba_page)((char*)a->pages + (n) * BA_SPAGE_SIZE(a)))
void ba_init(struct block_allocator * a, uint32_t block_size, uint32_t blocks) {
34:
a->num_pages = 0; a->allocated = 5;
-
a->pages =
(ba_page)
malloc(( BA_SPAGE_SIZE(a) + 2 * sizeof(uint16_t))
+
a->pages = malloc(( BA_SPAGE_SIZE(a) + 2 * sizeof(uint16_t))
* (1 << a->allocated)); if (!a->pages) { Pike_error("no mem"); }
-
a->htable = (uint16_t*)
(((char*)a->pages) +
BA_
SPAGE_SIZE
(a
)
*
(1 << a->allocated));
-
memset(
(void*)
a->pages, 0,
+
a->htable = (uint16_t*) BA_
PAGE
(a
,
(1 << a->allocated));
+
memset(a->pages, 0,
( BA_SPAGE_SIZE(a) + 2 * sizeof(uint16_t)) * (1 << a->allocated)); }
82:
hval = hash1(a, ptr); while ((n = a->htable[hval & BA_HASH_MASK(a)])) {
-
p =
&
a
->pages[
n-1
]
;
+
p =
BA_PAGE(
a
,
n-1
)
;
if (BA_CHECK_PTR(a, p, ptr)) { return n; }
91:
hval = hash2(a, ptr); while ((n = a->htable[hval & BA_HASH_MASK(a)])) {
-
p =
&
a
->pages[
n-1
]
;
+
p =
BA_PAGE(
a
,
n-1
)
;
if (BA_CHECK_PTR(a, p, ptr)) { return n; }
105:
size_t i, j; if (a->free == 0) {
-
p =
&
a
->pages[
a->num_pages++
]
;
+
p =
BA_PAGE(
a
,
a->num_pages++
)
;
p->data = malloc(BA_PAGESIZE(a)); if (!p->data) { Pike_error("no mem");
117:
p->mask[0] = ~TBITMASK(uintptr_t, 0); return p->data; } else {
-
p =
&
a
->pages[
a->free - 1
]
;
+
p =
BA_PAGE(
a
,
a->free - 1
)
;
-
for (i = 0; i < BA_MASK_NUM(a); i++) {
+
for (
j = -1,
i = 0; i < BA_MASK_NUM(a); i++) {
uintptr_t m = p->mask[i]; if (m) { if (sizeof(uintptr_t) == 8)
131:
} }
+
if (j == -1) {
+
Pike_error("This should not happen!\n");
+
}
+
// now empty. if (p->mask[i] == 0 && i == BA_MASK_NUM(a)-1) { a->free = p->free;
151:
Pike_error("Unknown pointer: %p\n", ptr); }
-
p =
&
a
->pages[
n-1
]
;
+
p =
BA_PAGE(
a
,
n-1
)
;
t = (uintptr_t)((char*)ptr - p->data)/a->block_size; mask = t / (sizeof(uintptr_t) * 8); bit = t & ((sizeof(uintptr_t)*8) - 1);
171:
p->free = a->free; a->free = n; } else {
-
ba_page tmp =
&
a
->pages[
a->free-1
]
;
+
ba_page tmp =
BA_PAGE(
a
,
a->free-1
)
;
while (tmp->free && tmp->free < n) {
-
tmp =
&
a
->pages[
tmp->free - 1
]
;
+
tmp =
BA_PAGE(
a
,
tmp->free - 1
)
;
} p->free = tmp->free; tmp->free = n;