Merge pull request #1 from fluencelabs/memory-grow-mk4

Simple memory grow
This commit is contained in:
vms 2019-04-07 11:35:20 +03:00 committed by GitHub
commit f5a94478d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 109 additions and 7 deletions

1
.gitignore vendored
View File

@ -6,3 +6,4 @@
config.mak
lib/musl-gcc.specs
/obj/
.idea/*

View File

@ -4,7 +4,7 @@
#include <stdlib.h>
#define a_barrier() syscall(__NR_membarrier)
#define a_barrier() (0)
#define a_cas a_cas
static inline int a_cas(volatile int *p, int t, int s)

View File

@ -7,3 +7,13 @@ static inline struct pthread *__pthread_self(void) {
#define CANCEL_REG_IP 16
#define MC_PC gregs[REG_EIP]
#define __wait __wait
static inline void __wait(volatile int *addr, volatile int *waiters, int cnt, int priv) {}
static inline void __wake(volatile void *addr, int cnt, int priv) {}
static inline void __futexwait(volatile void *addr, int val, int priv) {}
#define __block_all_sigs __block_all_sigs
static inline void __block_all_sigs(void *set) {}
static inline void __block_app_sigs(void *set) {}
static inline void __restore_sigs(void *set) {}

View File

@ -34,7 +34,13 @@ struct bin {
#define C_INUSE ((size_t)1)
#ifndef __wasm__
#define IS_MMAPPED(c) !((c)->csize & (C_INUSE))
#else
#undef MMAP_THRESHOLD
#define MMAP_THRESHOLD ((size_t)-1)
#define IS_MMAPPED(c) (0)
#endif
__attribute__((__visibility__("hidden")))
void __bin_chunk(struct chunk *);

View File

@ -150,6 +150,8 @@ void __vm_unlock(void);
int __timedwait(volatile int *, int, clockid_t, const struct timespec *, int);
int __timedwait_cp(volatile int *, int, clockid_t, const struct timespec *, int);
#ifndef __wait
void __wait(volatile int *, volatile int *, int, int);
static inline void __wake(volatile void *addr, int cnt, int priv)
{
@ -164,14 +166,17 @@ static inline void __futexwait(volatile void *addr, int val, int priv)
__syscall(SYS_futex, addr, FUTEX_WAIT|priv, val) != -ENOSYS ||
__syscall(SYS_futex, addr, FUTEX_WAIT, val);
}
#endif
void __acquire_ptc(void);
void __release_ptc(void);
void __inhibit_ptc(void);
#ifndef __block_all_sigs
void __block_all_sigs(void *);
void __block_app_sigs(void *);
void __restore_sigs(void *);
#endif
#define DEFAULT_STACK_SIZE 81920
#define DEFAULT_GUARD_SIZE 4096

View File

@ -99,6 +99,7 @@ static int bin_index_up(size_t x)
if (x <= 32) return x;
x--;
if (x < 512) return bin_tab[x/8-4] + 1;
if (x > 0x1c00) return 64;
return bin_tab[x/128-4] + 17;
}
@ -283,6 +284,34 @@ static void trim(struct chunk *self, size_t n)
__bin_chunk(split);
}
// returns chunk which size is more then given n from the 63-th unsorted bin
// if there is no suitable chunk returns 0
static struct chunk *get_chunk_from_unsorted_bin(size_t n)
{
const int unsorted_bin_id = 63;
struct chunk *current = 0;
lock_bin(unsorted_bin_id);
current = mal.bins[unsorted_bin_id].head;
while(current != 0 && current != mal.bins[unsorted_bin_id].tail) {
// use the simple first-fit strategy
if(current->csize >= n) {
if (!pretrim(current, n, unsorted_bin_id, unsorted_bin_id)) {
unbin(current, unsorted_bin_id);
}
unlock_bin(unsorted_bin_id);
return current;
}
current = current->next;
}
unlock_bin(unsorted_bin_id);
return 0;
}
void *malloc(size_t n)
{
struct chunk *c;
@ -293,7 +322,7 @@ void *malloc(size_t n)
if (n > MMAP_THRESHOLD) {
size_t len = n + OVERHEAD + PAGE_SIZE - 1 & -PAGE_SIZE;
char *base = __mmap(0, len, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
if (base == (void *)-1) return 0;
c = (void *)(base + SIZE_ALIGN - OVERHEAD);
c->csize = len - (SIZE_ALIGN - OVERHEAD);
@ -304,17 +333,25 @@ void *malloc(size_t n)
i = bin_index_up(n);
for (;;) {
uint64_t mask = mal.binmap & -(1ULL<<i);
if (!mask) {
if (i == 64) {
c = get_chunk_from_unsorted_bin(n);
if(c != 0) {
break;
}
}
if (i == 64 || !mask) {
c = expand_heap(n);
if (!c) return 0;
if (alloc_rev(c)) {
struct chunk *x = c;
c = PREV_CHUNK(c);
NEXT_CHUNK(x)->psize = c->csize =
x->csize + CHUNK_SIZE(c);
x->csize + CHUNK_SIZE(c);
}
break;
}
j = first_set(mask);
lock_bin(j);
c = mal.bins[j].head;

View File

@ -0,0 +1,28 @@
#include <limits.h>
#include <stdint.h>
#include <errno.h>
#include <sys/mman.h>
#include "libc.h"
#include "syscall.h"
#define WASM_PAGE_SIZE 65536
/* Expand the heap in-place use WebAssembly grow_memory operators.
* The caller is responsible for locking to prevent concurrent calls. */
void *__expand_heap(size_t *pn)
{
size_t n = *pn;
n += -n & WASM_PAGE_SIZE-1;
unsigned delta = n / WASM_PAGE_SIZE;
unsigned res = __builtin_wasm_memory_grow(0, delta);
if (res == (unsigned)-1) {
errno = ENOMEM;
return 0;
}
void *area = (void*)(WASM_PAGE_SIZE * res);
*pn = n;
return area;
}

