2000-02-17 20:38:33 +01:00
|
|
|
/* heap.cc: Cygwin heap manager.
|
|
|
|
|
2004-02-09 05:04:24 +01:00
|
|
|
Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 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"
|
2000-08-22 05:58:47 +02:00
|
|
|
#include "cygerrno.h"
|
2000-09-07 18:23:51 +02:00
|
|
|
#include "shared_info.h"
|
2001-10-01 06:10:07 +02:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 02:37:18 +02:00
|
|
|
#include "fhandler.h"
|
2001-09-07 23:32:07 +02:00
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
2006-03-13 22:10:14 +01:00
|
|
|
#include "child_info.h"
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#define assert(x)
|
|
|
|
|
2001-09-06 06:41:59 +02:00
|
|
|
static unsigned page_const;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
#define MINHEAP_SIZE (4 * 1024 * 1024)
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Initialize the heap at process start up. */
|
|
|
|
void
|
|
|
|
heap_init ()
|
|
|
|
{
|
2005-09-14 20:30:21 +02:00
|
|
|
const DWORD alloctype = MEM_RESERVE;
|
2000-02-17 20:38:33 +01:00
|
|
|
/* 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. */
|
|
|
|
|
2001-01-28 06:51:15 +01:00
|
|
|
page_const = system_info.dwPageSize;
|
2002-10-22 18:18:55 +02:00
|
|
|
if (!cygheap->user_heap.base)
|
|
|
|
{
|
|
|
|
cygheap->user_heap.chunk = cygwin_shared->heap_chunk_size ();
|
2006-10-31 19:41:16 +01:00
|
|
|
/* For some obscure reason Vista and 2003 sometimes reserve space after
|
|
|
|
calls to CreateProcess overlapping the spot where the heap has been
|
|
|
|
allocated. This apparently spoils fork. The behaviour looks quite
|
|
|
|
arbitrary. Experiments on Vista show a memory size of 0x37e000 or
|
|
|
|
0x1fd000 overlapping the usual heap by at most 0x1ed000. So what
|
|
|
|
we do here is to allocate the heap with an extra slop of (by default)
|
2009-06-05 13:15:28 +02:00
|
|
|
0x400000 and set the appropriate pointers to the start of the heap
|
2006-10-31 19:41:16 +01:00
|
|
|
area + slop. A forking child then creates its heap at the new start
|
|
|
|
address and without the slop factor. Since this is not entirely
|
|
|
|
foolproof we add a registry setting "heap_slop_in_mb" so the slop
|
|
|
|
factor can be influenced by the user if the need arises. */
|
|
|
|
cygheap->user_heap.slop = cygwin_shared->heap_slop_size ();
|
2002-10-22 18:18:55 +02:00
|
|
|
while (cygheap->user_heap.chunk >= MINHEAP_SIZE)
|
|
|
|
{
|
|
|
|
/* Initialize page mask and default heap size. Preallocate a heap
|
|
|
|
* to assure contiguous memory. */
|
|
|
|
cygheap->user_heap.base =
|
2006-10-31 19:41:16 +01:00
|
|
|
VirtualAlloc (NULL, cygheap->user_heap.chunk
|
|
|
|
+ cygheap->user_heap.slop,
|
|
|
|
alloctype, PAGE_NOACCESS);
|
2002-10-22 18:18:55 +02:00
|
|
|
if (cygheap->user_heap.base)
|
|
|
|
break;
|
|
|
|
cygheap->user_heap.chunk -= 1 * 1024 * 1024;
|
|
|
|
}
|
|
|
|
if (cygheap->user_heap.base == NULL)
|
2006-10-31 19:41:16 +01:00
|
|
|
api_fatal ("unable to allocate heap, heap_chunk_size %p, slop %p, %E",
|
|
|
|
cygheap->user_heap.chunk, cygheap->user_heap.slop);
|
|
|
|
cygheap->user_heap.base = (void *) ((char *) cygheap->user_heap.base
|
|
|
|
+ cygheap->user_heap.slop);
|
|
|
|
cygheap->user_heap.ptr = cygheap->user_heap.top = cygheap->user_heap.base;
|
|
|
|
cygheap->user_heap.max = (char *) cygheap->user_heap.base
|
|
|
|
+ cygheap->user_heap.chunk;
|
2002-10-22 18:18:55 +02:00
|
|
|
}
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-10-22 18:18:55 +02:00
|
|
|
DWORD chunk = cygheap->user_heap.chunk; /* allocation chunk */
|
2000-02-17 20:38:33 +01:00
|
|
|
/* total size commited in parent */
|
2002-10-22 18:18:55 +02:00
|
|
|
DWORD allocsize = (char *) cygheap->user_heap.top -
|
|
|
|
(char *) cygheap->user_heap.base;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Loop until we've managed to reserve an adequate amount of memory. */
|
2000-07-03 22:16:23 +02:00
|
|
|
char *p;
|
2004-01-14 16:45:37 +01:00
|
|
|
DWORD reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
|
|
|
|
while (1)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2002-10-22 18:18:55 +02:00
|
|
|
p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
|
2005-09-14 20:30:21 +02:00
|
|
|
alloctype, PAGE_READWRITE);
|
2000-02-17 20:38:33 +01:00
|
|
|
if (p)
|
|
|
|
break;
|
2005-09-10 03:37:20 +02:00
|
|
|
if ((reserve_size -= page_const) < allocsize)
|
2000-02-17 20:38:33 +01:00
|
|
|
break;
|
|
|
|
}
|
2006-03-13 22:10:14 +01:00
|
|
|
if (!p && in_forkee && !fork_info->handle_failure (GetLastError ()))
|
2006-01-18 21:40:52 +01:00
|
|
|
api_fatal ("couldn't allocate heap, %E, base %p, top %p, "
|
|
|
|
"reserve_size %d, allocsize %d, page_const %d",
|
|
|
|
cygheap->user_heap.base, cygheap->user_heap.top,
|
|
|
|
reserve_size, allocsize, page_const);
|
2002-10-22 18:18:55 +02:00
|
|
|
if (p != cygheap->user_heap.base)
|
2004-01-14 16:45:37 +01:00
|
|
|
api_fatal ("heap allocated at wrong address %p (mapped) != %p (expected)", p, cygheap->user_heap.base);
|
2006-06-12 15:59:17 +02:00
|
|
|
if (allocsize && !VirtualAlloc (cygheap->user_heap.base, allocsize, MEM_COMMIT, PAGE_READWRITE))
|
2000-02-17 20:38:33 +01:00
|
|
|
api_fatal ("MEM_COMMIT failed, %E");
|
|
|
|
}
|
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
debug_printf ("heap base %p, heap top %p", cygheap->user_heap.base,
|
|
|
|
cygheap->user_heap.top);
|
2000-07-03 22:16:23 +02:00
|
|
|
page_const--;
|
2004-01-14 16:45:37 +01:00
|
|
|
// malloc_init ();
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define pround(n) (((size_t)(n) + page_const) & ~page_const)
|
|
|
|
|
|
|
|
/* FIXME: This function no longer handles "split heaps". */
|
|
|
|
|
|
|
|
extern "C" void *
|
2002-10-21 03:00:58 +02:00
|
|
|
sbrk (int n)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
char *newtop, *newbrk;
|
|
|
|
unsigned commitbytes, newbrksize;
|
|
|
|
|
|
|
|
if (n == 0)
|
2003-08-22 21:25:56 +02:00
|
|
|
return cygheap->user_heap.ptr; /* Just wanted to find current cygheap->user_heap.ptr address */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
newbrk = (char *) cygheap->user_heap.ptr + n; /* Where new cygheap->user_heap.ptr will be */
|
2001-09-09 21:06:50 +02:00
|
|
|
newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
|
|
|
|
on page boundary */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
if (newtop == cygheap->user_heap.top)
|
2000-02-17 20:38:33 +01:00
|
|
|
goto good;
|
|
|
|
|
|
|
|
if (n < 0)
|
2001-09-09 21:06:50 +02:00
|
|
|
{ /* Freeing memory */
|
2002-10-22 18:18:55 +02:00
|
|
|
assert (newtop < cygheap->user_heap.top);
|
|
|
|
n = (char *) cygheap->user_heap.top - newtop;
|
2002-09-22 05:38:57 +02:00
|
|
|
if (VirtualFree (newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
|
2001-09-09 21:06:50 +02:00
|
|
|
goto good; /* Didn't take */
|
2000-02-17 20:38:33 +01:00
|
|
|
else
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
2002-10-22 18:18:55 +02:00
|
|
|
assert (newtop > cygheap->user_heap.top);
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2003-08-22 21:25:56 +02:00
|
|
|
/* Find the number of bytes to commit, rounded up to the nearest page. */
|
2002-10-22 18:18:55 +02:00
|
|
|
commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
|
2003-08-22 21:25:56 +02: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. 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-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Couldn't allocate memory. Maybe we can reserve some more.
|
2003-08-22 21:25:56 +02:00
|
|
|
Reserve either the maximum of the standard cygwin_shared->heap_chunk_size ()
|
|
|
|
or the requested amount. Then attempt to actually allocate it. */
|
2002-10-22 18:18:55 +02:00
|
|
|
if ((newbrksize = cygheap->user_heap.chunk) < commitbytes)
|
2000-02-17 20:38:33 +01:00
|
|
|
newbrksize = commitbytes;
|
|
|
|
|
2003-08-22 21:25:56 +02:00
|
|
|
if ((VirtualAlloc (cygheap->user_heap.top, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
|
2004-05-28 21:50:07 +02:00
|
|
|
|| VirtualAlloc (cygheap->user_heap.top, newbrksize = commitbytes, MEM_RESERVE, PAGE_NOACCESS))
|
2003-08-22 21:25:56 +02:00
|
|
|
&& VirtualAlloc (cygheap->user_heap.top, commitbytes, MEM_COMMIT, PAGE_READWRITE) != NULL)
|
|
|
|
{
|
2003-09-07 20:27:54 +02:00
|
|
|
cygheap->user_heap.max = (char *) cygheap->user_heap.max + pround (newbrksize);
|
2003-08-22 21:25:56 +02:00
|
|
|
goto good;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return (void *) -1;
|
|
|
|
|
|
|
|
good:
|
2002-10-22 18:18:55 +02:00
|
|
|
void *oldbrk = cygheap->user_heap.ptr;
|
|
|
|
cygheap->user_heap.ptr = newbrk;
|
|
|
|
cygheap->user_heap.top = newtop;
|
2000-02-17 20:38:33 +01:00
|
|
|
return oldbrk;
|
|
|
|
}
|