Skip to content

Commit 5325d10

Browse files
committed
Implement the heap stuff, move it to a separate file
svn path=/trunk/mono/; revision=51008
1 parent 8fd920b commit 5325d10

File tree

4 files changed

+203
-81
lines changed

4 files changed

+203
-81
lines changed

support/ChangeLog

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2005-09-29 Miguel de Icaza <miguel@novell.com>
2+
3+
* support-heap.c: Add meat to the Heap routines.
4+
15
2005-09-20 Jonathan Pryor <jonpryor@vt.edu>
26

37
* Makefile.am (refresh): Use the make-map.exe in Mono.Unix.Native.

support/Makefile.am

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,10 @@ libMonoSupportW_la_LDFLAGS = -no-undefined -avoid-version
7878

7979
libMonoSupportW_la_SOURCES = \
8080
supportw.c \
81+
support-heap.c \
8182
supportw.h
8283

83-
libMonoSupportW_la_LIBADD = \
84+
libMonoSupportW_la_LIBADD = \
8485
$(GLIB_LIBS)
8586

8687
#

support/support-heap.c

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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 */

support/supportw.c

+25-80
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
/*
2+
* Helper routines for some of the common methods that people P/Invoke
3+
* on their applications.
4+
*
5+
* Authors:
6+
* Gonzalo Paniagua (gonzalo@ximian.com)
7+
* Miguel de Icaza (miguel@novell.com)
8+
*
9+
* (C) 2005 Novell, Inc.
10+
*
11+
*/
112
#include <glib.h>
213
#include <stdlib.h>
314
#include <string.h>
@@ -14,24 +25,24 @@ typedef struct {
1425
void *fnptr;
1526
} FnPtr;
1627

17-
gpointer FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter,
18-
const char *classw, const char *window);
28+
gpointer FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter,
29+
const char *classw, const char *window);
1930

20-
gpointer HeapAlloc (gpointer unused1, gint32 unused2, gint32 nbytes);
21-
gpointer HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size);
22-
gboolean HeapSetInformation (gpointer handle, gpointer heap_info_class,
23-
gpointer heap_info, gint32 head_info_length);
31+
gpointer HeapAlloc (gpointer unused1, gint32 unused2, gint32 nbytes);
32+
gpointer HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size);
33+
gboolean HeapSetInformation (gpointer handle, gpointer heap_info_class,
34+
gpointer heap_info, gint32 head_info_length);
2435

2536
gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
26-
gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
37+
gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
2738

28-
gpointer HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes);
29-
gpointer HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
30-
gint32 HeapSize (gpointer handle, gint32 flags, gpointer mem);
31-
gboolean HeapFree (gpointer handle, gint32 flags, gpointer mem);
32-
gboolean HeapValidate (gpointer handle, gpointer mem);
33-
gboolean HeapDestroy (gpointer handle);
34-
gpointer GetProcessHeap (void);
39+
gpointer HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes);
40+
gpointer HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
41+
gint32 HeapSize (gpointer handle, gint32 flags, gpointer mem);
42+
gboolean HeapFree (gpointer handle, gint32 flags, gpointer mem);
43+
gboolean HeapValidate (gpointer handle, gpointer mem);
44+
gboolean HeapDestroy (gpointer handle);
45+
gpointer GetProcessHeap (void);
3546

3647
static FnPtr functions [] = {
3748
{ "FindWindowExW", NULL }, /* user32 */
@@ -161,70 +172,4 @@ FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw,
161172
return func (hwndParent, hwndChildAfter, classw, window);
162173
}
163174

164-
/* begin Heap* functions */
165-
gpointer
166-
HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
167-
{
168-
return (gpointer) 0xDEADBEEF;
169-
}
170-
171-
gboolean
172-
HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
173-
gint32 head_info_length)
174-
{
175-
return TRUE;
176-
}
177-
178-
gboolean
179-
HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
180-
gint32 head_info_length, gint32 *ret_length)
181-
{
182-
*ret_length = 0;
183-
return TRUE;
184-
}
185-
186-
gpointer
187-
HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
188-
{
189-
return g_malloc0 (nbytes);
190-
}
191-
192-
gpointer
193-
HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
194-
{
195-
return g_realloc (mem, nbytes);
196-
}
197-
198-
gint32
199-
HeapSize (gpointer handle, gint32 flags, gpointer mem)
200-
{
201-
return 0;
202-
}
203-
204-
gboolean
205-
HeapFree (gpointer handle, gint32 flags, gpointer mem)
206-
{
207-
g_free (mem);
208-
return TRUE;
209-
}
210-
211-
gboolean
212-
HeapValidate (gpointer handle, gpointer mem)
213-
{
214-
return TRUE;
215-
}
216-
217-
gboolean
218-
HeapDestroy (gpointer handle)
219-
{
220-
return TRUE;
221-
}
222-
223-
224-
gpointer
225-
GetProcessHeap ()
226-
{
227-
return (gpointer) 0xDEADBEEF;
228-
}
229-
/* end Heap* functions */
230175

0 commit comments

Comments
 (0)