diff options
-rw-r--r-- | include/libc.h | 12 | ||||
-rw-r--r-- | src/lib9/malloc.c | 49 |
2 files changed, 54 insertions, 7 deletions
diff --git a/include/libc.h b/include/libc.h index 8654bd98..18ac25aa 100644 --- a/include/libc.h +++ b/include/libc.h @@ -113,12 +113,9 @@ extern void* malloc(ulong); */ extern void* p9malloc(ulong); extern void* mallocz(ulong, int); -/* -extern void free(void*); -extern ulong msize(void*); -extern void* calloc(ulong, ulong); -extern void* realloc(void*, ulong); - */ +extern void p9free(void*); +extern void* p9calloc(ulong, ulong); +extern void* p9realloc(void*, ulong); extern void setmalloctag(void*, ulong); extern void setrealloctag(void*, ulong); extern ulong getmalloctag(void*); @@ -128,6 +125,9 @@ extern void* malloctopoolblock(void*); */ #ifndef NOPLAN9DEFINES #define malloc p9malloc +#define realloc p9realloc +#define calloc p9calloc +#define free p9free #endif /* diff --git a/src/lib9/malloc.c b/src/lib9/malloc.c index b75d2f07..7c5b1786 100644 --- a/src/lib9/malloc.c +++ b/src/lib9/malloc.c @@ -2,10 +2,57 @@ #define NOPLAN9DEFINES #include <libc.h> +/* + * The Unix libc routines cannot be trusted to do their own locking. + * Sad but apparently true. + */ + +static Lock malloclock; + void* p9malloc(ulong n) { + void *v; if(n == 0) n++; - return malloc(n); +//fprint(2, "%s %d malloc\n", argv0, getpid()); + lock(&malloclock); + v = malloc(n); + unlock(&malloclock); +//fprint(2, "%s %d donemalloc\n", argv0, getpid()); + return v; +} + +void +p9free(void *v) +{ +//fprint(2, "%s %d free\n", argv0, getpid()); + lock(&malloclock); + free(v); + unlock(&malloclock); +//fprint(2, "%s %d donefree\n", argv0, getpid()); +} + +void* +p9calloc(ulong a, ulong b) +{ + void *v; + +//fprint(2, "%s %d calloc\n", argv0, getpid()); + lock(&malloclock); + v = calloc(a, b); + unlock(&malloclock); +//fprint(2, "%s %d donecalloc\n", argv0, getpid()); + return v; +} + +void* +p9realloc(void *v, ulong n) +{ +//fprint(2, "%s %d realloc\n", argv0, getpid()); + lock(&malloclock); + v = realloc(v, n); + unlock(&malloclock); +//fprint(2, "%s %d donerealloc\n", argv0, getpid()); + return v; } |