]> git.proxmox.com Git - wasi-libc.git/blame - libc-bottom-half/sources/sbrk.c
threads: enable access to `pthread_attr_get` functions (#357)
[wasi-libc.git] / libc-bottom-half / sources / sbrk.c
CommitLineData
320054e8
DG
1#include <unistd.h>
2#include <stdlib.h>
3#include <errno.h>
4#include <__macro_PAGESIZE.h>
5
ca9046d8 6// Bare-bones implementation of sbrk.
320054e8 7void *sbrk(intptr_t increment) {
ca9046d8 8 // sbrk(0) returns the current memory size.
d253aa3d 9 if (increment == 0) {
ca9046d8 10 // The wasm spec doesn't guarantee that memory.grow of 0 always succeeds.
d253aa3d 11 return (void *)(__builtin_wasm_memory_size(0) * PAGESIZE);
12 }
13
ca9046d8 14 // We only support page-size increments.
320054e8
DG
15 if (increment % PAGESIZE != 0) {
16 abort();
17 }
18
ca9046d8 19 // WebAssembly doesn't support shrinking linear memory.
320054e8
DG
20 if (increment < 0) {
21 abort();
22 }
23
24 uintptr_t old = __builtin_wasm_memory_grow(0, (uintptr_t)increment / PAGESIZE);
ca9046d8 25
320054e8
DG
26 if (old == SIZE_MAX) {
27 errno = ENOMEM;
28 return (void *)-1;
29 }
30
31 return (void *)(old * PAGESIZE);
32}