(malloc_shared_memory): Removed the MMAP_ANON and open("/dev/zero") since they are not portable across a while enough spectrum of machines. Right now tinyproxy is using a "classic" temporary file method of sharing memory. This will likely be improved in the future.

This commit is contained in:
Robert James Kaes 2002-05-29 20:51:35 +00:00
parent 360deb4568
commit 12fc92828c

View File

@ -1,4 +1,4 @@
/* $Id: heap.c,v 1.2 2002-05-26 18:56:06 rjkaes Exp $ /* $Id: heap.c,v 1.3 2002-05-29 20:51:35 rjkaes Exp $
* *
* Debugging versions of various heap related functions are combined * Debugging versions of various heap related functions are combined
* here. The debugging versions include assertions and also print * here. The debugging versions include assertions and also print
@ -21,6 +21,7 @@
#include "tinyproxy.h" #include "tinyproxy.h"
#include "heap.h" #include "heap.h"
#include "text.h"
void * void *
debugging_calloc(size_t nmemb, size_t size, const char *file, debugging_calloc(size_t nmemb, size_t size, const char *file,
@ -92,24 +93,35 @@ debugging_strdup(const char* s, const char* file, unsigned long line)
} }
/* /*
* Allocate a block of memory in the "shared" memory region * Allocate a block of memory in the "shared" memory region.
*
* FIXME: This uses the most basic (and slowest) means of creating a
* shared memory location. It requires the use of a temporary file. We might
* want to look into something like MM (Shared Memory Library) for a better
* solution.
*/ */
void* void*
malloc_shared_memory(size_t size) malloc_shared_memory(size_t size)
{ {
int fd; int fd;
void* ptr; void* ptr;
char buffer[128];
static char* shared_file = "/tmp/tinyproxy.shared";
assert(size > 0); assert(size > 0);
assert(shared_file != NULL);
#ifdef MAP_ANON strlcpy(buffer, shared_file, sizeof(buffer) - 8);
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0); strlcat(buffer, ".XXXXXX", sizeof(buffer));
#else
fd = open("/dev/zero", O_RDWR, 0);
ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if ((fd = mkstemp(buffer)) == -1)
close(fd); return (void *)MAP_FAILED;
#endif unlink(buffer);
if (ftruncate(fd, size) == -1)
return (void *)MAP_FAILED;
ptr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
return ptr; return ptr;
} }
@ -130,8 +142,8 @@ calloc_shared_memory(size_t nmemb, size_t size)
length = nmemb * size; length = nmemb * size;
ptr = malloc_shared_memory(length); ptr = malloc_shared_memory(length);
if (!ptr) if (ptr == MAP_FAILED)
return NULL; return ptr;
memset(ptr, 0, length); memset(ptr, 0, length);