Fix the case of product and company names.
[wine] / memory / heap.c
1 /*
2  * Win32 heap functions
3  *
4  * Copyright 1996 Alexandre Julliard
5  * Copyright 1998 Ulrich Weigand
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
29
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"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(heap);
41
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 */
45
46 static HANDLE systemHeap;   /* globally shared heap */
47
48 /***********************************************************************
49  *           HEAP_CreateSystemHeap
50  *
51  * Create the system heap.
52  */
53 inline static HANDLE HEAP_CreateSystemHeap(void)
54 {
55     int created;
56     void *base;
57     HANDLE map, event;
58     UNICODE_STRING event_name;
59     OBJECT_ATTRIBUTES event_attr;
60
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);
64
65     if (!(base = MapViewOfFileEx( map, FILE_MAP_ALL_ACCESS, 0, 0, 0, SYSTEM_HEAP_BASE )))
66     {
67         /* pre-defined address not available */
68         ERR( "system heap base address %p not available\n", SYSTEM_HEAP_BASE );
69         return 0;
70     }
71
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 );
81
82     if (created)  /* newly created heap */
83     {
84         systemHeap = RtlCreateHeap( HEAP_SHARED, base, SYSTEM_HEAP_SIZE,
85                                     SYSTEM_HEAP_SIZE, NULL, NULL );
86         NtSetEvent( event, NULL );
87     }
88     else
89     {
90         /* wait for the heap to be initialized */
91         WaitForSingleObject( event, INFINITE );
92         systemHeap = (HANDLE)base;
93     }
94     CloseHandle( map );
95     return systemHeap;
96 }
97
98
99 /***********************************************************************
100  *           HeapCreate   (KERNEL32.@)
101  * RETURNS
102  *      Handle of heap: Success
103  *      NULL: Failure
104  */
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 */
109 ) {
110     HANDLE ret;
111
112     if ( flags & HEAP_SHARED )
113     {
114         if (!systemHeap) HEAP_CreateSystemHeap();
115         else WARN( "Shared Heap requested, returning system heap.\n" );
116         ret = systemHeap;
117     }
118     else
119     {
120         ret = RtlCreateHeap( flags, NULL, maxSize, initialSize, NULL, NULL );
121         if (!ret) SetLastError( ERROR_NOT_ENOUGH_MEMORY );
122     }
123     return ret;
124 }
125
126 /***********************************************************************
127  *           HeapDestroy   (KERNEL32.@)
128  * RETURNS
129  *      TRUE: Success
130  *      FALSE: Failure
131  */
132 BOOL WINAPI HeapDestroy( HANDLE heap /* [in] Handle of heap */ )
133 {
134     if (heap == systemHeap)
135     {
136         WARN( "attempt to destroy system heap, returning TRUE!\n" );
137         return TRUE;
138     }
139     if (!RtlDestroyHeap( heap )) return TRUE;
140     SetLastError( ERROR_INVALID_HANDLE );
141     return FALSE;
142 }
143
144
145 /***********************************************************************
146  *           HeapCompact   (KERNEL32.@)
147  */
148 SIZE_T WINAPI HeapCompact( HANDLE heap, DWORD flags )
149 {
150     return RtlCompactHeap( heap, flags );
151 }
152
153
154 /***********************************************************************
155  *           HeapLock   (KERNEL32.@)
156  * Attempts to acquire the critical section object for a specified heap.
157  *
158  * RETURNS
159  *      TRUE: Success
160  *      FALSE: Failure
161  */
162 BOOL WINAPI HeapLock(
163               HANDLE heap /* [in] Handle of heap to lock for exclusive access */
164 ) {
165     return RtlLockHeap( heap );
166 }
167
168
169 /***********************************************************************
170  *           HeapUnlock   (KERNEL32.@)
171  * Releases ownership of the critical section object.
172  *
173  * RETURNS
174  *      TRUE: Success
175  *      FALSE: Failure
176  */
177 BOOL WINAPI HeapUnlock(
178               HANDLE heap /* [in] Handle to the heap to unlock */
179 ) {
180     return RtlUnlockHeap( heap );
181 }
182
183
184 /***********************************************************************
185  *           HeapValidate   (KERNEL32.@)
186  * Validates a specified heap.
187  *
188  * NOTES
189  *      Flags is ignored.
190  *
191  * RETURNS
192  *      TRUE: Success
193  *      FALSE: Failure
194  */
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 */
199 ) {
200     return RtlValidateHeap( heap, flags, block );
201 }
202
203
204 /***********************************************************************
205  *           HeapWalk   (KERNEL32.@)
206  * Enumerates the memory blocks in a specified heap.
207  *
208  * TODO
209  *   - handling of PROCESS_HEAP_ENTRY_MOVEABLE and
210  *     PROCESS_HEAP_ENTRY_DDESHARE (needs heap.c support)
211  *
212  * RETURNS
213  *      TRUE: Success
214  *      FALSE: Failure
215  */
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 */
219 ) {
220     NTSTATUS ret = RtlWalkHeap( heap, entry );
221     if (ret) SetLastError( RtlNtStatusToDosError(ret) );
222     return !ret;
223 }
224
225
226 /***********************************************************************
227  *           GetProcessHeap    (KERNEL32.@)
228  */
229 HANDLE WINAPI GetProcessHeap(void)
230 {
231     return NtCurrentTeb()->Peb->ProcessHeap;
232 }
233
234
235 /***********************************************************************
236  *           GetProcessHeaps    (KERNEL32.@)
237  */
238 DWORD WINAPI GetProcessHeaps( DWORD count, HANDLE *heaps )
239 {
240     return RtlGetProcessHeaps( count, heaps );
241 }
242
243
244
245 /* FIXME: these functions are needed for dlls that aren't properly separated yet */
246
247 LPVOID WINAPI HeapAlloc( HANDLE heap, DWORD flags, SIZE_T size )
248 {
249     return RtlAllocateHeap( heap, flags, size );
250 }
251
252 BOOL WINAPI HeapFree( HANDLE heap, DWORD flags, LPVOID ptr )
253 {
254     return RtlFreeHeap( heap, flags, ptr );
255 }
256
257 LPVOID WINAPI HeapReAlloc( HANDLE heap, DWORD flags, LPVOID ptr, SIZE_T size )
258 {
259     return RtlReAllocateHeap( heap, flags, ptr, size );
260 }
261
262 SIZE_T WINAPI HeapSize( HANDLE heap, DWORD flags, LPVOID ptr )
263 {
264     return RtlSizeHeap( heap, flags, ptr );
265 }