1 | /* $Id: electric.c 915 2007-05-24 05:00:18Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * A simple electric heap implementation.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This program is free software; you can redistribute it and/or modify
|
---|
10 | * it under the terms of the GNU Lesser General Public License as published
|
---|
11 | * by the Free Software Foundation; either version 2 of the License, or
|
---|
12 | * (at your option) any later version.
|
---|
13 | *
|
---|
14 | * This program is distributed in the hope that it will be useful,
|
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
17 | * GNU Lesser General Public License for more details.
|
---|
18 | *
|
---|
19 | * You should have received a copy of the GNU Lesser General Public License
|
---|
20 | * along with This program; if not, write to the Free Software
|
---|
21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #ifdef ELECTRIC_HEAP
|
---|
26 |
|
---|
27 | # ifdef WINDOWS32
|
---|
28 | # include <windows.h>
|
---|
29 | # else
|
---|
30 | # include <sys/mman.h>
|
---|
31 | # endif
|
---|
32 | # include <string.h>
|
---|
33 | # include <stdlib.h>
|
---|
34 | # include <stdio.h>
|
---|
35 |
|
---|
36 |
|
---|
37 | # define FREED_ENTRIES 512
|
---|
38 | static struct
|
---|
39 | {
|
---|
40 | void *ptr;
|
---|
41 | unsigned aligned;
|
---|
42 | } freed[FREED_ENTRIES];
|
---|
43 | static unsigned freed_head = 0;
|
---|
44 | static unsigned freed_tail = 0;
|
---|
45 |
|
---|
46 |
|
---|
47 | static void fatal_error (const char *msg)
|
---|
48 | {
|
---|
49 | fprintf (stderr, "electric heap error: %s\n", msg);
|
---|
50 | __debugbreak ();
|
---|
51 | abort ();
|
---|
52 | exit (1);
|
---|
53 | }
|
---|
54 |
|
---|
55 | static void free_it (void *ptr, unsigned aligned)
|
---|
56 | {
|
---|
57 | # ifdef WINDOWS32
|
---|
58 | if (!VirtualFree (ptr, 0, MEM_RELEASE))
|
---|
59 | fatal_error ("VirtualFree failed");
|
---|
60 | # else
|
---|
61 | # endif
|
---|
62 | }
|
---|
63 |
|
---|
64 | /* Return 1 if something was freed, 0 otherwise. */
|
---|
65 | static int free_up_some (void)
|
---|
66 | {
|
---|
67 | if (freed_tail == freed_head)
|
---|
68 | return 0;
|
---|
69 | free_it (freed[freed_tail].ptr, freed[freed_tail].aligned);
|
---|
70 | freed[freed_tail].ptr = NULL;
|
---|
71 | freed[freed_tail].aligned = 0;
|
---|
72 | freed_tail = (freed_tail + 1) % FREED_ENTRIES;
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static unsigned *get_hdr (void *ptr)
|
---|
77 | {
|
---|
78 | if (((uintptr_t)ptr & 0xfff) < sizeof(unsigned))
|
---|
79 | return (unsigned *)(((uintptr_t)ptr - 0x1000) & ~0xfff);
|
---|
80 | return (unsigned *)((uintptr_t)ptr & ~0xfff);
|
---|
81 | }
|
---|
82 |
|
---|
83 | void xfree (void *ptr)
|
---|
84 | {
|
---|
85 | unsigned int size, aligned;
|
---|
86 | unsigned *hdr;
|
---|
87 | # ifdef WINDOWS32
|
---|
88 | DWORD fFlags = PAGE_NOACCESS;
|
---|
89 | # endif
|
---|
90 |
|
---|
91 | if (!ptr)
|
---|
92 | return;
|
---|
93 |
|
---|
94 | hdr = get_hdr (ptr);
|
---|
95 | size = *hdr;
|
---|
96 | aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
|
---|
97 | # ifdef WINDOWS32
|
---|
98 | if (!VirtualProtect (hdr, aligned - 0x1000, fFlags, &fFlags))
|
---|
99 | fatal_error ("failed to protect freed memory");
|
---|
100 | # else
|
---|
101 | # endif
|
---|
102 |
|
---|
103 | freed[freed_head].ptr = hdr;
|
---|
104 | freed[freed_head].aligned = aligned;
|
---|
105 | if (((freed_head + 1) % FREED_ENTRIES) == freed_tail)
|
---|
106 | free_up_some();
|
---|
107 | freed_head = (freed_head + 1) % FREED_ENTRIES;
|
---|
108 | }
|
---|
109 |
|
---|
110 | void *
|
---|
111 | xmalloc (unsigned int size)
|
---|
112 | {
|
---|
113 | /* Make sure we don't allocate 0, for pre-ANSI libraries. */
|
---|
114 | unsigned int aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
|
---|
115 | unsigned *hdr;
|
---|
116 | unsigned i;
|
---|
117 | for (i = 0; i < FREED_ENTRIES; i++)
|
---|
118 | {
|
---|
119 | # ifdef WINDOWS32
|
---|
120 | DWORD fFlags = PAGE_NOACCESS;
|
---|
121 | hdr = VirtualAlloc(NULL, aligned, MEM_COMMIT, PAGE_READWRITE);
|
---|
122 | if (hdr
|
---|
123 | && !VirtualProtect((char *)hdr + aligned - 0x1000, 0x1000, fFlags, &fFlags))
|
---|
124 | fatal_error ("failed to set guard page protection");
|
---|
125 | # else
|
---|
126 | # endif
|
---|
127 | if (hdr)
|
---|
128 | break;
|
---|
129 | if (!free_up_some ())
|
---|
130 | break;
|
---|
131 | }
|
---|
132 | if (hdr == 0)
|
---|
133 | fatal_error ("virtual memory exhausted");
|
---|
134 |
|
---|
135 | *hdr = size;
|
---|
136 | # if 0
|
---|
137 | return hdr + 1;
|
---|
138 | # else
|
---|
139 | return (char *)hdr + aligned - 0x1000 - size;
|
---|
140 | # endif
|
---|
141 | }
|
---|
142 |
|
---|
143 | void *
|
---|
144 | xcalloc (size_t size, size_t items)
|
---|
145 | {
|
---|
146 | void *result;
|
---|
147 | result = xmalloc (size * items);
|
---|
148 | return memset (result, 0, size * items);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void *
|
---|
152 | xrealloc (void *ptr, unsigned int size)
|
---|
153 | {
|
---|
154 | void *result;
|
---|
155 | result = xmalloc (size);
|
---|
156 | if (ptr)
|
---|
157 | {
|
---|
158 | unsigned *hdr = get_hdr (ptr);
|
---|
159 | unsigned int oldsize = *hdr;
|
---|
160 | memcpy (result, ptr, oldsize >= size ? size : oldsize);
|
---|
161 | xfree (ptr);
|
---|
162 | }
|
---|
163 | return result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | char *
|
---|
167 | xstrdup (const char *ptr)
|
---|
168 | {
|
---|
169 | size_t size = strlen (ptr) + 1;
|
---|
170 | char *result = xmalloc (size);
|
---|
171 | return memcpy (result, ptr, size);
|
---|
172 | }
|
---|
173 |
|
---|
174 | #endif /* ELECTRIC_HEAP */
|
---|
175 |
|
---|