diff options
author | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
---|---|---|
committer | Taru Karttunen <taruti@taruti.net> | 2011-03-30 15:46:40 +0300 |
commit | e5888a1ffdae813d7575f5fb02275c6bb07e5199 (patch) | |
tree | d8d51eac403f07814b9e936eed0c9a79195e2450 /sys/src/libventi/mem.c |
Import sources from 2011-03-30 iso image
Diffstat (limited to 'sys/src/libventi/mem.c')
-rwxr-xr-x | sys/src/libventi/mem.c | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/sys/src/libventi/mem.c b/sys/src/libventi/mem.c new file mode 100755 index 000000000..dea99a9d1 --- /dev/null +++ b/sys/src/libventi/mem.c @@ -0,0 +1,86 @@ +#include <u.h> +#include <libc.h> +#include <venti.h> + +enum { + IdealAlignment = 32, + ChunkSize = 128*1024 +}; + + +void +vtfree(void *p) +{ + if(p == 0) + return; + free(p); +} + +void * +vtmalloc(int size) +{ + void *p; + + p = malloc(size); + if(p == 0) + sysfatal("vtmalloc: out of memory"); + setmalloctag(p, getcallerpc(&size)); + return p; +} + +void * +vtmallocz(int size) +{ + void *p = vtmalloc(size); + memset(p, 0, size); + setmalloctag(p, getcallerpc(&size)); + return p; +} + +void * +vtrealloc(void *p, int size) +{ + if(p == nil) + return vtmalloc(size); + p = realloc(p, size); + if(p == 0) + sysfatal("vtMemRealloc: out of memory"); + setrealloctag(p, getcallerpc(&size)); + return p; +} + +void * +vtbrk(int n) +{ + static Lock lk; + static uchar *buf; + static int nbuf, nchunk; + int align, pad; + void *p; + + if(n >= IdealAlignment) + align = IdealAlignment; + else if(n > 8) + align = 8; + else + align = 4; + + lock(&lk); + pad = (align - (uintptr)buf) & (align-1); + if(n + pad > nbuf) { + buf = vtmallocz(ChunkSize); + nbuf = ChunkSize; + pad = (align - (uintptr)buf) & (align-1); + nchunk++; + } + + assert(n + pad <= nbuf); + + p = buf + pad; + buf += pad + n; + nbuf -= pad + n; + unlock(&lk); + + return p; +} + |