4 * Copyright 1996 Alexandre Julliard
5 * Copyright 1998 Ulrich Weigand
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(heap
);
39 /* address where we try to map the system heap */
40 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
41 #define SYSTEM_HEAP_SIZE 0x100000 /* Default heap size = 1Mb */
43 static HANDLE systemHeap
; /* globally shared heap */
45 /***********************************************************************
46 * HEAP_CreateSystemHeap
48 * Create the system heap.
50 inline static HANDLE
HEAP_CreateSystemHeap(void)
55 UNICODE_STRING event_name
;
56 OBJECT_ATTRIBUTES event_attr
;
58 if (!(map
= CreateFileMappingA( INVALID_HANDLE_VALUE
, NULL
, SEC_COMMIT
| PAGE_READWRITE
,
59 0, SYSTEM_HEAP_SIZE
, "__SystemHeap" ))) return 0;
60 created
= (GetLastError() != ERROR_ALREADY_EXISTS
);
62 if (!(base
= MapViewOfFileEx( map
, FILE_MAP_ALL_ACCESS
, 0, 0, 0, SYSTEM_HEAP_BASE
)))
64 /* pre-defined address not available */
65 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE
);
69 /* create the system heap event */
70 RtlCreateUnicodeStringFromAsciiz( &event_name
, "__SystemHeapEvent" );
71 event_attr
.Length
= sizeof(event_attr
);
72 event_attr
.RootDirectory
= 0;
73 event_attr
.ObjectName
= &event_name
;
74 event_attr
.Attributes
= 0;
75 event_attr
.SecurityDescriptor
= NULL
;
76 event_attr
.SecurityQualityOfService
= NULL
;
77 NtCreateEvent( &event
, EVENT_ALL_ACCESS
, &event_attr
, TRUE
, FALSE
);
79 if (created
) /* newly created heap */
81 systemHeap
= RtlCreateHeap( HEAP_SHARED
, base
, SYSTEM_HEAP_SIZE
,
82 SYSTEM_HEAP_SIZE
, NULL
, NULL
);
83 NtSetEvent( event
, NULL
);
87 /* wait for the heap to be initialized */
88 WaitForSingleObject( event
, INFINITE
);
89 systemHeap
= (HANDLE
)base
;
96 /***********************************************************************
97 * HeapCreate (KERNEL32.@)
99 * Handle of heap: Success
102 HANDLE WINAPI
HeapCreate(
103 DWORD flags
, /* [in] Heap allocation flag */
104 SIZE_T initialSize
, /* [in] Initial heap size */
105 SIZE_T maxSize
/* [in] Maximum heap size */
109 if ( flags
& HEAP_SHARED
)
111 if (!systemHeap
) HEAP_CreateSystemHeap();
112 else WARN( "Shared Heap requested, returning system heap.\n" );
117 ret
= RtlCreateHeap( flags
, NULL
, maxSize
, initialSize
, NULL
, NULL
);
118 if (!ret
) SetLastError( ERROR_NOT_ENOUGH_MEMORY
);
123 /***********************************************************************
124 * HeapDestroy (KERNEL32.@)
129 BOOL WINAPI
HeapDestroy( HANDLE heap
/* [in] Handle of heap */ )
131 if (heap
== systemHeap
)
133 WARN( "attempt to destroy system heap, returning TRUE!\n" );
136 if (!RtlDestroyHeap( heap
)) return TRUE
;
137 SetLastError( ERROR_INVALID_HANDLE
);
142 /***********************************************************************
143 * HeapCompact (KERNEL32.@)
145 SIZE_T WINAPI
HeapCompact( HANDLE heap
, DWORD flags
)
147 return RtlCompactHeap( heap
, flags
);
151 /***********************************************************************
152 * HeapLock (KERNEL32.@)
153 * Attempts to acquire the critical section object for a specified heap.
159 BOOL WINAPI
HeapLock(
160 HANDLE heap
/* [in] Handle of heap to lock for exclusive access */
162 return RtlLockHeap( heap
);
166 /***********************************************************************
167 * HeapUnlock (KERNEL32.@)
168 * Releases ownership of the critical section object.
174 BOOL WINAPI
HeapUnlock(
175 HANDLE heap
/* [in] Handle to the heap to unlock */
177 return RtlUnlockHeap( heap
);
181 /***********************************************************************
182 * HeapValidate (KERNEL32.@)
183 * Validates a specified heap.
192 BOOL WINAPI
HeapValidate(
193 HANDLE heap
, /* [in] Handle to the heap */
194 DWORD flags
, /* [in] Bit flags that control access during operation */
195 LPCVOID block
/* [in] Optional pointer to memory block to validate */
197 return RtlValidateHeap( heap
, flags
, block
);
201 /***********************************************************************
202 * HeapWalk (KERNEL32.@)
203 * Enumerates the memory blocks in a specified heap.
206 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
207 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
213 BOOL WINAPI
HeapWalk(
214 HANDLE heap
, /* [in] Handle to heap to enumerate */
215 LPPROCESS_HEAP_ENTRY entry
/* [out] Pointer to structure of enumeration info */
217 NTSTATUS ret
= RtlWalkHeap( heap
, entry
);
218 if (ret
) SetLastError( RtlNtStatusToDosError(ret
) );
223 /***********************************************************************
224 * GetProcessHeap (KERNEL32.@)
226 HANDLE WINAPI
GetProcessHeap(void)
228 return NtCurrentTeb()->Peb
->ProcessHeap
;
232 /***********************************************************************
233 * GetProcessHeaps (KERNEL32.@)
235 DWORD WINAPI
GetProcessHeaps( DWORD count
, HANDLE
*heaps
)
237 return RtlGetProcessHeaps( count
, heaps
);
242 /* FIXME: these functions are needed for dlls that aren't properly separated yet */
244 LPVOID WINAPI
HeapAlloc( HANDLE heap
, DWORD flags
, SIZE_T size
)
246 return RtlAllocateHeap( heap
, flags
, size
);
249 BOOL WINAPI
HeapFree( HANDLE heap
, DWORD flags
, LPVOID ptr
)
251 return RtlFreeHeap( heap
, flags
, ptr
);
254 LPVOID WINAPI
HeapReAlloc( HANDLE heap
, DWORD flags
, LPVOID ptr
, SIZE_T size
)
256 return RtlReAllocateHeap( heap
, flags
, ptr
, size
);
259 SIZE_T WINAPI
HeapSize( HANDLE heap
, DWORD flags
, LPVOID ptr
)
261 return RtlSizeHeap( heap
, flags
, ptr
);