*: Updated copyright to 2009 and normalized name & email.
[kbuild-mirror.git] / src / kmk / electric.c
blob876cbddac2db240366bd52b9996771b4c2dbfdd7
1 /* $Id$ */
2 /** @file
3 * A simple electric heap implementation.
4 */
6 /*
7 * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@anduin.net>
9 * This file is part of kBuild.
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild. If not, see <http://www.gnu.org/licenses/>
26 #ifdef ELECTRIC_HEAP
28 # ifdef WINDOWS32
29 # include <windows.h>
30 # else
31 # include <sys/mman.h>
32 # include <errno.h>
33 # endif
34 # include <string.h>
35 # include <stdlib.h>
36 # include <stdio.h>
39 # define FREED_ENTRIES 512
40 static struct
42 void *ptr;
43 unsigned aligned;
44 } freed[FREED_ENTRIES];
45 static unsigned freed_head = 0;
46 static unsigned freed_tail = 0;
49 static void fatal_error (const char *msg)
51 #ifdef _MSC_VER
52 fprintf (stderr, "electric heap error: %s\n", msg);
53 __debugbreak ();
54 #else
55 fprintf (stderr, "electric heap error: %s (errno=%d)\n", msg, errno);
56 __asm__ ("int3"); /* not portable... */
57 #endif
58 abort ();
59 exit (1);
62 static void free_it (void *ptr, unsigned aligned)
64 # ifdef WINDOWS32
65 if (!VirtualFree (ptr, 0, MEM_RELEASE))
66 fatal_error ("VirtualFree failed");
67 # else
68 if (munmap(ptr, aligned))
69 fatal_error ("munmap failed");
70 # endif
73 /* Return 1 if something was freed, 0 otherwise. */
74 static int free_up_some (void)
76 if (freed_tail == freed_head)
77 return 0;
78 free_it (freed[freed_tail].ptr, freed[freed_tail].aligned);
79 freed[freed_tail].ptr = NULL;
80 freed[freed_tail].aligned = 0;
81 freed_tail = (freed_tail + 1) % FREED_ENTRIES;
82 return 1;
85 static unsigned *get_hdr (void *ptr)
87 if (((uintptr_t)ptr & 0xfff) < sizeof(unsigned))
88 return (unsigned *)(((uintptr_t)ptr - 0x1000) & ~0xfff);
89 return (unsigned *)((uintptr_t)ptr & ~0xfff);
92 void xfree (void *ptr)
94 unsigned int size, aligned;
95 unsigned *hdr;
96 # ifdef WINDOWS32
97 DWORD fFlags = PAGE_NOACCESS;
98 # endif
100 if (!ptr)
101 return;
103 hdr = get_hdr (ptr);
104 size = *hdr;
105 aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
106 # ifdef WINDOWS32
107 if (!VirtualProtect (hdr, aligned - 0x1000, fFlags, &fFlags))
108 fatal_error ("failed to protect freed memory");
109 # else
110 if (mprotect(hdr, aligned - 0x1000, PROT_NONE))
111 fatal_error ("failed to protect freed memory");
112 # endif
114 freed[freed_head].ptr = hdr;
115 freed[freed_head].aligned = aligned;
116 if (((freed_head + 1) % FREED_ENTRIES) == freed_tail)
117 free_up_some();
118 freed_head = (freed_head + 1) % FREED_ENTRIES;
121 void *
122 xmalloc (unsigned int size)
124 /* Make sure we don't allocate 0, for pre-ANSI libraries. */
125 unsigned int aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
126 unsigned *hdr;
127 unsigned i;
128 for (i = 0; i < FREED_ENTRIES; i++)
130 # ifdef WINDOWS32
131 DWORD fFlags = PAGE_NOACCESS;
132 hdr = VirtualAlloc(NULL, aligned, MEM_COMMIT, PAGE_READWRITE);
133 if (hdr
134 && !VirtualProtect((char *)hdr + aligned - 0x1000, 0x1000, fFlags, &fFlags))
135 fatal_error ("failed to set guard page protection");
136 # else
137 hdr = mmap(NULL, aligned, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
138 if (hdr == MAP_FAILED)
139 hdr = 0;
140 if (hdr
141 && mprotect((char *)hdr + aligned - 0x1000, 0x1000, PROT_NONE))
142 fatal_error ("failed to set guard page protection");
143 # endif
144 if (hdr)
145 break;
146 if (!free_up_some ())
147 break;
149 if (hdr == 0)
150 fatal_error ("virtual memory exhausted");
152 *hdr = size;
153 # if 0
154 return hdr + 1;
155 # else
156 return (char *)hdr + aligned - 0x1000 - size;
157 # endif
160 void *
161 xcalloc (size_t size, size_t items)
163 void *result;
164 result = xmalloc (size * items);
165 return memset (result, 0, size * items);
168 void *
169 xrealloc (void *ptr, unsigned int size)
171 void *result;
172 result = xmalloc (size);
173 if (ptr)
175 unsigned *hdr = get_hdr (ptr);
176 unsigned int oldsize = *hdr;
177 memcpy (result, ptr, oldsize >= size ? size : oldsize);
178 xfree (ptr);
180 return result;
183 char *
184 xstrdup (const char *ptr)
186 size_t size = strlen (ptr) + 1;
187 char *result = xmalloc (size);
188 return memcpy (result, ptr, size);
191 #else /* !ELECTRIC_HEAP */
192 extern void electric_heap_keep_ansi_c_quiet (void);
193 #endif /* !ELECTRIC_HEAP */