2000-02-17 20:38:33 +01:00
|
|
|
/* heap.cc: Cygwin heap manager.
|
|
|
|
|
|
|
|
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"
|
2011-07-21 19:52:05 +02:00
|
|
|
#include "ntdll.h"
|
2011-05-16 12:27:14 +02:00
|
|
|
#include <sys/param.h>
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
#define assert(x)
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
static ptrdiff_t page_const;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-08-31 12:21:48 +02:00
|
|
|
/* Minimum size of the base heap. */
|
2002-10-22 18:18:55 +02:00
|
|
|
#define MINHEAP_SIZE (4 * 1024 * 1024)
|
2013-08-31 12:21:48 +02:00
|
|
|
/* Chunksize of subsequent heap reservations. */
|
|
|
|
#define RAISEHEAP_SIZE (1 * 1024 * 1024)
|
2002-10-22 18:18:55 +02:00
|
|
|
|
2011-07-21 17:23:24 +02:00
|
|
|
static uintptr_t
|
|
|
|
eval_start_address ()
|
|
|
|
{
|
2013-04-23 11:44:36 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
/* On 64 bit, we choose a fixed address outside the 32 bit area. The
|
|
|
|
executable starts at 0x1:00400000L, the Cygwin DLL starts at
|
|
|
|
0x1:80040000L, other rebased DLLs are located in the region from
|
|
|
|
0x2:00000000L up to 0x4:00000000L, -auto-image-based DLLs are located
|
|
|
|
in the region from 0x4:00000000L up to 0x6:00000000L.
|
|
|
|
So we let the heap start at 0x6:00000000L. */
|
|
|
|
uintptr_t start_address = 0x600000000L;
|
|
|
|
#else
|
2015-12-15 21:43:33 +01:00
|
|
|
/* Windows performs heap ASLR. This spoils the entire region below
|
|
|
|
0x20000000 for us, because that region is used by Windows to randomize
|
|
|
|
heap and stack addresses. Therefore we put our heap into a safe region
|
|
|
|
starting at 0x20000000. This should work right from the start in 99%
|
|
|
|
of the cases. */
|
2011-07-21 17:23:24 +02:00
|
|
|
uintptr_t start_address = 0x20000000L;
|
2018-12-19 21:10:37 +01:00
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
|
|
|
|
if (VirtualQuery ((void *) 0xbf000000L, &mbi, sizeof mbi))
|
2011-07-21 17:23:24 +02:00
|
|
|
{
|
2011-07-21 19:52:05 +02:00
|
|
|
/* However, if we're running on a /3GB enabled 32 bit system or on
|
|
|
|
a 64 bit system, and the executable is large address aware, then
|
|
|
|
we know that we have spare 1 Gig (32 bit) or even 2 Gigs (64 bit)
|
|
|
|
virtual address space. This memory region is practically unused
|
2018-12-19 21:10:37 +01:00
|
|
|
by Windows, only PEB and TEBs are allocated top-down here.
|
|
|
|
|
|
|
|
We used to use the current TEB address as very simple test that
|
|
|
|
this is a large address aware executable, but that fails on W10
|
|
|
|
WOW64 because the main TEB is apparently commited in the lower
|
|
|
|
2 Gigs these days.
|
|
|
|
|
|
|
|
The above test for address 0xbf000000 is supposed to make sure
|
|
|
|
that we really have 3GB on a 32 bit system. Windows supports
|
|
|
|
smaller large address regions, but then it's not that interesting
|
|
|
|
for us to use it for the heap. If the region is big enough, the
|
|
|
|
heap gets allocated at its start. What we get are 0.999 or 1.999
|
|
|
|
Gigs of free contiguous memory for heap, thread stacks, and shared
|
|
|
|
memory regions. */
|
2011-07-21 19:52:05 +02:00
|
|
|
start_address = 0x80000000L;
|
2011-07-21 17:23:24 +02:00
|
|
|
}
|
2013-04-23 11:44:36 +02:00
|
|
|
#endif
|
2011-07-21 17:23:24 +02:00
|
|
|
return start_address;
|
|
|
|
}
|
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
static SIZE_T
|
2011-08-09 14:00:27 +02:00
|
|
|
eval_initial_heap_size ()
|
|
|
|
{
|
|
|
|
PIMAGE_DOS_HEADER dosheader;
|
2013-04-23 11:44:36 +02:00
|
|
|
PIMAGE_NT_HEADERS ntheader;
|
|
|
|
SIZE_T size;
|
2011-08-09 14:00:27 +02:00
|
|
|
|
|
|
|
dosheader = (PIMAGE_DOS_HEADER) GetModuleHandle (NULL);
|
2013-04-23 11:44:36 +02:00
|
|
|
ntheader = (PIMAGE_NT_HEADERS) ((PBYTE) dosheader + dosheader->e_lfanew);
|
2011-08-09 14:00:27 +02:00
|
|
|
/* LoaderFlags is an obsolete DWORD member of the PE/COFF file header.
|
|
|
|
It's value is ignored by the loader, so we're free to use it for
|
2013-04-23 11:44:36 +02:00
|
|
|
Cygwin. If it's 0, we default to the usual 384 Megs on 32 bit and
|
|
|
|
512 on 64 bit. Otherwise, we use it as the default initial heap size
|
|
|
|
in megabyte. Valid values are between 4 and 2048/8388608 Megs. */
|
|
|
|
|
2011-08-09 14:00:27 +02:00
|
|
|
size = ntheader->OptionalHeader.LoaderFlags;
|
2013-04-23 11:44:36 +02:00
|
|
|
#ifdef __x86_64__
|
|
|
|
if (size == 0)
|
|
|
|
size = 512;
|
|
|
|
else if (size < 4)
|
|
|
|
size = 4;
|
|
|
|
else if (size > 8388608)
|
|
|
|
size = 8388608;
|
|
|
|
#else
|
2011-08-09 14:00:27 +02:00
|
|
|
if (size == 0)
|
|
|
|
size = 384;
|
|
|
|
else if (size < 4)
|
|
|
|
size = 4;
|
|
|
|
else if (size > 2048)
|
|
|
|
size = 2048;
|
2013-04-23 11:44:36 +02:00
|
|
|
#endif
|
2011-08-09 14:00:27 +02:00
|
|
|
return size << 20;
|
|
|
|
}
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
/* Initialize the heap at process start up. */
|
|
|
|
void
|
2013-08-30 23:02:02 +02:00
|
|
|
user_heap_info::init ()
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
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
|
2011-05-16 12:27:14 +02:00
|
|
|
as our parent. If not, we (almost) don't care where it ends up. */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2011-05-10 17:39:02 +02:00
|
|
|
page_const = wincap.page_size ();
|
2013-08-30 23:02:02 +02:00
|
|
|
if (!base)
|
2002-10-22 18:18:55 +02:00
|
|
|
{
|
2011-07-21 17:23:24 +02:00
|
|
|
uintptr_t start_address = eval_start_address ();
|
2011-07-18 10:43:49 +02:00
|
|
|
PVOID largest_found = NULL;
|
2013-04-23 11:44:36 +02:00
|
|
|
SIZE_T largest_found_size = 0;
|
2011-05-16 12:27:14 +02:00
|
|
|
SIZE_T ret;
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
chunk = eval_initial_heap_size ();
|
2011-05-16 12:27:14 +02:00
|
|
|
do
|
2002-10-22 18:18:55 +02:00
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
base = VirtualAlloc ((LPVOID) start_address, chunk, alloctype,
|
|
|
|
PAGE_NOACCESS);
|
|
|
|
if (base)
|
2002-10-22 18:18:55 +02:00
|
|
|
break;
|
2011-05-16 12:27:14 +02:00
|
|
|
|
|
|
|
/* Ok, so we are at the 1% which didn't work with 0x20000000 out
|
|
|
|
of the box. What we do now is to search for the next free
|
|
|
|
region which matches our desired heap size. While doing that,
|
2011-07-18 10:43:49 +02:00
|
|
|
we keep track of the largest region we found, including the
|
|
|
|
region starting at 0x20000000. */
|
2011-05-16 12:27:14 +02:00
|
|
|
while ((ret = VirtualQuery ((LPCVOID) start_address, &mbi,
|
|
|
|
sizeof mbi)) != 0)
|
|
|
|
{
|
|
|
|
if (mbi.State == MEM_FREE)
|
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
if (mbi.RegionSize >= chunk)
|
2011-05-16 12:27:14 +02:00
|
|
|
break;
|
|
|
|
if (mbi.RegionSize > largest_found_size)
|
|
|
|
{
|
2011-07-18 10:43:49 +02:00
|
|
|
largest_found = mbi.BaseAddress;
|
2011-05-16 12:27:14 +02:00
|
|
|
largest_found_size = mbi.RegionSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Since VirtualAlloc only reserves at allocation granularity
|
2011-06-06 07:02:13 +02:00
|
|
|
boundaries, we round up here, too. Otherwise we might end
|
2011-05-16 12:27:14 +02:00
|
|
|
up at a bogus page-aligned address. */
|
|
|
|
start_address = roundup2 (start_address + mbi.RegionSize,
|
|
|
|
wincap.allocation_granularity ());
|
|
|
|
}
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
/* In theory this should not happen. But if it happens, we have
|
|
|
|
collected the information about the largest available region
|
|
|
|
in the above loop. So, next we squeeze the heap into that
|
|
|
|
region, unless it's smaller than the minimum size. */
|
|
|
|
if (largest_found_size >= MINHEAP_SIZE)
|
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
chunk = largest_found_size;
|
|
|
|
base = VirtualAlloc (largest_found, chunk, alloctype,
|
|
|
|
PAGE_NOACCESS);
|
2011-05-16 12:27:14 +02:00
|
|
|
}
|
|
|
|
/* Last resort (but actually we are probably broken anyway):
|
|
|
|
Use the minimal heap size and let the system decide. */
|
2013-08-30 23:02:02 +02:00
|
|
|
if (!base)
|
2011-05-16 12:27:14 +02:00
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
chunk = MINHEAP_SIZE;
|
|
|
|
base = VirtualAlloc (NULL, chunk, alloctype, PAGE_NOACCESS);
|
2011-05-16 12:27:14 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-22 18:18:55 +02:00
|
|
|
}
|
2013-08-30 23:02:02 +02:00
|
|
|
while (!base && ret);
|
|
|
|
if (base == NULL)
|
2013-04-23 11:44:36 +02:00
|
|
|
api_fatal ("unable to allocate heap, heap_chunk_size %ly, %E",
|
2013-08-30 23:02:02 +02:00
|
|
|
chunk);
|
|
|
|
ptr = top = base;
|
|
|
|
max = (char *) base + chunk;
|
2002-10-22 18:18:55 +02:00
|
|
|
}
|
|
|
|
else
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
|
|
|
/* total size commited in parent */
|
2013-08-30 23:02:02 +02:00
|
|
|
SIZE_T allocsize = (char *) top - (char *) base;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
/* Loop until we've managed to reserve an adequate amount of memory. */
|
2013-04-23 11:44:36 +02:00
|
|
|
SIZE_T reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
|
2013-08-30 23:02:02 +02:00
|
|
|
|
2013-04-23 11:44:36 +02:00
|
|
|
/* With ptmalloc3 there's a good chance that there has been no memory
|
|
|
|
allocated on the heap. If we don't check that, reserve_size will
|
|
|
|
be 0 and from there, the below loop will end up overallocating due
|
|
|
|
to integer overflow. */
|
|
|
|
if (!reserve_size)
|
|
|
|
reserve_size = chunk;
|
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
char *p;
|
2004-01-14 16:45:37 +01:00
|
|
|
while (1)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
p = (char *) VirtualAlloc (base, reserve_size, 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;
|
|
|
|
}
|
2011-05-28 20:17:09 +02:00
|
|
|
if (!p && in_forkee && !fork_info->abort (NULL))
|
2006-01-18 21:40:52 +01:00
|
|
|
api_fatal ("couldn't allocate heap, %E, base %p, top %p, "
|
2013-04-23 11:44:36 +02:00
|
|
|
"reserve_size %ld, allocsize %ld, page_const %d",
|
2013-08-30 23:02:02 +02:00
|
|
|
base, top,
|
2006-01-18 21:40:52 +01:00
|
|
|
reserve_size, allocsize, page_const);
|
2013-08-30 23:02:02 +02:00
|
|
|
if (p != base)
|
2013-04-23 11:44:36 +02:00
|
|
|
api_fatal ("heap allocated at wrong address %p (mapped) "
|
2013-08-30 23:02:02 +02:00
|
|
|
"!= %p (expected)", p, base);
|
|
|
|
if (allocsize && !VirtualAlloc (base, allocsize,
|
2013-04-23 11:44:36 +02:00
|
|
|
MEM_COMMIT, PAGE_READWRITE))
|
2000-02-17 20:38:33 +01:00
|
|
|
api_fatal ("MEM_COMMIT failed, %E");
|
|
|
|
}
|
|
|
|
|
2012-05-21 21:45:12 +02:00
|
|
|
/* CV 2012-05-21: Moved printing heap size here from strace::activate.
|
|
|
|
The value printed in strace.activate was always wrong, because at the
|
|
|
|
time it's called, cygheap points to cygheap_dummy. Above all, the heap
|
|
|
|
size has not been evaluated yet, except in a forked child. Since
|
|
|
|
heap_init is called early, the heap size is printed pretty much at the
|
|
|
|
start of the strace output, so there isn't anything lost. */
|
2013-04-23 11:44:36 +02:00
|
|
|
debug_printf ("heap base %p, heap top %p, heap size %ly (%lu)",
|
2013-08-30 23:02:02 +02:00
|
|
|
base, top, chunk, chunk);
|
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)
|
2013-04-23 11:44:36 +02:00
|
|
|
/* Linux defines n to be intptr_t, newlib defines it to be ptrdiff_t.
|
|
|
|
It shouldn't matter much, though, since the function is not standarized
|
|
|
|
and sizeof(ptrdiff_t) == sizeof(intptr_t) anyway. */
|
2000-02-17 20:38:33 +01:00
|
|
|
extern "C" void *
|
2013-04-23 11:44:36 +02:00
|
|
|
sbrk (ptrdiff_t n)
|
2000-02-17 20:38:33 +01:00
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
return cygheap->user_heap.sbrk (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void __reg2 *
|
|
|
|
user_heap_info::sbrk (ptrdiff_t n)
|
|
|
|
{
|
|
|
|
/* FIXME: This function no longer handles "split heaps". */
|
|
|
|
|
2000-02-17 20:38:33 +01:00
|
|
|
char *newtop, *newbrk;
|
2013-08-30 22:01:10 +02:00
|
|
|
SIZE_T commitbytes, newbrksize, reservebytes;
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
if (n == 0)
|
2013-08-30 23:02:02 +02:00
|
|
|
return ptr; /* Just wanted to find current ptr
|
|
|
|
address */
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
newbrk = (char *) ptr + n; /* Where new cptr 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
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
if (newtop == top)
|
2000-02-17 20:38:33 +01:00
|
|
|
goto good;
|
|
|
|
|
|
|
|
if (n < 0)
|
2001-09-09 21:06:50 +02:00
|
|
|
{ /* Freeing memory */
|
2013-08-30 23:02:02 +02:00
|
|
|
assert (newtop < top);
|
|
|
|
n = (char *) top - newtop;
|
2013-08-30 22:01:10 +02:00
|
|
|
/* FIXME: This doesn't work if we cross a virtual memory reservation
|
|
|
|
border. If that happens, we have to free the space in multiple
|
|
|
|
VirtualFree calls, aligned to the former reservation borders. */
|
2002-09-22 05:38:57 +02:00
|
|
|
if (VirtualFree (newtop, n, MEM_DECOMMIT)) /* Give it back to OS */
|
2013-04-23 11:44:36 +02:00
|
|
|
goto good;
|
|
|
|
goto err; /* Didn't take */
|
2000-02-17 20:38:33 +01:00
|
|
|
}
|
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
assert (newtop > 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. */
|
2013-08-30 23:02:02 +02:00
|
|
|
commitbytes = pround (newtop - (char *) 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. */
|
2013-08-31 12:21:48 +02:00
|
|
|
if (newtop <= max)
|
|
|
|
{
|
|
|
|
if (VirtualAlloc (top, commitbytes, MEM_COMMIT, PAGE_READWRITE))
|
|
|
|
goto good;
|
|
|
|
goto err;
|
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
/* The remainder of the existing heap is too small to fulfill the memory
|
2013-08-30 22:01:10 +02:00
|
|
|
request. We have to extend the heap, so we reserve some more memory
|
|
|
|
and then commit the remainder of the old heap, if any, and the rest of
|
|
|
|
the required space from the extended heap. */
|
|
|
|
|
2013-08-31 12:21:48 +02:00
|
|
|
/* For subsequent chunks following the base heap, reserve either 1 Megs
|
|
|
|
per chunk, or the requested amount if it's bigger than 1 Megs. */
|
2013-08-30 23:02:02 +02:00
|
|
|
reservebytes = commitbytes - ((char *) max - (char *) top);
|
2013-08-30 22:01:10 +02:00
|
|
|
commitbytes -= reservebytes;
|
2013-08-31 12:21:48 +02:00
|
|
|
if ((newbrksize = RAISEHEAP_SIZE) < reservebytes)
|
2013-08-30 22:01:10 +02:00
|
|
|
newbrksize = reservebytes;
|
|
|
|
|
2013-08-30 23:02:02 +02:00
|
|
|
if (VirtualAlloc (max, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
|
|
|
|
|| VirtualAlloc (max, newbrksize = reservebytes, MEM_RESERVE,
|
|
|
|
PAGE_NOACCESS))
|
2013-08-30 22:01:10 +02:00
|
|
|
{
|
|
|
|
/* Now commit the requested memory. Windows keeps all virtual
|
|
|
|
reservations separate, so we can't commit the two regions in a single,
|
|
|
|
combined call or we suffer an ERROR_INVALID_ADDRESS. The same error
|
|
|
|
is returned when trying to VirtualAlloc 0 bytes, which would occur if
|
|
|
|
the existing heap was already full. */
|
2013-08-30 23:02:02 +02:00
|
|
|
if ((!commitbytes || VirtualAlloc (top, commitbytes, MEM_COMMIT,
|
|
|
|
PAGE_READWRITE))
|
|
|
|
&& VirtualAlloc (max, reservebytes, MEM_COMMIT, PAGE_READWRITE))
|
2013-08-30 22:01:10 +02:00
|
|
|
{
|
2013-08-30 23:02:02 +02:00
|
|
|
max = (char *) max + pround (newbrksize);
|
2013-08-30 22:01:10 +02:00
|
|
|
goto good;
|
|
|
|
}
|
|
|
|
/* If committing the memory failed, we must free the extendend reserved
|
|
|
|
region, otherwise any other try to fetch memory (for instance by using
|
|
|
|
mmap) may fail just because we still reserve memory we don't even know
|
|
|
|
about. */
|
2013-08-30 23:02:02 +02:00
|
|
|
VirtualFree (max, newbrksize, MEM_RELEASE);
|
2013-08-30 22:01:10 +02:00
|
|
|
}
|
2000-02-17 20:38:33 +01:00
|
|
|
|
|
|
|
err:
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return (void *) -1;
|
|
|
|
|
|
|
|
good:
|
2013-08-30 23:02:02 +02:00
|
|
|
void *oldbrk = ptr;
|
|
|
|
ptr = newbrk;
|
|
|
|
top = newtop;
|
2000-02-17 20:38:33 +01:00
|
|
|
return oldbrk;
|
|
|
|
}
|