newlib/winsup/cygwin/heap.cc

146 lines
4.3 KiB
C++
Raw Normal View History

2000-02-17 20:38:33 +01:00
/* heap.cc: Cygwin heap manager.
2001-03-18 04:34:05 +01:00
Copyright 1996, 1997, 1998, 1999, 2000, 2001 Red Hat, Inc.
2000-02-17 20:38:33 +01: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 <errno.h>
#include "cygerrno.h"
#include "sync.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 06:16:35 +02: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 06:10:07 +02:00
#include "path.h"
#include "dtable.h"
#include "cygheap.h"
2000-02-17 20:38:33 +01:00
#define assert(x)
static unsigned page_const;
2000-02-17 20:38:33 +01:00
extern "C" size_t getpagesize ();
2000-02-17 20:38:33 +01: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 06:51:15 +01:00
page_const = system_info.dwPageSize;
if (cygheap->heapbase)
2000-02-17 20:38:33 +01:00
{
DWORD chunk = cygwin_shared->heap_chunk_size (); /* allocation chunk */
2000-02-17 20:38:33 +01:00
/* total size commited in parent */
DWORD allocsize = (char *) cygheap->heaptop - (char *) cygheap->heapbase;
2000-02-17 20:38:33 +01: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-03 22:16:23 +02:00
char *p;
2000-02-17 20:38:33 +01:00
for (;;)
{
p = (char *) VirtualAlloc (cygheap->heapbase, reserve_size,
2000-07-03 22:16:23 +02:00
MEM_RESERVE, PAGE_READWRITE);
2000-02-17 20:38:33 +01:00
if (p)
break;
2000-07-03 22:16:23 +02:00
if ((reserve_size -= page_const) <= allocsize)
2000-02-17 20:38:33 +01:00
break;
}
if (p == NULL)
api_fatal ("1. unable to allocate heap %p, heap_chunk_size %d, pid %d, %E",
cygheap->heapbase, cygwin_shared->heap_chunk_size (), myself->pid);
if (p != cygheap->heapbase)
api_fatal ("heap allocated but not at %p", cygheap->heapbase);
if (! VirtualAlloc (cygheap->heapbase, allocsize, MEM_COMMIT, PAGE_READWRITE))
2000-02-17 20:38:33 +01:00
api_fatal ("MEM_COMMIT failed, %E");
}
else
{
/* Initialize page mask and default heap size. Preallocate a heap
* to assure contiguous memory. */
cygheap->heapptr = cygheap->heaptop = cygheap->heapbase =
VirtualAlloc(NULL, cygwin_shared->heap_chunk_size (), MEM_RESERVE,
2001-11-05 07:09:15 +01:00
PAGE_NOACCESS);
if (cygheap->heapbase == NULL)
2000-02-17 20:38:33 +01:00
api_fatal ("2. unable to allocate heap, heap_chunk_size %d, %E",
cygwin_shared->heap_chunk_size ());
2000-02-17 20:38:33 +01:00
}
debug_printf ("heap base %p, heap top %p", cygheap->heapbase,
2001-11-05 07:09:15 +01:00
cygheap->heaptop);
2000-07-03 22:16:23 +02:00
page_const--;
2000-02-17 20:38:33 +01: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)
{
sigframe thisframe (mainthread);
2000-02-17 20:38:33 +01:00
char *newtop, *newbrk;
unsigned commitbytes, newbrksize;
if (n == 0)
return cygheap->heapptr; /* Just wanted to find current cygheap->heapptr address */
2000-02-17 20:38:33 +01:00
newbrk = (char *) cygheap->heapptr + n; /* Where new cygheap->heapptr will be */
newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
on page boundary */
2000-02-17 20:38:33 +01:00
if (newtop == cygheap->heaptop)
2000-02-17 20:38:33 +01:00
goto good;
if (n < 0)
{ /* Freeing memory */
assert(newtop < cygheap->heaptop);
n = (char *) cygheap->heaptop - newtop;
2000-02-17 20:38:33 +01:00
if (VirtualFree(newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
goto good; /* Didn't take */
2000-02-17 20:38:33 +01:00
else
goto err;
}
assert(newtop > cygheap->heaptop);
2000-02-17 20:38:33 +01:00
/* 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. */
commitbytes = pround (newtop - (char *) cygheap->heaptop);
if (VirtualAlloc(cygheap->heaptop, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
2000-02-17 20:38:33 +01:00
goto good;
/* 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
2000-02-17 20:38:33 +01:00
amount. Then attempt to actually allocate it. */
if ((newbrksize = cygwin_shared->heap_chunk_size ()) < commitbytes)
2000-02-17 20:38:33 +01:00
newbrksize = commitbytes;
if ((VirtualAlloc(cygheap->heaptop, newbrksize, MEM_RESERVE, PAGE_NOACCESS) != NULL) &&
(VirtualAlloc(cygheap->heaptop, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL))
2000-02-17 20:38:33 +01:00
goto good;
err:
set_errno (ENOMEM);
return (void *) -1;
good:
void *oldbrk = cygheap->heapptr;
cygheap->heapptr = newbrk;
cygheap->heaptop = newtop;
2000-02-17 20:38:33 +01:00
return oldbrk;
}