3 * A simple electric heap implementation.
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/>
31 # include <sys/mman.h>
39 # define FREED_ENTRIES 512
44 } freed
[FREED_ENTRIES
];
45 static unsigned freed_head
= 0;
46 static unsigned freed_tail
= 0;
49 static void fatal_error (const char *msg
)
52 fprintf (stderr
, "electric heap error: %s\n", msg
);
55 fprintf (stderr
, "electric heap error: %s (errno=%d)\n", msg
, errno
);
56 __asm__ ("int3"); /* not portable... */
62 static void free_it (void *ptr
, unsigned aligned
)
65 if (!VirtualFree (ptr
, 0, MEM_RELEASE
))
66 fatal_error ("VirtualFree failed");
68 if (munmap(ptr
, aligned
))
69 fatal_error ("munmap failed");
73 /* Return 1 if something was freed, 0 otherwise. */
74 static int free_up_some (void)
76 if (freed_tail
== freed_head
)
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
;
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
;
97 DWORD fFlags
= PAGE_NOACCESS
;
105 aligned
= (size
+ 0x1fff + sizeof(unsigned)) & ~0xfff;
107 if (!VirtualProtect (hdr
, aligned
- 0x1000, fFlags
, &fFlags
))
108 fatal_error ("failed to protect freed memory");
110 if (mprotect(hdr
, aligned
- 0x1000, PROT_NONE
))
111 fatal_error ("failed to protect freed memory");
114 freed
[freed_head
].ptr
= hdr
;
115 freed
[freed_head
].aligned
= aligned
;
116 if (((freed_head
+ 1) % FREED_ENTRIES
) == freed_tail
)
118 freed_head
= (freed_head
+ 1) % FREED_ENTRIES
;
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;
128 for (i
= 0; i
< FREED_ENTRIES
; i
++)
131 DWORD fFlags
= PAGE_NOACCESS
;
132 hdr
= VirtualAlloc(NULL
, aligned
, MEM_COMMIT
, PAGE_READWRITE
);
134 && !VirtualProtect((char *)hdr
+ aligned
- 0x1000, 0x1000, fFlags
, &fFlags
))
135 fatal_error ("failed to set guard page protection");
137 hdr
= mmap(NULL
, aligned
, PROT_EXEC
| PROT_READ
| PROT_WRITE
, MAP_ANON
| MAP_PRIVATE
, -1, 0);
138 if (hdr
== MAP_FAILED
)
141 && mprotect((char *)hdr
+ aligned
- 0x1000, 0x1000, PROT_NONE
))
142 fatal_error ("failed to set guard page protection");
146 if (!free_up_some ())
150 fatal_error ("virtual memory exhausted");
156 return (char *)hdr
+ aligned
- 0x1000 - size
;
161 xcalloc (size_t size
, size_t items
)
164 result
= xmalloc (size
* items
);
165 return memset (result
, 0, size
* items
);
169 xrealloc (void *ptr
, unsigned int size
)
172 result
= xmalloc (size
);
175 unsigned *hdr
= get_hdr (ptr
);
176 unsigned int oldsize
= *hdr
;
177 memcpy (result
, ptr
, oldsize
>= size
? size
: oldsize
);
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 */