summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-06-30 15:46:03 -0400
committerJakob Kaivo <jkk@ung.org>2020-06-30 15:46:03 -0400
commit9a1d1230d08dd16ce49ce8b5253693bfe711fd5b (patch)
tree3aa4715be585c9f372aa0d5f19f8a34e91a1762b
parentf2a8df6171b3eae8c949c892e8a35d7acd51084c (diff)
fix some error handling, annotate future work
-rw-r--r--src/mapalloc.c27
1 files changed, 17 insertions, 10 deletions
diff --git a/src/mapalloc.c b/src/mapalloc.c
index 4848f94..95f63bf 100644
--- a/src/mapalloc.c
+++ b/src/mapalloc.c
@@ -12,6 +12,7 @@
#include "mapalloc.h"
#ifndef PAGESIZE
+#define PAGESIZE get_pagesize()
static size_t get_pagesize(void)
{
static size_t pagesize = 0;
@@ -20,10 +21,10 @@ static size_t get_pagesize(void)
}
return pagesize;
}
-
-#define PAGESIZE get_pagesize()
#endif
+#define MAPALLOC_EXIT_VALUE (127 + SIGSEGV)
+
struct bucket {
size_t used;
size_t allocated;
@@ -52,9 +53,9 @@ static void *page_alloc(size_t npages)
static void map_signal_action(int sig, siginfo_t *si, void *addr)
{
- (void)sig;
+ (void)sig; (void)addr;
fprintf(stderr, "error accessing %p\n", si->si_addr);
- _exit(127 + SIGSEGV);
+ _exit(MAPALLOC_EXIT_VALUE);
}
static void set_signal_handler(void)
@@ -69,6 +70,9 @@ static void set_signal_handler(void)
static struct bucket *get_bucket(void *ptr, int allocate)
{
+ /* FIXME: assumption that one page can hold UCHAR_MAX uintptr_t */
+ /* FIXME: check return values of page_alloc() */
+
static uintptr_t *trie_top = NULL;
if (trie_top == NULL) {
trie_top = page_alloc(1);
@@ -117,6 +121,9 @@ void *map_malloc(size_t nbytes)
}
char *ptr = page_alloc(pages);
+ if (ptr == MAP_FAILED) {
+ return NULL;
+ }
mprotect(ptr, PAGESIZE, PROT_NONE);
mprotect(ptr + ((pages - 1) * PAGESIZE), PAGESIZE, PROT_NONE);
@@ -136,13 +143,13 @@ void *map_realloc(void *ptr, size_t n)
struct bucket *b = get_bucket(ptr, 0);
if (b == NULL) {
- fprintf(stderr, "attempt to realloc() invalid pointer %p\n", ptr);
- abort();
+ fprintf(stderr, "%s(%p, %zu): invalid pointer\n", __func__, ptr, n);
+ _exit(MAPALLOC_EXIT_VALUE);
}
if (n < (b->allocated - (PAGESIZE * 2))) {
b->used = n;
- /* munmap() and mprotect() as necessary */
+ /* TODO: munmap() and mprotect() as necessary */
return ptr;
}
@@ -162,13 +169,13 @@ void map_free(void *ptr)
struct bucket *b = get_bucket(ptr, 0);
if (b == NULL) {
- fprintf(stderr, "attempt to free() invalid pointer %p\n", ptr);
- abort();
+ fprintf(stderr, "%s(%p): invalid pointer\n", __func__, ptr);
+ _exit(MAPALLOC_EXIT_VALUE);
}
char *base = ptr;
base -= PAGESIZE;
munmap(base, b->allocated);
- /* clear bucket */
+ /* TODO: clear bucket */
}