lock malloc ourselves

This commit is contained in:
rsc 2004-05-23 00:58:23 +00:00
parent c9896e2edb
commit 7e0e6522e5
2 changed files with 54 additions and 7 deletions

View file

@ -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
/*

View file

@ -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;
}