From 2e6ab58f37f142117921de04ebb07c71efef0a6e Mon Sep 17 00:00:00 2001 From: Robert James Kaes Date: Sun, 26 May 2002 18:56:06 +0000 Subject: Added the malloc_shared_memory() and calloc_shared_memory() function to allow the use of shared memory between all the children. --- src/heap.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- src/heap.h | 8 +++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/src/heap.c b/src/heap.c index 9a01001..146c87b 100644 --- a/src/heap.c +++ b/src/heap.c @@ -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; +} + diff --git a/src/heap.h b/src/heap.h index fca28cb..1bfe47a 100644 --- a/src/heap.h +++ b/src/heap.h @@ -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 -- cgit v1.2.3