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 (flags
& LMEM_WINE_ALIGN
)
83 m
= memalign (4, bytes
);
88 if (flags
& LMEM_ZEROINIT
)
92 printf ("Handle %d [%d] = %p\n", hMem
+1, bytes
, m
);
99 WORD
LocalCompact (WORD min_free
)
104 WORD
LocalFlags (HANDLE hMem
)
109 HANDLE
LocalFree (HANDLE hMem
)
114 m
= HEAP_FindSlot (hMem
);
121 BOOL
LocalInit (WORD segment
, WORD start
, WORD end
)
126 WORD
LocalLock (HANDLE hMem
)
128 void **m
= HEAP_FindSlot (hMem
);
130 printf (">%d->%p\n", hMem
, *m
);
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
)
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
)
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 ()
202 int HEAP_LocalFindHeap ()
209 DWORD
int GlobalHandle(WORD selector
)
215 #else /* WINELIB16 */
217 typedef struct { DWORD Size
; DWORD Padding
[3]; } HeapData
;
219 HANDLE
HEAP_Alloc (WORD flags
, DWORD bytes
)
223 bytes
+=sizeof(HeapData
);
224 if (flags
& LMEM_WINE_ALIGN
)
225 m
= memalign (4, bytes
);
229 if (flags
& LMEM_ZEROINIT
)
232 m
->Size
=bytes
-sizeof(HeapData
);
236 HANDLE
HEAP_Free (HANDLE hMem
)
246 DWORD
HEAP_Size (HANDLE hMem
)
248 HeapData
* m
=(HeapData
*)hMem
;
252 HANDLE
HEAP_ReAlloc(HANDLE hMem
,DWORD bytes
,UINT flags
)
254 HeapData
* m
=(HeapData
*)hMem
;
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
);
267 HANDLE
LocalAlloc (WORD flags
, WORD bytes
)
269 return HEAP_Alloc(flags
,bytes
);
272 UINT
LocalFlags (HANDLE hMem
)
277 HANDLE
LocalFree (HANDLE hMem
)
279 return HEAP_Free(hMem
);
282 BOOL
LocalInit (HANDLE segment
, WORD start
, WORD end
)
287 LPVOID
LocalLock (HANDLE hMem
)
292 HANDLE
LocalReAlloc (HANDLE hMem
, WORD new_size
, WORD flags
)
294 if (!(flags
& LMEM_MODIFY
))
295 return HEAP_ReAlloc (hMem
, new_size
, flags
);
300 UINT
LocalSize (HANDLE hMem
)
302 return HEAP_Size(hMem
);
305 BOOL
LocalUnlock (HANDLE hMem
)
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
)
325 BOOL
GlobalUnlock (HANDLE hMem
)
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
);