summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/mapalloc.c26
1 files changed, 17 insertions, 9 deletions
diff --git a/src/mapalloc.c b/src/mapalloc.c
index 0744ebf..6d49f9d 100644
--- a/src/mapalloc.c
+++ b/src/mapalloc.c
@@ -54,19 +54,29 @@ static struct bucket *get_bucket(void *ptr, int allocate)
static uintptr_t *trie_top = NULL;
if (trie_top == NULL) {
trie_top = page_alloc(1);
+ memset(trie_top, 0, PAGESIZE);
}
+ printf("- finding bucket %p (%d)\n", ptr, allocate);
uintptr_t *trie = trie_top;
uintptr_t addr = (uintptr_t)ptr;
for (size_t i = 0; i < sizeof(addr); i++) {
- if (trie == NULL && allocate == 0) {
- return NULL;
+ uintptr_t next = (addr >> ((sizeof(addr) - i) * CHAR_BIT))
+ & UCHAR_MAX;
+
+ printf("-- %02zx\n", next);
+ if (trie[next] == 0) {
+ if (allocate) {
+ uintptr_t *newtrie = page_alloc(1);
+ memset(newtrie, 0, PAGESIZE);
+ trie[next] = (uintptr_t) newtrie;
+ } else {
+ return NULL;
+ }
}
-
- uintptr_t next = (addr >> (i * CHAR_BIT)) & UCHAR_MAX;
trie = (uintptr_t*)trie[next];
}
- return addr ? NULL : NULL;
+ return trie ? (struct bucket *)trie : NULL;
}
void *map_calloc(size_t nelem, size_t elsize)
@@ -93,11 +103,9 @@ void *map_malloc(size_t nbytes)
mprotect(ptr, PAGESIZE, PROT_NONE);
mprotect(ptr + ((pages - 1) * PAGESIZE), PAGESIZE, PROT_NONE);
- /*
- struct bucket *b = get_bucket(ptr, 1);
+ struct bucket *b = get_bucket(ptr + PAGESIZE, 1);
b->used = nbytes;
b->allocated = pages * PAGESIZE;
- */
return ptr + PAGESIZE;
}
@@ -122,7 +130,7 @@ void *map_realloc(void *ptr, size_t n)
void *newptr = map_malloc(n);
if (newptr != NULL) {
- memcpy(newptr, ptr, n);
+ memcpy(newptr, ptr, b->used);
map_free(ptr);
}
return newptr;