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.
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
];
29 static handle_table_t handle_table
;
31 static void **HEAP_GetFreeSlot (HANDLE
*hNum
)
33 handle_table_t
*table
, *last
;
36 for (table
= &handle_table
, j
= 0; table
; table
= table
->next
, j
++){
37 for (i
= 0; i
< MAXBLOCKS
; i
++)
38 if (!table
->blocks
[i
])
44 last
->next
= xmalloc (sizeof (handle_table_t
));
46 memset (table
, 0, sizeof (handle_table_t
));
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
;
65 HEAP_Handle_is_Zero ();
68 for (j
= hNum
; j
> MAXBLOCKS
; j
-= MAXBLOCKS
){
72 return &table
->blocks
[hNum
%MAXBLOCKS
];
75 HANDLE
LocalAlloc (WORD flags
, WORD bytes
)
81 slot
= HEAP_GetFreeSlot (&hMem
);
82 if ((m
= malloc (bytes
)))
85 if (flags
& LMEM_ZEROINIT
)
89 printf ("Handle %d [%d] = %p\n", hMem
+1, bytes
, m
);
96 WORD
LocalCompact (WORD min_free
)
101 WORD
LocalFlags (HANDLE hMem
)
106 HANDLE
LocalFree (HANDLE hMem
)
111 m
= HEAP_FindSlot (hMem
);
118 BOOL
LocalInit (WORD segment
, WORD start
, WORD end
)
123 WORD
LocalLock (HANDLE hMem
)
125 void **m
= HEAP_FindSlot (hMem
);
127 printf (">%d->%p\n", hMem
, *m
);
132 HANDLE
LocalReAlloc (HANDLE hMem
, WORD flags
, WORD bytes
)
134 void **m
= HEAP_FindSlot (hMem
);
136 xrealloc (*m
, bytes
);
139 WORD
LocalSize (HANDLE hMem
)
141 /* Not implemented yet */
145 BOOL
LocalUnlock (HANDLE hMem
)
150 HANDLE
GlobalAlloc (WORD flags
, DWORD size
)
152 return LocalAlloc (flags
, size
);
155 HANDLE
GlobalFree (HANDLE hMem
)
157 return LocalFree (hMem
);
160 char *GlobalLock (HANDLE hMem
)
162 return LocalLock (hMem
);
165 BOOL
GlobalUnlock (HANDLE hMem
)
167 return LocalUnlock (hMem
);
170 WORD
GlobalFlags (HANDLE hMem
)
172 return LocalFlags (hMem
);
175 DWORD
GlobalSize (HANDLE hMem
)
177 return LocalSize (hMem
);
180 DWORD
GlobalCompact(DWORD desired
)
185 return 0x01000000; /* Should check the available core. */
188 HANDLE
GlobalReAlloc(HANDLE hMem
, DWORD new_size
, WORD flags
)
190 if (!(flags
& GMEM_MODIFY
))
191 return LocalReAlloc (hMem
, new_size
, flags
);
194 int HEAP_LocalSize ()
199 int HEAP_LocalFindHeap ()
206 DWORD
int GlobalHandle(WORD selector
)
212 #else /* WINELIB16 */
214 typedef struct { DWORD Size
; DWORD Padding
[3]; } HeapData
;
216 HANDLE
HEAP_Alloc (WORD flags
, DWORD bytes
)
220 bytes
+=sizeof(HeapData
);
221 if ((m
= malloc (bytes
)))
223 if (flags
& LMEM_ZEROINIT
)
226 m
->Size
=bytes
-sizeof(HeapData
);
230 HANDLE
HEAP_Free (HANDLE hMem
)
240 DWORD
HEAP_Size (HANDLE hMem
)
242 HeapData
* m
=(HeapData
*)hMem
;
246 HANDLE
HEAP_ReAlloc(HANDLE hMem
,DWORD bytes
,UINT flags
)
248 HeapData
* m
=(HeapData
*)hMem
;
252 return 0; /* Inaccurate behavior, but should suffice */
254 m
=realloc (m
-1, bytes
+sizeof(HeapData
));
255 if(flags
& LMEM_ZEROINIT
&& bytes
> m
->Size
)
256 bzero ((char*)m
+sizeof(HeapData
)+m
->Size
, bytes
-m
->Size
);
261 HANDLE
LocalAlloc (WORD flags
, WORD bytes
)
263 return HEAP_Alloc(flags
,bytes
);
266 UINT
LocalFlags (HANDLE hMem
)
271 HANDLE
LocalFree (HANDLE hMem
)
273 return HEAP_Free(hMem
);
276 BOOL
LocalInit (HANDLE segment
, WORD start
, WORD end
)
281 LPVOID
LocalLock (HANDLE hMem
)
286 HANDLE
LocalReAlloc (HANDLE hMem
, WORD new_size
, WORD flags
)
288 if (!(flags
& LMEM_MODIFY
))
289 return HEAP_ReAlloc (hMem
, new_size
, flags
);
294 UINT
LocalSize (HANDLE hMem
)
296 return HEAP_Size(hMem
);
299 BOOL
LocalUnlock (HANDLE hMem
)
304 HANDLE
GlobalAlloc (WORD flags
, DWORD size
)
306 return HEAP_Alloc (flags
,size
);
309 HANDLE
GlobalFree (HANDLE hMem
)
311 return HEAP_Free (hMem
);
314 LPVOID
GlobalLock (HGLOBAL hMem
)
319 BOOL
GlobalUnlock (HANDLE hMem
)
324 WORD
GlobalFlags (HANDLE hMem
)
326 return LocalFlags (hMem
);
329 DWORD
GlobalSize (HANDLE hMem
)
331 return HEAP_Size (hMem
);
334 HANDLE
GlobalReAlloc(HANDLE hMem
, DWORD new_size
, WORD flags
)
336 if (!(flags
& GMEM_MODIFY
))
337 return HEAP_ReAlloc (hMem
, new_size
, flags
);