diff options
author | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-03-11 00:55:26 +0100 |
---|---|---|
committer | cinap_lenrek <cinap_lenrek@gmx.de> | 2013-03-11 00:55:26 +0100 |
commit | d526ee0750815820ac70e030ff87775ac56785e3 (patch) | |
tree | fd7ae34e401c7e8ed8ae0d6ebe0262df02ac52be /sys/src/ape | |
parent | 48b0c10681bb4e0785fa9f737d287531d06fecb7 (diff) |
ape/malloc: make malloc and free threadsafe for python
Diffstat (limited to 'sys/src/ape')
-rw-r--r-- | sys/src/ape/lib/ap/plan9/malloc.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/sys/src/ape/lib/ap/plan9/malloc.c b/sys/src/ape/lib/ap/plan9/malloc.c index 09f3c178e..07caf78d7 100644 --- a/sys/src/ape/lib/ap/plan9/malloc.c +++ b/sys/src/ape/lib/ap/plan9/malloc.c @@ -1,6 +1,8 @@ #include <stdlib.h> #include <string.h> +#include <lock.h> + typedef unsigned int uint; enum @@ -24,6 +26,7 @@ typedef struct Arena Arena; struct Arena { Bucket *btab[MAX2SIZE]; + Lock; }; static Arena arena; @@ -47,16 +50,19 @@ malloc(size_t size) return nil; good: /* Allocate off this list */ + lock(&arena); bp = arena.btab[pow]; if(bp) { arena.btab[pow] = bp->next; + unlock(&arena); if(bp->magic != 0) abort(); bp->magic = MAGIC; - return bp->data; + return bp->data; } + size = sizeof(Bucket)+(1<<pow); size += 7; size &= ~7; @@ -64,8 +70,10 @@ good: if(pow < CUTOFF) { n = (CUTOFF-pow)+2; bp = sbrk(size*n); - if((int)bp < 0) + if((int)bp < 0){ + unlock(&arena); return nil; + } next = (uint)bp+size; nbp = (Bucket*)next; @@ -80,9 +88,12 @@ good: } else { bp = sbrk(size); - if((int)bp < 0) + if((int)bp < 0){ + unlock(&arena); return nil; + } } + unlock(&arena); bp->size = pow; bp->magic = MAGIC; @@ -106,8 +117,10 @@ free(void *ptr) bp->magic = 0; l = &arena.btab[bp->size]; + lock(&arena); bp->next = *l; *l = bp; + unlock(&arena); } void* |