diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-05-26 18:56:06 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-05-26 18:56:06 +0000 |
commit | 2e6ab58f37f142117921de04ebb07c71efef0a6e (patch) | |
tree | 3179a61626dbcab41e7ddd063a5131276b85eec9 /src/heap.c | |
parent | 63f481c61a47e21b171e33c35d415102b679f143 (diff) |
Added the malloc_shared_memory() and calloc_shared_memory() function to
allow the use of shared memory between all the children.
Diffstat (limited to 'src/heap.c')
-rw-r--r-- | src/heap.c | 50 |
1 files changed, 49 insertions, 1 deletions
@@ -1,4 +1,4 @@ -/* $Id: heap.c,v 1.1 2002-05-23 04:41:10 rjkaes Exp $ +/* $Id: heap.c,v 1.2 2002-05-26 18:56:06 rjkaes Exp $ * * Debugging versions of various heap related functions are combined * here. The debugging versions include assertions and also print @@ -90,3 +90,51 @@ debugging_strdup(const char* s, const char* file, unsigned long line) fprintf(stderr, "{strdup: %p:%u} %s:%lu\n", ptr, len, file, line); return ptr; } + +/* + * Allocate a block of memory in the "shared" memory region + */ +void* +malloc_shared_memory(size_t size) +{ + int fd; + void* ptr; + + assert(size > 0); + +#ifdef MAP_ANON + ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); +#else + fd = open("/dev/zero", O_RDWR, 0); + + ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + close(fd); +#endif + + return ptr; +} + +/* + * Allocate a block of memory from the "shared" region an initialize it to + * zero. + */ +void* +calloc_shared_memory(size_t nmemb, size_t size) +{ + void* ptr; + long length; + + assert(nmemb > 0); + assert(size > 0); + + length = nmemb * size; + + ptr = malloc_shared_memory(length); + if (!ptr) + return NULL; + + memset(ptr, 0, length); + + return ptr; +} + |