matter.c

Cross-platform minimalist libc
git clone git://git.finwo.net/lib/matter.c
Log | Files | Refs | README | LICENSE

brk.c (594B)


      1 #include <stddef.h>
      2 #include <unistd.h>
      3 
      4 #include <matter/page_size.h>
      5 
      6 
      7 extern unsigned char __heap_base;
      8 void *break_pointer = &__heap_base;
      9 
     10 int brk(void *addr) {
     11   break_pointer = addr;
     12   return 0;
     13 }
     14 
     15 #ifdef __wasm__
     16 
     17 void * sbrk(ssize_t increment) {
     18   size_t current_pages = __builtin_wasm_memory_size(0);
     19   void   *heap_limit   = (void *)(current_pages * PAGE_SIZE);
     20 
     21   void *ret = break_pointer;
     22   break_pointer += increment;
     23 
     24   if (break_pointer > heap_limit) {
     25     __builtin_wasm_memory_grow(0, ((break_pointer - heap_limit) / PAGE_SIZE) + 1);
     26   }
     27 
     28   return ret;
     29 }
     30 
     31 #endif // __wasm__