On TlsFree, clear the released TLS index in all threads.
[wine/multimedia.git] / memory / heap.c
blob689d3a2d8d8fbcb1de0b389a2b67e7a89e85912b
1 /*
2 * Win32 heap functions
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
22 #include "config.h"
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winerror.h"
33 #include "winnt.h"
34 #include "winreg.h"
35 #include "winternl.h"
36 #include "wine/unicode.h"
37 #include "thread.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(heap);
42 /* address where we try to map the system heap */
43 #define SYSTEM_HEAP_BASE ((void*)0x65430000)
44 #define SYSTEM_HEAP_SIZE 0x100000 /* Default heap size = 1Mb */
46 static HANDLE systemHeap; /* globally shared heap */
48 /***********************************************************************
49 * HEAP_CreateSystemHeap
51 * Create the system heap.
53 inline static HANDLE HEAP_CreateSystemHeap(void)
55 int created;
56 void *base;
57 HANDLE map, event;
58 UNICODE_STRING event_name;
59 OBJECT_ATTRIBUTES event_attr;
61 if (!(map = CreateFileMappingA( INVALID_HANDLE_VALUE, NULL, SEC_COMMIT | PAGE_READWRITE,
62 0, SYSTEM_HEAP_SIZE, "__SystemHeap" ))) return 0;
63 created = (GetLastError() != ERROR_ALREADY_EXISTS);
65 if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
67 /* pre-defined address not available */
68 ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
69 return 0;
72 /* create the system heap event */
73 RtlCreateUnicodeStringFromAsciiz( &event_name, "__SystemHeapEvent" );
74 event_attr.Length = sizeof(event_attr);
75 event_attr.RootDirectory = 0;
76 event_attr.ObjectName = &event_name;
77 event_attr.Attributes = 0;
78 event_attr.SecurityDescriptor = NULL;
79 event_attr.SecurityQualityOfService = NULL;
80 NtCreateEvent( &event, EVENT_ALL_ACCESS, &event_attr, TRUE, FALSE );
82 if (created) /* newly created heap */
84 systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
85 SYSTEM_HEAP_SIZE, NULL, NULL );
86 NtSetEvent( event, NULL );
88 else
90 /* wait for the heap to be initialized */
91 WaitForSingleObject( event, INFINITE );
92 systemHeap = (HANDLE)base;
94 CloseHandle( map );
95 return systemHeap;
99 /***********************************************************************
100 * HeapCreate (KERNEL32.@)
101 * RETURNS
102 * Handle of heap: Success
103 * NULL: Failure
105 HANDLE WINAPI HeapCreate(
106 DWORD flags, /* [in] Heap allocation flag */
107 SIZE_T initialSize, /* [in] Initial heap size */
108 SIZE_T maxSize /* [in] Maximum heap size */
110 HANDLE ret;
112 if ( flags & HEAP_SHARED )
114 if (!systemHeap) HEAP_CreateSystemHeap();
115 else WARN( "Shared Heap requested, returning system heap.\n" );
116 ret = systemHeap;
118 else
120 ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
121 if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
123 return ret;
126 /***********************************************************************
127 * HeapDestroy (KERNEL32.@)
128 * RETURNS
129 * TRUE: Success
130 * FALSE: Failure
132 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
134 if (heap == systemHeap)
136 WARN( "attempt to destroy system heap, returning TRUE!\n" );
137 return TRUE;
139 if (!RtlDestroyHeap( heap )) return TRUE;
140 SetLastError( ERROR_INVALID_HANDLE );
141 return FALSE;
145 /***********************************************************************
146 * HeapCompact (KERNEL32.@)
148 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
150 return RtlCompactHeap( heap, flags );
154 /***********************************************************************
155 * HeapLock (KERNEL32.@)
156 * Attempts to acquire the critical section object for a specified heap.
158 * RETURNS
159 * TRUE: Success
160 * FALSE: Failure
162 BOOL WINAPI HeapLock(
163 HANDLE heap /* [in] Handle of heap to lock for exclusive access */
165 return RtlLockHeap( heap );
169 /***********************************************************************
170 * HeapUnlock (KERNEL32.@)
171 * Releases ownership of the critical section object.
173 * RETURNS
174 * TRUE: Success
175 * FALSE: Failure
177 BOOL WINAPI HeapUnlock(
178 HANDLE heap /* [in] Handle to the heap to unlock */
180 return RtlUnlockHeap( heap );
184 /***********************************************************************
185 * HeapValidate (KERNEL32.@)
186 * Validates a specified heap.
188 * NOTES
189 * Flags is ignored.
191 * RETURNS
192 * TRUE: Success
193 * FALSE: Failure
195 BOOL WINAPI HeapValidate(
196 HANDLE heap, /* [in] Handle to the heap */
197 DWORD flags, /* [in] Bit flags that control access during operation */
198 LPCVOID block /* [in] Optional pointer to memory block to validate */
200 return RtlValidateHeap( heap, flags, block );
204 /***********************************************************************
205 * HeapWalk (KERNEL32.@)
206 * Enumerates the memory blocks in a specified heap.
208 * TODO
209 * - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
210 * PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
212 * RETURNS
213 * TRUE: Success
214 * FALSE: Failure
216 BOOL WINAPI HeapWalk(
217 HANDLE heap, /* [in] Handle to heap to enumerate */
218 LPPROCESS_HEAP_ENTRY entry /* [out] Pointer to structure of enumeration info */
220 NTSTATUS ret = RtlWalkHeap( heap, entry );
221 if (ret) SetLastError( RtlNtStatusToDosError(ret) );
222 return !ret;
226 /***********************************************************************
227 * GetProcessHeap (KERNEL32.@)
229 HANDLE WINAPI GetProcessHeap(void)
231 return NtCurrentTeb()->Peb->ProcessHeap;
235 /***********************************************************************
236 * GetProcessHeaps (KERNEL32.@)
238 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
240 return RtlGetProcessHeaps( count, heaps );
245 /* FIXME: these functions are needed for dlls that aren't properly separated yet */
247 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
249 return RtlAllocateHeap( heap, flags, size );
252 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
254 return RtlFreeHeap( heap, flags, ptr );
257 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
259 return RtlReAllocateHeap( heap, flags, ptr, size );
262 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
264 return RtlSizeHeap( heap, flags, ptr );