Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / support / support-heap.c
1 /*
2  * Emulates the Heap* routines.
3  *
4  * Authors:
5  *   Gonzalo Paniagua (gonzalo@ximian.com)
6  *   Miguel de Icaza  (miguel@novell.com)
7  *
8  * (C) 2005 Novell, Inc.
9  *
10  */
11 #include <glib.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include "supportw.h"
16
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);
21
22 gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
23                                gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
24
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);
31
32 typedef struct _HeapInfo {
33         gint32 flags;
34         gint32 initial_size;
35         gint32 max_size;
36         GHashTable *hash;
37 } HeapInfo;
38
39 /* Some initial value for the process heap */
40 HeapInfo *process_heap;
41
42 static GHashTable *heaps;
43
44 gpointer
45 HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
46 {
47         HeapInfo *hi;
48         
49         if (heaps == NULL)
50                 heaps = g_hash_table_new (g_direct_hash, g_direct_equal);
51
52         if (flags != 0)
53                 g_warning ("Flags for HeapCreate are the unsupported value non-zero");
54         
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);
60         
61         g_hash_table_insert (heaps, hi, hi);
62         
63         return hi;
64 }
65
66 gboolean
67 HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
68                     gint32 head_info_length)
69 {
70         return TRUE;
71 }
72
73 gboolean
74 HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
75                         gint32 head_info_length, gint32 *ret_length)
76 {
77         *ret_length = 0;
78         return TRUE;
79 }
80
81 gpointer
82 HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
83 {
84         HeapInfo *heap = (HeapInfo *) handle;
85         void *ptr;
86         
87         ptr = g_malloc0 (nbytes);
88
89         g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
90         
91         return ptr;
92 }
93
94 gpointer
95 HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
96 {
97         HeapInfo *heap = (HeapInfo *) handle;
98         void *ptr;
99         
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));
103
104         return ptr;
105 }
106
107 gint32
108 HeapSize (gpointer handle, gint32 flags, gpointer mem)
109 {
110         HeapInfo *heap = (HeapInfo *) handle;
111
112         gint32 size = GPOINTER_TO_INT (g_hash_table_lookup (heap->hash, mem));
113
114         return size;
115 }
116
117 gboolean
118 HeapFree (gpointer handle, gint32 flags, gpointer mem)
119 {
120         HeapInfo *heap = (HeapInfo *) handle;
121
122         g_hash_table_remove (heap->hash, GINT_TO_POINTER (mem));
123         g_free (mem);
124         
125         return TRUE;
126 }
127
128 gboolean
129 HeapValidate (gpointer handle, gpointer mem)
130 {
131         return TRUE;
132 }
133
134 static void
135 free_handles (gpointer key, gpointer value, gpointer user_data)
136 {
137         g_free (key);
138 }
139
140 gboolean
141 HeapDestroy (gpointer handle)
142 {
143         HeapInfo *heap = (HeapInfo *) handle;
144
145         /* Failure is zero */
146         if (handle == process_heap)
147                 return 0;
148         
149         g_hash_table_foreach (heap->hash, free_handles, NULL);
150         g_hash_table_destroy (heap->hash);
151         
152         g_hash_table_remove (heaps, handle);
153         g_free (heap);
154
155         return 1;
156 }
157
158 gpointer GetProcessHeap (void);
159
160 gpointer 
161 GetProcessHeap (void)
162 {
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;
168                 
169         }
170         return process_heap;
171 }
172 /* end Heap* functions */