newlib-cygwin/winsup/cygwin/heap.cc

163 lines
5.0 KiB
C++
Raw Normal View History

2000-02-18 03:38:33 +08:00
/* heap.cc: Cygwin heap manager.
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
2000-02-18 03:38:33 +08:00
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 "cygerrno.h"
#include "sigproc.h"
#include "pinfo.h"
* Makefile.in: Add cygheap.o. * child_info.h: Add specific exec class. * cygheap.h: New file. Contains declarations for cygwin heap. * cygheap.cc: New file. Implements cygwin heap functions. * dcrt0.cc (quoted): Simplify due to new method for passing arguments between cygwin programs. (alloc_stack_hard_way): Attempt to handle overlapped stack. (dll_crt0_1): Move child_info processing here. Accomodate new method for passing arguments between cygwin programs. Initialize cygwin heap. Establish __argc and __argv variables. (_dll_crt0): Move most of child_info processing to dll_crt0_1. (cygwin_dll_init): Remove duplication. * dtable.cc (dtable::extend): Allocate dtable using cygwin heap. (dtable::build_fhandler): Ditto for fhandler type being constructed. (dtable::dup_worker): Free new fhandler from cygwin heap on error. (dtable::select_*): Don't assume that this == fdtab. (dtable::linearize_fd_array): Delete. (dtable::delinearize_fd_array): Delete. (dtable::fixup_after_exec): New file. (dtable::vfork_child_dup): Use cygwin heap. (dtable::vfork_parent_restore): Ditto. * dtable.h: Remove obsolete methods. Add new method. * environ.cc (posify): Eliminate already_posix parameter and logic. (envsize): New function. (_addenv): Use envsize. (environ_init): Accept an argument pointing to an existing environment list. If supplied, allocate space for this in the the program's heap. * fhandler.cc (fhandler_base::operator =): Move here from fhandler.h. Use cygwin heap to allocate filenames. (fhandler_base::set_name): Allocate/free names from cygwin heap. (fhandler_base::linearize): Delete. (fhandler_base::de_linearize): Delete. (fhandler_base::operator delete): Free from cygwin heap. (fhandler_base::~fhandler_base): Ditto. * fhandler.h: Accomodate elimination of *linearize and other changes above. * fhandler_console.cc (fhandler_console::fixup_after_exec): Rename from de_linearize. * heap.h: New file. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Use cygwin heap for name. fhandler_tty::fixup_after_exec): Rename from de_linearize. * fork.cc (fork): Call cygheap_fixup_in_child. * heap.cc: Use declarations in heap.h. * malloc.cc: Sprinkle assertions throughout to catch attempts to free/realloc something from the cygwin heap. * path.cc: Throughout, eliminate use of per-thread cache for cwd. Use cwd_* functions rather than cwd_* variables to access cwd_win32 and cwd_posix. (cwd_win32): New function. (cwd_posix): New function. (cwd_hash): New function. (cwd_fixup_after_exec): New function. * path.h: Accomodate path.cc changes. * pinfo.cc (pinfo_init): Accept a pointer to an environment table. Pass this to environ_init. Eliminate old 'title' tests. * pinfo.h: Accomodate above change in argument. * spawn.cc (struct av): New method for building argv list. (av::unshift): New method. (spawn_guts): Allocate everything that the child process needs in the cygwin heap and pass a pointer to this to the child. Build argv list using new method. Eliminate delinearize stuff. * thread.h: Eliminate _cwd_win32 and _cwd_posix buffers. * winsup.h: Eliminate obsolete functions. Add envsize() declaration.
2000-09-03 12:16:35 +08:00
#include "heap.h"
#include "shared_info.h"
#include "security.h"
#include "fhandler.h"
Add "path.h" include throughout, where needed. Use new path_conv methods and operators to simplify testing for directory and attributes, throughout. * path.h (path_conv::exists): New method. (path_conv::has_attribute): Ditto. (path_conv::isdir): Ditto. (path_conv::DWORD &): New operator. (path_conv::int &): Ditto. * dir.cc (rmdir): Eliminate a goto. * dtable.cc (dtable::build_fhandler): Accept opt and suffix info for path_conv.check. Return fh == NULL on path_conv error. Pass unit to set_name as appropriate. (dtable::reset_unix_path_name): New method. * dtable.h (dtable): Declare new method. Reflect arg changes to build_fhandler. * fhandler.cc (fhandler_disk_dummy_name): Eliminate. (fhandler_base::set_name): Expect paths to be NULL. Build unix_path_name from win32_path_name when it is a device. (fhandler_base::reset_unix_path_name): New method. (fhandler_base::raw_read): Report EISDIR when ERROR_INVALID_FUNCTION or ERROR_INVALID_PARAMETER and reading a directory. (fhandler_disk_file::fstat): Don't call stat_dev since we should now never be calling fhandler_disk_file methods with devices. (fhandler_base::fhandler_base): Clear {unix,win32}_path_name. (fhandler_base::~fhandler_base): Always free {unix,win32}_path_name. (fhandler_disk_file::fhandler_disk_file): Remove set_no_free_names kludge. (fhandler_disk_file::open): Ditto. * fhandler.h (fhandler_base::no_free_names): Eliminate. (fhandler_base::set_no_free_names): Ditto. * fhandler_tty.cc (fhandler_tty_slave::fhandler_tty_slave): Don't set unix_path_name here. * path.cc (fchdir): Lock fd table throughout. Use new dtable::reset_unix_path_name method to reset path. * syscalls.cc (stat_worker): Reorganize to always call fstat method. Pass path_conv method to fhandler_*::open. (chroot): Elminate a goto.
2001-10-01 12:10:07 +08:00
#include "path.h"
#include "dtable.h"
#include "cygheap.h"
#include "registry.h"
#include "cygwin_version.h"
2000-02-18 03:38:33 +08:00
#define assert(x)
static unsigned page_const;
2000-02-18 03:38:33 +08:00
extern "C" size_t getpagesize ();
#define MINHEAP_SIZE (4 * 1024 * 1024)
2000-02-18 03:38:33 +08:00
/* Initialize the heap at process start up. */
void
heap_init ()
{
/* If we're the forkee, we must allocate the heap at exactly the same place
as our parent. If not, we don't care where it ends up. */
Throughout, change 'cygwin_shared.mount' to 'mount_table'. * child_info.h (child_info): Move shared_h, console_h to cygheap. Add mount_h. * cygheap.h (init_cygheap): Add shared_h, console_h. * cygheap.cc (init_cheap): Initialize heap at a fixed location after the shared memory regions. Initialize cygheap->user name here. * dcrt0.cc (dll_crt0_1): Call getpagesize () to initialize constants. Remove cygheap_init since it is done in shared_init now. (_dll_crt0): Initialize mount_h, remove shared_h and console_h initialization. * fhandler_console.cc (console_shared_h): Eliminate. (get_tty_stuff): Use cygheap->console_h rather than console_shared_h. * heap.cc (heap_init): Use page size constant calculated earlier in initialization. * shared.cc: Eliminate cygwin_shared_h. Add cygwin_mount_h. (mount_table_init): New function for initializing a user mount table. (open_shared_file_map): Use constant for shared memory region. Initialize cygheap and mount table here. (open_shared): Improve debugging output. (shared_info::initialize): Eliminate call to mount.init. (shared_terminate): Use cygheap->shared_h. Close cygwin_mount_h. (open_shared_file_map): Eliminate. * shared_info.h (mount_info): Add a version field. (shared_align_past): New macro for calculating location for shared memory regions. * sigproc.cc (init_child_info): Eliminate shared_h, console_h. * spawn.cc (spawn_guts): Pass on cygwin_mount_h iff not a different user. * syscalls.cc (system_info): New global holding system memory defaults. (getpagesize): Use system_info. * uinfo.cc (internal_getlogin): Only fill in user name if nonexistent. * winsup.h: Declare system_info. * passwd.cc (read_etc_passwd): Use cygheap->user.name () rather than retrieving the name again.
2001-01-28 13:51:15 +08:00
page_const = system_info.dwPageSize;
if (!cygheap->user_heap.base)
{
cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
{
/* Initialize page mask and default heap size. Preallocate a heap
* to assure contiguous memory. */
cygheap->user_heap.ptr = cygheap->user_heap.top =
cygheap->user_heap.base =
VirtualAlloc (NULL, cygheap->user_heap.chunk, MEM_RESERVE, PAGE_NOACCESS);
if (cygheap->user_heap.base)
break;
cygheap->user_heap.chunk -= 1 * 1024 * 1024;
}
if (cygheap->user_heap.base == NULL)
api_fatal ("unable to allocate heap, heap_chunk_size %d, %E",
cygheap->user_heap.chunk);
cygheap->user_heap.max = (char *) cygheap->user_heap.base + cygheap->user_heap.chunk;
}
else
2000-02-18 03:38:33 +08:00
{
DWORD chunk = cygheap->user_heap.chunk; /* allocation chunk */
2000-02-18 03:38:33 +08:00
/* total size commited in parent */
DWORD allocsize = (char *) cygheap->user_heap.top -
(char *) cygheap->user_heap.base;
2000-02-18 03:38:33 +08:00
/* round up by chunk size */
DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
/* Loop until we've managed to reserve an adequate amount of memory. */
2000-07-04 04:16:23 +08:00
char *p;
2000-02-18 03:38:33 +08:00
for (;;)
{
p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
2000-07-04 04:16:23 +08:00
MEM_RESERVE, PAGE_READWRITE);
2000-02-18 03:38:33 +08:00
if (p)
break;
2000-07-04 04:16:23 +08:00
if ((reserve_size -= page_const) <= allocsize)
2000-02-18 03:38:33 +08:00
break;
}
if (p != cygheap->user_heap.base)
api_fatal ("heap allocated but not at %p", cygheap->user_heap.base);
if (!VirtualAlloc (cygheap->user_heap.base, allocsize, MEM_COMMIT, PAGE_READWRITE))
2000-02-18 03:38:33 +08:00
api_fatal ("MEM_COMMIT failed, %E");
}
debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
cygheap->user_heap.top);
2000-07-04 04:16:23 +08:00
page_const--;
2000-02-18 03:38:33 +08:00
malloc_init ();
}
#define pround(n) (((size_t)(n) + page_const) & ~page_const)
/* FIXME: This function no longer handles "split heaps". */
extern "C" void *
sbrk (int n)
2000-02-18 03:38:33 +08:00
{
sigframe thisframe (mainthread);
2000-02-18 03:38:33 +08:00
char *newtop, *newbrk;
unsigned commitbytes, newbrksize;
if (n == 0)
return cygheap->user_heap.ptr; /* Just wanted to find current cygheap->user_heap.ptr address */
2000-02-18 03:38:33 +08:00
newbrk = (char *) cygheap->user_heap.ptr + n; /* Where new cygheap->user_heap.ptr will be */
newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
on page boundary */
2000-02-18 03:38:33 +08:00
if (newtop == cygheap->user_heap.top)
2000-02-18 03:38:33 +08:00
goto good;
if (n < 0)
{ /* Freeing memory */
assert (newtop < cygheap->user_heap.top);
n = (char *) cygheap->user_heap.top - newtop;
if (VirtualFree (newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
goto good; /* Didn't take */
2000-02-18 03:38:33 +08:00
else
goto err;
}
assert (newtop > cygheap->user_heap.top);
2000-02-18 03:38:33 +08:00
/* Find the number of bytes to commit, rounded up to the nearest page. */
commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
/* Need to grab more pages from the OS. If this fails it may be because
we have used up previously reserved memory. Or, we're just plumb out
of memory. Only attempt to commit memory that we know we've previously
reserved. */
if (newtop <= cygheap->user_heap.max)
{
if (VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
goto good;
}
2000-02-18 03:38:33 +08:00
/* Couldn't allocate memory. Maybe we can reserve some more.
Reserve either the maximum of the standard cygwin_shared->heap_chunk_size ()
or the requested amount. Then attempt to actually allocate it. */
if ((newbrksize = cygheap->user_heap.chunk) < commitbytes)
2000-02-18 03:38:33 +08:00
newbrksize = commitbytes;
if ((VirtualAlloc (cygheap->user_heap.top, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
|| VirtualAlloc (cygheap->user_heap.top, newbrksize = commitbytes, MEM_RESERVE, PAGE_NOACCESS))
&& VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
{
(char *) cygheap->user_heap.max += pround (newbrksize);
goto good;
}
2000-02-18 03:38:33 +08:00
err:
set_errno (ENOMEM);
return (void *) -1;
good:
void *oldbrk = cygheap->user_heap.ptr;
cygheap->user_heap.ptr = newbrk;
cygheap->user_heap.top = newtop;
2000-02-18 03:38:33 +08:00
return oldbrk;
}