VirtualBox

source: kBuild/trunk/src/gmakenew/electric.c@ 909

最後變更 在這個檔案從909是 909,由 bird 提交於 18 年 前

A very simple electric fence heap (windows only for now).

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 3.9 KB
 
1/* $Id: electric.c 909 2007-05-24 03:09:09Z 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
38static struct
39{
40 void *ptr;
41 unsigned aligned;
42} freed[FREED_ENTRIES];
43static unsigned freed_head = 0;
44static unsigned freed_tail = 0;
45
46
47static void fatal_error (const char *msg)
48{
49 fprintf (stderr, "electric heap error: %s\n", msg);
50 abort ();
51 exit (1);
52}
53
54static void free_it (void *ptr, unsigned aligned)
55{
56# ifdef WINDOWS32
57 if (!VirtualFree (ptr, 0, MEM_RELEASE))
58 fatal_error ("VirtualFree failed");
59# else
60# endif
61}
62
63/* Return 1 if something was freed, 0 otherwise. */
64static int free_up_some (void)
65{
66 if (freed_tail == freed_head)
67 return 0;
68 free_it (freed[freed_tail].ptr, freed[freed_tail].aligned);
69 freed[freed_tail].ptr = NULL;
70 freed[freed_tail].aligned = 0;
71 freed_tail = (freed_tail + 1) % FREED_ENTRIES;
72 return 1;
73}
74
75static unsigned *get_hdr (void *ptr)
76{
77 if (((uintptr_t)ptr & 0xfff) < sizeof(unsigned))
78 return (unsigned *)(((uintptr_t)ptr - 0x1000) & ~0xfff);
79 return (unsigned *)((uintptr_t)ptr & ~0xfff);
80}
81
82void xfree (void *ptr)
83{
84 unsigned int size, aligned;
85 unsigned *hdr;
86# ifdef WINDOWS32
87 DWORD fFlags = PAGE_NOACCESS;
88# endif
89
90 if (!ptr)
91 return;
92
93 hdr = get_hdr (ptr);
94 size = *hdr;
95 aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
96# ifdef WINDOWS32
97 if (!VirtualProtect (hdr, aligned - 0x1000, fFlags, &fFlags))
98 fatal_error ("failed to protect freed memory");
99# else
100# endif
101
102 freed[freed_head].ptr = hdr;
103 freed[freed_head].aligned = aligned;
104 if (((freed_head + 1) % FREED_ENTRIES) == freed_tail)
105 free_up_some();
106 freed_head = (freed_head + 1) % FREED_ENTRIES;
107}
108
109void *
110xmalloc (unsigned int size)
111{
112 /* Make sure we don't allocate 0, for pre-ANSI libraries. */
113 unsigned int aligned = (size + 0x1fff + sizeof(unsigned)) & ~0xfff;
114 unsigned *hdr;
115 unsigned i;
116 for (i = 0; i < FREED_ENTRIES; i++)
117 {
118# ifdef WINDOWS32
119 DWORD fFlags = PAGE_NOACCESS;
120 hdr = VirtualAlloc(NULL, aligned, MEM_COMMIT, PAGE_READWRITE);
121 if (hdr
122 && !VirtualProtect((char *)hdr + aligned - 0x1000, 0x1000, fFlags, &fFlags))
123 fatal_error ("failed to set guard page protection");
124# else
125# endif
126 if (hdr)
127 break;
128 if (!free_up_some ())
129 break;
130 }
131 if (hdr == 0)
132 fatal_error ("virtual memory exhausted");
133
134 *hdr = size;
135# if 0
136 return hdr + 1;
137# else
138 return (char *)hdr + aligned - 0x1000 - size;
139# endif
140}
141
142void *
143xcalloc (size_t size, size_t items)
144{
145 void *result;
146 result = xmalloc (size * items);
147 return memset (result, 0, size * items);
148}
149
150void *
151xrealloc (void *ptr, unsigned int size)
152{
153 void *result;
154 result = xmalloc (size);
155 if (ptr)
156 {
157 unsigned *hdr = get_hdr (ptr);
158 unsigned int oldsize = *hdr;
159 memcpy (result, ptr, oldsize >= size ? size : oldsize);
160 xfree (ptr);
161 }
162 return result;
163}
164
165char *
166xstrdup (const char *ptr)
167{
168 size_t size = strlen (ptr) + 1;
169 char *result = xmalloc (size);
170 return memcpy (result, ptr, size);
171}
172
173#endif /* ELECTRIC_HEAP */
174
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette