1 /* tinyproxy - A fast light-weight HTTP proxy
2 * Copyright (C) 2002 Robert James Kaes <rjkaes@users.sourceforge.net>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 /* Debugging versions of various heap related functions are combined
20 * here. The debugging versions include assertions and also print
21 * (to standard error) the function called along with the amount
22 * of memory allocated, and where the memory is pointing. The
23 * format of the log message is standardized.
26 #include "tinyproxy.h"
31 debugging_calloc (size_t nmemb, size_t size, const char *file,
39 ptr = calloc (nmemb, size);
40 fprintf (stderr, "{calloc: %p:%zu x %zu} %s:%lu\n", ptr, nmemb, size, file,
46 debugging_malloc (size_t size, const char *file, unsigned long line)
53 fprintf (stderr, "{malloc: %p:%zu} %s:%lu\n", ptr, size, file, line);
58 debugging_realloc (void *ptr, size_t size, const char *file,
65 newptr = realloc (ptr, size);
66 fprintf (stderr, "{realloc: %p -> %p:%zu} %s:%lu\n", ptr, newptr, size,
72 debugging_free (void *ptr, const char *file, unsigned long line)
74 fprintf (stderr, "{free: %p} %s:%lu\n", ptr, file, line);
82 debugging_strdup (const char *s, const char *file, unsigned long line)
95 fprintf (stderr, "{strdup: %p:%zu} %s:%lu\n", ptr, len, file, line);
100 * Allocate a block of memory in the "shared" memory region.
102 * FIXME: This uses the most basic (and slowest) means of creating a
103 * shared memory location. It requires the use of a temporary file. We might
104 * want to look into something like MM (Shared Memory Library) for a better
108 malloc_shared_memory (size_t size)
114 static char *shared_file = "/tmp/tinyproxy.shared.XXXXXX";
118 strlcpy (buffer, shared_file, sizeof (buffer));
120 /* Only allow u+rw bits. This may be required for some versions
121 * of glibc so that mkstemp() doesn't make us vulnerable.
125 if ((fd = mkstemp (buffer)) == -1)
129 if (ftruncate (fd, size) == -1)
131 ptr = mmap (NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
137 * Allocate a block of memory from the "shared" region an initialize it to
141 calloc_shared_memory (size_t nmemb, size_t size)
149 length = nmemb * size;
151 ptr = malloc_shared_memory (length);
152 if (ptr == MAP_FAILED)
155 memset (ptr, 0, length);