2000-09-03 12:16:35 +08:00
|
|
|
/* cygheap.cc: Cygwin heap manager.
|
|
|
|
|
2010-04-20 18:44:52 +08:00
|
|
|
Copyright 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
|
2011-02-15 23:56:03 +08:00
|
|
|
2010, 2011 Red Hat, Inc.
|
2000-09-03 12:16:35 +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 <assert.h>
|
2000-10-02 10:26:04 +08:00
|
|
|
#include <stdlib.h>
|
2005-01-13 06:40:46 +08:00
|
|
|
#include "cygerrno.h"
|
2001-07-27 03:22:24 +08:00
|
|
|
#include "security.h"
|
2001-09-10 00:52:37 +08:00
|
|
|
#include "path.h"
|
2011-04-21 08:53:55 +08:00
|
|
|
#include "tty.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 08:37:18 +08:00
|
|
|
#include "fhandler.h"
|
2001-10-01 12:10:07 +08:00
|
|
|
#include "dtable.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "cygheap.h"
|
2001-07-17 11:41:52 +08:00
|
|
|
#include "child_info.h"
|
2000-09-03 12:16:35 +08:00
|
|
|
#include "heap.h"
|
2003-09-01 10:05:32 +08:00
|
|
|
#include "sigproc.h"
|
2005-06-01 11:46:56 +08:00
|
|
|
#include "pinfo.h"
|
|
|
|
#include <unistd.h>
|
2008-02-15 00:47:11 +08:00
|
|
|
#include <wchar.h>
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2009-06-08 11:53:40 +08:00
|
|
|
static mini_cygheap NO_COPY cygheap_at_start =
|
|
|
|
{
|
|
|
|
{__utf8_mbtowc, __utf8_wctomb}
|
|
|
|
};
|
|
|
|
|
|
|
|
init_cygheap NO_COPY *cygheap = (init_cygheap *) &cygheap_at_start;
|
2002-10-15 04:25:52 +08:00
|
|
|
void NO_COPY *cygheap_max;
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2005-06-01 12:00:40 +08:00
|
|
|
extern "C" char _cygheap_end[];
|
2005-06-01 11:46:56 +08:00
|
|
|
|
2005-04-05 12:31:00 +08:00
|
|
|
static NO_COPY muto cygheap_protect;
|
2000-10-02 10:26:04 +08:00
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
struct cygheap_entry
|
2009-06-08 11:53:40 +08:00
|
|
|
{
|
|
|
|
int type;
|
|
|
|
struct cygheap_entry *next;
|
|
|
|
char data[0];
|
|
|
|
};
|
2001-07-17 11:41:52 +08:00
|
|
|
|
2001-09-06 11:39:18 +08:00
|
|
|
#define NBUCKETS (sizeof (cygheap->buckets) / sizeof (cygheap->buckets[0]))
|
2001-07-17 11:41:52 +08:00
|
|
|
#define N0 ((_cmalloc_entry *) NULL)
|
2003-01-18 11:26:07 +08:00
|
|
|
#define to_cmalloc(s) ((_cmalloc_entry *) (((char *) (s)) - (unsigned) (N0->data)))
|
2001-07-17 11:41:52 +08:00
|
|
|
|
2001-08-05 05:10:52 +08:00
|
|
|
#define CFMAP_OPTIONS (SEC_RESERVE | PAGE_READWRITE)
|
|
|
|
#define MVMAP_OPTIONS (FILE_MAP_WRITE)
|
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
extern "C" {
|
2005-06-01 11:46:56 +08:00
|
|
|
static void __stdcall _cfree (void *) __attribute__((regparm(1)));
|
|
|
|
static void *__stdcall _csbrk (int);
|
2001-08-05 05:10:52 +08:00
|
|
|
}
|
2005-08-12 10:39:13 +08:00
|
|
|
|
2001-07-17 11:41:52 +08:00
|
|
|
/* Called by fork or spawn to reallocate cygwin heap */
|
|
|
|
void __stdcall
|
2002-06-16 05:59:32 +08:00
|
|
|
cygheap_fixup_in_child (bool execed)
|
2001-07-17 11:41:52 +08:00
|
|
|
{
|
2005-06-01 11:46:56 +08:00
|
|
|
cygheap_max = child_proc_info->cygheap;
|
|
|
|
cygheap = (init_cygheap *) cygheap_max;
|
|
|
|
_csbrk ((char *) child_proc_info->cygheap_max - (char *) cygheap);
|
2005-12-17 12:36:39 +08:00
|
|
|
child_copy (child_proc_info->parent, false, "cygheap", cygheap, cygheap_max, NULL);
|
2005-07-17 08:51:03 +08:00
|
|
|
cygheap_init ();
|
|
|
|
debug_fixup_after_fork_exec ();
|
2001-07-17 11:41:52 +08:00
|
|
|
if (execed)
|
|
|
|
{
|
2005-03-23 03:00:31 +08:00
|
|
|
cygheap->hooks.next = NULL;
|
2002-10-23 00:18:55 +08:00
|
|
|
cygheap->user_heap.base = NULL; /* We can allocate the heap anywhere */
|
2001-07-17 11:41:52 +08:00
|
|
|
/* 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;
|
2001-09-07 02:06:27 +08:00
|
|
|
if (!rvc->ptr || rvc->b >= NBUCKETS || ce->type <= HEAP_1_START)
|
2001-07-17 11:41:52 +08:00
|
|
|
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 */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-14 12:28:45 +08:00
|
|
|
int
|
|
|
|
init_cygheap::manage_console_count (const char *something, int amount, bool avoid_freeing_console)
|
|
|
|
{
|
2006-03-16 10:57:37 +08:00
|
|
|
if (console_count == 0 && amount > 0)
|
|
|
|
init_console_handler (true);
|
2005-11-14 12:28:45 +08:00
|
|
|
console_count += amount;
|
|
|
|
debug_printf ("%s: console_count %d, amount %d, %s, avoid_freeing_console %d",
|
|
|
|
something, console_count, amount, myctty (), avoid_freeing_console);
|
2005-12-22 01:14:34 +08:00
|
|
|
if (!avoid_freeing_console && amount <= 0 && !console_count && myself->ctty == -1)
|
2005-11-14 12:28:45 +08:00
|
|
|
{
|
2006-03-16 10:57:37 +08:00
|
|
|
BOOL res = FreeConsole ();
|
|
|
|
debug_printf ("freed console, res %d", res);
|
|
|
|
init_console_handler (false);
|
2005-11-14 12:28:45 +08:00
|
|
|
}
|
|
|
|
return console_count;
|
|
|
|
}
|
2005-12-06 04:20:18 +08:00
|
|
|
|
2004-01-14 23:45:37 +08:00
|
|
|
void
|
|
|
|
init_cygheap::close_ctty ()
|
|
|
|
{
|
|
|
|
debug_printf ("closing cygheap->ctty %p", cygheap->ctty);
|
2011-04-21 08:53:55 +08:00
|
|
|
/* FIXME: Support for console-as-ctty is limited due to the fact that
|
|
|
|
the console doesn't use archetypes - even though they could and should */
|
|
|
|
if (cygheap->ctty->get_ttyp ()
|
|
|
|
&& cygheap->ctty->get_ttyp ()->ntty != TTY_CONSOLE)
|
|
|
|
cygheap->ctty->close ();
|
2004-01-24 07:05:33 +08:00
|
|
|
cygheap->ctty = NULL;
|
2004-01-14 23:45:37 +08:00
|
|
|
}
|
|
|
|
|
2005-06-01 11:46:56 +08:00
|
|
|
#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
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
static void *__stdcall
|
|
|
|
_csbrk (int sbs)
|
|
|
|
{
|
2002-08-07 09:20:59 +08:00
|
|
|
void *prebrk = cygheap_max;
|
2005-11-29 06:32:29 +08:00
|
|
|
size_t granmask = getpagesize () - 1;
|
2005-06-01 11:46:56 +08:00
|
|
|
char *newbase = nextpage (prebrk);
|
2003-09-08 02:27:54 +08:00
|
|
|
cygheap_max = (char *) cygheap_max + sbs;
|
2007-01-13 03:40:20 +08:00
|
|
|
if (!sbs || (newbase >= cygheap_max) || (cygheap_max <= _cygheap_end))
|
2002-08-07 09:20:59 +08:00
|
|
|
/* nothing to do */;
|
2005-06-01 11:46:56 +08:00
|
|
|
else
|
2002-10-09 12:08:05 +08:00
|
|
|
{
|
2005-06-01 11:46:56 +08:00
|
|
|
if (prebrk <= _cygheap_end)
|
|
|
|
newbase = _cygheap_end;
|
|
|
|
|
|
|
|
DWORD adjsbs = allocsize ((char *) cygheap_max - newbase);
|
2007-12-11 23:16:19 +08:00
|
|
|
if (adjsbs && !VirtualAlloc (newbase, adjsbs, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE))
|
2005-06-01 11:46:56 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2002-10-09 12:08:05 +08:00
|
|
|
}
|
2000-09-30 12:54:11 +08:00
|
|
|
|
2002-07-14 04:00:27 +08:00
|
|
|
return prebrk;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2009-08-02 03:52:46 +08:00
|
|
|
void __stdcall
|
2000-11-14 13:53:32 +08:00
|
|
|
cygheap_init ()
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.init ("cygheap_protect");
|
2009-06-08 11:53:40 +08:00
|
|
|
if (cygheap == &cygheap_at_start)
|
2002-08-07 09:20:59 +08:00
|
|
|
{
|
2007-03-30 00:37:36 +08:00
|
|
|
cygheap = (init_cygheap *) memset (_cygheap_start, 0,
|
2011-02-12 00:05:26 +08:00
|
|
|
sizeof (*cygheap));
|
2005-06-01 11:46:56 +08:00
|
|
|
cygheap_max = cygheap;
|
2005-07-07 04:05:03 +08:00
|
|
|
_csbrk (sizeof (*cygheap));
|
2009-05-15 03:49:37 +08:00
|
|
|
/* Default locale settings. */
|
|
|
|
cygheap->locale.mbtowc = __utf8_mbtowc;
|
|
|
|
cygheap->locale.wctomb = __utf8_wctomb;
|
2009-09-28 18:43:49 +08:00
|
|
|
strcpy (cygheap->locale.charset, "UTF-8");
|
2009-05-06 19:52:20 +08:00
|
|
|
/* Set umask to a sane default. */
|
|
|
|
cygheap->umask = 022;
|
2010-04-20 18:44:52 +08:00
|
|
|
cygheap->rlim_core = RLIM_INFINITY;
|
2002-08-07 09:20:59 +08:00
|
|
|
}
|
2001-04-19 05:10:15 +08:00
|
|
|
if (!cygheap->fdtab)
|
|
|
|
cygheap->fdtab.init ();
|
2003-09-01 10:05:32 +08:00
|
|
|
if (!cygheap->sigs)
|
|
|
|
sigalloc ();
|
2000-11-14 13:53:32 +08:00
|
|
|
}
|
|
|
|
|
2000-09-03 12:16:35 +08:00
|
|
|
/* Copyright (C) 1997, 2000 DJ Delorie */
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
static void *__stdcall _cmalloc (unsigned size) __attribute__ ((regparm(1)));
|
|
|
|
static void *__stdcall _crealloc (void *ptr, unsigned size) __attribute__ ((regparm(2)));
|
2000-10-17 07:55:58 +08:00
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
static void *__stdcall __attribute__ ((regparm(1)))
|
2003-01-18 11:26:07 +08:00
|
|
|
_cmalloc (unsigned size)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc;
|
2001-07-19 01:05:34 +08:00
|
|
|
unsigned b, sz;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
/* Calculate "bit bucket" and size as a power of two. */
|
2003-01-18 11:26:07 +08:00
|
|
|
for (b = 3, sz = 8; sz && sz < size; b++, sz <<= 1)
|
2000-10-17 07:55:58 +08:00
|
|
|
continue;
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.acquire ();
|
2001-09-06 11:39:18 +08:00
|
|
|
if (cygheap->buckets[b])
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc = (_cmalloc_entry *) cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = rvc->ptr;
|
2000-09-30 12:54:11 +08:00
|
|
|
rvc->b = b;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
2000-10-02 10:26:04 +08:00
|
|
|
else
|
|
|
|
{
|
2003-01-18 11:26:07 +08:00
|
|
|
rvc = (_cmalloc_entry *) _csbrk (sz + sizeof (_cmalloc_entry));
|
2002-10-09 12:08:05 +08:00
|
|
|
if (!rvc)
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2002-10-09 12:08:05 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
|
2000-10-02 10:26:04 +08:00
|
|
|
rvc->b = b;
|
2000-11-14 13:53:32 +08:00
|
|
|
rvc->prev = cygheap->chain;
|
|
|
|
cygheap->chain = rvc;
|
2000-10-02 10:26:04 +08:00
|
|
|
}
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2000-09-30 12:54:11 +08:00
|
|
|
return rvc->data;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
static void __stdcall __attribute__ ((regparm(1)))
|
2000-09-03 12:16:35 +08:00
|
|
|
_cfree (void *ptr)
|
|
|
|
{
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.acquire ();
|
2000-09-30 12:54:11 +08:00
|
|
|
_cmalloc_entry *rvc = to_cmalloc (ptr);
|
|
|
|
DWORD b = rvc->b;
|
2001-09-06 11:39:18 +08:00
|
|
|
rvc->ptr = cygheap->buckets[b];
|
|
|
|
cygheap->buckets[b] = (char *) rvc;
|
2005-04-05 12:31:00 +08:00
|
|
|
cygheap_protect.release ();
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
static void *__stdcall __attribute__ ((regparm(2)))
|
2003-01-18 11:26:07 +08:00
|
|
|
_crealloc (void *ptr, unsigned size)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2000-09-05 11:16:28 +08:00
|
|
|
void *newptr;
|
|
|
|
if (ptr == NULL)
|
|
|
|
newptr = _cmalloc (size);
|
|
|
|
else
|
|
|
|
{
|
2003-01-18 11:26:07 +08:00
|
|
|
unsigned oldsize = 1 << to_cmalloc (ptr)->b;
|
2000-09-05 11:16:28 +08:00
|
|
|
if (size <= oldsize)
|
|
|
|
return ptr;
|
|
|
|
newptr = _cmalloc (size);
|
2007-11-24 00:37:05 +08:00
|
|
|
if (newptr)
|
|
|
|
{
|
|
|
|
memcpy (newptr, ptr, oldsize);
|
|
|
|
_cfree (ptr);
|
|
|
|
}
|
2000-09-05 11:16:28 +08:00
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
return newptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* End Copyright (C) 1997 DJ Delorie */
|
|
|
|
|
2002-09-23 08:31:31 +08:00
|
|
|
#define sizeof_cygheap(n) ((n) + sizeof (cygheap_entry))
|
2000-09-03 12:16:35 +08:00
|
|
|
|
|
|
|
#define N ((cygheap_entry *) NULL)
|
|
|
|
#define tocygheap(s) ((cygheap_entry *) (((char *) (s)) - (int) (N->data)))
|
|
|
|
|
2000-10-17 07:55:58 +08:00
|
|
|
inline static void *
|
2007-11-27 05:30:49 +08:00
|
|
|
creturn (cygheap_types x, cygheap_entry * c, unsigned len, const char *fn = NULL)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2008-09-11 12:34:24 +08:00
|
|
|
if (c)
|
|
|
|
/* nothing to do */;
|
|
|
|
else if (fn)
|
|
|
|
api_fatal ("%s would have returned NULL", fn);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
set_errno (ENOMEM);
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-09-03 12:16:35 +08:00
|
|
|
c->type = x;
|
2001-02-02 12:32:02 +08:00
|
|
|
char *cend = ((char *) c + sizeof (*c) + len);
|
|
|
|
if (cygheap_max < cend)
|
|
|
|
cygheap_max = cend;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
return (void *) c->data;
|
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
inline static void *
|
|
|
|
cmalloc (cygheap_types x, DWORD n, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (x, c, n, fn);
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
extern "C" void *
|
|
|
|
cmalloc (cygheap_types x, DWORD n)
|
|
|
|
{
|
|
|
|
return cmalloc (x, n, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void *
|
|
|
|
cmalloc_abort (cygheap_types x, DWORD n)
|
|
|
|
{
|
|
|
|
return cmalloc (x, n, "cmalloc");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static void *
|
|
|
|
crealloc (void *s, DWORD n, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
if (s == NULL)
|
|
|
|
return cmalloc (HEAP_STR, n); // kludge
|
|
|
|
|
|
|
|
assert (!inheap (s));
|
|
|
|
cygheap_entry *c = tocygheap (s);
|
2000-09-30 12:54:11 +08:00
|
|
|
cygheap_types t = (cygheap_types) c->type;
|
2000-09-03 12:16:35 +08:00
|
|
|
c = (cygheap_entry *) _crealloc (c, sizeof_cygheap (n));
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (t, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void *__stdcall __attribute__ ((regparm(2)))
|
2007-11-27 05:30:49 +08:00
|
|
|
crealloc (void *s, DWORD n)
|
|
|
|
{
|
|
|
|
return crealloc (s, n, NULL);
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void *__stdcall __attribute__ ((regparm(2)))
|
2007-11-27 05:30:49 +08:00
|
|
|
crealloc_abort (void *s, DWORD n)
|
|
|
|
{
|
|
|
|
return crealloc (s, n, "crealloc");
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void __stdcall __attribute__ ((regparm(1)))
|
2000-09-03 12:16:35 +08:00
|
|
|
cfree (void *s)
|
|
|
|
{
|
|
|
|
assert (!inheap (s));
|
2005-07-07 04:05:03 +08:00
|
|
|
_cfree (tocygheap (s));
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void __stdcall __attribute__ ((regparm(2)))
|
2002-06-28 04:44:27 +08:00
|
|
|
cfree_and_set (char *&s, char *what)
|
|
|
|
{
|
|
|
|
if (s && s != almost_null)
|
|
|
|
cfree (s);
|
|
|
|
s = what;
|
|
|
|
}
|
|
|
|
|
2007-11-27 05:30:49 +08:00
|
|
|
inline static void *
|
|
|
|
ccalloc (cygheap_types x, DWORD n, DWORD size, const char *fn)
|
2000-09-03 12:16:35 +08:00
|
|
|
{
|
|
|
|
cygheap_entry *c;
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2001-09-14 05:15:34 +08:00
|
|
|
n *= size;
|
|
|
|
c = (cygheap_entry *) _cmalloc (sizeof_cygheap (n));
|
2000-09-03 12:16:35 +08:00
|
|
|
if (c)
|
2001-09-14 05:15:34 +08:00
|
|
|
memset (c->data, 0, n);
|
2007-11-27 05:30:49 +08:00
|
|
|
return creturn (x, c, n, fn);
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void *__stdcall __attribute__ ((regparm(3)))
|
2007-11-27 05:30:49 +08:00
|
|
|
ccalloc (cygheap_types x, DWORD n, DWORD size)
|
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, NULL);
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" void *__stdcall __attribute__ ((regparm(3)))
|
2007-11-27 05:30:49 +08:00
|
|
|
ccalloc_abort (cygheap_types x, DWORD n, DWORD size)
|
|
|
|
{
|
|
|
|
return ccalloc (x, n, size, "ccalloc");
|
2000-09-03 12:16:35 +08:00
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" PWCHAR __stdcall __attribute__ ((regparm(1)))
|
2008-02-15 00:47:11 +08:00
|
|
|
cwcsdup (const PWCHAR s)
|
|
|
|
{
|
|
|
|
MALLOC_CHECK;
|
2010-04-13 18:01:34 +08:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-15 00:47:11 +08:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
MALLOC_CHECK;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" PWCHAR __stdcall __attribute__ ((regparm(1)))
|
2008-02-15 00:47:11 +08:00
|
|
|
cwcsdup1 (const PWCHAR s)
|
|
|
|
{
|
|
|
|
MALLOC_CHECK;
|
2010-04-13 18:01:34 +08:00
|
|
|
PWCHAR p = (PWCHAR) cmalloc (HEAP_1_STR, (wcslen (s) + 1) * sizeof (WCHAR));
|
2008-02-15 00:47:11 +08:00
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
wcpcpy (p, s);
|
|
|
|
MALLOC_CHECK;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" char *__stdcall __attribute__ ((regparm(1)))
|
2000-09-03 12:16:35 +08:00
|
|
|
cstrdup (const char *s)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
char *p = (char *) cmalloc (HEAP_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-03 12:16:35 +08:00
|
|
|
return p;
|
|
|
|
}
|
2000-09-27 13:12:09 +08:00
|
|
|
|
2011-02-10 10:22:36 +08:00
|
|
|
extern "C" char *__stdcall __attribute__ ((regparm(1)))
|
2000-09-27 13:12:09 +08:00
|
|
|
cstrdup1 (const char *s)
|
|
|
|
{
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 13:12:09 +08:00
|
|
|
char *p = (char *) cmalloc (HEAP_1_STR, strlen (s) + 1);
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
strcpy (p, s);
|
2000-10-02 10:26:04 +08:00
|
|
|
MALLOC_CHECK;
|
2000-09-27 13:12:09 +08:00
|
|
|
return p;
|
|
|
|
}
|
2000-11-15 08:13:09 +08:00
|
|
|
|
2001-06-03 10:31:16 +08:00
|
|
|
void
|
2008-07-17 04:20:45 +08:00
|
|
|
cygheap_root::set (const char *posix, const char *native, bool caseinsensitive)
|
2000-11-15 08:13:09 +08:00
|
|
|
{
|
2001-08-07 08:01:42 +08:00
|
|
|
if (*posix == '/' && posix[1] == '\0')
|
|
|
|
{
|
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
cfree (m);
|
|
|
|
m = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2001-06-03 10:31:16 +08:00
|
|
|
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';
|
2008-07-17 04:20:45 +08:00
|
|
|
m->caseinsensitive = caseinsensitive;
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cygheap_user::~cygheap_user ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cygheap_user::set_name (const char *new_name)
|
|
|
|
{
|
2002-06-27 03:39:05 +08:00
|
|
|
bool allocated = !!pname;
|
2002-06-27 10:29:57 +08:00
|
|
|
|
2002-06-27 03:39:05 +08:00
|
|
|
if (allocated)
|
2002-06-27 10:29:57 +08:00
|
|
|
{
|
2010-06-01 02:52:02 +08:00
|
|
|
/* Windows user names are case-insensitive. Here we want the correct
|
|
|
|
username, though, even if it only differs by case. */
|
|
|
|
if (!strcmp (new_name, pname))
|
2002-06-27 10:29:57 +08:00
|
|
|
return;
|
|
|
|
cfree (pname);
|
|
|
|
}
|
2002-06-27 03:39:05 +08:00
|
|
|
|
2000-11-15 08:13:09 +08:00
|
|
|
pname = cstrdup (new_name ? new_name : "");
|
2002-06-27 03:39:05 +08:00
|
|
|
if (!allocated)
|
|
|
|
return; /* Initializing. Don't bother with other stuff. */
|
|
|
|
|
2002-06-28 04:44:27 +08:00
|
|
|
cfree_and_set (homedrive);
|
|
|
|
cfree_and_set (homepath);
|
|
|
|
cfree_and_set (plogsrv);
|
|
|
|
cfree_and_set (pdomain);
|
2002-06-29 10:36:08 +08:00
|
|
|
cfree_and_set (pwinname);
|
2000-11-15 08:13:09 +08:00
|
|
|
}
|