2 * Emulates the Heap* routines.
5 * Gonzalo Paniagua (gonzalo@ximian.com)
6 * Miguel de Icaza (miguel@novell.com)
8 * (C) 2005 Novell, Inc.
17 gpointer
HeapAlloc (gpointer unused1
, gint32 unused2
, gint32 nbytes
);
18 gpointer
HeapCreate (gint32 flags
, gint32 initial_size
, gint32 max_size
);
19 gboolean
HeapSetInformation (gpointer handle
, gpointer heap_info_class
,
20 gpointer heap_info
, gint32 head_info_length
);
22 gboolean
HeapQueryInformation (gpointer handle
, gpointer heap_info_class
,
23 gpointer heap_info
, gint32 head_info_length
, gint32
*ret_length
);
25 gpointer
HeapAlloc (gpointer handle
, gint32 flags
, gint32 nbytes
);
26 gpointer
HeapReAlloc (gpointer handle
, gint32 flags
, gpointer mem
, gint32 nbytes
);
27 gint32
HeapSize (gpointer handle
, gint32 flags
, gpointer mem
);
28 gboolean
HeapFree (gpointer handle
, gint32 flags
, gpointer mem
);
29 gboolean
HeapValidate (gpointer handle
, gpointer mem
);
30 gboolean
HeapDestroy (gpointer handle
);
32 typedef struct _HeapInfo
{
39 /* Some initial value for the process heap */
40 HeapInfo
*process_heap
;
42 static GHashTable
*heaps
;
45 HeapCreate (gint32 flags
, gint32 initial_size
, gint32 max_size
)
50 heaps
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
53 g_warning ("Flags for HeapCreate are the unsupported value non-zero");
55 hi
= g_new (HeapInfo
, 1);
57 hi
->initial_size
= initial_size
;
58 hi
->max_size
= max_size
;
59 hi
->hash
= g_hash_table_new (g_direct_hash
, g_direct_equal
);
61 g_hash_table_insert (heaps
, hi
, hi
);
67 HeapSetInformation (gpointer handle
, gpointer heap_info_class
, gpointer heap_info
,
68 gint32 head_info_length
)
74 HeapQueryInformation (gpointer handle
, gpointer heap_info_class
, gpointer heap_info
,
75 gint32 head_info_length
, gint32
*ret_length
)
82 HeapAlloc (gpointer handle
, gint32 flags
, gint32 nbytes
)
84 HeapInfo
*heap
= (HeapInfo
*) handle
;
87 ptr
= g_malloc0 (nbytes
);
89 g_hash_table_insert (heap
->hash
, ptr
, GINT_TO_POINTER (nbytes
));
95 HeapReAlloc (gpointer handle
, gint32 flags
, gpointer mem
, gint32 nbytes
)
97 HeapInfo
*heap
= (HeapInfo
*) handle
;
100 g_hash_table_remove (heap
->hash
, mem
);
101 ptr
= g_realloc (mem
, nbytes
);
102 g_hash_table_insert (heap
->hash
, ptr
, GINT_TO_POINTER (nbytes
));
108 HeapSize (gpointer handle
, gint32 flags
, gpointer mem
)
110 HeapInfo
*heap
= (HeapInfo
*) handle
;
112 gint32 size
= GPOINTER_TO_INT (g_hash_table_lookup (heap
->hash
, mem
));
118 HeapFree (gpointer handle
, gint32 flags
, gpointer mem
)
120 HeapInfo
*heap
= (HeapInfo
*) handle
;
122 g_hash_table_remove (heap
->hash
, GINT_TO_POINTER (mem
));
129 HeapValidate (gpointer handle
, gpointer mem
)
135 free_handles (gpointer key
, gpointer value
, gpointer user_data
)
141 HeapDestroy (gpointer handle
)
143 HeapInfo
*heap
= (HeapInfo
*) handle
;
145 /* Failure is zero */
146 if (handle
== process_heap
)
149 g_hash_table_foreach (heap
->hash
, free_handles
, NULL
);
150 g_hash_table_destroy (heap
->hash
);
152 g_hash_table_remove (heaps
, handle
);
158 gpointer
GetProcessHeap (void);
161 GetProcessHeap (void)
163 if (process_heap
== NULL
){
164 process_heap
= g_new (HeapInfo
, 1);
165 process_heap
->flags
= 0;
166 process_heap
->initial_size
= 1024;
167 process_heap
->max_size
= 1024*1024*1024;
172 /* end Heap* functions */