| 1 | /* $Id: electric.c 2243 2009-01-10 02:24:02Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * A simple electric heap implementation.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2007-2009 knut st. osmundsen <bird-kBuild-spamix@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 | # endif
|
|---|
| 34 | # include <string.h>
|
|---|
| 35 | # include <stdlib.h>
|
|---|
| 36 | # include <stdio.h>
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | # define FREED_ENTRIES 512
|
|---|
| 40 | static struct
|
|---|
| 41 | {
|
|---|
| 42 | void *ptr;
|
|---|
| 43 | unsigned aligned;
|
|---|
| 44 | } freed[FREED_ENTRIES];
|
|---|
| 45 | static unsigned freed_head = 0;
|
|---|
| 46 | static unsigned freed_tail = 0;
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 | static void fatal_error (const char *msg)
|
|---|
| 50 | {
|
|---|
| 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);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static void free_it (void *ptr, unsigned aligned)
|
|---|
| 63 | {
|
|---|
| 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
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | /* Return 1 if something was freed, 0 otherwise. */
|
|---|
| 74 | static int free_up_some (void)
|
|---|
| 75 | {
|
|---|
| 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;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | static unsigned *get_hdr (void *ptr)
|
|---|
| 86 | {
|
|---|
| 87 | if (((uintptr_t)ptr & 0xfff) < sizeof(unsigned))
|
|---|
| 88 | return (unsigned *)(((uintptr_t)ptr - 0x1000) & ~0xfff);
|
|---|
| 89 | return (unsigned *)((uintptr_t)ptr & ~0xfff);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | void xfree (void *ptr)
|
|---|
| 93 | {
|
|---|
| 94 | unsigned int size, aligned;
|
|---|
| 95 | unsigned *hdr;
|
|---|
| 96 | # ifdef WINDOWS32
|
|---|
| 97 | DWORD fFlags = PAGE_NOACCESS;
|
|---|
| 98 | # endif
|
|---|
| 99 |
|
|---|
| 100 | if (!ptr)
|
|---|
| 101 | return;
|
|---|
| 102 |
|
|---|
| 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
|
|---|
| 113 |
|
|---|
| 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;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | void *
|
|---|
| 122 | xmalloc (unsigned int size)
|
|---|
| 123 | {
|
|---|
| 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++)
|
|---|
| 129 | {
|
|---|
| 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;
|
|---|
| 148 | }
|
|---|
| 149 | if (hdr == 0)
|
|---|
| 150 | fatal_error ("virtual memory exhausted");
|
|---|
| 151 |
|
|---|
| 152 | *hdr = size;
|
|---|
| 153 | # if 0
|
|---|
| 154 | return hdr + 1;
|
|---|
| 155 | # else
|
|---|
| 156 | return (char *)hdr + aligned - 0x1000 - size;
|
|---|
| 157 | # endif
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | void *
|
|---|
| 161 | xcalloc (size_t size, size_t items)
|
|---|
| 162 | {
|
|---|
| 163 | void *result;
|
|---|
| 164 | result = xmalloc (size * items);
|
|---|
| 165 | return memset (result, 0, size * items);
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | void *
|
|---|
| 169 | xrealloc (void *ptr, unsigned int size)
|
|---|
| 170 | {
|
|---|
| 171 | void *result;
|
|---|
| 172 | result = xmalloc (size);
|
|---|
| 173 | if (ptr)
|
|---|
| 174 | {
|
|---|
| 175 | unsigned *hdr = get_hdr (ptr);
|
|---|
| 176 | unsigned int oldsize = *hdr;
|
|---|
| 177 | memcpy (result, ptr, oldsize >= size ? size : oldsize);
|
|---|
| 178 | xfree (ptr);
|
|---|
| 179 | }
|
|---|
| 180 | return result;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | char *
|
|---|
| 184 | xstrdup (const char *ptr)
|
|---|
| 185 | {
|
|---|
| 186 | size_t size = strlen (ptr) + 1;
|
|---|
| 187 | char *result = xmalloc (size);
|
|---|
| 188 | return memcpy (result, ptr, size);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | #else /* !ELECTRIC_HEAP */
|
|---|
| 192 | extern void electric_heap_keep_ansi_c_quiet (void);
|
|---|
| 193 | #endif /* !ELECTRIC_HEAP */
|
|---|
| 194 |
|
|---|