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 | |
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')
-rw-r--r-- | src/heap.c | 50 | ||||
-rw-r--r-- | src/heap.h | 8 |
2 files changed, 56 insertions, 2 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; +} + @@ -1,4 +1,4 @@ -/* $Id: heap.h,v 1.1 2002-05-23 04:41:10 rjkaes Exp $ +/* $Id: heap.h,v 1.2 2002-05-26 18:56:06 rjkaes Exp $ * * See 'heap.c' for a detailed description. * @@ -46,4 +46,10 @@ extern char *debugging_strdup(const char* s, const char* file, # define safestrdup(x) strdup(x) #endif +/* + * Allocate memory from the "shared" region of memory. + */ +extern void* malloc_shared_memory(size_t size); +extern void* calloc_shared_memory(size_t nmemb, size_t size); + #endif |