]> sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/cygheap.cc
* Makefile.in: Add cygheap.o.
[newlib-cygwin.git] / winsup / cygwin / cygheap.cc
1 /* cygheap.cc: Cygwin heap manager.
2
3 Copyright 2000 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <errno.h>
13 #include <fhandler.h>
14 #include <assert.h>
15 #include "cygheap.h"
16 #include "heap.h"
17 #include "cygerrno.h"
18
19 #define HEAP_START ((void *) 0x12010000)
20 inline static void
21 init_cheap ()
22 {
23 if (!VirtualAlloc (HEAP_START, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
24 api_fatal ("Couldn't reserve space for child's heap, %E");
25 cygheap = cygheap_max = HEAP_START;
26 }
27
28 #define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
29 static void *__stdcall
30 _csbrk (int sbs)
31 {
32 void *lastheap;
33 if (!cygheap)
34 init_cheap ();
35 lastheap = cygheap_max;
36 (char *) cygheap_max += sbs;
37 void *heapalign = (void *) pagetrunc (lastheap);
38 int needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
39 if (needalloc && !VirtualAlloc (lastheap, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
40 api_fatal ("couldn't commit memory for cygwin heap, %E");
41
42 return lastheap;
43 }
44
45 /* Copyright (C) 1997, 2000 DJ Delorie */
46
47 char *buckets[32] = {0};
48 int bucket2size[32] = {0};
49
50 static inline int
51 size2bucket (int size)
52 {
53 int rv = 0x1f;
54 int bit = ~0x10;
55 int i;
56
57 if (size < 4)
58 size = 4;
59 size = (size + 3) & ~3;
60
61 for (i = 0; i < 5; i++)
62 {
63 if (bucket2size[rv & bit] >= size)
64 rv &= bit;
65 bit >>= 1;
66 }
67 return rv;
68 }
69
70 static inline void
71 init_buckets ()
72 {
73 unsigned b;
74 for (b = 0; b < 32; b++)
75 bucket2size[b] = (1 << b);
76 }
77
78 static void *__stdcall
79 _cmalloc (int size)
80 {
81 char *rv;
82 int b;
83
84 if (bucket2size[0] == 0)
85 init_buckets ();
86
87 b = size2bucket (size);
88 if (buckets[b])
89 {
90 rv = buckets[b];
91 buckets[b] = *(char **) rv;
92 return rv;
93 }
94
95 size = bucket2size[b] + 4;
96 rv = (char *) _csbrk (size);
97
98 *(int *) rv = b;
99 rv += 4;
100 return rv;
101 }
102
103 static void __stdcall
104 _cfree (void *ptr)
105 {
106 int b = *(int *) ((char *) ptr - 4);
107 *(char **) ptr = buckets[b];
108 buckets[b] = (char *) ptr;
109 }
110
111 static void *__stdcall
112 _crealloc (void *ptr, int size)
113 {
114 char *newptr;
115 int oldsize = bucket2size[*(int *) ((char *) ptr - 4)];
116 if (size <= oldsize)
117 return ptr;
118 newptr = (char *) _cmalloc (size);
119 memcpy (newptr, ptr, oldsize);
120 _cfree (ptr);
121 return newptr;
122 }
123
124 /* End Copyright (C) 1997 DJ Delorie */
125
126 void *cygheap = NULL;
127 void *cygheap_max = NULL;
128
129 #define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
130
131 struct cygheap_entry
132 {
133 cygheap_types type;
134 char data[0];
135 };
136
137 #define N ((cygheap_entry *) NULL)
138 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
139
140 void
141 cygheap_init ()
142 {
143 if (!cygheap)
144 init_cheap ();
145 }
146
147 extern "C" void __stdcall
148 cygheap_fixup_in_child (HANDLE parent)
149 {
150 DWORD m, n;
151 n = (DWORD) cygheap_max - (DWORD) cygheap;
152 if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
153 api_fatal ("Couldn't reserve space for child's heap, %E");
154
155 if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
156 api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
157 cygheap, n);
158 m = 0;
159 n = (DWORD) pagetrunc (n + 4095);
160 if (!ReadProcessMemory (parent, cygheap, cygheap, n, &m) ||
161 m != n)
162 api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
163 n, m);
164 }
165
166 static void *__stdcall
167 creturn (cygheap_types x, cygheap_entry * c, int len)
168 {
169 if (!c)
170 {
171 __seterrno ();
172 return NULL;
173 }
174 c->type = x;
175 if (cygheap_max < ((char *) c + len))
176 cygheap_max = (char *) c + len;
177 return (void *) c->data;
178 }
179
180 extern "C" void *__stdcall
181 cmalloc (cygheap_types x, DWORD n)
182 {
183 cygheap_entry *c;
184 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
185 if (!c)
186 system_printf ("cmalloc returned NULL");
187 return creturn (x, c, n);
188 }
189
190 extern "C" void *__stdcall
191 crealloc (void *s, DWORD n)
192 {
193 if (s == NULL)
194 return cmalloc (HEAP_STR, n); // kludge
195
196 assert (!inheap (s));
197 cygheap_entry *c = tocygheap (s);
198 cygheap_types t = c->type;
199 c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
200 if (!c)
201 system_printf ("crealloc returned NULL");
202 return creturn (t, c, n);
203 }
204
205 extern "C" void __stdcall
206 cfree (void *s)
207 {
208 assert (!inheap (s));
209 (void) _cfree (tocygheap (s));
210 }
211
212 extern "C" void *__stdcall
213 ccalloc (cygheap_types x, DWORD n, DWORD size)
214 {
215 cygheap_entry *c;
216 c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n * size));
217 if (c)
218 memset (c->data, 0, size);
219 if (!c)
220 system_printf ("ccalloc returned NULL");
221 return creturn (x, c, n);
222 }
223
224 extern "C" char *__stdcall
225 cstrdup (const char *s)
226 {
227 char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
228 if (!p)
229 return NULL;
230 strcpy (p, s);
231 return p;
232 }
This page took 0.050822 seconds and 6 git commands to generate.