View File

@ -0,0 +1,6 @@
#include <sys/mman.h>
#include "libc.h"
int __madvise(void *addr, size_t len, int advice) { return 0; }
weak_alias(__madvise, madvise);

View File

@ -0,0 +1 @@
#include "pthread_impl.h"

View File

@ -0,0 +1,3 @@
#include <signal.h>
int raise(int sig) { }

View File

@ -215,7 +215,8 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
if (t == 'c') {
if (width<1) width = 1;
invert = 1;
set = L"";
static const wchar_t empty[] = { 0 };
set = empty;
} else if (t == 's') {
invert = 1;
static const wchar_t spaces[] = {

View File

@ -12,8 +12,9 @@ static size_t do_read(FILE *f, unsigned char *buf, size_t len)
{
size_t i;
const wchar_t *wcs = f->cookie;
static const wchar_t strudel[] = { '@', 0 };
if (!wcs[0]) wcs=L"@";
if (!wcs[0]) wcs=strudel;
for (i=0; i<f->buf_size && wcs[i]; i++)
f->buf[i] = wcs[i] < 128 ? wcs[i] : '@';
f->rpos = f->buf;

View File

@ -14,8 +14,9 @@ static size_t do_read(FILE *f, unsigned char *buf, size_t len)
{
size_t i;
const wchar_t *wcs = f->cookie;
static const wchar_t strudel[] = { '@', 0 };
if (!wcs[0]) wcs=L"@";
if (!wcs[0]) wcs=strudel;
for (i=0; i<f->buf_size && wcs[i]; i++)
f->buf[i] = wcs[i] < 128 ? wcs[i] : '@';
f->rpos = f->buf;

View File

@ -0,0 +1,2 @@
#include "pthread_impl.h"