9base

revived minimalist port of Plan 9 userland to Unix
git clone git://git.suckless.org/9base
Log | Files | Refs | README | LICENSE

malloc.c (690B)


      1 /*
      2  * These are here mainly so that I can link against
      3  * debugmalloc.c instead and not recompile the world.
      4  */
      5 
      6 #include <u.h>
      7 #define NOPLAN9DEFINES
      8 #include <libc.h>
      9 
     10 static Lock malloclock;
     11 
     12 void*
     13 p9malloc(ulong n)
     14 {
     15 	void *v;
     16 	
     17 	if(n == 0)
     18 		n++;
     19 	lock(&malloclock);
     20 	v = malloc(n);
     21 	unlock(&malloclock);
     22 	return v;
     23 }
     24 
     25 void
     26 p9free(void *v)
     27 {
     28 	if(v == nil)
     29 		return;
     30 	lock(&malloclock);
     31 	free(v);
     32 	unlock(&malloclock);
     33 }
     34 
     35 void*
     36 p9calloc(ulong a, ulong b)
     37 {
     38 	void *v;
     39 	
     40 	if(a*b == 0)
     41 		a = b = 1;
     42 
     43 	lock(&malloclock);
     44 	v = calloc(a*b, 1);
     45 	unlock(&malloclock);
     46 	return v;
     47 }
     48 
     49 void*
     50 p9realloc(void *v, ulong n)
     51 {
     52 	lock(&malloclock);
     53 	v = realloc(v, n);
     54 	unlock(&malloclock);
     55 	return v;
     56 }