| 1 | /* $Id: electric.c 2798 2015-09-19 20:35:03Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * A simple electric heap implementation.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kBuild.
|
|---|
| 10 | *
|
|---|
| 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.
|
|---|
| 15 | *
|
|---|
| 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.
|
|---|
| 20 | *
|
|---|
| 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/>
|
|---|
| 23 | *
|
|---|
| 24 | */
|
|---|
| 25 |
|
|---|
| 26 | #ifdef ELECTRIC_HEAP
|
|---|
| 27 |
|
|---|
| 28 | # ifdef WINDOWS32
|
|---|
| 29 | # include <windows.h>
|
|---|
| 30 | # else
|
|---|
| 31 | # include <sys/mman.h>
|
|---|
| 32 | # include <errno.h>
|
|---|
| 33 | # include <stdint.h>
|
|---|
| 34 | # endif
|
|---|
| 35 | # include <string.h>
|
|---|
| 36 | # include <stdlib.h>
|
|---|
| 37 | # include <stdio.h>
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | # define FREED_ENTRIES 512
|
|---|
| 41 | static struct
|
|---|
| 42 | {
|
|---|
| 43 | void *ptr;
|
|---|
| 44 | unsigned aligned;
|
|---|
| 45 | } freed[FREED_ENTRIES];
|
|---|
| 46 | static unsigned freed_head = 0;
|
|---|
| 47 | static unsigned freed_tail = 0;
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | static void fatal_error (const char *msg)
|
|---|
| 51 | {
|
|---|
| 52 | #ifdef _MSC_VER
|
|---|
| 53 | fprintf (stderr, "electric heap error: %s\n", msg);
|
|---|
| 54 | __debugbreak ();
|
|---|
| 55 | #else
|
|---|
| 56 | fprintf (stderr, "electric heap error: %s (errno=%d)\n", msg, errno);
|
|---|
| 57 | __asm__ ("int3"); /* not portable... */
|
|---|
| 58 | #endif
|
|---|
| 59 | abort ();
|
|---|
| 60 | exit (1);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | static void free_it (void *ptr, unsigned aligned)
|
|---|
| 64 | {
|
|---|
| 65 | # ifdef WINDOWS32
|
|---|
| 66 | if (!VirtualFree (ptr, 0, MEM_RELEASE))
|
|---|
| 67 | fatal_error ("VirtualFree failed");
|
|---|
| 68 | # else
|
|---|
| 69 | if (munmap(ptr, aligned))
|
|---|
| 70 | fatal_error ("munmap failed");
|
|---|
| 71 | # endif
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | /* Return 1 if something was freed, 0 otherwise. */
|
|---|
| 75 | static int free_up_some (void)
|
|---|
| 76 | {
|
|---|
| 77 | if (freed_tail == freed_head)
|
|---|
| 78 | return 0;
|
|---|
| 79 | free_it (freed[freed_tail].ptr, freed[freed_tail].aligned);
|
|---|
| 80 | freed[freed_tail].ptr = NULL;
|
|---|
| 81 | freed[freed_tail].aligned = 0;
|
|---|
| 82 | freed_tail = (freed_tail + 1) % FREED_ENTRIES;
|
|---|
| 83 | return 1;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | static unsigned *get_hdr (void *ptr)
|
|---|
| 87 | {
|
|---|
| 88 | if (((uintptr_t)ptr & 0xfff) < sizeof(unsigned))
|
|---|
| 89 | return (unsigned *)(((uintptr_t)ptr - 0x1000) & ~0xfff);
|
|---|
| 90 | return (unsigned *)((uintptr_t)ptr & ~0xfff);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | void xfree (void *ptr)
|
|---|
| 94 | {
|
|---|
| 95 | unsigned int size, aligned;
|
|---|
| 96 | unsigned *hdr;
|
|---|
| 97 | # ifdef WINDOWS32
|
|---|
| 98 | DWORD fFlags = PAGE_NOACCESS;
|
|---|
| 99 | # endif
|
|---|
| 100 |
|
|---|
| 101 | if (!ptr)
|
|---|
| 102 | return;
|
|---|
| 103 |
|
|---|
| 104 | hdr = get_hdr (ptr);
|
|---|
| 105 | size = *hdr;
|
|---|
| 106 | aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
|
|---|
| 107 | # ifdef WINDOWS32
|
|---|
| 108 | if (!VirtualProtect (hdr, aligned - 0x1000, fFlags, &fFlags))
|
|---|
| 109 | fatal_error ("failed to protect freed memory");
|
|---|
| 110 | # else
|
|---|
| 111 | if (mprotect(hdr, aligned - 0x1000, PROT_NONE))
|
|---|
| 112 | fatal_error ("failed to protect freed memory");
|
|---|
| 113 | # endif
|
|---|
| 114 |
|
|---|
| 115 | freed[freed_head].ptr = hdr;
|
|---|
| 116 | freed[freed_head].aligned = aligned;
|
|---|
| 117 | if (((freed_head + 1) % FREED_ENTRIES) == freed_tail)
|
|---|
| 118 | free_up_some();
|
|---|
| 119 | freed_head = (freed_head + 1) % FREED_ENTRIES;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | void *
|
|---|
| 123 | xmalloc (unsigned int size)
|
|---|
| 124 | {
|
|---|
| 125 | /* Make sure we don't allocate 0, for pre-ANSI libraries. */
|
|---|
| 126 | unsigned int aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
|
|---|
| 127 | unsigned *hdr;
|
|---|
| 128 | unsigned i;
|
|---|
| 129 | for (i = 0; i < FREED_ENTRIES; i++)
|
|---|
| 130 | {
|
|---|
| 131 | # ifdef WINDOWS32
|
|---|
| 132 | DWORD fFlags = PAGE_NOACCESS;
|
|---|
| 133 | hdr = VirtualAlloc(NULL, aligned, MEM_COMMIT, PAGE_READWRITE);
|
|---|
| 134 | if (hdr
|
|---|
| 135 | && !VirtualProtect((char *)hdr + aligned - 0x1000, 0x1000, fFlags, &fFlags))
|
|---|
| 136 | fatal_error ("failed to set guard page protection");
|
|---|
| 137 | # else
|
|---|
| 138 | hdr = mmap(NULL, aligned, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
|
|---|
| 139 | if (hdr == MAP_FAILED)
|
|---|
| 140 | hdr = 0;
|
|---|
| 141 | if (hdr
|
|---|
| 142 | && mprotect((char *)hdr + aligned - 0x1000, 0x1000, PROT_NONE))
|
|---|
| 143 | fatal_error ("failed to set guard page protection");
|
|---|
| 144 | # endif
|
|---|
| 145 | if (hdr)
|
|---|
| 146 | break;
|
|---|
| 147 | if (!free_up_some ())
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 | if (hdr == 0)
|
|---|
| 151 | fatal_error ("virtual memory exhausted");
|
|---|
| 152 |
|
|---|
| 153 | *hdr = size;
|
|---|
| 154 | # if 0
|
|---|
| 155 | return hdr + 1;
|
|---|
| 156 | # else
|
|---|
| 157 | return (char *)hdr + aligned - 0x1000 - size;
|
|---|
| 158 | # endif
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|
| 162 | void *
|
|---|
| 163 | xcalloc (unsigned size)
|
|---|
| 164 | {
|
|---|
| 165 | void *result;
|
|---|
| 166 | result = xmalloc (size);
|
|---|
| 167 | return memset (result, 0, size);
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | void *
|
|---|
| 171 | xrealloc (void *ptr, unsigned int size)
|
|---|
| 172 | {
|
|---|
| 173 | void *result;
|
|---|
| 174 | result = xmalloc (size);
|
|---|
| 175 | if (ptr)
|
|---|
| 176 | {
|
|---|
| 177 | unsigned *hdr = get_hdr (ptr);
|
|---|
| 178 | unsigned int oldsize = *hdr;
|
|---|
| 179 | memcpy (result, ptr, oldsize >= size ? size : oldsize);
|
|---|
| 180 | xfree (ptr);
|
|---|
| 181 | }
|
|---|
| 182 | return result;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | char *
|
|---|
| 186 | xstrdup (const char *ptr)
|
|---|
| 187 | {
|
|---|
| 188 | if (ptr)
|
|---|
| 189 | {
|
|---|
| 190 | size_t size = strlen (ptr) + 1;
|
|---|
| 191 | char *result = xmalloc (size);
|
|---|
| 192 | return memcpy (result, ptr, size);
|
|---|
| 193 | }
|
|---|
| 194 | return NULL;
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | # ifdef __GNUC__
|
|---|
| 198 | void *
|
|---|
| 199 | xmalloc_size_t (size_t size)
|
|---|
| 200 | {
|
|---|
| 201 | return xmalloc(size);
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void *
|
|---|
| 205 | xcalloc_size_t (size_t size, size_t items)
|
|---|
| 206 | {
|
|---|
| 207 | return xcalloc(size * items);
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | void *
|
|---|
| 211 | xrealloc_size_t (void *ptr, size_t size)
|
|---|
| 212 | {
|
|---|
| 213 | return xrealloc(ptr, size);
|
|---|
| 214 | }
|
|---|
| 215 | # endif /* __GNUC__ */
|
|---|
| 216 |
|
|---|
| 217 | #else /* !ELECTRIC_HEAP */
|
|---|
| 218 | extern void electric_heap_keep_ansi_c_quiet (void);
|
|---|
| 219 | #endif /* !ELECTRIC_HEAP */
|
|---|
| 220 |
|
|---|