* cygheap.cc (init_cheap): Set aside space for heap walk pointer.

(_csbrk): Make logic for detecting when to alloc cognizant of initialization
condition.
(_cmalloc): Use a structure to hold bucket size and heap chain pointer.  Store
pointer to next freed block in bucket size location so that it will be easy to
see if a block is allocated.
(_cfree): Store pointer to next freed block in bucket size location.
(_crealloc): Use macro to retrieve bucket size.
(cygheap_init): Eliminate.
(cygheap_fixup_in_child): Add second argument to determine if we were execed or
not.  In execed case, walk the heap, cleaning up any orphaned blocks.
* cygheap.h: Add a "MAX" value to cygheap_types.  Remove cygheap_init
declaration.  Accomodate new argument to cygheap_fixup_in child.
* fork.cc (fork): Accomodate extra argument to cygheap_fixup_in_child.
* dcrt0.cc (dll_crt0_1): Ditto.  Remove call to cygheap_init.
This commit is contained in:
Christopher Faylor 2000-09-30 04:54:11 +00:00
parent 90fe77395a
commit 8d82847a29
5 changed files with 104 additions and 39 deletions

View File

@ -1,3 +1,22 @@
Sat Sep 30 00:43:42 2000 Christopher Faylor <cgf@cygnus.com>
* cygheap.cc (init_cheap): Set aside space for heap walk pointer.
(_csbrk): Make logic for detecting when to alloc cognizant of
initialization condition.
(_cmalloc): Use a structure to hold bucket size and heap chain pointer.
Store pointer to next freed block in bucket size location so that it
will be easy to see if a block is allocated.
(_cfree): Store pointer to next freed block in bucket size location.
(_crealloc): Use macro to retrieve bucket size.
(cygheap_init): Eliminate.
(cygheap_fixup_in_child): Add second argument to determine if we were
execed or not. In execed case, walk the heap, cleaning up any orphaned
blocks.
* cygheap.h: Add a "MAX" value to cygheap_types. Remove cygheap_init
declaration. Accomodate new argument to cygheap_fixup_in child.
* fork.cc (fork): Accomodate extra argument to cygheap_fixup_in_child.
* dcrt0.cc (dll_crt0_1): Ditto. Remove call to cygheap_init.
Fri Sep 29 21:49:27 2000 Christopher Faylor <cgf@cygnus.com>
* path.cc (symlink_info::check): Set executable bit for a file if the

View File

