]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/libventi/mem.c
venti: fix memory layers
[plan9front.git] / sys / src / libventi / mem.c
index dea99a9d1a0abacef20424505f00549d516066ab..57e108a5adbe0a4ac1afad80157573a8f86f6263 100644 (file)
@@ -7,55 +7,55 @@ enum {
        ChunkSize       = 128*1024
 };
 
-
 void
 vtfree(void *p)
 {
-       if(p == 0)
-               return;
        free(p);
 }
 
 void *
-vtmalloc(int size)
+vtmalloc(ulong size)
 {
-       void *p;
-
-       p = malloc(size);
-       if(p == 0)
-               sysfatal("vtmalloc: out of memory");
+       void *p = mallocz(size, 0);
+       if(p == nil){
+               fprint(2, "vtmalloc: out of memory allocating %lud", size);
+               abort();
+       }
        setmalloctag(p, getcallerpc(&size));
        return p;
 }
 
 void *
-vtmallocz(int size)
+vtmallocz(ulong size)
 {
-       void *p = vtmalloc(size);
-       memset(p, 0, size);
+       void *p = mallocz(size, 1);
+       if(p == nil){
+               fprint(2, "vtmallocz: out of memory allocating %lud", size);
+               abort();
+       }
        setmalloctag(p, getcallerpc(&size));
        return p;
 }
 
 void *
-vtrealloc(void *p, int size)
+vtrealloc(void *p, ulong size)
 {
-       if(p == nil)
-               return vtmalloc(size);
        p = realloc(p, size);
-       if(p == 0)
-               sysfatal("vtMemRealloc: out of memory");
+       if(p == 0 && size != 0){
+               fprint(2, "vtrealloc: out of memory allocating %lud", size);
+               abort();
+       }
        setrealloctag(p, getcallerpc(&size));
        return p;
 }
 
 void *
-vtbrk(int n)
+vtbrk(ulong n)
 {
        static Lock lk;
        static uchar *buf;
-       static int nbuf, nchunk;
-       int align, pad;
+       static ulong nbuf, nchunk;
+       ulong align, pad;
        void *p;
 
        if(n >= IdealAlignment)
@@ -65,10 +65,20 @@ vtbrk(int n)
        else    
                align = 4;
 
+       if(n > ChunkSize){
+               p = sbrk(n);
+               if(p == (void*)-1)
+                       sysfatal("Failed to allocate permanent chunk size %lud", n);
+               memset(p, 0, n);
+               return (uchar*)p;
+       }
        lock(&lk);
        pad = (align - (uintptr)buf) & (align-1);
        if(n + pad > nbuf) {
-               buf = vtmallocz(ChunkSize);
+               buf = sbrk(ChunkSize);
+               if(buf == (void*)-1)
+                       sysfatal("Failed to allocate permanent chunk size %ud", ChunkSize);
+               memset(buf, 0, ChunkSize);
                nbuf = ChunkSize;
                pad = (align - (uintptr)buf) & (align-1);
                nchunk++;