2005-09-08 00:09:32 +00:00
|
|
|
/* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* FILE: lib/rtl/image.c
|
|
|
|
* PURPOSE: Image handling functions
|
|
|
|
* PROGRAMMERS: Copyright 1996 Alexandre Julliard
|
|
|
|
* Copyright 1998 Ulrich Weigand
|
2004-05-31 19:33:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Note: the heap data structures are based on what Pietrek describes in his
|
|
|
|
* book 'Windows 95 System Programming Secrets'. The layout is not exactly
|
|
|
|
* the same, but could be easily adapted if it turns out some programs
|
|
|
|
* require it.
|
|
|
|
*/
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* INCLUDES *****************************************************************/
|
|
|
|
|
2005-07-26 08:39:07 +00:00
|
|
|
#include <rtl.h>
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
2005-09-08 00:09:32 +00:00
|
|
|
/* FUNCTIONS *****************************************************************/
|
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
#define WARN_ON(x) (1)
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
#define TRACE_ON(x) (0)
|
|
|
|
#else
|
|
|
|
#define TRACE_ON(x) (1)
|
|
|
|
#endif
|
|
|
|
|
2004-12-31 20:05:16 +00:00
|
|
|
static RTL_CRITICAL_SECTION RtlpProcessHeapsListLock;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct tagARENA_INUSE
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size; /* Block size; must be the first field */
|
|
|
|
USHORT threadId; /* Allocating thread id */
|
|
|
|
USHORT magic; /* Magic number */
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
ARENA_INUSE;
|
|
|
|
|
|
|
|
typedef struct tagARENA_FREE
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size; /* Block size; must be the first field */
|
|
|
|
USHORT threadId; /* Freeing thread id */
|
|
|
|
USHORT magic; /* Magic number */
|
2004-05-31 19:33:59 +00:00
|
|
|
struct tagARENA_FREE *next; /* Next free arena */
|
|
|
|
struct tagARENA_FREE *prev; /* Prev free arena */
|
|
|
|
}
|
|
|
|
ARENA_FREE;
|
|
|
|
|
|
|
|
#define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
|
|
|
|
#define ARENA_FLAG_PREV_FREE 0x00000002
|
|
|
|
#define ARENA_SIZE_MASK 0xfffffffc
|
|
|
|
#define ARENA_INUSE_MAGIC 0x4842 /* Value for arena 'magic' field */
|
|
|
|
#define ARENA_FREE_MAGIC 0x4846 /* Value for arena 'magic' field */
|
|
|
|
|
|
|
|
#define ARENA_INUSE_FILLER 0x55
|
|
|
|
#define ARENA_FREE_FILLER 0xaa
|
|
|
|
|
|
|
|
#define QUIET 1 /* Suppress messages */
|
|
|
|
#define NOISY 0 /* Report all errors */
|
|
|
|
|
|
|
|
#define HEAP_NB_FREE_LISTS 4 /* Number of free lists */
|
|
|
|
|
|
|
|
/* Max size of the blocks on the free lists */
|
2004-11-25 19:25:06 +00:00
|
|
|
static const ULONG HEAP_freeListSizes[HEAP_NB_FREE_LISTS] =
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
0x20, 0x80, 0x200, 0xffffffff
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size;
|
2004-05-31 19:33:59 +00:00
|
|
|
ARENA_FREE arena;
|
|
|
|
}
|
|
|
|
FREE_LIST_ENTRY;
|
|
|
|
|
|
|
|
struct tagHEAP;
|
|
|
|
|
|
|
|
typedef struct tagSUBHEAP
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size; /* Size of the whole sub-heap */
|
|
|
|
ULONG commitSize; /* Committed size of the sub-heap */
|
|
|
|
ULONG headerSize; /* Size of the heap header */
|
2004-05-31 19:33:59 +00:00
|
|
|
struct tagSUBHEAP *next; /* Next sub-heap */
|
|
|
|
struct tagHEAP *heap; /* Main heap structure */
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG magic; /* Magic number */
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
SUBHEAP, *PSUBHEAP;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
#define SUBHEAP_MAGIC ((ULONG)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
typedef struct tagHEAP
|
|
|
|
{
|
|
|
|
SUBHEAP subheap; /* First sub-heap */
|
|
|
|
struct tagHEAP *next; /* Next heap for this process */
|
|
|
|
FREE_LIST_ENTRY freeList[HEAP_NB_FREE_LISTS]; /* Free lists */
|
2004-12-31 20:05:16 +00:00
|
|
|
RTL_CRITICAL_SECTION critSection; /* Critical section for serialization */
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG flags; /* Heap flags */
|
|
|
|
ULONG magic; /* Magic number */
|
2005-08-01 10:19:22 +00:00
|
|
|
PRTL_HEAP_COMMIT_ROUTINE commitRoutine;
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
HEAP, *PHEAP;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
#define HEAP_MAGIC ((ULONG)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
#define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
|
|
|
|
#define HEAP_MIN_BLOCK_SIZE (sizeof(ARENA_FREE) + 8) /* Min. heap block size */
|
|
|
|
#define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
|
|
|
|
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_IsRealArena( HANDLE heap, ULONG flags, PVOID block, BOOLEAN quiet );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Dump
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
VOID
|
2004-05-31 19:33:59 +00:00
|
|
|
HEAP_Dump(PHEAP heap)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
char *ptr;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "Heap: %p\n", heap );
|
|
|
|
DPRINT( "Next: %p Sub-heaps: %p",
|
|
|
|
heap->next, &heap->subheap );
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap = &heap->subheap;
|
|
|
|
while (subheap->next)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( " -> %p", subheap->next );
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap = subheap->next;
|
|
|
|
}
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "\nFree lists:\n Block Stat Size Id\n" );
|
2004-05-31 19:33:59 +00:00
|
|
|
for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "%p free %08lx %04x prev=%p next=%p\n",
|
|
|
|
&heap->freeList[i].arena,
|
|
|
|
heap->freeList[i].arena.size,
|
|
|
|
heap->freeList[i].arena.threadId,
|
|
|
|
heap->freeList[i].arena.prev,
|
|
|
|
heap->freeList[i].arena.next );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
subheap = &heap->subheap;
|
|
|
|
while (subheap)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
|
|
|
|
DPRINT( "\n\nSub-heap %p: size=%08lx committed=%08lx\n",
|
|
|
|
subheap, subheap->size, subheap->commitSize );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "\n Block Stat Size Id\n" );
|
2004-05-31 19:33:59 +00:00
|
|
|
ptr = (char*)subheap + subheap->headerSize;
|
|
|
|
while (ptr < (char *)subheap + subheap->size)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (*(PULONG)ptr & ARENA_FLAG_FREE)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ARENA_FREE *pArena = (ARENA_FREE *)ptr;
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "%p free %08lx %04x prev=%p next=%p\n",
|
|
|
|
pArena, pArena->size & ARENA_SIZE_MASK,
|
|
|
|
pArena->threadId, pArena->prev,
|
|
|
|
pArena->next);
|
2004-05-31 19:33:59 +00:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_FREE);
|
|
|
|
freeSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
else if (*(PULONG)ptr & ARENA_FLAG_PREV_FREE)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "%p Used %08lx %04x back=%08lx\n",
|
|
|
|
pArena, pArena->size & ARENA_SIZE_MASK,
|
|
|
|
pArena->threadId, *((PULONG)pArena - 1));
|
2004-05-31 19:33:59 +00:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_INUSE);
|
|
|
|
usedSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "%p used %08lx %04x\n",
|
|
|
|
pArena, pArena->size & ARENA_SIZE_MASK,
|
|
|
|
pArena->threadId);
|
2004-05-31 19:33:59 +00:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_INUSE);
|
|
|
|
usedSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap->size, subheap->commitSize, freeSize, usedSize,
|
|
|
|
arenaSize, (arenaSize * 100) / subheap->size );
|
|
|
|
subheap = subheap->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_GetPtr
|
|
|
|
* RETURNS
|
|
|
|
* Pointer to the heap
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
static PHEAP
|
|
|
|
HEAP_GetPtr(HANDLE heap) /* [in] Handle to the heap */
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = (HEAP *)heap;
|
|
|
|
if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
|
|
|
|
{
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("Invalid heap %p!\n", heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (TRACE_ON(heap) && !HEAP_IsRealArena( heap, 0, NULL, NOISY ))
|
|
|
|
{
|
|
|
|
HEAP_Dump( heapPtr );
|
2004-11-25 19:25:06 +00:00
|
|
|
ASSERT( FALSE );
|
2004-05-31 19:33:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return heapPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_InsertFreeBlock
|
|
|
|
*
|
|
|
|
* Insert a free block into the free list.
|
|
|
|
*/
|
|
|
|
static VOID
|
|
|
|
HEAP_InsertFreeBlock(PHEAP heap,
|
|
|
|
ARENA_FREE *pArena,
|
2004-11-25 19:25:06 +00:00
|
|
|
BOOLEAN last)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
FREE_LIST_ENTRY *pEntry = heap->freeList;
|
|
|
|
while (pEntry->size < pArena->size)
|
|
|
|
pEntry++;
|
|
|
|
if (last)
|
|
|
|
{
|
|
|
|
/* insert at end of free list, i.e. before next free list entry */
|
|
|
|
pEntry++;
|
|
|
|
if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS])
|
|
|
|
{
|
|
|
|
pEntry = heap->freeList;
|
|
|
|
}
|
|
|
|
pArena->prev = pEntry->arena.prev;
|
|
|
|
pArena->prev->next = pArena;
|
|
|
|
pArena->next = &pEntry->arena;
|
|
|
|
pEntry->arena.prev = pArena;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* insert at head of free list */
|
|
|
|
pArena->next = pEntry->arena.next;
|
|
|
|
pArena->next->prev = pArena;
|
|
|
|
pArena->prev = &pEntry->arena;
|
|
|
|
pEntry->arena.next = pArena;
|
|
|
|
}
|
|
|
|
pArena->size |= ARENA_FLAG_FREE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_FindSubHeap
|
|
|
|
* Find the sub-heap containing a given address.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Pointer: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
static PSUBHEAP
|
|
|
|
HEAP_FindSubHeap(HEAP *heap, /* [in] Heap pointer */
|
2004-11-25 19:25:06 +00:00
|
|
|
PVOID ptr) /* [in] Address */
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
PSUBHEAP sub = &heap->subheap;
|
2004-05-31 19:33:59 +00:00
|
|
|
while (sub)
|
|
|
|
{
|
|
|
|
if (((char *)ptr >= (char *)sub) &&
|
|
|
|
((char *)ptr < (char *)sub + sub->size))
|
|
|
|
return sub;
|
|
|
|
sub = sub->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Commit
|
|
|
|
*
|
|
|
|
* Make sure the heap storage is committed up to (not including) ptr.
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
static __inline BOOLEAN
|
2004-05-31 19:33:59 +00:00
|
|
|
HEAP_Commit(SUBHEAP *subheap,
|
2004-11-25 19:25:06 +00:00
|
|
|
PVOID ptr,
|
|
|
|
ULONG flags)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2005-11-28 23:43:40 +00:00
|
|
|
SIZE_T size = (SIZE_T)((char *)ptr - (char *)subheap);
|
2004-05-31 19:33:59 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
PVOID address;
|
2005-11-28 23:43:40 +00:00
|
|
|
SIZE_T commitsize;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
size = (size + COMMIT_MASK) & ~COMMIT_MASK;
|
|
|
|
if (size > subheap->size)
|
|
|
|
size = subheap->size;
|
|
|
|
if (size <= subheap->commitSize)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
address = (PVOID)((char *)subheap + subheap->commitSize);
|
|
|
|
commitsize = size - subheap->commitSize;
|
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
if (subheap->heap->commitRoutine != NULL)
|
|
|
|
{
|
|
|
|
Status = subheap->heap->commitRoutine(subheap->heap, &address, &commitsize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-04-05 08:05:55 +00:00
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
2005-08-01 10:19:22 +00:00
|
|
|
&address,
|
|
|
|
0,
|
|
|
|
&commitsize,
|
|
|
|
MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE);
|
|
|
|
}
|
2004-12-31 20:05:16 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-12-31 20:05:16 +00:00
|
|
|
DPRINT( "Could not commit %08lx bytes at %p for heap %p\n",
|
|
|
|
size - subheap->commitSize,
|
|
|
|
((char *)subheap + subheap->commitSize),
|
|
|
|
subheap->heap );
|
|
|
|
return FALSE;
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
2004-12-31 20:05:16 +00:00
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap->commitSize += commitsize;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
#if 0
|
2004-05-31 19:33:59 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Decommit
|
|
|
|
*
|
|
|
|
* If possible, decommit the heap storage from (including) 'ptr'.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static inline BOOLEAN HEAP_Decommit( SUBHEAP *subheap, PVOID ptr, ULONG flags )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size = (ULONG)((char *)ptr - (char *)subheap);
|
2004-05-31 19:33:59 +00:00
|
|
|
PVOID address;
|
|
|
|
ULONG decommitsize;
|
|
|
|
NTSTATUS Status;
|
|
|
|
/* round to next block and add one full block */
|
|
|
|
size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
|
|
|
|
if (size >= subheap->commitSize)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
address = (PVOID)((char *)subheap + size);
|
|
|
|
decommitsize = subheap->commitSize - size;
|
|
|
|
|
2004-12-31 20:05:16 +00:00
|
|
|
Status = ZwFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&address,
|
|
|
|
&decommitsize,
|
|
|
|
MEM_DECOMMIT);
|
|
|
|
if (!NT_SUCCESS(Status))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-12-31 20:05:16 +00:00
|
|
|
DPRINT( "Could not decommit %08lx bytes at %p for heap %p\n",
|
|
|
|
subheap->commitSize - size,
|
|
|
|
((char *)subheap + size),
|
|
|
|
subheap->heap );
|
|
|
|
return FALSE;
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap->commitSize -= decommitsize;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2005-08-01 10:19:22 +00:00
|
|
|
#endif
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_CreateFreeBlock
|
|
|
|
*
|
|
|
|
* Create a free block at a specified address. 'size' is the size of the
|
|
|
|
* whole block, including the new arena.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static VOID HEAP_CreateFreeBlock( SUBHEAP *subheap, PVOID ptr, ULONG size )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ARENA_FREE *pFree;
|
2004-11-25 19:25:06 +00:00
|
|
|
BOOLEAN last;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* Create a free arena */
|
|
|
|
|
|
|
|
pFree = (ARENA_FREE *)ptr;
|
2004-11-25 19:25:06 +00:00
|
|
|
pFree->threadId = (ULONG)NtCurrentTeb()->Cid.UniqueThread;
|
2004-05-31 19:33:59 +00:00
|
|
|
pFree->magic = ARENA_FREE_MAGIC;
|
|
|
|
|
|
|
|
/* If debugging, erase the freed block content */
|
|
|
|
|
|
|
|
if (TRACE_ON(heap))
|
|
|
|
{
|
|
|
|
char *pEnd = (char *)ptr + size;
|
|
|
|
if (pEnd > (char *)subheap + subheap->commitSize)
|
|
|
|
pEnd = (char *)subheap + subheap->commitSize;
|
|
|
|
if (pEnd > (char *)(pFree + 1))
|
|
|
|
memset( pFree + 1, ARENA_FREE_FILLER, pEnd - (char *)(pFree + 1) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if next block is free also */
|
|
|
|
|
|
|
|
if (((char *)ptr + size < (char *)subheap + subheap->size) &&
|
2004-11-25 19:25:06 +00:00
|
|
|
(*(PULONG)((char *)ptr + size) & ARENA_FLAG_FREE))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
/* Remove the next arena from the free list */
|
|
|
|
ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
|
|
|
|
pNext->next->prev = pNext->prev;
|
|
|
|
pNext->prev->next = pNext->next;
|
|
|
|
size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
|
|
|
|
if (TRACE_ON(heap))
|
|
|
|
memset( pNext, ARENA_FREE_FILLER, sizeof(ARENA_FREE) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the next block PREV_FREE flag and pointer */
|
|
|
|
|
|
|
|
last = ((char *)ptr + size >= (char *)subheap + subheap->size);
|
|
|
|
if (!last)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
PULONG pNext = (PULONG)((char *)ptr + size);
|
2004-05-31 19:33:59 +00:00
|
|
|
*pNext |= ARENA_FLAG_PREV_FREE;
|
|
|
|
*(ARENA_FREE **)(pNext - 1) = pFree;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Last, insert the new block into the free list */
|
|
|
|
|
|
|
|
pFree->size = size - sizeof(*pFree);
|
|
|
|
HEAP_InsertFreeBlock( subheap->heap, pFree, last );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_MakeInUseBlockFree
|
|
|
|
*
|
|
|
|
* Turn an in-use block into a free block. Can also decommit the end of
|
|
|
|
* the heap, and possibly even free the sub-heap altogether.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static VOID HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena,
|
|
|
|
ULONG flags)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ARENA_FREE *pFree;
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
|
2004-12-31 20:05:16 +00:00
|
|
|
ULONG dummySize = 0;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* Check if we can merge with previous block */
|
|
|
|
|
|
|
|
if (pArena->size & ARENA_FLAG_PREV_FREE)
|
|
|
|
{
|
|
|
|
pFree = *((ARENA_FREE **)pArena - 1);
|
|
|
|
size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
|
|
|
|
/* Remove it from the free list */
|
|
|
|
pFree->next->prev = pFree->prev;
|
|
|
|
pFree->prev->next = pFree->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pFree = (ARENA_FREE *)pArena;
|
|
|
|
|
|
|
|
/* Create a free block */
|
|
|
|
|
|
|
|
HEAP_CreateFreeBlock( subheap, pFree, size );
|
|
|
|
size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
|
|
|
|
if ((char *)pFree + size < (char *)subheap + subheap->size)
|
|
|
|
return; /* Not the last block, so nothing more to do */
|
|
|
|
|
|
|
|
/* Free the whole sub-heap if it's empty and not the original one */
|
|
|
|
|
|
|
|
if (((char *)pFree == (char *)subheap + subheap->headerSize) &&
|
|
|
|
(subheap != &subheap->heap->subheap))
|
|
|
|
{
|
|
|
|
SUBHEAP *pPrev = &subheap->heap->subheap;
|
|
|
|
/* Remove the free block from the list */
|
|
|
|
pFree->next->prev = pFree->prev;
|
|
|
|
pFree->prev->next = pFree->next;
|
|
|
|
/* Remove the subheap from the list */
|
|
|
|
while (pPrev && (pPrev->next != subheap))
|
|
|
|
pPrev = pPrev->next;
|
|
|
|
if (pPrev)
|
|
|
|
pPrev->next = subheap->next;
|
|
|
|
/* Free the memory */
|
|
|
|
subheap->magic = 0;
|
2004-12-31 20:05:16 +00:00
|
|
|
ZwFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
(PVOID*)&subheap,
|
|
|
|
&dummySize,
|
|
|
|
MEM_RELEASE);
|
2004-05-31 19:33:59 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decommit the end of the heap */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ShrinkBlock
|
|
|
|
*
|
|
|
|
* Shrink an in-use block.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, ULONG size)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_BLOCK_SIZE)
|
|
|
|
{
|
|
|
|
HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
|
|
|
|
(pArena->size & ARENA_SIZE_MASK) - size );
|
|
|
|
/* assign size plus previous arena flags */
|
|
|
|
pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Turn off PREV_FREE flag in next block */
|
|
|
|
char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
if (pNext < (char *)subheap + subheap->size)
|
2004-11-25 19:25:06 +00:00
|
|
|
*(PULONG)pNext &= ~ARENA_FLAG_PREV_FREE;
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_InitSubHeap
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_InitSubHeap( HEAP *heap, PVOID address, ULONG flags,
|
2005-11-28 23:43:40 +00:00
|
|
|
SIZE_T commitSize, ULONG totalSize,
|
2005-08-01 10:19:22 +00:00
|
|
|
PRTL_HEAP_PARAMETERS Parameters )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
SUBHEAP *subheap = (SUBHEAP *)address;
|
|
|
|
FREE_LIST_ENTRY *pEntry;
|
|
|
|
int i;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Fill the sub-heap structure */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
subheap = (PSUBHEAP)address;
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap->heap = heap;
|
|
|
|
subheap->size = totalSize;
|
|
|
|
subheap->commitSize = commitSize;
|
|
|
|
subheap->magic = SUBHEAP_MAGIC;
|
|
|
|
|
|
|
|
if ( subheap != (SUBHEAP *)heap )
|
|
|
|
{
|
|
|
|
/* If this is a secondary subheap, insert it into list */
|
|
|
|
|
|
|
|
subheap->headerSize = sizeof(SUBHEAP);
|
|
|
|
subheap->next = heap->subheap.next;
|
|
|
|
heap->subheap.next = subheap;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If this is a primary subheap, initialize main heap */
|
|
|
|
|
|
|
|
subheap->headerSize = sizeof(HEAP);
|
|
|
|
subheap->next = NULL;
|
|
|
|
heap->next = NULL;
|
|
|
|
heap->flags = flags;
|
|
|
|
heap->magic = HEAP_MAGIC;
|
2005-08-01 10:19:22 +00:00
|
|
|
if (Parameters)
|
|
|
|
heap->commitRoutine = Parameters->CommitRoutine;
|
|
|
|
else
|
|
|
|
heap->commitRoutine = NULL;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* Build the free lists */
|
|
|
|
|
|
|
|
for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
|
|
|
|
{
|
|
|
|
pEntry->size = HEAP_freeListSizes[i];
|
|
|
|
pEntry->arena.size = 0 | ARENA_FLAG_FREE;
|
|
|
|
pEntry->arena.next = i < HEAP_NB_FREE_LISTS-1 ?
|
|
|
|
&heap->freeList[i+1].arena : &heap->freeList[0].arena;
|
|
|
|
pEntry->arena.prev = i ? &heap->freeList[i-1].arena :
|
|
|
|
&heap->freeList[HEAP_NB_FREE_LISTS-1].arena;
|
|
|
|
pEntry->arena.threadId = 0;
|
|
|
|
pEntry->arena.magic = ARENA_FREE_MAGIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize critical section */
|
|
|
|
|
2006-04-05 08:05:55 +00:00
|
|
|
if (RtlpGetMode() == UserMode)
|
|
|
|
{
|
|
|
|
RtlInitializeHeapLock( &heap->critSection );
|
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
/* Commit memory */
|
|
|
|
if (heap->commitRoutine)
|
|
|
|
{
|
2006-04-05 08:05:55 +00:00
|
|
|
if (subheap != (SUBHEAP *)heap)
|
|
|
|
{
|
|
|
|
Status = heap->commitRoutine(heap, &address, &commitSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* the caller is responsible for committing the first page! */
|
|
|
|
Status = STATUS_SUCCESS;
|
|
|
|
}
|
2005-08-01 10:19:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&address,
|
|
|
|
0,
|
|
|
|
&commitSize,
|
|
|
|
MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE);
|
|
|
|
}
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
DPRINT("Could not commit %08lx bytes for sub-heap %p\n",
|
|
|
|
commitSize, address);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
/* Create the first free block */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP_CreateFreeBlock( subheap, (PUCHAR)subheap + subheap->headerSize,
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap->size - subheap->headerSize );
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_CreateSubHeap
|
|
|
|
*
|
|
|
|
* Create a sub-heap of the given size.
|
|
|
|
* If heap == NULL, creates a main heap.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static PSUBHEAP
|
|
|
|
HEAP_CreateSubHeap(PVOID BaseAddress,
|
2005-08-01 10:19:22 +00:00
|
|
|
HEAP *heap, ULONG flags,
|
2005-11-28 23:43:40 +00:00
|
|
|
SIZE_T commitSize, ULONG totalSize,
|
2005-08-01 10:19:22 +00:00
|
|
|
PRTL_HEAP_PARAMETERS Parameters )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
PVOID address;
|
2004-05-31 19:33:59 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
/* Round-up sizes on a 64K boundary */
|
|
|
|
|
|
|
|
totalSize = (totalSize + 0xffff) & 0xffff0000;
|
|
|
|
commitSize = (commitSize + 0xffff) & 0xffff0000;
|
|
|
|
if (!commitSize)
|
|
|
|
commitSize = 0x10000;
|
|
|
|
if (totalSize < commitSize)
|
|
|
|
totalSize = commitSize;
|
|
|
|
|
|
|
|
/* Allocate the memory block */
|
|
|
|
address = BaseAddress;
|
2004-12-31 20:05:16 +00:00
|
|
|
if (!address)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
|
|
|
|
&address,
|
|
|
|
0,
|
|
|
|
(PULONG)&totalSize,
|
|
|
|
MEM_RESERVE | MEM_COMMIT,
|
|
|
|
PAGE_EXECUTE_READWRITE);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Could not VirtualAlloc %08lx bytes\n",
|
|
|
|
totalSize );
|
2004-05-31 19:33:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize subheap */
|
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
if (!HEAP_InitSubHeap( heap ? heap : (HEAP *)address,
|
|
|
|
address, flags, commitSize, totalSize,
|
|
|
|
Parameters ))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-12-31 20:05:16 +00:00
|
|
|
if (!BaseAddress)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ULONG dummySize = 0;
|
|
|
|
ZwFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
&address,
|
|
|
|
&dummySize,
|
|
|
|
MEM_RELEASE);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (SUBHEAP *)address;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_FindFreeBlock
|
|
|
|
*
|
|
|
|
* Find a free block at least as large as the requested size, and make sure
|
|
|
|
* the requested size is committed.
|
|
|
|
*/
|
2005-11-28 23:43:40 +00:00
|
|
|
static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, SIZE_T size,
|
2004-05-31 19:33:59 +00:00
|
|
|
SUBHEAP **ppSubHeap )
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
ARENA_FREE *pArena;
|
|
|
|
FREE_LIST_ENTRY *pEntry = heap->freeList;
|
|
|
|
|
|
|
|
/* Find a suitable free list, and in it find a block large enough */
|
|
|
|
|
|
|
|
while (pEntry->size < size)
|
|
|
|
pEntry++;
|
|
|
|
pArena = pEntry->arena.next;
|
|
|
|
while (pArena != &heap->freeList[0].arena)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG arena_size = (pArena->size & ARENA_SIZE_MASK) +
|
2004-05-31 19:33:59 +00:00
|
|
|
sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
|
|
|
if (arena_size >= size)
|
|
|
|
{
|
|
|
|
subheap = HEAP_FindSubHeap( heap, pArena );
|
|
|
|
if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
|
|
|
|
+ size + HEAP_MIN_BLOCK_SIZE,
|
|
|
|
heap->flags))
|
|
|
|
return NULL;
|
|
|
|
*ppSubHeap = subheap;
|
|
|
|
return pArena;
|
|
|
|
}
|
|
|
|
|
|
|
|
pArena = pArena->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no block was found, attempt to grow the heap */
|
|
|
|
|
|
|
|
if (!(heap->flags & HEAP_GROWABLE))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Not enough space in heap %p for %08lx bytes\n",
|
|
|
|
heap, size );
|
2004-05-31 19:33:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* make sure that we have a big enough size *committed* to fit another
|
|
|
|
* last free arena in !
|
|
|
|
* So just one heap struct, one first free arena which will eventually
|
|
|
|
* get inuse, and HEAP_MIN_BLOCK_SIZE for the second free arena that
|
|
|
|
* might get assigned all remaining free space in HEAP_ShrinkBlock() */
|
|
|
|
size += sizeof(SUBHEAP) + sizeof(ARENA_FREE) + HEAP_MIN_BLOCK_SIZE;
|
|
|
|
if (!(subheap = HEAP_CreateSubHeap( NULL, heap, heap->flags, size,
|
2005-08-01 10:19:22 +00:00
|
|
|
max( HEAP_DEF_SIZE, size ), NULL )))
|
2004-05-31 19:33:59 +00:00
|
|
|
return NULL;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("created new sub-heap %p of %08lx bytes for heap %p\n",
|
|
|
|
subheap, size, heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
*ppSubHeap = subheap;
|
|
|
|
return (ARENA_FREE *)(subheap + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_IsValidArenaPtr
|
|
|
|
*
|
|
|
|
* Check that the pointer is inside the range possible for arenas.
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_IsValidArenaPtr( HEAP *heap, PVOID ptr )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
|
|
|
|
if (!subheap)
|
|
|
|
return FALSE;
|
|
|
|
if ((char *)ptr >= (char *)subheap + subheap->headerSize)
|
|
|
|
return TRUE;
|
|
|
|
if (subheap != &heap->subheap)
|
|
|
|
return FALSE;
|
|
|
|
for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
|
2004-11-25 19:25:06 +00:00
|
|
|
if (ptr == (PVOID)&heap->freeList[i].arena)
|
2004-05-31 19:33:59 +00:00
|
|
|
return TRUE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ValidateFreeArena
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
char *heapEnd = (char *)subheap + subheap->size;
|
|
|
|
|
|
|
|
/* Check magic number */
|
|
|
|
if (pArena->magic != ARENA_FREE_MAGIC)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: invalid free arena magic for %p\n",
|
|
|
|
subheap->heap, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check size flags */
|
|
|
|
if (!(pArena->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pArena->size & ARENA_FLAG_PREV_FREE))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad flags %lx for free arena %p\n",
|
|
|
|
subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
/* Check arena size */
|
|
|
|
if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad size %08lx for free arena %p\n",
|
|
|
|
subheap->heap, (ULONG)pArena->size & ARENA_SIZE_MASK, pArena );
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next pointer is valid */
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->next ))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad next ptr %p for arena %p\n",
|
|
|
|
subheap->heap, pArena->next, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next arena is free */
|
|
|
|
if (!(pArena->next->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pArena->next->magic != ARENA_FREE_MAGIC))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: next arena %p invalid for %p\n",
|
|
|
|
subheap->heap, pArena->next, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev pointer is valid */
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, pArena->prev ))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad prev ptr %p for arena %p\n",
|
|
|
|
subheap->heap, pArena->prev, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is free */
|
|
|
|
if (!(pArena->prev->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pArena->prev->magic != ARENA_FREE_MAGIC))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: prev arena %p invalid for %p\n",
|
|
|
|
subheap->heap, pArena->prev, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next block has PREV_FREE flag */
|
|
|
|
if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(*(PULONG)((char *)(pArena + 1) +
|
2004-05-31 19:33:59 +00:00
|
|
|
(pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: free arena %p next block has no PREV_FREE flag\n",
|
|
|
|
subheap->heap, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check next block back pointer */
|
|
|
|
if (*((ARENA_FREE **)((char *)(pArena + 1) +
|
|
|
|
(pArena->size & ARENA_SIZE_MASK)) - 1) != pArena)
|
|
|
|
{
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("Heap %p: arena %p has wrong back ptr %lx\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
subheap->heap, pArena,
|
|
|
|
*((PULONG)((char *)(pArena+1)+ (pArena->size & ARENA_SIZE_MASK)) - 1));
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ValidateInUseArena
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_ValidateInUseArena( SUBHEAP *subheap, ARENA_INUSE *pArena, BOOLEAN quiet )
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
char *heapEnd = (char *)subheap + subheap->size;
|
|
|
|
|
|
|
|
/* Check magic number */
|
|
|
|
if (pArena->magic != ARENA_INUSE_MAGIC)
|
|
|
|
{
|
|
|
|
if (quiet == NOISY)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: invalid in-use arena magic for %p\n",
|
|
|
|
subheap->heap, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
if (TRACE_ON(heap))
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
}
|
|
|
|
else if (WARN_ON(heap))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: invalid in-use arena magic for %p\n",
|
|
|
|
subheap->heap, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
if (TRACE_ON(heap))
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check size flags */
|
|
|
|
if (pArena->size & ARENA_FLAG_FREE)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad flags %lx for in-use arena %p\n",
|
|
|
|
subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check arena size */
|
|
|
|
if ((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) > heapEnd)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad size %08lx for in-use arena %p\n",
|
|
|
|
subheap->heap, (ULONG)pArena->size & ARENA_SIZE_MASK, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check next arena PREV_FREE flag */
|
|
|
|
if (((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK) < heapEnd) &&
|
2004-11-25 19:25:06 +00:00
|
|
|
(*(PULONG)((char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK)) & ARENA_FLAG_PREV_FREE))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: in-use arena %p next block has PREV_FREE flag\n",
|
|
|
|
subheap->heap, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check prev free arena */
|
|
|
|
if (pArena->size & ARENA_FLAG_PREV_FREE)
|
|
|
|
{
|
|
|
|
ARENA_FREE *pPrev = *((ARENA_FREE **)pArena - 1);
|
|
|
|
/* Check prev pointer */
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: bad back ptr %p for arena %p\n",
|
|
|
|
subheap->heap, pPrev, pArena );
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is free */
|
|
|
|
if (!(pPrev->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pPrev->magic != ARENA_FREE_MAGIC))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: prev arena %p invalid for in-use %p\n",
|
|
|
|
subheap->heap, pPrev, pArena);
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is really the previous block */
|
|
|
|
if ((char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (char *)pArena)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: prev arena %p is not prev for in-use %p\n",
|
|
|
|
subheap->heap, pPrev, pArena );
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_IsInsideHeap
|
|
|
|
* Checks whether the pointer points to a block inside a given heap.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* Should this return BOOL32?
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* !0: Success
|
|
|
|
* 0: Failure
|
|
|
|
*/
|
|
|
|
int HEAP_IsInsideHeap(
|
|
|
|
HANDLE heap, /* [in] Heap */
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG flags, /* [in] Flags */
|
|
|
|
PVOID ptr /* [in] Pointer */
|
2004-05-31 19:33:59 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
if (!heapPtr)
|
|
|
|
return 0;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
ret = (((subheap = HEAP_FindSubHeap( heapPtr, ptr )) != NULL) &&
|
|
|
|
(((char *)ptr >= (char *)subheap + subheap->headerSize
|
|
|
|
+ sizeof(ARENA_INUSE))));
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-12-11 00:13:37 +00:00
|
|
|
void DumpStackFrames ( PULONG Frame, ULONG FrameCount )
|
|
|
|
{
|
|
|
|
ULONG i=0;
|
|
|
|
|
|
|
|
DbgPrint("Frames: ");
|
|
|
|
if ( !Frame )
|
|
|
|
{
|
|
|
|
#if defined __GNUC__
|
2005-09-25 12:39:32 +00:00
|
|
|
__asm__("mov %%ebp, %0" : "=r" (Frame) : );
|
2004-12-11 00:13:37 +00:00
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
__asm mov [Frame], ebp
|
|
|
|
#endif
|
|
|
|
Frame = (PULONG)Frame[0]; // step out of DumpStackFrames
|
|
|
|
}
|
|
|
|
while ( Frame != 0 && (ULONG)Frame != 0xDEADBEEF && (ULONG)Frame != 0xcdcdcdcd && (ULONG)Frame != 0xcccccccc && i++ < FrameCount )
|
|
|
|
{
|
2005-07-04 22:25:46 +00:00
|
|
|
DbgPrint("<%p>", (PVOID)Frame[1]);
|
2004-12-11 00:13:37 +00:00
|
|
|
if (Frame[1] == 0xdeadbeef)
|
|
|
|
break;
|
|
|
|
Frame = (PULONG)Frame[0];
|
|
|
|
DbgPrint(" ");
|
|
|
|
}
|
|
|
|
DbgPrint("\n");
|
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_IsRealArena [Internal]
|
|
|
|
* Validates a block is a valid arena.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
2004-11-25 19:25:06 +00:00
|
|
|
static BOOLEAN HEAP_IsRealArena(
|
2004-05-31 19:33:59 +00:00
|
|
|
HANDLE heap, /* [in] Handle to the heap */
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG flags, /* [in] Bit flags that control access during operation */
|
|
|
|
PVOID block, /* [in] Optional pointer to memory block to validate */
|
|
|
|
BOOLEAN quiet /* [in] Flag - if true, HEAP_ValidateInUseArena
|
|
|
|
* does not complain */
|
2004-05-31 19:33:59 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
HEAP *heapPtr = (HEAP *)(heap);
|
2004-11-25 19:25:06 +00:00
|
|
|
BOOLEAN ret = TRUE;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
|
|
|
|
{
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("Invalid heap %p!\n", heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags &= HEAP_NO_SERIALIZE;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
/* calling HeapLock may result in infinite recursion, so do the critsect directly */
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
if (block)
|
|
|
|
{
|
|
|
|
/* Only check this single memory block */
|
|
|
|
|
|
|
|
/* The following code is really HEAP_IsInsideHeap *
|
|
|
|
* with serialization already done. */
|
|
|
|
if (!(subheap = HEAP_FindSubHeap( heapPtr, block )) ||
|
|
|
|
((char *)block < (char *)subheap + subheap->headerSize
|
|
|
|
+ sizeof(ARENA_INUSE)))
|
|
|
|
{
|
|
|
|
if (quiet == NOISY)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("Heap %p: block %p is not inside heap\n",
|
|
|
|
heap, block );
|
2004-12-11 00:13:37 +00:00
|
|
|
DumpStackFrames(NULL,10);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
else if (WARN_ON(heap))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT1("Heap %p: block %p is not inside heap\n",
|
|
|
|
heap, block );
|
2004-12-11 00:13:37 +00:00
|
|
|
DumpStackFrames(NULL,10);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)block - 1, quiet );
|
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
subheap = &heapPtr->subheap;
|
|
|
|
while (subheap && ret)
|
|
|
|
{
|
|
|
|
char *ptr = (char *)subheap + subheap->headerSize;
|
|
|
|
while (ptr < (char *)subheap + subheap->size)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (*(PULONG)ptr & ARENA_FLAG_FREE)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr ))
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
ptr += sizeof(ARENA_FREE) + (*(PULONG)ptr & ARENA_SIZE_MASK);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY ))
|
|
|
|
{
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
ptr += sizeof(ARENA_INUSE) + (*(PULONG)ptr & ARENA_SIZE_MASK);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
subheap = subheap->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapCreate (KERNEL32.336)
|
|
|
|
* RETURNS
|
|
|
|
* Handle of heap: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
HANDLE NTAPI
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlCreateHeap(ULONG flags,
|
|
|
|
PVOID BaseAddress,
|
2005-08-01 10:19:22 +00:00
|
|
|
SIZE_T maxSize,
|
|
|
|
SIZE_T initialSize,
|
|
|
|
PVOID Lock,
|
|
|
|
PRTL_HEAP_PARAMETERS Parameters)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
2004-12-16 15:10:01 +00:00
|
|
|
HEAP *heapPtr;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* Allocate the heap block */
|
|
|
|
|
|
|
|
if (!maxSize)
|
|
|
|
{
|
|
|
|
maxSize = HEAP_DEF_SIZE;
|
|
|
|
flags |= HEAP_GROWABLE;
|
|
|
|
}
|
2005-08-01 10:19:22 +00:00
|
|
|
if (!(subheap = HEAP_CreateSubHeap( BaseAddress, NULL, flags, initialSize,
|
|
|
|
maxSize, Parameters )))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
if (RtlpGetMode() == UserMode)
|
|
|
|
{
|
|
|
|
/* link it into the per-process heap list */
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock (&RtlpProcessHeapsListLock);
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
heapPtr = subheap->heap;
|
|
|
|
heapPtr->next = (HEAP*)NtCurrentPeb()->ProcessHeaps;
|
|
|
|
NtCurrentPeb()->ProcessHeaps = (HANDLE)heapPtr;
|
|
|
|
NtCurrentPeb()->NumberOfHeaps++;
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock (&RtlpProcessHeapsListLock);
|
2005-08-01 10:19:22 +00:00
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
return (HANDLE)subheap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapDestroy (KERNEL32.337)
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
2005-05-09 01:41:02 +00:00
|
|
|
*
|
2004-12-16 15:10:01 +00:00
|
|
|
* RETURNS
|
|
|
|
* Success: A NULL HANDLE, if heap is NULL or it was destroyed
|
|
|
|
* Failure: The Heap handle, if heap is the process heap.
|
2004-05-31 19:33:59 +00:00
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
HANDLE NTAPI
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlDestroyHeap(HANDLE heap) /* [in] Handle of heap */
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
SUBHEAP *subheap;
|
2004-12-16 15:10:01 +00:00
|
|
|
ULONG flags;
|
|
|
|
HEAP **pptr;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("%p\n", heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!heapPtr)
|
2004-12-16 15:10:01 +00:00
|
|
|
return heap;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
if (RtlpGetMode() == UserMode)
|
|
|
|
{
|
|
|
|
if (heap == NtCurrentPeb()->ProcessHeap)
|
|
|
|
return heap; /* cannot delete the main process heap */
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
/* remove it from the per-process list */
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock (&RtlpProcessHeapsListLock);
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-08-01 10:19:22 +00:00
|
|
|
pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps;
|
|
|
|
while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next;
|
|
|
|
if (*pptr) *pptr = (*pptr)->next;
|
|
|
|
NtCurrentPeb()->NumberOfHeaps--;
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock (&RtlpProcessHeapsListLock);
|
2005-08-01 10:19:22 +00:00
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlDeleteHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap = &heapPtr->subheap;
|
|
|
|
// We must save the flags. The first subheap is located after
|
|
|
|
// the heap structure. If we release the first subheap,
|
|
|
|
// we release also the heap structure.
|
|
|
|
flags = heapPtr->flags;
|
|
|
|
while (subheap)
|
|
|
|
{
|
|
|
|
SUBHEAP *next = subheap->next;
|
2004-12-31 20:05:16 +00:00
|
|
|
ULONG dummySize = 0;
|
|
|
|
ZwFreeVirtualMemory(NtCurrentProcess(),
|
|
|
|
(PVOID*)&subheap,
|
|
|
|
&dummySize,
|
|
|
|
MEM_RELEASE);
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap = next;
|
|
|
|
}
|
2004-12-16 15:10:01 +00:00
|
|
|
return (HANDLE)NULL;
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapAlloc (KERNEL32.334)
|
|
|
|
* RETURNS
|
|
|
|
* Pointer to allocated memory block
|
|
|
|
* NULL: Failure
|
|
|
|
* 0x7d030f60--invalid flags in RtlHeapAllocate
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
PVOID NTAPI
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlAllocateHeap(HANDLE heap, /* [in] Handle of private heap block */
|
|
|
|
ULONG flags, /* [in] Heap allocation control flags */
|
|
|
|
ULONG size) /* [in] Number of bytes to allocate */
|
|
|
|
{
|
|
|
|
ARENA_FREE *pArena;
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
if (!heapPtr)
|
|
|
|
{
|
|
|
|
if (flags & HEAP_GENERATE_EXCEPTIONS)
|
|
|
|
RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
size = (size + 7) & ~7;
|
|
|
|
if (size < HEAP_MIN_BLOCK_SIZE)
|
|
|
|
size = HEAP_MIN_BLOCK_SIZE;
|
|
|
|
|
|
|
|
/* Locate a suitable free block */
|
|
|
|
|
|
|
|
if (!(pArena = HEAP_FindFreeBlock( heapPtr, size, &subheap )))
|
|
|
|
{
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%08lx): returning NULL\n",
|
2004-05-31 19:33:59 +00:00
|
|
|
heap, flags, size );
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (flags & HEAP_GENERATE_EXCEPTIONS)
|
|
|
|
RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the arena from the free list */
|
|
|
|
|
|
|
|
pArena->next->prev = pArena->prev;
|
|
|
|
pArena->prev->next = pArena->next;
|
|
|
|
|
|
|
|
/* Build the in-use arena */
|
|
|
|
|
|
|
|
pInUse = (ARENA_INUSE *)pArena;
|
|
|
|
pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
|
|
|
|
+ sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
2004-11-25 19:25:06 +00:00
|
|
|
pInUse->threadId = (ULONG)NtCurrentTeb()->Cid.UniqueThread;
|
2004-05-31 19:33:59 +00:00
|
|
|
pInUse->magic = ARENA_INUSE_MAGIC;
|
|
|
|
|
|
|
|
/* Shrink the block */
|
|
|
|
|
|
|
|
HEAP_ShrinkBlock( subheap, pInUse, size );
|
|
|
|
|
|
|
|
if (flags & HEAP_ZERO_MEMORY)
|
|
|
|
memset( pInUse + 1, 0, pInUse->size & ARENA_SIZE_MASK );
|
|
|
|
else if (TRACE_ON(heap))
|
|
|
|
memset( pInUse + 1, ARENA_INUSE_FILLER, pInUse->size & ARENA_SIZE_MASK );
|
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%08lx): returning %p\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
heap, flags, size, (PVOID)(pInUse + 1) );
|
|
|
|
return (PVOID)(pInUse + 1);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapFree (KERNEL32.338)
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI RtlFreeHeap(
|
2004-05-31 19:33:59 +00:00
|
|
|
HANDLE heap, /* [in] Handle of heap */
|
|
|
|
ULONG flags, /* [in] Heap freeing flags */
|
|
|
|
PVOID ptr /* [in] Address of memory to free */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
if (!heapPtr)
|
|
|
|
return FALSE;
|
|
|
|
if (!ptr) /* Freeing a NULL ptr is doesn't indicate an error in Win2k */
|
|
|
|
{
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p): asked to free NULL\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
heap, flags, ptr );
|
2004-05-31 19:33:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags &= HEAP_NO_SERIALIZE;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!HEAP_IsRealArena( heap, HEAP_NO_SERIALIZE, ptr, QUIET ))
|
|
|
|
{
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p): returning FALSE\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
heap, flags, ptr );
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn the block into a free block */
|
|
|
|
|
|
|
|
pInUse = (ARENA_INUSE *)ptr - 1;
|
|
|
|
subheap = HEAP_FindSubHeap( heapPtr, pInUse );
|
|
|
|
HEAP_MakeInUseBlockFree( subheap, pInUse, heapPtr->flags );
|
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p): returning TRUE\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
heap, flags, ptr );
|
2004-05-31 19:33:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlReAllocateHeap
|
|
|
|
* PARAMS
|
|
|
|
* Heap [in] Handle of heap block
|
|
|
|
* Flags [in] Heap reallocation flags
|
|
|
|
* Ptr, [in] Address of memory to reallocate
|
|
|
|
* Size [in] Number of bytes to reallocate
|
|
|
|
*
|
2004-05-31 19:33:59 +00:00
|
|
|
* RETURNS
|
|
|
|
* Pointer to reallocated memory block
|
|
|
|
* NULL: Failure
|
|
|
|
* 0x7d030f60--invalid flags in RtlHeapAllocate
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
PVOID NTAPI RtlReAllocateHeap(
|
2004-11-25 19:25:06 +00:00
|
|
|
HANDLE Heap,
|
|
|
|
ULONG Flags,
|
|
|
|
PVOID Ptr,
|
|
|
|
ULONG Size
|
2004-05-31 19:33:59 +00:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena;
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG oldSize;
|
2004-05-31 19:33:59 +00:00
|
|
|
HEAP *heapPtr;
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!Ptr)
|
2004-08-28 22:14:08 +00:00
|
|
|
return FALSE;
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(heapPtr = HEAP_GetPtr( Heap )))
|
2004-05-31 19:33:59 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
Flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
|
2004-05-31 19:33:59 +00:00
|
|
|
HEAP_REALLOC_IN_PLACE_ONLY;
|
2004-11-25 19:25:06 +00:00
|
|
|
Flags |= heapPtr->flags;
|
|
|
|
Size = (Size + 7) & ~7;
|
|
|
|
if (Size < HEAP_MIN_BLOCK_SIZE)
|
|
|
|
Size = HEAP_MIN_BLOCK_SIZE;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!HEAP_IsRealArena( Heap, HEAP_NO_SERIALIZE, Ptr, QUIET ))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p,%08lx): returning NULL\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
Heap, Flags, Ptr, Size );
|
|
|
|
if (Flags & HEAP_GENERATE_EXCEPTIONS)
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if we need to grow the block */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
pArena = (ARENA_INUSE *)Ptr - 1;
|
|
|
|
pArena->threadId = (ULONG)NtCurrentTeb()->Cid.UniqueThread;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
subheap = HEAP_FindSubHeap( heapPtr, pArena );
|
|
|
|
oldSize = (pArena->size & ARENA_SIZE_MASK);
|
2004-11-25 19:25:06 +00:00
|
|
|
if (Size > oldSize)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
char *pNext = (char *)(pArena + 1) + oldSize;
|
|
|
|
if ((pNext < (char *)subheap + subheap->size) &&
|
2004-11-25 19:25:06 +00:00
|
|
|
(*(PULONG)pNext & ARENA_FLAG_FREE) &&
|
|
|
|
(oldSize + (*(PULONG)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= Size))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
/* The next block is free and large enough */
|
|
|
|
ARENA_FREE *pFree = (ARENA_FREE *)pNext;
|
|
|
|
pFree->next->prev = pFree->prev;
|
|
|
|
pFree->prev->next = pFree->next;
|
|
|
|
pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
|
|
|
|
if (!HEAP_Commit( subheap, (char *)pArena + sizeof(ARENA_INUSE)
|
2004-11-25 19:25:06 +00:00
|
|
|
+ Size + HEAP_MIN_BLOCK_SIZE,
|
2004-05-31 19:33:59 +00:00
|
|
|
heapPtr->flags))
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-11-25 19:25:06 +00:00
|
|
|
if (Flags & HEAP_GENERATE_EXCEPTIONS)
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP_ShrinkBlock( subheap, pArena, Size );
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
else /* Do it the hard way */
|
|
|
|
{
|
|
|
|
ARENA_FREE *pNew;
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *newsubheap;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
if ((Flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
|
|
|
|
!(pNew = HEAP_FindFreeBlock( heapPtr, Size, &newsubheap )))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-11-25 19:25:06 +00:00
|
|
|
if (Flags & HEAP_GENERATE_EXCEPTIONS)
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build the in-use arena */
|
|
|
|
|
|
|
|
pNew->next->prev = pNew->prev;
|
|
|
|
pNew->prev->next = pNew->next;
|
|
|
|
pInUse = (ARENA_INUSE *)pNew;
|
|
|
|
pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
|
|
|
|
+ sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
2004-11-25 19:25:06 +00:00
|
|
|
pInUse->threadId = (ULONG)NtCurrentTeb()->Cid.UniqueThread;
|
2004-05-31 19:33:59 +00:00
|
|
|
pInUse->magic = ARENA_INUSE_MAGIC;
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP_ShrinkBlock( newsubheap, pInUse, Size );
|
2004-05-31 19:33:59 +00:00
|
|
|
memcpy( pInUse + 1, pArena + 1, oldSize );
|
|
|
|
|
|
|
|
/* Free the previous block */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP_MakeInUseBlockFree( subheap, pArena, Flags );
|
2004-05-31 19:33:59 +00:00
|
|
|
subheap = newsubheap;
|
|
|
|
pArena = pInUse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP_ShrinkBlock( subheap, pArena, Size ); /* Shrink the block */
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* Clear the extra bytes if needed */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
if (Size > oldSize)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
if (Flags & HEAP_ZERO_MEMORY)
|
2004-05-31 19:33:59 +00:00
|
|
|
memset( (char *)(pArena + 1) + oldSize, 0,
|
|
|
|
(pArena->size & ARENA_SIZE_MASK) - oldSize );
|
|
|
|
else if (TRACE_ON(heap))
|
|
|
|
memset( (char *)(pArena + 1) + oldSize, ARENA_INUSE_FILLER,
|
|
|
|
(pArena->size & ARENA_SIZE_MASK) - oldSize );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the new arena */
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p,%08lx): returning %p\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
Heap, Flags, Ptr, Size, (PVOID)(pArena + 1) );
|
|
|
|
return (PVOID)(pArena + 1);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlCompactHeap
|
2004-05-31 19:33:59 +00:00
|
|
|
*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
ULONG NTAPI
|
2004-11-25 19:25:06 +00:00
|
|
|
RtlCompactHeap(HANDLE Heap,
|
|
|
|
ULONG Flags)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
UNIMPLEMENTED;
|
2004-05-31 19:33:59 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlLockHeap
|
2004-05-31 19:33:59 +00:00
|
|
|
* Attempts to acquire the critical section object for a specified heap.
|
|
|
|
*
|
2004-11-25 19:25:06 +00:00
|
|
|
* PARAMS
|
|
|
|
* Heap [in] Handle of heap to lock for exclusive access
|
|
|
|
*
|
2004-05-31 19:33:59 +00:00
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI
|
2004-11-25 19:25:06 +00:00
|
|
|
RtlLockHeap(IN HANDLE Heap)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP *heapPtr = HEAP_GetPtr( Heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!heapPtr)
|
|
|
|
return FALSE;
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlUnlockHeap
|
2004-05-31 19:33:59 +00:00
|
|
|
* Releases ownership of the critical section object.
|
|
|
|
*
|
2004-11-25 19:25:06 +00:00
|
|
|
* PARAMS
|
|
|
|
* Heap [in] Handle to the heap to unlock
|
|
|
|
*
|
2004-05-31 19:33:59 +00:00
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI
|
2004-11-25 19:25:06 +00:00
|
|
|
RtlUnlockHeap(HANDLE Heap)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP *heapPtr = HEAP_GetPtr( Heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!heapPtr)
|
|
|
|
return FALSE;
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlSizeHeap
|
|
|
|
* PARAMS
|
|
|
|
* Heap [in] Handle of heap
|
|
|
|
* Flags [in] Heap size control flags
|
|
|
|
* Ptr [in] Address of memory to return size for
|
|
|
|
*
|
2004-05-31 19:33:59 +00:00
|
|
|
* RETURNS
|
|
|
|
* Size in bytes of allocated memory
|
|
|
|
* 0xffffffff: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
ULONG NTAPI
|
2004-11-25 19:25:06 +00:00
|
|
|
RtlSizeHeap(
|
|
|
|
HANDLE Heap,
|
|
|
|
ULONG Flags,
|
|
|
|
PVOID Ptr
|
2004-05-31 19:33:59 +00:00
|
|
|
)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ULONG ret;
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( Heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
if (!heapPtr)
|
2004-11-25 19:25:06 +00:00
|
|
|
return 0;
|
|
|
|
Flags &= HEAP_NO_SERIALIZE;
|
|
|
|
Flags |= heapPtr->flags;
|
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!HEAP_IsRealArena( Heap, HEAP_NO_SERIALIZE, Ptr, QUIET ))
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ret = 0xffffffff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)Ptr - 1;
|
2004-05-31 19:33:59 +00:00
|
|
|
ret = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
2004-11-25 19:25:06 +00:00
|
|
|
if (!(Flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-12-08 23:44:31 +00:00
|
|
|
DPRINT("(%p,%08lx,%p): returning %08lx\n",
|
2004-11-25 19:25:06 +00:00
|
|
|
Heap, Flags, Ptr, ret );
|
2004-05-31 19:33:59 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
2004-11-25 19:25:06 +00:00
|
|
|
* RtlValidateHeap
|
2004-05-31 19:33:59 +00:00
|
|
|
* Validates a specified heap.
|
|
|
|
*
|
2004-11-25 19:25:06 +00:00
|
|
|
* PARAMS
|
|
|
|
* Heap [in] Handle to the heap
|
|
|
|
* Flags [in] Bit flags that control access during operation
|
|
|
|
* Block [in] Optional pointer to memory block to validate
|
|
|
|
*
|
2004-05-31 19:33:59 +00:00
|
|
|
* NOTES
|
|
|
|
* Flags is ignored.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI RtlValidateHeap(
|
2004-11-25 19:25:06 +00:00
|
|
|
HANDLE Heap,
|
|
|
|
ULONG Flags,
|
|
|
|
PVOID Block
|
2004-05-31 19:33:59 +00:00
|
|
|
)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
HEAP *heapPtr = HEAP_GetPtr( Heap );
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!heapPtr)
|
|
|
|
return FALSE;
|
2004-11-25 19:25:06 +00:00
|
|
|
return HEAP_IsRealArena( heapPtr, Flags, Block, QUIET );
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HeapWalk (KERNEL32.344)
|
|
|
|
* Enumerates the memory blocks in a specified heap.
|
|
|
|
* See HEAP_Dump() for info on heap structure.
|
|
|
|
*
|
|
|
|
* TODO
|
|
|
|
* - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
|
|
|
|
* PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
#if 0
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI HeapWalk(
|
2004-05-31 19:33:59 +00:00
|
|
|
HANDLE heap, /* [in] Handle to heap to enumerate */
|
|
|
|
LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
|
|
|
|
)
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr(heap);
|
|
|
|
SUBHEAP *sub, *currentheap = NULL;
|
2004-11-25 19:25:06 +00:00
|
|
|
BOOLEAN ret = FALSE;
|
2004-05-31 19:33:59 +00:00
|
|
|
char *ptr;
|
|
|
|
int region_index = 0;
|
|
|
|
|
|
|
|
if (!heapPtr || !entry)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(heapPtr->flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
/* set ptr to the next arena to be examined */
|
|
|
|
|
|
|
|
if (!entry->lpData) /* first call (init) ? */
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("begin walking of heap 0x%08x.\n", heap);
|
2004-05-31 19:33:59 +00:00
|
|
|
/*HEAP_Dump(heapPtr);*/
|
|
|
|
currentheap = &heapPtr->subheap;
|
|
|
|
ptr = (char*)currentheap + currentheap->headerSize;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = entry->lpData;
|
|
|
|
sub = &heapPtr->subheap;
|
|
|
|
while (sub)
|
|
|
|
{
|
|
|
|
if (((char *)ptr >= (char *)sub) &&
|
|
|
|
((char *)ptr < (char *)sub + sub->size))
|
|
|
|
{
|
|
|
|
currentheap = sub;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sub = sub->next;
|
|
|
|
region_index++;
|
|
|
|
}
|
|
|
|
if (currentheap == NULL)
|
|
|
|
{
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("no matching subheap found, shouldn't happen !\n");
|
2004-05-31 19:33:59 +00:00
|
|
|
goto HW_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr += entry->cbData; /* point to next arena */
|
|
|
|
if (ptr > (char *)currentheap + currentheap->size - 1)
|
|
|
|
{ /* proceed with next subheap */
|
|
|
|
if (!(currentheap = currentheap->next))
|
|
|
|
{ /* successfully finished */
|
2004-11-25 19:25:06 +00:00
|
|
|
DPRINT("end reached.\n");
|
2004-05-31 19:33:59 +00:00
|
|
|
goto HW_end;
|
|
|
|
}
|
|
|
|
ptr = (char*)currentheap + currentheap->headerSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->wFlags = 0;
|
2004-11-25 19:25:06 +00:00
|
|
|
if (*(PULONG)ptr & ARENA_FLAG_FREE)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
ARENA_FREE *pArena = (ARENA_FREE *)ptr;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
/*DPRINT("free, magic: %04x\n", pArena->magic);*/
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
entry->lpData = pArena + 1;
|
|
|
|
entry->cbData = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
entry->cbOverhead = sizeof(ARENA_FREE);
|
|
|
|
entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
/*DPRINT("busy, magic: %04x\n", pArena->magic);*/
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
entry->lpData = pArena + 1;
|
|
|
|
entry->cbData = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
entry->cbOverhead = sizeof(ARENA_INUSE);
|
|
|
|
entry->wFlags = PROCESS_HEAP_ENTRY_BUSY;
|
|
|
|
/* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
|
|
|
|
and PROCESS_HEAP_ENTRY_DDESHARE yet */
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->iRegionIndex = region_index;
|
|
|
|
|
|
|
|
/* first element of heap ? */
|
|
|
|
if (ptr == (char *)(currentheap + currentheap->headerSize))
|
|
|
|
{
|
|
|
|
entry->wFlags |= PROCESS_HEAP_REGION;
|
|
|
|
entry->Foo.Region.dwCommittedSize = currentheap->commitSize;
|
|
|
|
entry->Foo.Region.dwUnCommittedSize =
|
|
|
|
currentheap->size - currentheap->commitSize;
|
|
|
|
entry->Foo.Region.lpFirstBlock = /* first valid block */
|
|
|
|
currentheap + currentheap->headerSize;
|
|
|
|
entry->Foo.Region.lpLastBlock = /* first invalid block */
|
|
|
|
currentheap + currentheap->size;
|
|
|
|
}
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
HW_end:
|
|
|
|
if (!(heapPtr->flags & HEAP_NO_SERIALIZE))
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock( &heapPtr->critSection );
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
VOID
|
|
|
|
RtlInitializeHeapManager(VOID)
|
|
|
|
{
|
|
|
|
PPEB Peb;
|
|
|
|
|
|
|
|
Peb = NtCurrentPeb();
|
|
|
|
|
|
|
|
Peb->NumberOfHeaps = 0;
|
2004-12-16 15:10:01 +00:00
|
|
|
Peb->MaximumNumberOfHeaps = -1; /* no limit */
|
|
|
|
Peb->ProcessHeaps = NULL;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlInitializeHeapLock(&RtlpProcessHeapsListLock);
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
NTSTATUS NTAPI
|
2004-12-31 20:05:16 +00:00
|
|
|
RtlEnumProcessHeaps(PHEAP_ENUMERATION_ROUTINE HeapEnumerationRoutine,
|
|
|
|
PVOID lParam)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
|
|
|
NTSTATUS Status = STATUS_SUCCESS;
|
2004-12-16 15:10:01 +00:00
|
|
|
HEAP** pptr;
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock(&RtlpProcessHeapsListLock);
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2004-12-16 15:10:01 +00:00
|
|
|
for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-12-31 20:05:16 +00:00
|
|
|
Status = HeapEnumerationRoutine(*pptr,lParam);
|
2004-05-31 19:33:59 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock(&RtlpProcessHeapsListLock);
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
ULONG NTAPI
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlGetProcessHeaps(ULONG HeapCount,
|
|
|
|
HANDLE *HeapArray)
|
|
|
|
{
|
|
|
|
ULONG Result = 0;
|
2004-12-16 15:10:01 +00:00
|
|
|
HEAP ** pptr;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock(&RtlpProcessHeapsListLock);
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2004-12-16 15:10:01 +00:00
|
|
|
Result = NtCurrentPeb()->NumberOfHeaps;
|
2005-05-09 01:41:02 +00:00
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
if (NtCurrentPeb()->NumberOfHeaps <= HeapCount)
|
|
|
|
{
|
2004-12-16 15:10:01 +00:00
|
|
|
int i = 0;
|
|
|
|
for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next)
|
|
|
|
{
|
|
|
|
HeapArray[i++] = *pptr;
|
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock (&RtlpProcessHeapsListLock);
|
2004-05-31 19:33:59 +00:00
|
|
|
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI
|
2004-05-31 19:33:59 +00:00
|
|
|
RtlValidateProcessHeaps(VOID)
|
|
|
|
{
|
|
|
|
BOOLEAN Result = TRUE;
|
2004-12-16 15:10:01 +00:00
|
|
|
HEAP ** pptr;
|
2004-05-31 19:33:59 +00:00
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlEnterHeapLock(&RtlpProcessHeapsListLock);
|
2004-12-16 15:10:01 +00:00
|
|
|
|
|
|
|
for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next)
|
2004-05-31 19:33:59 +00:00
|
|
|
{
|
2004-12-16 15:10:01 +00:00
|
|
|
if (!RtlValidateHeap(*pptr, 0, NULL))
|
|
|
|
{
|
2004-05-31 19:33:59 +00:00
|
|
|
Result = FALSE;
|
2004-12-16 15:10:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-05-31 19:33:59 +00:00
|
|
|
}
|
|
|
|
|
2005-09-08 02:29:30 +00:00
|
|
|
RtlLeaveHeapLock (&RtlpProcessHeapsListLock);
|
2004-12-16 15:10:01 +00:00
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2004-11-25 19:25:06 +00:00
|
|
|
|
2004-08-12 16:43:37 +00:00
|
|
|
/*
|
|
|
|
* @unimplemented
|
|
|
|
*/
|
2005-10-19 17:03:38 +00:00
|
|
|
BOOLEAN NTAPI
|
2004-08-12 16:43:37 +00:00
|
|
|
RtlZeroHeap(
|
|
|
|
IN PVOID HeapHandle,
|
|
|
|
IN ULONG Flags
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
2004-11-25 19:25:06 +00:00
|
|
|
return FALSE;
|
2004-08-12 16:43:37 +00:00
|
|
|
}
|
|
|
|
|
2004-05-31 19:33:59 +00:00
|
|
|
/* EOF */
|