Release 951212
[wine/gsoc-2012-control.git] / toolkit / heap.c
blob7bb3afd6a9801dc9ff8b692ded3ab942cdd6b551
1 /*
2 * Memory alllocation for the Wine Library toolkit
4 * Copyright (C) 1994 Miguel de Icaza
6 * All the memory management is being done by the libc malloc and friends.
7 */
9 /* #ifndef __STDC__ */
10 #include <malloc.h>
11 #include <stdio.h>
12 #include <string.h>
13 /* #endif */
14 #include "windows.h"
15 #include "xmalloc.h"
17 #ifdef WINELIB16
19 /* Controls the blocks per handle table */
20 #define MAXBLOCKS 1024
22 static char Copyright [] = "Copyright (C) 1994 Miguel de Icaza";
24 typedef struct handle_table {
25 struct handle_table *next;
26 void *blocks [MAXBLOCKS];
27 } handle_table_t;
29 static handle_table_t handle_table;
31 static void **HEAP_GetFreeSlot (HANDLE *hNum)
33 handle_table_t *table, *last;
34 int i, j;
36 for (table = &handle_table, j = 0; table; table = table->next, j++){
37 for (i = 0; i < MAXBLOCKS; i++)
38 if (!table->blocks [i])
39 goto AssignBlock;
40 last = table;
43 /* No free slots */
44 last->next = xmalloc (sizeof (handle_table_t));
45 table = last->next;
46 memset (table, 0, sizeof (handle_table_t));
47 i = 0;
49 AssignBlock:
50 *hNum = j*MAXBLOCKS+i;
51 return &table->blocks [i];
54 static void HEAP_Handle_is_Zero ()
56 printf ("Warning: Handle is Zero, segmentation fault comming\n");
59 static void **HEAP_FindSlot (HANDLE hNum)
61 handle_table_t *table = &handle_table;
62 int i, j;
64 if (!hNum)
65 HEAP_Handle_is_Zero ();
67 hNum--;
68 for (j = hNum; j > MAXBLOCKS; j -= MAXBLOCKS){
69 table = table->next;
70 if (!table) return 0;
72 return &table->blocks [hNum%MAXBLOCKS];
75 HANDLE LocalAlloc (WORD flags, WORD bytes)
77 void *m;
78 void **slot;
79 HANDLE hMem;
81 slot = HEAP_GetFreeSlot (&hMem);
82 if (flags & LMEM_WINE_ALIGN)
83 m = memalign (4, bytes);
84 else
85 m = malloc (bytes);
86 if (m){
87 *slot = m;
88 if (flags & LMEM_ZEROINIT)
89 bzero (m, bytes);
91 #ifdef DEBUG_HEAP
92 printf ("Handle %d [%d] = %p\n", hMem+1, bytes, m);
93 #endif
94 return hMem+1;
96 return 0;
99 WORD LocalCompact (WORD min_free)
101 return min_free;
104 WORD LocalFlags (HANDLE hMem)
106 return 0;
109 HANDLE LocalFree (HANDLE hMem)
111 void **m;
112 if(!hMem)
113 return 0;
114 m = HEAP_FindSlot (hMem);
116 free (*m);
117 *m = 0;
118 return 0;
121 BOOL LocalInit (WORD segment, WORD start, WORD end)
123 return 1;
126 WORD LocalLock (HANDLE hMem)
128 void **m = HEAP_FindSlot (hMem);
129 #ifdef DEBUG_HEAP
130 printf (">%d->%p\n", hMem, *m);
131 #endif
132 return m ? *m : 0;
135 HANDLE LocalReAlloc (HANDLE hMem, WORD flags, WORD bytes)
137 void **m = HEAP_FindSlot (hMem);
139 xrealloc (*m, bytes);
142 WORD LocalSize (HANDLE hMem)
144 /* Not implemented yet */
148 BOOL LocalUnlock (HANDLE hMem)
150 return 0;
153 HANDLE GlobalAlloc (WORD flags, DWORD size)
155 return LocalAlloc (flags, size);
158 HANDLE GlobalFree (HANDLE hMem)
160 return LocalFree (hMem);
163 char *GlobalLock (HANDLE hMem)
165 return LocalLock (hMem);
168 BOOL GlobalUnlock (HANDLE hMem)
170 return LocalUnlock (hMem);
173 WORD GlobalFlags (HANDLE hMem)
175 return LocalFlags (hMem);
178 DWORD GlobalSize (HANDLE hMem)
180 return LocalSize (hMem);
183 DWORD GlobalCompact(DWORD desired)
185 if (desired)
186 return desired;
187 else
188 return 0x01000000; /* Should check the available core. */
191 HANDLE GlobalReAlloc(HANDLE hMem, DWORD new_size, WORD flags)
193 if (!(flags & GMEM_MODIFY))
194 return LocalReAlloc (hMem, new_size, flags);
197 int HEAP_LocalSize ()
199 return 0;
202 int HEAP_LocalFindHeap ()
204 return 0;
207 #ifdef UNIMPLEMENTED
209 DWORD int GlobalHandle(WORD selector)
213 #endif
215 #else /* WINELIB16 */
217 typedef struct { DWORD Size; DWORD Padding[3]; } HeapData;
219 HANDLE HEAP_Alloc (WORD flags, DWORD bytes)
221 HeapData* m;
223 bytes+=sizeof(HeapData);
224 if (flags & LMEM_WINE_ALIGN)
225 m = memalign (4, bytes);
226 else
227 m = malloc (bytes);
228 if (m){
229 if (flags & LMEM_ZEROINIT)
230 bzero (m, bytes);
232 m->Size=bytes-sizeof(HeapData);
233 return m+1;
236 HANDLE HEAP_Free (HANDLE hMem)
238 HeapData* m;
239 if(!hMem)
240 return 0;
241 m=(HeapData*)hMem;
242 free(m-1);
243 return 0;
246 DWORD HEAP_Size (HANDLE hMem)
248 HeapData* m=(HeapData*)hMem;
249 return (m-1)->Size;
252 HANDLE HEAP_ReAlloc(HANDLE hMem,DWORD bytes,UINT flags)
254 HeapData* m=(HeapData*)hMem;
255 if(!bytes)
257 free(m-1);
258 return 0; /* Inaccurate behavior, but should suffice */
260 m=realloc (m-1, bytes+sizeof(HeapData));
261 if(flags & LMEM_ZEROINIT && bytes > m->Size)
262 bzero ((char*)m+sizeof(HeapData)+m->Size, bytes-m->Size);
263 m->Size=bytes;
264 return m+1;
267 HANDLE LocalAlloc (WORD flags, WORD bytes)
269 return HEAP_Alloc(flags,bytes);
272 UINT LocalFlags (HANDLE hMem)
274 return 0;
277 HANDLE LocalFree (HANDLE hMem)
279 return HEAP_Free(hMem);
282 BOOL LocalInit (HANDLE segment, WORD start, WORD end)
284 return TRUE;
287 LPVOID LocalLock (HANDLE hMem)
289 return hMem;
292 HANDLE LocalReAlloc (HANDLE hMem, WORD new_size, WORD flags)
294 if (!(flags & LMEM_MODIFY))
295 return HEAP_ReAlloc (hMem, new_size, flags);
296 else
297 return hMem;
300 UINT LocalSize (HANDLE hMem)
302 return HEAP_Size(hMem);
305 BOOL LocalUnlock (HANDLE hMem)
307 return 0;
310 HANDLE GlobalAlloc (WORD flags, DWORD size)
312 return HEAP_Alloc (flags,size);
315 HANDLE GlobalFree (HANDLE hMem)
317 return HEAP_Free (hMem);
320 LPVOID GlobalLock (HGLOBAL hMem)
322 return hMem;
325 BOOL GlobalUnlock (HANDLE hMem)
327 return 0;
330 WORD GlobalFlags (HANDLE hMem)
332 return LocalFlags (hMem);
335 DWORD GlobalSize (HANDLE hMem)
337 return HEAP_Size (hMem);
340 HANDLE GlobalReAlloc(HANDLE hMem, DWORD new_size, WORD flags)
342 if (!(flags & GMEM_MODIFY))
343 return HEAP_ReAlloc (hMem, new_size, flags);
344 else
345 return hMem;
348 #endif