summaryrefslogtreecommitdiff
path: root/src/stack-allocator.c
diff options
context:
space:
mode:
authorBen Smith <binjimin@gmail.com>2017-01-31 17:16:19 -0800
committerGitHub <noreply@github.com>2017-01-31 17:16:19 -0800
commit4fdbb4647c519a42f3e54ed7a858194780ca2ae4 (patch)
tree3aebad7746c492930c32786061c655bb017fdc81 /src/stack-allocator.c
parentedb3a471bc249855d5ece45dc21ee69af700b19b (diff)
downloadwabt-4fdbb4647c519a42f3e54ed7a858194780ca2ae4.tar.gz
wabt-4fdbb4647c519a42f3e54ed7a858194780ca2ae4.tar.bz2
wabt-4fdbb4647c519a42f3e54ed7a858194780ca2ae4.zip
Rename all wasm prefixes to wabt (#298)
Diffstat (limited to 'src/stack-allocator.c')
-rw-r--r--src/stack-allocator.c132
1 files changed, 66 insertions, 66 deletions
diff --git a/src/stack-allocator.c b/src/stack-allocator.c
index 1710e329..55aeda9a 100644
--- a/src/stack-allocator.c
+++ b/src/stack-allocator.c
@@ -29,22 +29,22 @@
#define TRACEF(...)
#endif
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
#include <stdio.h>
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
#define CHUNK_ALIGN 8
#define CHUNK_SIZE (1024 * 1024)
-#define CHUNK_MAX_AVAIL (CHUNK_SIZE - sizeof(WasmStackAllocatorChunk))
+#define CHUNK_MAX_AVAIL (CHUNK_SIZE - sizeof(WabtStackAllocatorChunk))
typedef struct StackAllocatorMark {
- WasmStackAllocatorChunk* chunk;
+ WabtStackAllocatorChunk* chunk;
void* chunk_current;
void* last_allocation;
} StackAllocatorMark;
#ifndef NDEBUG
-static WasmBool is_power_of_two(uint32_t x) {
+static WabtBool is_power_of_two(uint32_t x) {
return x && ((x & (x - 1)) == 0);
}
#endif /* NDEBUG */
@@ -53,54 +53,54 @@ static void* align_up(void* p, size_t align) {
return (void*)(((intptr_t)p + align - 1) & ~(align - 1));
}
-static WasmBool allocation_in_chunk(WasmStackAllocatorChunk* chunk, void* p) {
+static WabtBool allocation_in_chunk(WabtStackAllocatorChunk* chunk, void* p) {
return p >= (void*)chunk && p < chunk->end;
}
-static WasmStackAllocatorChunk* allocate_chunk(
- WasmStackAllocator* stack_allocator,
+static WabtStackAllocatorChunk* allocate_chunk(
+ WabtStackAllocator* stack_allocator,
size_t max_avail,
const char* file,
int line) {
- assert(max_avail < SIZE_MAX - sizeof(WasmStackAllocatorChunk));
- size_t real_size = max_avail + sizeof(WasmStackAllocatorChunk);
+ assert(max_avail < SIZE_MAX - sizeof(WabtStackAllocatorChunk));
+ size_t real_size = max_avail + sizeof(WabtStackAllocatorChunk);
/* common case of allocating a chunk of exactly CHUNK_SIZE */
if (real_size == CHUNK_SIZE) {
if (stack_allocator->next_free) {
- WasmStackAllocatorChunk* chunk = stack_allocator->next_free;
+ WabtStackAllocatorChunk* chunk = stack_allocator->next_free;
stack_allocator->next_free = stack_allocator->next_free->next_free;
return chunk;
}
}
- WasmStackAllocatorChunk* chunk =
- wasm_alloc(stack_allocator->fallback, real_size, CHUNK_ALIGN);
+ WabtStackAllocatorChunk* chunk =
+ wabt_alloc(stack_allocator->fallback, real_size, CHUNK_ALIGN);
if (!chunk) {
if (stack_allocator->has_jmpbuf)
longjmp(stack_allocator->jmpbuf, 1);
- WASM_FATAL("%s:%d: memory allocation failed\n", file, line);
+ WABT_FATAL("%s:%d: memory allocation failed\n", file, line);
}
- /* use the same allocation for the WasmStackAllocatorChunk and its data. + 1
- * skips over the WasmStackAllocatorChunk */
+ /* use the same allocation for the WabtStackAllocatorChunk and its data. + 1
+ * skips over the WabtStackAllocatorChunk */
chunk->start = chunk + 1;
chunk->current = chunk->start;
chunk->end = (void*)((intptr_t)chunk->start + max_avail);
chunk->prev = NULL;
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
stack_allocator->chunk_alloc_count++;
stack_allocator->total_chunk_bytes += CHUNK_SIZE;
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
return chunk;
}
-static void* stack_alloc(WasmAllocator* allocator,
+static void* stack_alloc(WabtAllocator* allocator,
size_t size,
size_t align,
const char* file,
int line) {
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
assert(is_power_of_two(align));
- WasmStackAllocatorChunk* chunk = stack_allocator->last;
+ WabtStackAllocatorChunk* chunk = stack_allocator->last;
assert(size < SIZE_MAX - align + 1);
size_t alloc_size = size + align - 1;
void* result;
@@ -141,14 +141,14 @@ static void* stack_alloc(WasmAllocator* allocator,
}
#endif /* TRACE_ALLOCATOR */
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
stack_allocator->alloc_count++;
stack_allocator->total_alloc_bytes += size;
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
return result;
}
-static void* stack_realloc(WasmAllocator* allocator,
+static void* stack_realloc(WabtAllocator* allocator,
void* p,
size_t size,
size_t align,
@@ -157,9 +157,9 @@ static void* stack_realloc(WasmAllocator* allocator,
if (!p)
return stack_alloc(allocator, size, align, file, line);
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
/* TODO(binji): optimize */
- WasmStackAllocatorChunk* chunk = stack_allocator->last;
+ WabtStackAllocatorChunk* chunk = stack_allocator->last;
while (chunk) {
if (allocation_in_chunk(chunk, p))
break;
@@ -183,17 +183,17 @@ static void* stack_realloc(WasmAllocator* allocator,
size_t old_max_size = (size_t)chunk->end - (size_t)p;
size_t copy_size = size > old_max_size ? old_max_size : size;
memmove(result, p, copy_size);
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
/* count this is as a realloc, not an alloc */
stack_allocator->alloc_count--;
stack_allocator->realloc_count++;
stack_allocator->total_alloc_bytes -= size;
stack_allocator->total_realloc_bytes += size;
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
return result;
}
-static void stack_free(WasmAllocator* allocator,
+static void stack_free(WabtAllocator* allocator,
void* p,
const char* file,
int line) {
@@ -201,41 +201,41 @@ static void stack_free(WasmAllocator* allocator,
return;
TRACEF("%s:%d: stack_free(%p)\n", file, line, p);
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
stack_allocator->free_count++;
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
if (p != stack_allocator->last_allocation)
return;
- WasmStackAllocatorChunk* chunk = stack_allocator->last;
+ WabtStackAllocatorChunk* chunk = stack_allocator->last;
assert(allocation_in_chunk(chunk, p));
chunk->current = p;
}
-static void stack_destroy(WasmAllocator* allocator) {
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+static void stack_destroy(WabtAllocator* allocator) {
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
/* destroy the free chunks */
- WasmStackAllocatorChunk* chunk = stack_allocator->next_free;
+ WabtStackAllocatorChunk* chunk = stack_allocator->next_free;
while (chunk) {
- WasmStackAllocatorChunk* next_free = chunk->next_free;
- wasm_free(stack_allocator->fallback, chunk);
+ WabtStackAllocatorChunk* next_free = chunk->next_free;
+ wabt_free(stack_allocator->fallback, chunk);
chunk = next_free;
}
/* destroy the used chunks */
chunk = stack_allocator->last;
while (chunk) {
- WasmStackAllocatorChunk* prev = chunk->prev;
- wasm_free(stack_allocator->fallback, chunk);
+ WabtStackAllocatorChunk* prev = chunk->prev;
+ wabt_free(stack_allocator->fallback, chunk);
chunk = prev;
}
}
-static WasmAllocatorMark stack_mark(WasmAllocator* allocator) {
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+static WabtAllocatorMark stack_mark(WabtAllocator* allocator) {
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
/* allocate the space for the mark, but copy the current stack state now, so
* when we reset we reset before the mark was allocated */
@@ -245,24 +245,24 @@ static WasmAllocatorMark stack_mark(WasmAllocator* allocator) {
mark.last_allocation = stack_allocator->last_allocation;
StackAllocatorMark* allocated_mark = stack_alloc(
- allocator, sizeof(StackAllocatorMark), WASM_DEFAULT_ALIGN, NULL, 0);
-#if WASM_STACK_ALLOCATOR_STATS
+ allocator, sizeof(StackAllocatorMark), WABT_DEFAULT_ALIGN, NULL, 0);
+#if WABT_STACK_ALLOCATOR_STATS
/* don't count this allocation */
stack_allocator->alloc_count--;
stack_allocator->total_alloc_bytes -= sizeof(StackAllocatorMark);
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
*allocated_mark = mark;
return allocated_mark;
}
-static void stack_reset_to_mark(WasmAllocator* allocator,
- WasmAllocatorMark mark) {
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+static void stack_reset_to_mark(WabtAllocator* allocator,
+ WabtAllocatorMark mark) {
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
StackAllocatorMark* stack_mark = (StackAllocatorMark*)mark;
- WasmStackAllocatorChunk* chunk = stack_allocator->last;
+ WabtStackAllocatorChunk* chunk = stack_allocator->last;
while (chunk && chunk != stack_mark->chunk) {
- WasmStackAllocatorChunk* prev = chunk->prev;
+ WabtStackAllocatorChunk* prev = chunk->prev;
/* reset this chunk for future use, and thread it into the free list */
chunk->current = chunk->start;
chunk->next_free = stack_allocator->next_free;
@@ -276,7 +276,7 @@ static void stack_reset_to_mark(WasmAllocator* allocator,
stack_allocator->last_allocation = stack_mark->last_allocation;
}
-#if WASM_STACK_ALLOCATOR_STATS
+#if WABT_STACK_ALLOCATOR_STATS
static const char* human_readable(size_t count) {
/* printing a size_t in decimal requires ceil(sizeof(size_t) * 8 / log2(10))
* characters. We want to add one comma for each group of three digits, so
@@ -304,12 +304,12 @@ static const char* human_readable(size_t count) {
}
return buffer;
}
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
-static void stack_print_stats(WasmAllocator* allocator) {
-#if WASM_STACK_ALLOCATOR_STATS
+static void stack_print_stats(WabtAllocator* allocator) {
+#if WABT_STACK_ALLOCATOR_STATS
#define VALUE(name) human_readable(stack_allocator->name)
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
printf("STACK ALLOCATOR STATS:\n");
printf("===============================================\n");
printf("chunk_alloc_count: %s\n", VALUE(chunk_alloc_count));
@@ -320,18 +320,18 @@ static void stack_print_stats(WasmAllocator* allocator) {
printf("total_alloc_bytes: %s\n", VALUE(total_alloc_bytes));
printf("total_realloc_bytes: %s\n", VALUE(total_realloc_bytes));
#undef VALUE
-#endif /* WASM_STACK_ALLOCATOR_STATS */
+#endif /* WABT_STACK_ALLOCATOR_STATS */
}
-static int stack_setjmp_handler(WasmAllocator* allocator) {
- WasmStackAllocator* stack_allocator = (WasmStackAllocator*)allocator;
- stack_allocator->has_jmpbuf = WASM_TRUE;
+static int stack_setjmp_handler(WabtAllocator* allocator) {
+ WabtStackAllocator* stack_allocator = (WabtStackAllocator*)allocator;
+ stack_allocator->has_jmpbuf = WABT_TRUE;
return setjmp(stack_allocator->jmpbuf);
}
-WasmResult wasm_init_stack_allocator(WasmStackAllocator* stack_allocator,
- WasmAllocator* fallback) {
- WASM_ZERO_MEMORY(*stack_allocator);
+WabtResult wabt_init_stack_allocator(WabtStackAllocator* stack_allocator,
+ WabtAllocator* fallback) {
+ WABT_ZERO_MEMORY(*stack_allocator);
stack_allocator->allocator.alloc = stack_alloc;
stack_allocator->allocator.realloc = stack_realloc;
stack_allocator->allocator.free = stack_free;
@@ -342,13 +342,13 @@ WasmResult wasm_init_stack_allocator(WasmStackAllocator* stack_allocator,
stack_allocator->allocator.setjmp_handler = stack_setjmp_handler;
stack_allocator->fallback = fallback;
- WasmStackAllocatorChunk* chunk =
+ WabtStackAllocatorChunk* chunk =
allocate_chunk(stack_allocator, CHUNK_MAX_AVAIL, __FILE__, __LINE__);
chunk->prev = NULL;
stack_allocator->first = stack_allocator->last = chunk;
- return WASM_OK;
+ return WABT_OK;
}
-void wasm_destroy_stack_allocator(WasmStackAllocator* stack_allocator) {
- stack_destroy((WasmAllocator*)stack_allocator);
+void wabt_destroy_stack_allocator(WabtStackAllocator* stack_allocator) {
+ stack_destroy((WabtAllocator*)stack_allocator);
}