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