2007-03-09 Chris Toshok <toshok@ximian.com>
[mono-project/dkf.git] / support / support-heap.c
blob89cb3468e0a73e1215350da3ea2a2d4b94997fe0
1 /*
2 * Emulates the Heap* routines.
4 * Authors:
5 * Gonzalo Paniagua (gonzalo@ximian.com)
6 * Miguel de Icaza (miguel@novell.com)
8 * (C) 2005 Novell, Inc.
11 #include <glib.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "supportw.h"
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 {
33 gint32 flags;
34 gint32 initial_size;
35 gint32 max_size;
36 GHashTable *hash;
37 } HeapInfo;
39 /* Some initial value for the process heap */
40 HeapInfo *process_heap;
42 static GHashTable *heaps;
44 gpointer
45 HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
47 HeapInfo *hi;
49 if (heaps == NULL)
50 heaps = g_hash_table_new (g_direct_hash, g_direct_equal);
52 if (flags != 0)
53 g_warning ("Flags for HeapCreate are the unsupported value non-zero");
55 hi = g_new (HeapInfo, 1);
56 hi->flags = flags;
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);
63 return hi;
66 gboolean
67 HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
68 gint32 head_info_length)
70 return TRUE;
73 gboolean
74 HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
75 gint32 head_info_length, gint32 *ret_length)
77 *ret_length = 0;
78 return TRUE;
81 gpointer
82 HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
84 HeapInfo *heap = (HeapInfo *) handle;
85 void *ptr;
87 ptr = g_malloc0 (nbytes);
89 g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
91 return ptr;
94 gpointer
95 HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
97 HeapInfo *heap = (HeapInfo *) handle;
98 void *ptr;
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));
104 return ptr;
107 gint32
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));
114 return size;
117 gboolean
118 HeapFree (gpointer handle, gint32 flags, gpointer mem)
120 HeapInfo *heap = (HeapInfo *) handle;
122 g_hash_table_remove (heap->hash, GINT_TO_POINTER (mem));
123 g_free (mem);
125 return TRUE;
128 gboolean
129 HeapValidate (gpointer handle, gpointer mem)
131 return TRUE;
134 static void
135 free_handles (gpointer key, gpointer value, gpointer user_data)
137 g_free (key);
140 gboolean
141 HeapDestroy (gpointer handle)
143 HeapInfo *heap = (HeapInfo *) handle;
145 /* Failure is zero */
146 if (handle == process_heap)
147 return 0;
149 g_hash_table_foreach (heap->hash, free_handles, NULL);
150 g_hash_table_destroy (heap->hash);
152 g_hash_table_remove (heaps, handle);
153 g_free (heap);
155 return 1;
158 gpointer GetProcessHeap (void);
160 gpointer
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;
170 return process_heap;
172 /* end Heap* functions */