f90e23f271
* cygheap.cc (_csbrk): Call getpagesize instead of getshmlba. * dcrt0.cc (dll_crt0_0): Call mmap_init. * external.cc (cygwin_internal): Call getpagesize instead of getshmlba. * fhandler.h (fhandler_base::mmap): Change access to prot parameter. (fhandler_base::fixup_mmap_after_fork): Ditto. (fhandler_disk_file::mmap): Ditto. (fhandler_disk_file::fixup_mmap_after_fork): Ditto. (fhandler_dev_mem::mmap): Ditto. (fhandler_dev_mem::fixup_mmap_after_fork): Ditto. * fhandler_mem.cc (fhandler_dev_mem::write): Call getsystempagesize instead of getpagesize. (fhandler_dev_mem::read): Ditto. (fhandler_dev_mem::fstat): Ditto. (fhandler_dev_mem::mmap): Move to mmap.cc. (fhandler_dev_mem::munmap): Ditto. (fhandler_dev_mem::msync): Ditto. (fhandler_dev_mem::fixup_mmap_after_fork): Ditto. * fhandler_proc.cc (format_proc_meminfo): Call getsystempagesize instead of getpagesize. * fhandler_process.cc (format_process_stat): Ditto. (format_process_status): Ditto. (get_mem_values): Ditto. * mmap.cc: Fix formatting. Try to make more readable and modular. Take advantage of pagesize==granularity. (gen_protect): New static function to evaluate Windows protection bits from POSIX protection and flags. (gen_access): Ditto for Windows access mode. (VirtualProt9x): Wrapper function to call VirtualProtect on 9x. (VirtualProtNT): Ditto for NT. (VirtualProtEx9x): Ditto for VirtualProtectEx on 9x. (VirtualProtExNT): Ditto for NT. (CreateMapping9x): Wrapper function for creating a mapping handle on 9x. (CreateMappingNT): Ditto for NT. (MapView9x): Wrapper function to map a view on 9x. (MapViewNT): Ditto for NT. (mmap_funcs_9x): Structure containing function pointers to wrapper functions for 9x. (mmap_funcs_nt): Ditto for NT. (mmap_func): Pointer to wrapper functions used in subsequent code. (mmap_init): Initialize mmap_func depending on OS. (class mmap_record): Use sensible member names. Add POSIX protection member. Drop Windows access flags member. Constify more methods. Use accessors instead of direct member access inside of own methods. (mmap_record::gen_protect): Class wrapper to evaluate matching Windows protection bits. (mmap_record::gen_access): Ditto for Windows access flags. (mmap_record::compatible_flags): New function to check if flags are compatible with flags of existing map. (list::add_record): Drop offset and length arguments. (class map): Change counters to unsigned. Match usage throughout. (mmapped_areas): Convert from pointer to global struct. (mmap_record::alloc_page_map): Simplify. (mmap_record::map_pages): Ditto. (mmap_record::fixup_page_map): Delete. (mmap64): Simplify. Add workaround for Windows 98 bug. Fix bug on NT that existing anonymous mappings weren't searched for a match. (munmap): Add workaround for Windows 98 bug. (msync): Simplify. (mprotect): Handle existing maps correctly. (mlock): Add local pagesize variable and enlightening comment. (fhandler_disk_file::mmap): Main functionality now in CreateMapping/ MapView wrapper functions. (fhandler_disk_file::fixup_mmap_after_fork): Call MapView wrapper. (fhandler_dev_mem::mmap): Moved from fhandler_mem.cc. Simplify by calling MapViewNT. (fhandler_dev_mem::munmap): Moved from fhandler_mem.cc. (fhandler_dev_mem::msync): Ditto. (fhandler_dev_mem::fixup_mmap_after_fork): Ditto. Call MapViewNT. (fixup_mmaps_after_fork): Restructure and hopefully speed up loop for setting protection and memory content on MAP_PRIVATE maps. * ntdll.h (AT_ROUND_TO_PAGE): Remove define. (AT_EXTENDABLE_FILE): Add define. (NtCreateSection): Add prototype. * syscalls.cc (getpagesize): Return granularity as pagesize now. (getsystempagesize): New function to retrieve "real" pagesize. (getshmlba): Delete since it's replaced by getpagesize now. * wincap.h (wincaps::has_mmap_alignment_bug): New element. * wincap.cc: Implement above element throughout. * winsup.h (getshmlba): Drop prototype. (getsystempagesize): Add prototype. (mmap_init): Ditto. * include/sys/mman.h: (Not yet) define MAP_NORESERVE.
434 lines
10 KiB
C++
434 lines
10 KiB
C++
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
|
|
|
|
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 <string.h>
|
|
#include <assert.h>
|
|
#include <stdlib.h>
|
|
#include "cygerrno.h"
|
|
#include "security.h"
|
|
#include "path.h"
|
|
#include "fhandler.h"
|
|
#include "dtable.h"
|
|
#include "cygheap.h"
|
|
#include "child_info.h"
|
|
#include "heap.h"
|
|
#include "sync.h"
|
|
#include "sigproc.h"
|
|
#include "pinfo.h"
|
|
#include <unistd.h>
|
|
|
|
init_cygheap NO_COPY *cygheap;
|
|
void NO_COPY *cygheap_max;
|
|
|
|
extern "C" char _cygheap_mid[] __attribute__((section(".cygheap")));
|
|
extern "C" char _cygheap_end[];
|
|
|
|
static NO_COPY muto cygheap_protect;
|
|
|
|
struct cygheap_entry
|
|
{
|
|
int type;
|
|
struct cygheap_entry *next;
|
|
char data[0];
|
|
};
|
|
|
|
#define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
|
|
#define N0 ((_cmalloc_entry *) NULL)
|
|
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
|
|
|
|
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
|
|
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
|
|
|
|
extern "C" {
|
|
static void __stdcall _cfree (void *) __attribute__((regparm(1)));
|
|
static void *__stdcall _csbrk (int);
|
|
}
|
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
|
void __stdcall
|
|
cygheap_fixup_in_child (bool execed)
|
|
{
|
|
cygheap_max = child_proc_info->cygheap;
|
|
cygheap = (init_cygheap *) cygheap_max;
|
|
_csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
|
|
child_copy (child_proc_info->parent, child_proc_info->dwProcessId, "cygheap", cygheap, cygheap_max);
|
|
cygheap_init ();
|
|
debug_fixup_after_fork_exec ();
|
|
|
|
/* Need to do this after debug_fixup_after_fork_exec or DEBUGGING handling of
|
|
handles might get confused. */
|
|
if (execed)
|
|
{
|
|
CloseHandle (child_proc_info->parent);
|
|
child_proc_info->parent = NULL;
|
|
}
|
|
|
|
if (execed)
|
|
{
|
|
cygheap->hooks.next = NULL;
|
|
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
|
|
/* 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->ptr || 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 */
|
|
}
|
|
}
|
|
}
|
|
|
|
int
|
|
init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
|
|
{
|
|
console_count += amount;
|
|
debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
|
|
something, console_count, amount, myctty (), avoid_freeing_console);
|
|
if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
|
|
{
|
|
FreeConsole ();
|
|
debug_printf ("freed console");
|
|
}
|
|
return console_count;
|
|
}
|
|
|
|
void
|
|
init_cygheap::close_ctty ()
|
|
{
|
|
debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
|
|
#ifdef NEWVFORK
|
|
int usecount = cygheap->ctty->usecount;
|
|
#endif
|
|
cygheap->ctty->close ();
|
|
#ifndef NEWVFORK
|
|
cygheap->ctty = NULL;
|
|
#else // FIXME: This code ain't right
|
|
if (cygheap->ctty_on_hold == cygheap->ctty)
|
|
cygheap->ctty_on_hold = NULL;
|
|
if (usecount == 1)
|
|
{
|
|
cygheap->ctty = NULL;
|
|
debug_printf ("setting cygheap->ctty to NULL");
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#define nextpage(x) ((char *) (((DWORD) ((char *) x + granmask)) & ~granmask))
|
|
#define allocsize(x) ((DWORD) nextpage (x))
|
|
#ifdef DEBUGGING
|
|
#define somekinda_printf debug_printf
|
|
#else
|
|
#define somekinda_printf malloc_printf
|
|
#endif
|
|
|
|
static void *__stdcall
|
|
_csbrk (int sbs)
|
|
{
|
|
void *prebrk = cygheap_max;
|
|
size_t granmask = getpagesize () - 1;
|
|
char *newbase = nextpage (prebrk);
|
|
cygheap_max = (char *) cygheap_max + sbs;
|
|
if (!sbs || (newbase > cygheap_max) || (cygheap_max < _cygheap_end))
|
|
/* nothing to do */;
|
|
else
|
|
{
|
|
if (prebrk <= _cygheap_end)
|
|
newbase = _cygheap_end;
|
|
|
|
DWORD adjsbs = allocsize ((char *) cygheap_max - newbase);
|
|
if (!VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
|
|
{
|
|
MEMORY_BASIC_INFORMATION m;
|
|
if (!VirtualQuery (newbase, &m, sizeof m))
|
|
system_printf ("couldn't get memory info, %E");
|
|
somekinda_printf ("Couldn't reserve/commit %d bytes of space for cygwin's heap, %E",
|
|
adjsbs);
|
|
somekinda_printf ("AllocationBase %p, BaseAddress %p, RegionSize %p, State %p\n",
|
|
m.AllocationBase, m.BaseAddress, m.RegionSize, m.State);
|
|
__seterrno ();
|
|
cygheap_max = (char *) cygheap_max - sbs;
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
return prebrk;
|
|
}
|
|
|
|
extern "C" void __stdcall
|
|
cygheap_init ()
|
|
{
|
|
cygheap_protect.init ("cygheap_protect");
|
|
if (!cygheap)
|
|
{
|
|
cygheap = (init_cygheap *) memset (_cygheap_start, 0, _cygheap_mid - _cygheap_start);
|
|
cygheap_max = cygheap;
|
|
_csbrk (sizeof (*cygheap));
|
|
}
|
|
if (!cygheap->fdtab)
|
|
cygheap->fdtab.init ();
|
|
if (!cygheap->sigs)
|
|
sigalloc ();
|
|
|
|
if (!cygheap->shared_prefix)
|
|
cygheap->shared_prefix = cstrdup (
|
|
wincap.has_terminal_services ()
|
|
&& (set_privilege (hProcToken, SE_CREATE_GLOBAL_PRIV, true) >= 0
|
|
|| GetLastError () == ERROR_NO_SUCH_PRIVILEGE)
|
|
? "Global\\" : "");
|
|
}
|
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
static void *_cmalloc (unsigned size) __attribute ((regparm(1)));
|
|
static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute ((regparm(2)));
|
|
|
|
static void *__stdcall
|
|
_cmalloc (unsigned size)
|
|
{
|
|
_cmalloc_entry *rvc;
|
|
unsigned b, sz;
|
|
|
|
/* Calculate "bit bucket" and size as a power of two. */
|
|
for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
|
|
continue;
|
|
|
|
cygheap_protect.acquire ();
|
|
if (cygheap->buckets[b])
|
|
{
|
|
rvc = (_cmalloc_entry *) cygheap->buckets[b];
|
|
cygheap->buckets[b] = rvc->ptr;
|
|
rvc->b = b;
|
|
}
|
|
else
|
|
{
|
|
rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
|
|
if (!rvc)
|
|
{
|
|
cygheap_protect.release ();
|
|
return NULL;
|
|
}
|
|
|
|
rvc->b = b;
|
|
rvc->prev = cygheap->chain;
|
|
cygheap->chain = rvc;
|
|
}
|
|
cygheap_protect.release ();
|
|
return rvc->data;
|
|
}
|
|
|
|
static void __stdcall
|
|
_cfree (void *ptr)
|
|
{
|
|
cygheap_protect.acquire ();
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
|
DWORD b = rvc->b;
|
|
rvc->ptr = cygheap->buckets[b];
|
|
cygheap->buckets[b] = (char *) rvc;
|
|
cygheap_protect.release ();
|
|
}
|
|
|
|
static void *__stdcall
|
|
_crealloc (void *ptr, unsigned size)
|
|
{
|
|
void *newptr;
|
|
if (ptr == NULL)
|
|
newptr = _cmalloc (size);
|
|
else
|
|
{
|
|
unsigned oldsize = 1 << to_cmalloc (ptr)->b;
|
|
if (size <= oldsize)
|
|
return ptr;
|
|
newptr = _cmalloc (size);
|
|
memcpy (newptr, ptr, oldsize);
|
|
_cfree (ptr);
|
|
}
|
|
return newptr;
|
|
}
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
|
|
|
|
#define N ((cygheap_entry *) NULL)
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
|
|
|
|
inline static void *
|
|
creturn (cygheap_types x, cygheap_entry * c, unsigned len)
|
|
{
|
|
if (!c)
|
|
{
|
|
set_errno (ENOMEM);
|
|
return NULL;
|
|
}
|
|
c->type = x;
|
|
char *cend = ((char *) c + sizeof (*c) + len);
|
|
if (cygheap_max < cend)
|
|
cygheap_max = cend;
|
|
MALLOC_CHECK;
|
|
return (void *) c->data;
|
|
}
|
|
|
|
extern "C" void *__stdcall
|
|
cmalloc (cygheap_types x, DWORD n)
|
|
{
|
|
cygheap_entry *c;
|
|
MALLOC_CHECK;
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
|
if (!c)
|
|
{
|
|
#ifdef DEBUGGING
|
|
system_printf ("cmalloc returned NULL");
|
|
try_to_debug ();
|
|
#endif
|
|
}
|
|
return creturn (x, c, n);
|
|
}
|
|
|
|
extern "C" void *__stdcall
|
|
crealloc (void *s, DWORD n)
|
|
{
|
|
MALLOC_CHECK;
|
|
if (s == NULL)
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
assert (!inheap (s));
|
|
cygheap_entry *c = tocygheap (s);
|
|
cygheap_types t = (cygheap_types) c->type;
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
|
#ifdef DEBUGGING
|
|
if (!c)
|
|
system_printf ("crealloc returned NULL");
|
|
#endif
|
|
return creturn (t, c, n);
|
|
}
|
|
|
|
extern "C" void __stdcall
|
|
cfree (void *s)
|
|
{
|
|
assert (!inheap (s));
|
|
_cfree (tocygheap (s));
|
|
MALLOC_CHECK;
|
|
}
|
|
|
|
extern "C" void __stdcall
|
|
cfree_and_set (char *&s, char *what)
|
|
{
|
|
if (s && s != almost_null)
|
|
cfree (s);
|
|
s = what;
|
|
}
|
|
|
|
extern "C" void *__stdcall
|
|
ccalloc (cygheap_types x, DWORD n, DWORD size)
|
|
{
|
|
cygheap_entry *c;
|
|
MALLOC_CHECK;
|
|
n *= size;
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
|
if (c)
|
|
memset (c->data, 0, n);
|
|
#ifdef DEBUGGING
|
|
if (!c)
|
|
system_printf ("ccalloc returned NULL");
|
|
#endif
|
|
return creturn (x, c, n);
|
|
}
|
|
|
|
extern "C" char *__stdcall
|
|
cstrdup (const char *s)
|
|
{
|
|
MALLOC_CHECK;
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
if (!p)
|
|
return NULL;
|
|
strcpy (p, s);
|
|
MALLOC_CHECK;
|
|
return p;
|
|
}
|
|
|
|
extern "C" char *__stdcall
|
|
cstrdup1 (const char *s)
|
|
{
|
|
MALLOC_CHECK;
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
if (!p)
|
|
return NULL;
|
|
strcpy (p, s);
|
|
MALLOC_CHECK;
|
|
return p;
|
|
}
|
|
|
|
void
|
|
cygheap_root::set (const char *posix, const char *native)
|
|
{
|
|
if (*posix == '/' && posix[1] == '\0')
|
|
{
|
|
if (m)
|
|
{
|
|
cfree (m);
|
|
m = NULL;
|
|
}
|
|
return;
|
|
}
|
|
if (!m)
|
|
m = (struct cygheap_root_mount_info *) ccalloc (HEAP_MOUNT, 1, sizeof (*m));
|
|
strcpy (m->posix_path, posix);
|
|
m->posix_pathlen = strlen (posix);
|
|
if (m->posix_pathlen >= 1 && m->posix_path[m->posix_pathlen - 1] == '/')
|
|
m->posix_path[--m->posix_pathlen] = '\0';
|
|
|
|
strcpy (m->native_path, native);
|
|
m->native_pathlen = strlen (native);
|
|
if (m->native_pathlen >= 1 && m->native_path[m->native_pathlen - 1] == '\\')
|
|
m->native_path[--m->native_pathlen] = '\0';
|
|
}
|
|
|
|
cygheap_user::~cygheap_user ()
|
|
{
|
|
#if 0
|
|
if (pname)
|
|
cfree (pname);
|
|
if (plogsrv)
|
|
cfree (plogsrv - 2);
|
|
if (pdomain)
|
|
cfree (pdomain);
|
|
if (psid)
|
|
cfree (psid);
|
|
#endif
|
|
}
|
|
|
|
void
|
|
cygheap_user::set_name (const char *new_name)
|
|
{
|
|
bool allocated = !!pname;
|
|
|
|
if (allocated)
|
|
{
|
|
if (strcasematch (new_name, pname))
|
|
return;
|
|
cfree (pname);
|
|
}
|
|
|
|
pname = cstrdup (new_name ? new_name : "");
|
|
if (!allocated)
|
|
return; /* Initializing. Don't bother with other stuff. */
|
|
|
|
cfree_and_set (homedrive);
|
|
cfree_and_set (homepath);
|
|
cfree_and_set (plogsrv);
|
|
cfree_and_set (pdomain);
|
|
cfree_and_set (pwinname);
|
|
}
|