2000-09-03 06:16:35 +02:00
|
|
|
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
|
|
|
|
Copyright 2000 Cygnus Solutions.
|
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fhandler.h>
|
|
|
|
#include <assert.h>
|
2000-10-02 04:26:04 +02:00
|
|
|
#include <stdlib.h>
|
2000-09-03 06:16:35 +02:00
|
|
|
#include "cygheap.h"
|
|
|
|
#include "heap.h"
|
|
|
|
#include "cygerrno.h"
|
2000-10-02 04:26:04 +02:00
|
|
|
#include "sync.h"
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-13 00:15:47 +02:00
|
|
|
void NO_COPY *cygheap = NULL;
|
|
|
|
void NO_COPY *cygheap_max = NULL;
|
2000-09-30 06:54:11 +02:00
|
|
|
|
2000-10-02 04:26:04 +02:00
|
|
|
static NO_COPY muto *cygheap_protect = NULL;
|
|
|
|
|
|
|
|
extern "C" void __stdcall
|
|
|
|
cygheap_init ()
|
|
|
|
{
|
|
|
|
cygheap_protect = new_muto (FALSE, "cygheap_protect");
|
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
inline static void
|
|
|
|
init_cheap ()
|
|
|
|
{
|
2000-09-11 02:25:57 +02:00
|
|
|
cygheap = VirtualAlloc (NULL, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS);
|
|
|
|
if (!cygheap)
|
|
|
|
api_fatal ("Couldn't reserve space for cygwin's heap, %E");
|
2000-09-30 06:54:11 +02:00
|
|
|
cygheap_max = (((char **) cygheap) + 1);
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define pagetrunc(x) ((void *) (((DWORD) (x)) & ~(4096 - 1)))
|
2000-09-30 06:54:11 +02:00
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
static void *__stdcall
|
|
|
|
_csbrk (int sbs)
|
|
|
|
{
|
|
|
|
void *lastheap;
|
2000-09-30 06:54:11 +02:00
|
|
|
bool needalloc;
|
|
|
|
|
|
|
|
if (cygheap)
|
|
|
|
needalloc = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
init_cheap ();
|
|
|
|
needalloc = 1;
|
|
|
|
}
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
lastheap = cygheap_max;
|
|
|
|
(char *) cygheap_max += sbs;
|
|
|
|
void *heapalign = (void *) pagetrunc (lastheap);
|
2000-09-30 06:54:11 +02:00
|
|
|
|
|
|
|
if (!needalloc)
|
|
|
|
needalloc = sbs && ((heapalign == lastheap) || heapalign != pagetrunc (cygheap_max));
|
2000-09-03 06:16:35 +02:00
|
|
|
if (needalloc && !VirtualAlloc (lastheap, (DWORD) sbs, MEM_COMMIT, PAGE_READWRITE))
|
|
|
|
api_fatal ("couldn't commit memory for cygwin heap, %E");
|
|
|
|
|
|
|
|
return lastheap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
|
2000-09-30 06:54:11 +02:00
|
|
|
#define NBUCKETS 32
|
|
|
|
char *buckets[NBUCKETS] = {0};
|
|
|
|
int bucket2size[NBUCKETS] = {0};
|
2000-09-03 06:16:35 +02:00
|
|
|
|
|
|
|
static inline int
|
|
|
|
size2bucket (int size)
|
|
|
|
{
|
|
|
|
int rv = 0x1f;
|
|
|
|
int bit = ~0x10;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (size < 4)
|
|
|
|
size = 4;
|
|
|
|
size = (size + 3) & ~3;
|
|
|
|
|
|
|
|
for (i = 0; i < 5; i++)
|
|
|
|
{
|
|
|
|
if (bucket2size[rv & bit] >= size)
|
|
|
|
rv &= bit;
|
|
|
|
bit >>= 1;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
init_buckets ()
|
|
|
|
{
|
|
|
|
unsigned b;
|
2000-09-30 06:54:11 +02:00
|
|
|
for (b = 0; b < NBUCKETS; b++)
|
2000-09-03 06:16:35 +02:00
|
|
|
bucket2size[b] = (1 << b);
|
|
|
|
}
|
|
|
|
|
2000-09-30 06:54:11 +02:00
|
|
|
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)
|
|
|
|
|
2000-09-03 06:16:35 +02:00
|
|
|
static void *__stdcall
|
|
|
|
_cmalloc (int size)
|
|
|
|
{
|
2000-09-30 06:54:11 +02:00
|
|
|
_cmalloc_entry *rvc;
|
2000-09-03 06:16:35 +02:00
|
|
|
int b;
|
|
|
|
|
|
|
|
if (bucket2size[0] == 0)
|
|
|
|
init_buckets ();
|
|
|
|
|
|
|
|
b = size2bucket (size);
|
2000-10-02 04:26:04 +02:00
|
|
|
cygheap_protect->acquire ();
|
2000-09-03 06:16:35 +02:00
|
|
|
if (buckets[b])
|
|
|
|
{
|
2000-09-30 06:54:11 +02:00
|
|
|
rvc = (_cmalloc_entry *) buckets[b];
|
|
|
|
buckets[b] = rvc->ptr;
|
|
|
|
rvc->b = b;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
2000-10-02 04:26:04 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size = bucket2size[b] + sizeof (_cmalloc_entry);
|
|
|
|
rvc = (_cmalloc_entry *) _csbrk (size);
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-10-02 04:26:04 +02:00
|
|
|
rvc->b = b;
|
|
|
|
rvc->prev = *cygheap_chain;
|
|
|
|
*cygheap_chain = rvc;
|
|
|
|
}
|
|
|
|
cygheap_protect->release ();
|
2000-09-30 06:54:11 +02:00
|
|
|
return rvc->data;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __stdcall
|
|
|
|
_cfree (void *ptr)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
cygheap_protect->acquire ();
|
2000-09-30 06:54:11 +02:00
|
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
|
|
|
DWORD b = rvc->b;
|
|
|
|
rvc->ptr = buckets[b];
|
|
|
|
buckets[b] = (char *) rvc;
|
2000-10-02 04:26:04 +02:00
|
|
|
cygheap_protect->release ();
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *__stdcall
|
|
|
|
_crealloc (void *ptr, int size)
|
|
|
|
{
|
2000-09-05 05:16:28 +02:00
|
|
|
void *newptr;
|
|
|
|
if (ptr == NULL)
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
else
|
|
|
|
{
|
2000-09-30 06:54:11 +02:00
|
|
|
int oldsize = bucket2size[to_cmalloc (ptr)->b];
|
2000-09-05 05:16:28 +02:00
|
|
|
if (size <= oldsize)
|
|
|
|
return ptr;
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
memcpy (newptr, ptr, oldsize);
|
|
|
|
_cfree (ptr);
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
|
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof(cygheap_entry))
|
|
|
|
|
|
|
|
struct cygheap_entry
|
|
|
|
{
|
2000-09-30 06:54:11 +02:00
|
|
|
int type;
|
|
|
|
struct cygheap_entry *next;
|
2000-09-03 06:16:35 +02:00
|
|
|
char data[0];
|
|
|
|
};
|
|
|
|
|
|
|
|
#define N ((cygheap_entry *) NULL)
|
|
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
|
|
|
|
|
2000-09-30 06:54:11 +02:00
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
2000-09-03 06:16:35 +02:00
|
|
|
extern "C" void __stdcall
|
2000-09-30 06:54:11 +02:00
|
|
|
cygheap_fixup_in_child (HANDLE parent, bool execed)
|
2000-09-03 06:16:35 +02:00
|
|
|
{
|
|
|
|
DWORD m, n;
|
|
|
|
n = (DWORD) cygheap_max - (DWORD) cygheap;
|
2000-09-30 06:54:11 +02:00
|
|
|
|
|
|
|
/* Reserve cygwin heap in same spot as parent */
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!VirtualAlloc (cygheap, CYGHEAPSIZE, MEM_RESERVE, PAGE_NOACCESS))
|
2000-09-11 02:25:57 +02:00
|
|
|
api_fatal ("Couldn't reserve space for cygwin's heap in child, %E");
|
2000-09-03 06:16:35 +02:00
|
|
|
|
2000-09-30 06:54:11 +02:00
|
|
|
/* Allocate same amount of memory as parent */
|
2000-09-03 06:16:35 +02:00
|
|
|
if (!VirtualAlloc (cygheap, n, MEM_COMMIT, PAGE_READWRITE))
|
|
|
|
api_fatal ("Couldn't allocate space for child's heap %p, size %d, %E",
|
|
|
|
cygheap, n);
|
2000-09-30 06:54:11 +02:00
|
|
|
|
|
|
|
/* Copy memory from the parent */
|
2000-09-03 06:16:35 +02:00
|
|
|
m = 0;
|
2000-10-14 07:52:38 +02:00
|
|
|
if (!ReadProcessMemory (parent, cygheap, cygheap, n, &m) || m != n)
|
2000-09-03 06:16:35 +02:00
|
|
|
api_fatal ("Couldn't read parent's cygwin heap %d bytes != %d, %E",
|
|
|
|
n, m);
|
2000-09-30 06:54:11 +02:00
|
|
|
|
|
|
|
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 */
|
|
|
|
}
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *__stdcall
|
|
|
|
creturn (cygheap_types x, cygheap_entry * c, int len)
|
|
|
|
{
|
|
|
|
if (!c)
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
c->type = x;
|
|
|
|
if (cygheap_max < ((char *) c + len))
|
|
|
|
cygheap_max = (char *) c + len;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
return (void *) c->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__stdcall
|
|
|
|
cmalloc (cygheap_types x, DWORD n)
|
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
|
|
|
if (!c)
|
|
|
|
system_printf ("cmalloc returned NULL");
|
|
|
|
return creturn (x, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__stdcall
|
|
|
|
crealloc (void *s, DWORD n)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
if (s == NULL)
|
|
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
|
|
|
|
assert (!inheap (s));
|
|
|
|
cygheap_entry *c = tocygheap (s);
|
2000-09-30 06:54:11 +02:00
|
|
|
cygheap_types t = (cygheap_types) c->type;
|
2000-09-03 06:16:35 +02:00
|
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
|
|
|
if (!c)
|
|
|
|
system_printf ("crealloc returned NULL");
|
|
|
|
return creturn (t, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void __stdcall
|
|
|
|
cfree (void *s)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
assert (!inheap (s));
|
|
|
|
(void) _cfree (tocygheap (s));
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *__stdcall
|
|
|
|
ccalloc (cygheap_types x, DWORD n, DWORD size)
|
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n * size));
|
|
|
|
if (c)
|
|
|
|
memset (c->data, 0, size);
|
|
|
|
if (!c)
|
|
|
|
system_printf ("ccalloc returned NULL");
|
|
|
|
return creturn (x, c, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" char *__stdcall
|
|
|
|
cstrdup (const char *s)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 06:16:35 +02:00
|
|
|
return p;
|
|
|
|
}
|
2000-09-27 07:12:09 +02:00
|
|
|
|
|
|
|
extern "C" char *__stdcall
|
|
|
|
cstrdup1 (const char *s)
|
|
|
|
{
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 07:12:09 +02:00
|
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 04:26:04 +02:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 07:12:09 +02:00
|
|
|
return p;
|
|
|
|
}
|