From 9cbb3d72e1b0cf50edc92e3f92ed8e739b86e020 Mon Sep 17 00:00:00 2001 From: Gunnar Dalsnes Date: Thu, 16 Dec 2004 15:10:01 +0000 Subject: [PATCH] -link heaps together instead of using stupid table past peb -RtlValidateProcessHeaps/RtlGetProcessHeaps/RtlEnumProcessHeaps didn't process all heaps -correct RtlDestroyHeap proto. / return value All changes based on Wine svn path=/trunk/; revision=12151 --- reactos/include/ntos/rtl.h | 4 +- reactos/lib/kernel32/mem/heap.c | 11 ++++- reactos/lib/rtl/heap.c | 88 +++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 41 deletions(-) diff --git a/reactos/include/ntos/rtl.h b/reactos/include/ntos/rtl.h index f7cea88563c..179622b1ea2 100755 --- a/reactos/include/ntos/rtl.h +++ b/reactos/include/ntos/rtl.h @@ -1,4 +1,4 @@ -/* $Id: rtl.h,v 1.39 2004/11/25 19:22:07 ekohl Exp $ +/* $Id: rtl.h,v 1.40 2004/12/16 15:10:00 gdalsnes Exp $ * */ #ifndef __DDK_RTL_H @@ -960,7 +960,7 @@ RtlDescribeChunk(IN USHORT CompressionFormat, NTSTATUS STDCALL RtlDestroyAtomTable (IN PRTL_ATOM_TABLE AtomTable); -BOOLEAN STDCALL +HANDLE STDCALL RtlDestroyHeap (HANDLE hheap); NTSTATUS diff --git a/reactos/lib/kernel32/mem/heap.c b/reactos/lib/kernel32/mem/heap.c index 037d4348d98..6e2f31e74c0 100644 --- a/reactos/lib/kernel32/mem/heap.c +++ b/reactos/lib/kernel32/mem/heap.c @@ -1,4 +1,4 @@ -/* $Id: heap.c,v 1.27 2004/06/13 20:04:55 navaraf Exp $ +/* $Id: heap.c,v 1.28 2004/12/16 15:10:00 gdalsnes Exp $ * * kernel/heap.c * Copyright (C) 1996, Onno Hovers, All rights reserved @@ -53,7 +53,14 @@ HANDLE STDCALL HeapCreate(DWORD flags, DWORD dwInitialSize, DWORD dwMaximumSize) */ BOOL WINAPI HeapDestroy(HANDLE hheap) { - return(RtlDestroyHeap(hheap)); + if (hheap == RtlGetProcessHeap()) + { + return FALSE; + } + + if (RtlDestroyHeap( hheap )==NULL) return TRUE; + SetLastError( ERROR_INVALID_HANDLE ); + return FALSE; } /********************************************************************* diff --git a/reactos/lib/rtl/heap.c b/reactos/lib/rtl/heap.c index 144f28c9207..1d1de6e9417 100644 --- a/reactos/lib/rtl/heap.c +++ b/reactos/lib/rtl/heap.c @@ -1077,7 +1077,7 @@ RtlCreateHeap(ULONG flags, PRTL_HEAP_DEFINITION Definition) { SUBHEAP *subheap; - ULONG i; + HEAP *heapPtr; /* Allocate the heap block */ @@ -1091,15 +1091,14 @@ RtlCreateHeap(ULONG flags, return 0; } + /* link it into the per-process heap list */ RtlEnterCriticalSection (&RtlpProcessHeapsListLock); - for (i = 0; i < NtCurrentPeb ()->NumberOfHeaps; i++) - { - if (NtCurrentPeb ()->ProcessHeaps[i] == NULL) - { - NtCurrentPeb()->ProcessHeaps[i] = (PVOID)subheap; - break; - } - } + + heapPtr = subheap->heap; + heapPtr->next = (HEAP*)NtCurrentPeb()->ProcessHeaps; + NtCurrentPeb()->ProcessHeaps = (HANDLE)heapPtr; + NtCurrentPeb()->NumberOfHeaps++; + RtlLeaveCriticalSection (&RtlpProcessHeapsListLock); return (HANDLE)subheap; @@ -1112,27 +1111,34 @@ RtlCreateHeap(ULONG flags, * FALSE: Failure * * @implemented + * + * RETURNS + * Success: A NULL HANDLE, if heap is NULL or it was destroyed + * Failure: The Heap handle, if heap is the process heap. */ -BOOLEAN STDCALL +HANDLE STDCALL RtlDestroyHeap(HANDLE heap) /* [in] Handle of heap */ { HEAP *heapPtr = HEAP_GetPtr( heap ); SUBHEAP *subheap; - ULONG i, flags; + ULONG flags; + HEAP **pptr; DPRINT("%08x\n", heap ); if (!heapPtr) - return FALSE; + return heap; + if (heap == NtCurrentPeb()->ProcessHeap) + return heap; /* cannot delete the main process heap */ + + /* remove it from the per-process list */ RtlEnterCriticalSection (&RtlpProcessHeapsListLock); - for (i = 0; i < NtCurrentPeb ()->NumberOfHeaps; i++) - { - if (NtCurrentPeb ()->ProcessHeaps[i] == heap) - { - NtCurrentPeb()->ProcessHeaps[i] = NULL; - break; - } - } + + pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; + while (*pptr && *pptr != heapPtr) pptr = &(*pptr)->next; + if (*pptr) *pptr = (*pptr)->next; + NtCurrentPeb()->NumberOfHeaps--; + RtlLeaveCriticalSection (&RtlpProcessHeapsListLock); RtlDeleteCriticalSection( &heapPtr->critSection ); @@ -1155,7 +1161,7 @@ RtlDestroyHeap(HANDLE heap) /* [in] Handle of heap */ } subheap = next; } - return TRUE; + return (HANDLE)NULL; } @@ -1713,8 +1719,8 @@ RtlInitializeHeapManager(VOID) Peb = NtCurrentPeb(); Peb->NumberOfHeaps = 0; - Peb->MaximumNumberOfHeaps = (PAGE_SIZE - sizeof(PEB)) / sizeof(HANDLE); - Peb->ProcessHeaps = (PVOID)Peb + sizeof(PEB); + Peb->MaximumNumberOfHeaps = -1; /* no limit */ + Peb->ProcessHeaps = NULL; RtlInitializeCriticalSection(&RtlpProcessHeapsListLock); } @@ -1728,13 +1734,13 @@ RtlEnumProcessHeaps(NTSTATUS STDCALL_FUNC(*func)(PVOID, LONG), LONG lParam) { NTSTATUS Status = STATUS_SUCCESS; - ULONG i; - + HEAP** pptr; + RtlEnterCriticalSection(&RtlpProcessHeapsListLock); - for (i = 0; i < NtCurrentPeb()->NumberOfHeaps; i++) + for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next) { - Status = func(NtCurrentPeb()->ProcessHeaps[i],lParam); + Status = func(*pptr,lParam); if (!NT_SUCCESS(Status)) break; } @@ -1753,15 +1759,19 @@ RtlGetProcessHeaps(ULONG HeapCount, HANDLE *HeapArray) { ULONG Result = 0; + HEAP ** pptr; RtlEnterCriticalSection(&RtlpProcessHeapsListLock); + Result = NtCurrentPeb()->NumberOfHeaps; + if (NtCurrentPeb()->NumberOfHeaps <= HeapCount) { - Result = NtCurrentPeb()->NumberOfHeaps; - memmove(HeapArray, - NtCurrentPeb()->ProcessHeaps, - Result * sizeof(HANDLE)); + int i = 0; + for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next) + { + HeapArray[i++] = *pptr; + } } RtlLeaveCriticalSection (&RtlpProcessHeapsListLock); @@ -1776,18 +1786,22 @@ RtlGetProcessHeaps(ULONG HeapCount, BOOLEAN STDCALL RtlValidateProcessHeaps(VOID) { - HANDLE Heaps[128]; BOOLEAN Result = TRUE; - ULONG HeapCount; - ULONG i; + HEAP ** pptr; - HeapCount = RtlGetProcessHeaps(128, Heaps); - for (i = 0; i < HeapCount; i++) + RtlEnterCriticalSection(&RtlpProcessHeapsListLock); + + for (pptr = (HEAP**)&NtCurrentPeb()->ProcessHeaps; *pptr; pptr = &(*pptr)->next) { - if (!RtlValidateHeap(Heaps[i], 0, NULL)) + if (!RtlValidateHeap(*pptr, 0, NULL)) + { Result = FALSE; + break; + } } + RtlLeaveCriticalSection (&RtlpProcessHeapsListLock); + return Result; }