@ -16,26 +16,40 @@
#include "heap.h"
#include "cygerrno.h"
void *cygheap = NULL;
void *cygheap_max = NULL;
inline static void
init_cheap ()
{
cygheap = VirtualAlloc (NULL, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS);
if (!cygheap)
api_fatal ("Couldn't reserve space for cygwin's heap, %E");
cygheap_max = cygheap;
cygheap_max = (((char **) cygheap) + 1);
}
#define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
static void *__stdcall
_csbrk (int sbs)
{
void *lastheap;
if (!cygheap)
init_cheap ();
bool needalloc;
if (cygheap)
needalloc = 0;
else
{
init_cheap ();
needalloc = 1;
}
lastheap = cygheap_max;
(char *) cygheap_max += sbs;
void *heapalign = (void *) pagetrunc (lastheap);
int needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
if (!needalloc)
needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
if (needalloc && !VirtualAlloc (lastheap, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
api_fatal ("couldn't commit memory for cygwin heap, %E");
@ -44,8 +58,9 @@ _csbrk (int sbs)
/* Copyright (C) 1997, 2000 DJ Delorie */
char *buckets[32] = {0};
int bucket2size[32] = {0};
#define NBUCKETS 32
char *buckets[NBUCKETS] = {0};
int bucket2size[NBUCKETS] = {0};
static inline int
size2bucket (int size)
@ -71,14 +86,30 @@ static inline void
init_buckets ()
{
unsigned b;
for (b = 0; b < 32; b++)
for (b = 0; b < NBUCKETS; b++)
bucket2size[b] = (1 << b);
}
struct _cmalloc_entry
{
union
{
DWORD b;
char *ptr;
};
struct _cmalloc_entry *prev;
char data[0];
};
#define N0 ((_cmalloc_entry *) NULL)
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (int) (N0->data)))
#define cygheap_chain ((_cmalloc_entry **)cygheap)
static void *__stdcall
_cmalloc (int size)
{
char *rv;
_cmalloc_entry *rvc;
int b;
if (bucket2size[0] == 0)
@ -87,25 +118,28 @@ _cmalloc (int size)
b = size2bucket (size);
if (buckets[b])
{
rv = buckets[b];
buckets[b] = *(char **) rv;
return rv;
rvc = (_cmalloc_entry *) buckets[b];
buckets[b] = rvc->ptr;
rvc->b = b;
return rvc->data;
}
size = bucket2size[b] + 4;
rv = (char *) _csbrk (size);
size = bucket2size[b] + sizeof (_cmalloc_entry);
rvc = (_cmalloc_entry *) _csbrk (size);
*(int *) rv = b;
rv += 4;
return rv;
rvc->b = b;
rvc->prev = *cygheap_chain;
*cygheap_chain = rvc;
return rvc->data;
}
static void __stdcall
_cfree (void *ptr)
{
int b = *(int *) ((char *) ptr - 4);
*(char **) ptr = buckets[b];
buckets[b] = (char *) ptr;
_cmalloc_entry *rvc = to_cmalloc (ptr);
DWORD b = rvc->b;
rvc->ptr = buckets[b];
buckets[b] = (char *) rvc;
}
static void *__stdcall
@ -116,7 +150,7 @@ _crealloc (void *ptr, int size)
newptr = _cmalloc (size);
else
{
int oldsize = bucket2size[*(int *) ((char *) ptr - 4)];
int oldsize = bucket2size[to_cmalloc (ptr)->b];
if (size <= oldsize)
return ptr;
newptr = _cmalloc (size);
@ -128,44 +162,57 @@ _crealloc (void *ptr, int size)
/* End Copyright (C) 1997 DJ Delorie */
void *cygheap = NULL;
void *cygheap_max = NULL;
#define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
struct cygheap_entry
{
cygheap_types type;
int type;
struct cygheap_entry *next;
char data[0];
};
#define N ((cygheap_entry *) NULL)
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
void
cygheap_init ()
{
if (!cygheap)
init_cheap ();
}
/* Called by fork or spawn to reallocate cygwin heap */
extern "C" void __stdcall
cygheap_fixup_in_child (HANDLE parent)
cygheap_fixup_in_child (HANDLE parent, bool execed)
{
DWORD m, n;
n = (DWORD) cygheap_max - (DWORD) cygheap;
/* Reserve cygwin heap in same spot as parent */
if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
api_fatal ("Couldn't reserve space for cygwin's heap in child, %E");
/* Allocate same amount of memory as parent */
if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
cygheap, n);
/* Copy memory from the parent */
m = 0;
n = (DWORD) pagetrunc (n + 4095);
if (!ReadProcessMemory (parent, cygheap, cygheap, n, &m) ||
m != n)
api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
n, m);
if (!execed)
return; /* Forked. Nothing extra to do. */
/* Walk the allocated memory chain looking for orphaned memory from
previous execs */
for (_cmalloc_entry *rvc = *cygheap_chain; rvc; rvc = rvc->prev)
{
cygheap_entry *ce = (cygheap_entry *) rvc->data;
if (rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
continue;
else if (ce->type < HEAP_1_MAX)
ce->type += HEAP_1_MAX; /* Mark for freeing after next exec */
else
_cfree (ce); /* Marked by parent for freeing in child */
}
}
static void *__stdcall
@ -200,7 +247,7 @@ crealloc (void *s, DWORD n)
assert (!inheap (s));
cygheap_entry *c = tocygheap (s);
cygheap_types t = c->type;
cygheap_types t = (cygheap_types) c->type;
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
if (!c)
system_printf ("crealloc returned NULL");

View File

@ -20,7 +20,8 @@ enum cygheap_types
HEAP_1_STR,
HEAP_1_ARGV,
HEAP_1_BUF,
HEAP_1_EXEC
HEAP_1_EXEC,
HEAP_1_MAX = 100
};
#define CYGHEAPSIZE ((1000 * sizeof (fhandler_union)) + (2 * 65536))
@ -30,10 +31,9 @@ extern HANDLE cygheap_max;
#define incygheap(s) (cygheap && ((char *) (s) >= (char *) cygheap) && ((char *) (s) <= ((char *) cygheap) + CYGHEAPSIZE))
void cygheap_init ();
extern "C" {
void __stdcall cfree (void *);
void __stdcall cygheap_fixup_in_child (HANDLE);
void __stdcall cygheap_fixup_in_child (HANDLE, bool);
void *__stdcall cmalloc (cygheap_types, DWORD);
void *__stdcall crealloc (void *, DWORD);
void *__stdcall ccalloc (cygheap_types, DWORD, DWORD);

View File

@ -678,7 +678,7 @@ dll_crt0_1 ()
HANDLE h;
cygheap = spawn_info->cygheap;
cygheap_max = spawn_info->cygheap_max;
cygheap_fixup_in_child (spawn_info->parent);
cygheap_fixup_in_child (spawn_info->parent, 1);
if (!spawn_info->moreinfo->myself_pinfo ||
!DuplicateHandle (hMainProc, spawn_info->moreinfo->myself_pinfo,
hMainProc, &h, 0, 0,
@ -746,7 +746,6 @@ dll_crt0_1 ()
longjmp (fork_info->jmp, fork_info->cygpid);
}
cygheap_init (); /* Initialize cygwin's heap */
cygcwd.init ();
/* Initialize our process table entry. */

View File

@ -571,7 +571,7 @@ fork ()
MALLOC_CHECK;
cygheap_fixup_in_child (hParent);
cygheap_fixup_in_child (hParent, 0);
fdtab.fixup_after_fork (hParent);
signal_fixup_after_fork ();
exec_fixup_after_fork ();