From 12fc92828cea5f775de316086f81da8cc4cbf5bd Mon Sep 17 00:00:00 2001
From: Robert James Kaes <rjkaes@users.sourceforge.net>
Date: Wed, 29 May 2002 20:51:35 +0000
Subject: (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.

---
 src/heap.c | 36 ++++++++++++++++++++++++------------
 1 file changed, 24 insertions(+), 12 deletions(-)

(limited to 'src')

diff --git a/src/heap.c b/src/heap.c
index 146c87b..7da4601 100644
--- a/src/heap.c
+++ b/src/heap.c
@@ -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
  * here.  The debugging versions include assertions and also print
@@ -21,6 +21,7 @@
 
 #include "tinyproxy.h"
 #include "heap.h"
+#include "text.h"
 
 void *
 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*
 malloc_shared_memory(size_t size)
 {
 	int fd;
 	void* ptr;
+	char buffer[128];
+
+	static char* shared_file = "/tmp/tinyproxy.shared";
 
 	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);
+	assert(shared_file != NULL);
+
+	strlcpy(buffer, shared_file, sizeof(buffer) - 8);
+	strlcat(buffer, ".XXXXXX", sizeof(buffer));
 
-	ptr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
-	close(fd);
-#endif
+	if ((fd = mkstemp(buffer)) == -1)
+		return (void *)MAP_FAILED;
+	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;
 }
@@ -130,8 +142,8 @@ calloc_shared_memory(size_t nmemb, size_t size)
 	length = nmemb * size;
 
 	ptr = malloc_shared_memory(length);
-	if (!ptr)
-		return NULL;
+	if (ptr == MAP_FAILED)
+		return ptr;
 
 	memset(ptr, 0, length);
 
-- 
cgit v1.2.3