]>
sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/cygheap.cc
1 /* cygheap.cc: Cygwin heap manager.
3 Copyright 2000 Cygnus Solutions.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
22 cygheap
= VirtualAlloc (NULL
, CYGHEAPSIZE
, MEM_RESERVE
, PAGE_NOACCESS
);
24 api_fatal ("Couldn't reserve space for cygwin's heap, %E");
25 cygheap_max
= cygheap
;
28 #define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
29 static void *__stdcall
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");
45 /* Copyright (C) 1997, 2000 DJ Delorie */
47 char *buckets
[32] = {0};
48 int bucket2size
[32] = {0};
51 size2bucket (int size
)
59 size
= (size
+ 3) & ~3;
61 for (i
= 0; i
< 5; i
++)
63 if (bucket2size
[rv
& bit
] >= size
)
74 for (b
= 0; b
< 32; b
++)
75 bucket2size
[b
] = (1 << b
);
78 static void *__stdcall
84 if (bucket2size
[0] == 0)
87 b
= size2bucket (size
);
91 buckets
[b
] = *(char **) rv
;
95 size
= bucket2size
[b
] + 4;
96 rv
= (char *) _csbrk (size
);
103 static void __stdcall
106 int b
= *(int *) ((char *) ptr
- 4);
107 *(char **) ptr
= buckets
[b
];
108 buckets
[b
] = (char *) ptr
;
111 static void *__stdcall
112 _crealloc (void *ptr
, int size
)
116 newptr
= _cmalloc (size
);
119 int oldsize
= bucket2size
[*(int *) ((char *) ptr
- 4)];
122 newptr
= _cmalloc (size
);
123 memcpy (newptr
, ptr
, oldsize
);
129 /* End Copyright (C) 1997 DJ Delorie */
131 void *cygheap
= NULL
;
132 void *cygheap_max
= NULL
;
134 #define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
142 #define N ((cygheap_entry *) NULL)
143 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
152 extern "C" void __stdcall
153 cygheap_fixup_in_child (HANDLE parent
)
156 n
= (DWORD
) cygheap_max
- (DWORD
) cygheap
;
157 if (!VirtualAlloc (cygheap
, CYGHEAPSIZE
, MEM_RESERVE
, PAGE_NOACCESS
))
158 api_fatal ("Couldn't reserve space for cygwin's heap in child, %E");
160 if (!VirtualAlloc (cygheap
, n
, MEM_COMMIT
, PAGE_READWRITE
))
161 api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
164 n
= (DWORD
) pagetrunc (n
+ 4095);
165 if (!ReadProcessMemory (parent
, cygheap
, cygheap
, n
, &m
) ||
167 api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
171 static void *__stdcall
172 creturn (cygheap_types x
, cygheap_entry
* c
, int len
)
180 if (cygheap_max
< ((char *) c
+ len
))
181 cygheap_max
= (char *) c
+ len
;
182 return (void *) c
->data
;
185 extern "C" void *__stdcall
186 cmalloc (cygheap_types x
, DWORD n
)
189 c
= (cygheap_entry
*) _cmalloc (sizeof_cygheap (n
));
191 system_printf ("cmalloc returned NULL");
192 return creturn (x
, c
, n
);
195 extern "C" void *__stdcall
196 crealloc (void *s
, DWORD n
)
199 return cmalloc (HEAP_STR
, n
); // kludge
201 assert (!inheap (s
));
202 cygheap_entry
*c
= tocygheap (s
);
203 cygheap_types t
= c
->type
;
204 c
= (cygheap_entry
*) _crealloc (c
, sizeof_cygheap (n
));
206 system_printf ("crealloc returned NULL");
207 return creturn (t
, c
, n
);
210 extern "C" void __stdcall
213 assert (!inheap (s
));
214 (void) _cfree (tocygheap (s
));
217 extern "C" void *__stdcall
218 ccalloc (cygheap_types x
, DWORD n
, DWORD size
)
221 c
= (cygheap_entry
*) _cmalloc (sizeof_cygheap (n
* size
));
223 memset (c
->data
, 0, size
);
225 system_printf ("ccalloc returned NULL");
226 return creturn (x
, c
, n
);
229 extern "C" char *__stdcall
230 cstrdup (const char *s
)
232 char *p
= (char *) cmalloc (HEAP_STR
, strlen (s
) + 1);
239 extern "C" char *__stdcall
240 cstrdup1 (const char *s
)
242 char *p
= (char *) cmalloc (HEAP_1_STR
, strlen (s
) + 1);
This page took 0.103239 seconds and 6 git commands to generate.