* cygheap.h (user_heap_info::sbrk): Declare new function.
(user_heap_info::init): Ditto. * heap.cc (user_heap_info::init): Rename from heap_init(). Avoid explictly using cygheap->user_heap. (sbrk): Use user_heap_info method via cygheap->user_heap. (user_heap_info::sbrk): Renamed from sbrk(). Eliminate explicit use of cygheap->user_heap. Change some pointer arithmetic to use (char *) for consistency. * shared.cc (shared_info::initialize): Change heap_init call to cygheap->user_heap.init.
This commit is contained in:
parent
9f0de58c22
commit
6fc77d3e75
@ -1,3 +1,16 @@
|
|||||||
|
2013-08-30 Christopher Faylor <me.cygwin2013@cgf.cx>
|
||||||
|
|
||||||
|
* cygheap.h (user_heap_info::sbrk): Declare new function.
|
||||||
|
(user_heap_info::init): Ditto.
|
||||||
|
* heap.cc (user_heap_info::init): Rename from heap_init(). Avoid
|
||||||
|
explictly using cygheap->user_heap.
|
||||||
|
(sbrk): Use user_heap_info method via cygheap->user_heap.
|
||||||
|
(user_heap_info::sbrk): Renamed from sbrk(). Eliminate explicit use of
|
||||||
|
cygheap->user_heap. Change some pointer arithmetic to use (char *) for
|
||||||
|
consistency.
|
||||||
|
* shared.cc (shared_info::initialize): Change heap_init call to
|
||||||
|
cygheap->user_heap.init.
|
||||||
|
|
||||||
2013-08-30 Corinna Vinschen <corinna@vinschen.de>
|
2013-08-30 Corinna Vinschen <corinna@vinschen.de>
|
||||||
|
|
||||||
* heap.cc (sbrk): Add a FIXME comment to VirtualFree call. Fix memory
|
* heap.cc (sbrk): Add a FIXME comment to VirtualFree call. Fix memory
|
||||||
|
@ -351,6 +351,8 @@ struct user_heap_info
|
|||||||
void *top;
|
void *top;
|
||||||
void *max;
|
void *max;
|
||||||
SIZE_T chunk;
|
SIZE_T chunk;
|
||||||
|
void __reg2 *sbrk (ptrdiff_t);
|
||||||
|
void __reg1 init ();
|
||||||
};
|
};
|
||||||
|
|
||||||
struct hook_chain
|
struct hook_chain
|
||||||
|
@ -102,14 +102,14 @@ eval_initial_heap_size ()
|
|||||||
|
|
||||||
/* Initialize the heap at process start up. */
|
/* Initialize the heap at process start up. */
|
||||||
void
|
void
|
||||||
heap_init ()
|
user_heap_info::init ()
|
||||||
{
|
{
|
||||||
const DWORD alloctype = MEM_RESERVE;
|
const DWORD alloctype = MEM_RESERVE;
|
||||||
/* If we're the forkee, we must allocate the heap at exactly the same place
|
/* If we're the forkee, we must allocate the heap at exactly the same place
|
||||||
as our parent. If not, we (almost) don't care where it ends up. */
|
as our parent. If not, we (almost) don't care where it ends up. */
|
||||||
|
|
||||||
page_const = wincap.page_size ();
|
page_const = wincap.page_size ();
|
||||||
if (!cygheap->user_heap.base)
|
if (!base)
|
||||||
{
|
{
|
||||||
uintptr_t start_address = eval_start_address ();
|
uintptr_t start_address = eval_start_address ();
|
||||||
PVOID largest_found = NULL;
|
PVOID largest_found = NULL;
|
||||||
@ -117,13 +117,12 @@ heap_init ()
|
|||||||
SIZE_T ret;
|
SIZE_T ret;
|
||||||
MEMORY_BASIC_INFORMATION mbi;
|
MEMORY_BASIC_INFORMATION mbi;
|
||||||
|
|
||||||
cygheap->user_heap.chunk = eval_initial_heap_size ();
|
chunk = eval_initial_heap_size ();
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
cygheap->user_heap.base = VirtualAlloc ((LPVOID) start_address,
|
base = VirtualAlloc ((LPVOID) start_address, chunk, alloctype,
|
||||||
cygheap->user_heap.chunk,
|
PAGE_NOACCESS);
|
||||||
alloctype, PAGE_NOACCESS);
|
if (base)
|
||||||
if (cygheap->user_heap.base)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
/* Ok, so we are at the 1% which didn't work with 0x20000000 out
|
/* Ok, so we are at the 1% which didn't work with 0x20000000 out
|
||||||
@ -136,7 +135,7 @@ heap_init ()
|
|||||||
{
|
{
|
||||||
if (mbi.State == MEM_FREE)
|
if (mbi.State == MEM_FREE)
|
||||||
{
|
{
|
||||||
if (mbi.RegionSize >= cygheap->user_heap.chunk)
|
if (mbi.RegionSize >= chunk)
|
||||||
break;
|
break;
|
||||||
if (mbi.RegionSize > largest_found_size)
|
if (mbi.RegionSize > largest_found_size)
|
||||||
{
|
{
|
||||||
@ -158,40 +157,34 @@ heap_init ()
|
|||||||
region, unless it's smaller than the minimum size. */
|
region, unless it's smaller than the minimum size. */
|
||||||
if (largest_found_size >= MINHEAP_SIZE)
|
if (largest_found_size >= MINHEAP_SIZE)
|
||||||
{
|
{
|
||||||
cygheap->user_heap.chunk = largest_found_size;
|
chunk = largest_found_size;
|
||||||
cygheap->user_heap.base =
|
base = VirtualAlloc (largest_found, chunk, alloctype,
|
||||||
VirtualAlloc (largest_found, cygheap->user_heap.chunk,
|
PAGE_NOACCESS);
|
||||||
alloctype, PAGE_NOACCESS);
|
|
||||||
}
|
}
|
||||||
/* Last resort (but actually we are probably broken anyway):
|
/* Last resort (but actually we are probably broken anyway):
|
||||||
Use the minimal heap size and let the system decide. */
|
Use the minimal heap size and let the system decide. */
|
||||||
if (!cygheap->user_heap.base)
|
if (!base)
|
||||||
{
|
{
|
||||||
cygheap->user_heap.chunk = MINHEAP_SIZE;
|
chunk = MINHEAP_SIZE;
|
||||||
cygheap->user_heap.base =
|
base = VirtualAlloc (NULL, chunk, alloctype, PAGE_NOACCESS);
|
||||||
VirtualAlloc (NULL, cygheap->user_heap.chunk,
|
|
||||||
alloctype, PAGE_NOACCESS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while (!cygheap->user_heap.base && ret);
|
while (!base && ret);
|
||||||
if (cygheap->user_heap.base == NULL)
|
if (base == NULL)
|
||||||
api_fatal ("unable to allocate heap, heap_chunk_size %ly, %E",
|
api_fatal ("unable to allocate heap, heap_chunk_size %ly, %E",
|
||||||
cygheap->user_heap.chunk);
|
chunk);
|
||||||
cygheap->user_heap.ptr = cygheap->user_heap.top = cygheap->user_heap.base;
|
ptr = top = base;
|
||||||
cygheap->user_heap.max = (char *) cygheap->user_heap.base
|
max = (char *) base + chunk;
|
||||||
+ cygheap->user_heap.chunk;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SIZE_T chunk = cygheap->user_heap.chunk; /* allocation chunk */
|
|
||||||
/* total size commited in parent */
|
/* total size commited in parent */
|
||||||
SIZE_T allocsize = (char *) cygheap->user_heap.top -
|
SIZE_T allocsize = (char *) top - (char *) base;
|
||||||
(char *) cygheap->user_heap.base;
|
|
||||||
|
|
||||||
/* Loop until we've managed to reserve an adequate amount of memory. */
|
/* Loop until we've managed to reserve an adequate amount of memory. */
|
||||||
char *p;
|
|
||||||
SIZE_T reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
|
SIZE_T reserve_size = chunk * ((allocsize + (chunk - 1)) / chunk);
|
||||||
|
|
||||||
/* With ptmalloc3 there's a good chance that there has been no memory
|
/* 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
|
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
|
be 0 and from there, the below loop will end up overallocating due
|
||||||
@ -199,10 +192,11 @@ heap_init ()
|
|||||||
if (!reserve_size)
|
if (!reserve_size)
|
||||||
reserve_size = chunk;
|
reserve_size = chunk;
|
||||||
|
|
||||||
|
char *p;
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
p = (char *) VirtualAlloc (cygheap->user_heap.base, reserve_size,
|
p = (char *) VirtualAlloc (base, reserve_size, alloctype,
|
||||||
alloctype, PAGE_READWRITE);
|
PAGE_READWRITE);
|
||||||
if (p)
|
if (p)
|
||||||
break;
|
break;
|
||||||
if ((reserve_size -= page_const) < allocsize)
|
if ((reserve_size -= page_const) < allocsize)
|
||||||
@ -211,12 +205,12 @@ heap_init ()
|
|||||||
if (!p && in_forkee && !fork_info->abort (NULL))
|
if (!p && in_forkee && !fork_info->abort (NULL))
|
||||||
api_fatal ("couldn't allocate heap, %E, base %p, top %p, "
|
api_fatal ("couldn't allocate heap, %E, base %p, top %p, "
|
||||||
"reserve_size %ld, allocsize %ld, page_const %d",
|
"reserve_size %ld, allocsize %ld, page_const %d",
|
||||||
cygheap->user_heap.base, cygheap->user_heap.top,
|
base, top,
|
||||||
reserve_size, allocsize, page_const);
|
reserve_size, allocsize, page_const);
|
||||||
if (p != cygheap->user_heap.base)
|
if (p != base)
|
||||||
api_fatal ("heap allocated at wrong address %p (mapped) "
|
api_fatal ("heap allocated at wrong address %p (mapped) "
|
||||||
"!= %p (expected)", p, cygheap->user_heap.base);
|
"!= %p (expected)", p, base);
|
||||||
if (allocsize && !VirtualAlloc (cygheap->user_heap.base, allocsize,
|
if (allocsize && !VirtualAlloc (base, allocsize,
|
||||||
MEM_COMMIT, PAGE_READWRITE))
|
MEM_COMMIT, PAGE_READWRITE))
|
||||||
api_fatal ("MEM_COMMIT failed, %E");
|
api_fatal ("MEM_COMMIT failed, %E");
|
||||||
}
|
}
|
||||||
@ -228,39 +222,44 @@ heap_init ()
|
|||||||
heap_init is called early, the heap size is printed pretty much at the
|
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. */
|
start of the strace output, so there isn't anything lost. */
|
||||||
debug_printf ("heap base %p, heap top %p, heap size %ly (%lu)",
|
debug_printf ("heap base %p, heap top %p, heap size %ly (%lu)",
|
||||||
cygheap->user_heap.base, cygheap->user_heap.top,
|
base, top, chunk, chunk);
|
||||||
cygheap->user_heap.chunk, cygheap->user_heap.chunk);
|
|
||||||
page_const--;
|
page_const--;
|
||||||
// malloc_init ();
|
// malloc_init ();
|
||||||
}
|
}
|
||||||
|
|
||||||
#define pround(n) (((size_t)(n) + page_const) & ~page_const)
|
#define pround(n) (((size_t)(n) + page_const) & ~page_const)
|
||||||
|
|
||||||
/* FIXME: This function no longer handles "split heaps". */
|
|
||||||
|
|
||||||
/* Linux defines n to be intptr_t, newlib defines it to be ptrdiff_t.
|
/* 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
|
It shouldn't matter much, though, since the function is not standarized
|
||||||
and sizeof(ptrdiff_t) == sizeof(intptr_t) anyway. */
|
and sizeof(ptrdiff_t) == sizeof(intptr_t) anyway. */
|
||||||
extern "C" void *
|
extern "C" void *
|
||||||
sbrk (ptrdiff_t n)
|
sbrk (ptrdiff_t n)
|
||||||
{
|
{
|
||||||
|
return cygheap->user_heap.sbrk (n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void __reg2 *
|
||||||
|
user_heap_info::sbrk (ptrdiff_t n)
|
||||||
|
{
|
||||||
|
/* FIXME: This function no longer handles "split heaps". */
|
||||||
|
|
||||||
char *newtop, *newbrk;
|
char *newtop, *newbrk;
|
||||||
SIZE_T commitbytes, newbrksize, reservebytes;
|
SIZE_T commitbytes, newbrksize, reservebytes;
|
||||||
|
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
return cygheap->user_heap.ptr; /* Just wanted to find current cygheap->user_heap.ptr address */
|
return ptr; /* Just wanted to find current ptr
|
||||||
|
address */
|
||||||
|
|
||||||
newbrk = (char *) cygheap->user_heap.ptr + n; /* Where new cygheap->user_heap.ptr will be */
|
newbrk = (char *) ptr + n; /* Where new cptr will be */
|
||||||
newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
|
newtop = (char *) pround (newbrk); /* Actual top of allocated memory -
|
||||||
on page boundary */
|
on page boundary */
|
||||||
|
|
||||||
if (newtop == cygheap->user_heap.top)
|
if (newtop == top)
|
||||||
goto good;
|
goto good;
|
||||||
|
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
{ /* Freeing memory */
|
{ /* Freeing memory */
|
||||||
assert (newtop < cygheap->user_heap.top);
|
assert (newtop < top);
|
||||||
n = (char *) cygheap->user_heap.top - newtop;
|
n = (char *) top - newtop;
|
||||||
/* FIXME: This doesn't work if we cross a virtual memory reservation
|
/* FIXME: This doesn't work if we cross a virtual memory reservation
|
||||||
border. If that happens, we have to free the space in multiple
|
border. If that happens, we have to free the space in multiple
|
||||||
VirtualFree calls, aligned to the former reservation borders. */
|
VirtualFree calls, aligned to the former reservation borders. */
|
||||||
@ -269,59 +268,52 @@ sbrk (ptrdiff_t n)
|
|||||||
goto err; /* Didn't take */
|
goto err; /* Didn't take */
|
||||||
}
|
}
|
||||||
|
|
||||||
assert (newtop > cygheap->user_heap.top);
|
assert (newtop > top);
|
||||||
|
|
||||||
/* Find the number of bytes to commit, rounded up to the nearest page. */
|
/* Find the number of bytes to commit, rounded up to the nearest page. */
|
||||||
commitbytes = pround (newtop - (char *) cygheap->user_heap.top);
|
commitbytes = pround (newtop - (char *) top);
|
||||||
|
|
||||||
/* Need to grab more pages from the OS. If this fails it may be because
|
/* 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
|
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
|
of memory. Only attempt to commit memory that we know we've previously
|
||||||
reserved. */
|
reserved. */
|
||||||
if (newtop <= cygheap->user_heap.max)
|
if (newtop <= max && VirtualAlloc (top, commitbytes, MEM_COMMIT,
|
||||||
{
|
PAGE_READWRITE))
|
||||||
if (VirtualAlloc (cygheap->user_heap.top, commitbytes,
|
goto good;
|
||||||
MEM_COMMIT, PAGE_READWRITE) != NULL)
|
|
||||||
goto good;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The remainder of the existing heap is too small to fulfil the memory
|
/* The remainder of the existing heap is too small to fulfill the memory
|
||||||
request. We have to extend the heap, so we reserve some more memory
|
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
|
and then commit the remainder of the old heap, if any, and the rest of
|
||||||
the required space from the extended heap. */
|
the required space from the extended heap. */
|
||||||
|
|
||||||
/* Reserve either the maximum of the standard heap chunk size
|
/* Reserve either the maximum of the standard heap chunk size
|
||||||
or the requested amount. Then attempt to actually allocate it. */
|
or the requested amount. Then attempt to actually allocate it. */
|
||||||
reservebytes = commitbytes - ((ptrdiff_t) cygheap->user_heap.max
|
reservebytes = commitbytes - ((char *) max - (char *) top);
|
||||||
- (ptrdiff_t) cygheap->user_heap.top);
|
|
||||||
commitbytes -= reservebytes;
|
commitbytes -= reservebytes;
|
||||||
if ((newbrksize = cygheap->user_heap.chunk) < reservebytes)
|
if ((newbrksize = chunk) < reservebytes)
|
||||||
newbrksize = reservebytes;
|
newbrksize = reservebytes;
|
||||||
|
|
||||||
if (VirtualAlloc (cygheap->user_heap.max, newbrksize,
|
if (VirtualAlloc (max, newbrksize, MEM_RESERVE, PAGE_NOACCESS)
|
||||||
MEM_RESERVE, PAGE_NOACCESS)
|
|| VirtualAlloc (max, newbrksize = reservebytes, MEM_RESERVE,
|
||||||
|| VirtualAlloc (cygheap->user_heap.max, newbrksize = reservebytes,
|
PAGE_NOACCESS))
|
||||||
MEM_RESERVE, PAGE_NOACCESS))
|
|
||||||
{
|
{
|
||||||
/* Now commit the requested memory. Windows keeps all virtual
|
/* Now commit the requested memory. Windows keeps all virtual
|
||||||
reservations separate, so we can't commit the two regions in a single,
|
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
|
combined call or we suffer an ERROR_INVALID_ADDRESS. The same error
|
||||||
is returned when trying to VirtualAlloc 0 bytes, which would occur if
|
is returned when trying to VirtualAlloc 0 bytes, which would occur if
|
||||||
the existing heap was already full. */
|
the existing heap was already full. */
|
||||||
if ((!commitbytes || VirtualAlloc (cygheap->user_heap.top, commitbytes,
|
if ((!commitbytes || VirtualAlloc (top, commitbytes, MEM_COMMIT,
|
||||||
MEM_COMMIT, PAGE_READWRITE))
|
PAGE_READWRITE))
|
||||||
&& VirtualAlloc (cygheap->user_heap.max, reservebytes,
|
&& VirtualAlloc (max, reservebytes, MEM_COMMIT, PAGE_READWRITE))
|
||||||
MEM_COMMIT, PAGE_READWRITE))
|
|
||||||
{
|
{
|
||||||
cygheap->user_heap.max = (char *) cygheap->user_heap.max
|
max = (char *) max + pround (newbrksize);
|
||||||
+ pround (newbrksize);
|
|
||||||
goto good;
|
goto good;
|
||||||
}
|
}
|
||||||
/* If committing the memory failed, we must free the extendend reserved
|
/* If committing the memory failed, we must free the extendend reserved
|
||||||
region, otherwise any other try to fetch memory (for instance by using
|
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
|
mmap) may fail just because we still reserve memory we don't even know
|
||||||
about. */
|
about. */
|
||||||
VirtualFree (cygheap->user_heap.max, newbrksize, MEM_RELEASE);
|
VirtualFree (max, newbrksize, MEM_RELEASE);
|
||||||
}
|
}
|
||||||
|
|
||||||
err:
|
err:
|
||||||
@ -329,8 +321,8 @@ err:
|
|||||||
return (void *) -1;
|
return (void *) -1;
|
||||||
|
|
||||||
good:
|
good:
|
||||||
void *oldbrk = cygheap->user_heap.ptr;
|
void *oldbrk = ptr;
|
||||||
cygheap->user_heap.ptr = newbrk;
|
ptr = newbrk;
|
||||||
cygheap->user_heap.top = newtop;
|
top = newtop;
|
||||||
return oldbrk;
|
return oldbrk;
|
||||||
}
|
}
|
||||||
|
@ -341,7 +341,8 @@ shared_info::initialize ()
|
|||||||
else if (cb != sizeof (*this))
|
else if (cb != sizeof (*this))
|
||||||
system_printf ("size of shared memory region changed from %lu to %u",
|
system_printf ("size of shared memory region changed from %lu to %u",
|
||||||
sizeof (*this), cb);
|
sizeof (*this), cb);
|
||||||
heap_init ();
|
/* FIXME? Shouldn't this be in memory_init? */
|
||||||
|
cygheap->user_heap.init ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
Loading…
x
Reference in New Issue
Block a user