]>
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
19 #define HEAP_START ((void *) 0x12010000)
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
;
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
)
115 int oldsize
= bucket2size
[*(int *) ((char *) ptr
- 4)];
118 newptr
= (char *) _cmalloc (size
);
119 memcpy (newptr
, ptr
, oldsize
);
124 /* End Copyright (C) 1997 DJ Delorie */
126 void *cygheap
= NULL
;
127 void *cygheap_max
= NULL
;
129 #define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
137 #define N ((cygheap_entry *) NULL)
138 #define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
147 extern "C" void __stdcall
148 cygheap_fixup_in_child (HANDLE parent
)
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");
155 if (!VirtualAlloc (cygheap
, n
, MEM_COMMIT
, PAGE_READWRITE
))
156 api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
159 n
= (DWORD
) pagetrunc (n
+ 4095);
160 if (!ReadProcessMemory (parent
, cygheap
, cygheap
, n
, &m
) ||
162 api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
166 static void *__stdcall
167 creturn (cygheap_types x
, cygheap_entry
* c
, int len
)
175 if (cygheap_max
< ((char *) c
+ len
))
176 cygheap_max
= (char *) c
+ len
;
177 return (void *) c
->data
;
180 extern "C" void *__stdcall
181 cmalloc (cygheap_types x
, DWORD n
)
184 c
= (cygheap_entry
*) _cmalloc (sizeof_cygheap (n
));
186 system_printf ("cmalloc returned NULL");
187 return creturn (x
, c
, n
);
190 extern "C" void *__stdcall
191 crealloc (void *s
, DWORD n
)
194 return cmalloc (HEAP_STR
, n
); // kludge
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
));
201 system_printf ("crealloc returned NULL");
202 return creturn (t
, c
, n
);
205 extern "C" void __stdcall
208 assert (!inheap (s
));
209 (void) _cfree (tocygheap (s
));
212 extern "C" void *__stdcall
213 ccalloc (cygheap_types x
, DWORD n
, DWORD size
)
216 c
= (cygheap_entry
*) _cmalloc (sizeof_cygheap (n
* size
));
218 memset (c
->data
, 0, size
);
220 system_printf ("ccalloc returned NULL");
221 return creturn (x
, c
, n
);
224 extern "C" char *__stdcall
225 cstrdup (const char *s
)
227 char *p
= (char *) cmalloc (HEAP_STR
, strlen (s
) + 1);
This page took 0.050822 seconds and 6 git commands to generate.