Release 941210
[wine/multimedia.git] / toolkit / heap.c
blobcc4e2d7f929caaa3d6cd4f82bbbd87d58b046495
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 #endif
12 #include "windows.h"
14 /* Controls the blocks per handle table */
15 #define MAXBLOCKS 1024
17 static char Copyright [] = "Copyright (C) 1994 Miguel de Icaza";
19 typedef struct handle_table {
20 struct handle_table *next;
21 void *blocks [MAXBLOCKS];
22 } handle_table_t;
24 static handle_table_t handle_table;
26 static void **HEAP_GetFreeSlot (HANDLE *hNum)
28 handle_table_t *table, *last;
29 int i, j;
31 for (table = &handle_table, j = 0; table; table = table->next, j++){
32 for (i = 0; i < MAXBLOCKS; i++)
33 if (!table->blocks [i])
34 goto AssignBlock;
35 last = table;
38 /* No free slots */
39 last->next = malloc (sizeof (handle_table_t));
40 table = last->next;
41 memset (table, 0, sizeof (handle_table_t));
42 i = 0;
44 AssignBlock:
45 *hNum = j*MAXBLOCKS+i;
46 return &table->blocks [i];
49 static void HEAP_Handle_is_Zero ()
51 printf ("Warning: Handle is Zero, segmentation fault comming\n");
54 static void **HEAP_FindSlot (HANDLE hNum)
56 handle_table_t *table = &handle_table;
57 int i, j;
59 if (!hNum)
60 HEAP_Handle_is_Zero ();
62 hNum--;
63 for (j = hNum; j > MAXBLOCKS; j -= MAXBLOCKS){
64 table = table->next;
65 if (!table) return 0;
67 return &table->blocks [hNum%MAXBLOCKS];
70 HANDLE LocalAlloc (WORD flags, WORD bytes)
72 void *m;
73 void **slot;
74 HANDLE hMem;
76 slot = HEAP_GetFreeSlot (&hMem);
77 if (flags & LMEM_WINE_ALIGN)
78 m = memalign (4, bytes);
79 else
80 m = malloc (bytes);
81 if (m){
82 *slot = m;
83 if (flags & LMEM_ZEROINIT)
84 bzero (m, bytes);
86 #ifdef DEBUG_HEAP
87 printf ("Handle %d [%d] = %p\n", hMem+1, bytes, m);
88 #endif
89 return hMem+1;
91 return 0;
94 WORD LocalCompact (WORD min_free)
96 return min_free;
99 WORD LocalFlags (HANDLE hMem)
101 return 0;
104 HANDLE LocalFree (HANDLE hMem)
106 void **m = HEAP_FindSlot (hMem);
108 free (*m);
109 *m = 0;
110 return 0;
113 BOOL LocalInit (WORD segment, WORD start, WORD end)
115 return 1;
118 char *LocalLock (HANDLE hMem)
120 void **m = HEAP_FindSlot (hMem);
121 #ifdef DEBUG_HEAP
122 printf (">%d->%p\n", hMem, *m);
123 #endif
124 return m ? *m : 0;
127 HANDLE LocalReAlloc (HANDLE hMem, WORD flags, WORD bytes)
129 void **m = HEAP_FindSlot (hMem);
131 realloc (*m, bytes);
134 WORD LocalSize (HANDLE hMem)
136 /* Not implemented yet */
140 BOOL LocalUnLock (HANDLE hMem)
142 return 0;
145 HANDLE GlobalAlloc (WORD flags, DWORD size)
147 return LocalAlloc (flags, size);
150 HANDLE GlobalFree (HANDLE hMem)
152 return LocalFree (hMem);
155 char *GlobalLock (HANDLE hMem)
157 return LocalLock (hMem);
160 BOOL GlobalUnlock (HANDLE hMem)
162 return LocalUnLock (hMem);
165 WORD GlobalFlags (HANDLE hMem)
167 return LocalFlags (hMem);
170 DWORD GlobalSize (HANDLE hMem)
172 return LocalSize (hMem);
175 DWORD GlobalCompact(DWORD desired)
177 if (desired)
178 return desired;
179 else
180 return 0x01000000; /* Should check the available core. */
183 HANDLE GlobalReAlloc(HANDLE hMem, DWORD new_size, WORD flags)
185 if (!(flags & GMEM_MODIFY))
186 return LocalReAlloc (hMem, new_size, flags);
189 char *GlobalLinearLock (HANDLE hMem)
191 return GlobalLock (hMem);
194 HANDLE GlobalLinearUnlock (HANDLE hMem)
196 return GlobalUnlock (hMem);
199 int HEAP_LocalSize ()
201 return 0;
204 int HEAP_LocalFindHeap ()
206 return 0;
209 #ifdef UNIMPLEMENTED
210 void *GlobalQuickAlloc(int size)
214 DWORD int GlobalHandle(WORD selector)
218 unsigned int GlobalHandleFromPointer(void *block)
222 #endif