Last active
May 14, 2019 15:12
-
-
Save axic/04c04fc69f3a38f831b6b9947669d33c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* QIMalloc - Quick, incremental malloc. | |
* | |
* This is a dummy malloc implementation, without the option to free memory space. | |
*/ | |
static int __malloc_ptr = 0; | |
static int __malloc_end = 0; | |
void *malloc(size_t size) { | |
if (__malloc_end == 0) { | |
__malloc_end = memsize(); | |
} | |
// FIXME: skip the stack space | |
// Increase allocated memory space | |
if ((__malloc_ptr + size) > __malloc_end) { | |
__malloc_end = sbrk(size); | |
if (__malloc_end == -1) { | |
__builtin_unreachable(); | |
} | |
__malloc_end += size; | |
} | |
const int ret = __malloc_ptr; | |
__malloc_ptr += size; | |
return ret; | |
} | |
// NOTE: no need to zero out space as WASM memory is pre-set to 0 and we don't reuse blocks | |
void *calloc(size_t count, size_t size) { | |
return malloc(count * size); | |
} | |
void free(void *ptr) { | |
__builtin_unreachable(); | |
} | |
void *realloc(void *ptr, size_t size) { | |
__builtin_unreachable(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; | |
;; Very basic sbrk implementation in wasm. Written in 2016. | |
;; | |
(module | |
(memory 1) | |
(export "sbrk" $__sbrk) | |
(export "memsize" $__memsize) | |
(func $__sbrk | |
(param $inc i32) | |
(result i32) | |
(local $end i32) | |
(set_local $end (call $__memsize)) | |
(if (i32.eq (grow_memory (i32.div_u (get_local $inc) (i32.const 65536))) (i32.const -1)) | |
(then (i32.const -1)) | |
(else (get_local $end)) | |
) | |
) | |
(func $__memsize | |
(result i32) | |
(i32.mul (current_memory) (i32.const 65536)) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment