Implement the heap stuff, move it to a separate file
authorMiguel de Icaza <miguel@gnome.org>
Thu, 29 Sep 2005 20:26:16 +0000 (20:26 -0000)
committerMiguel de Icaza <miguel@gnome.org>
Thu, 29 Sep 2005 20:26:16 +0000 (20:26 -0000)
svn path=/trunk/mono/; revision=51008

support/ChangeLog
support/Makefile.am
support/support-heap.c [new file with mode: 0644]
support/supportw.c

index fb6a88868841f179149b715177c86814d87d8f82..6190488476b7c3f79e016ec3403e51c5925bf31a 100644 (file)
@@ -1,3 +1,7 @@
+2005-09-29  Miguel de Icaza  <miguel@novell.com>
+
+       * support-heap.c: Add meat to the Heap routines. 
+
 2005-09-20  Jonathan Pryor  <jonpryor@vt.edu>
 
        * Makefile.am (refresh): Use the make-map.exe in Mono.Unix.Native.
index ee1e2770042659c5cf8eae8cf73d5986a48ea452..c73de3351bc998409fa3860c681d08dcc7e62fdd 100644 (file)
@@ -78,9 +78,10 @@ libMonoSupportW_la_LDFLAGS = -no-undefined -avoid-version
 
 libMonoSupportW_la_SOURCES =                   \
                supportw.c                      \
+               support-heap.c                  \
                supportw.h
 
-libMonoSupportW_la_LIBADD =                                    \
+libMonoSupportW_la_LIBADD =                    \
                $(GLIB_LIBS)
 
 # 
diff --git a/support/support-heap.c b/support/support-heap.c
new file mode 100644 (file)
index 0000000..89cb346
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Emulates the Heap* routines.
+ *
+ * Authors:
+ *   Gonzalo Paniagua (gonzalo@ximian.com)
+ *   Miguel de Icaza  (miguel@novell.com)
+ *
+ * (C) 2005 Novell, Inc.
+ *
+ */
+#include <glib.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include "supportw.h"
+
+gpointer HeapAlloc            (gpointer unused1, gint32 unused2, gint32 nbytes);
+gpointer HeapCreate           (gint32 flags, gint32 initial_size, gint32 max_size);
+gboolean HeapSetInformation   (gpointer handle, gpointer heap_info_class,
+                              gpointer heap_info, gint32 head_info_length);
+
+gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
+                              gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
+
+gpointer HeapAlloc            (gpointer handle, gint32 flags, gint32 nbytes);
+gpointer HeapReAlloc          (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
+gint32   HeapSize             (gpointer handle, gint32 flags, gpointer mem);
+gboolean HeapFree             (gpointer handle, gint32 flags, gpointer mem);
+gboolean HeapValidate         (gpointer handle, gpointer mem);
+gboolean HeapDestroy          (gpointer handle);
+
+typedef struct _HeapInfo {
+       gint32 flags;
+       gint32 initial_size;
+       gint32 max_size;
+       GHashTable *hash;
+} HeapInfo;
+
+/* Some initial value for the process heap */
+HeapInfo *process_heap;
+
+static GHashTable *heaps;
+
+gpointer
+HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
+{
+       HeapInfo *hi;
+       
+       if (heaps == NULL)
+               heaps = g_hash_table_new (g_direct_hash, g_direct_equal);
+
+       if (flags != 0)
+               g_warning ("Flags for HeapCreate are the unsupported value non-zero");
+       
+       hi = g_new (HeapInfo, 1);
+       hi->flags = flags;
+       hi->initial_size = initial_size;
+       hi->max_size = max_size;
+       hi->hash = g_hash_table_new (g_direct_hash, g_direct_equal);
+       
+       g_hash_table_insert (heaps, hi, hi);
+       
+       return hi;
+}
+
+gboolean
+HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
+                   gint32 head_info_length)
+{
+       return TRUE;
+}
+
+gboolean
+HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
+                       gint32 head_info_length, gint32 *ret_length)
+{
+       *ret_length = 0;
+       return TRUE;
+}
+
+gpointer
+HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
+{
+       HeapInfo *heap = (HeapInfo *) handle;
+       void *ptr;
+       
+       ptr = g_malloc0 (nbytes);
+
+       g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
+       
+       return ptr;
+}
+
+gpointer
+HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
+{
+       HeapInfo *heap = (HeapInfo *) handle;
+       void *ptr;
+       
+       g_hash_table_remove (heap->hash, mem);
+       ptr = g_realloc (mem, nbytes);
+       g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
+
+       return ptr;
+}
+
+gint32
+HeapSize (gpointer handle, gint32 flags, gpointer mem)
+{
+       HeapInfo *heap = (HeapInfo *) handle;
+
+       gint32 size = GPOINTER_TO_INT (g_hash_table_lookup (heap->hash, mem));
+
+       return size;
+}
+
+gboolean
+HeapFree (gpointer handle, gint32 flags, gpointer mem)
+{
+       HeapInfo *heap = (HeapInfo *) handle;
+
+       g_hash_table_remove (heap->hash, GINT_TO_POINTER (mem));
+       g_free (mem);
+       
+       return TRUE;
+}
+
+gboolean
+HeapValidate (gpointer handle, gpointer mem)
+{
+       return TRUE;
+}
+
+static void
+free_handles (gpointer key, gpointer value, gpointer user_data)
+{
+       g_free (key);
+}
+
+gboolean
+HeapDestroy (gpointer handle)
+{
+       HeapInfo *heap = (HeapInfo *) handle;
+
+       /* Failure is zero */
+       if (handle == process_heap)
+               return 0;
+       
+       g_hash_table_foreach (heap->hash, free_handles, NULL);
+       g_hash_table_destroy (heap->hash);
+       
+       g_hash_table_remove (heaps, handle);
+       g_free (heap);
+
+       return 1;
+}
+
+gpointer GetProcessHeap (void);
+
+gpointer 
+GetProcessHeap (void)
+{
+       if (process_heap == NULL){
+               process_heap = g_new (HeapInfo, 1);
+               process_heap->flags = 0;
+               process_heap->initial_size = 1024;
+               process_heap->max_size = 1024*1024*1024;
+               
+       }
+       return process_heap;
+}
+/* end Heap* functions */
index 158fce068be1a60a773f894bcc2ec6e5f66ffb12..f1fa2d16ff7ab1946d5cbb1f0929d391e0ee3b69 100644 (file)
@@ -1,3 +1,14 @@
+/*
+ * Helper routines for some of the common methods that people P/Invoke
+ * on their applications.
+ *
+ * Authors:
+ *   Gonzalo Paniagua (gonzalo@ximian.com)
+ *   Miguel de Icaza  (miguel@novell.com)
+ *
+ * (C) 2005 Novell, Inc.
+ *
+ */
 #include <glib.h>
 #include <stdlib.h>
 #include <string.h>
@@ -14,24 +25,24 @@ typedef struct {
        void *fnptr;
 } FnPtr;
 
-gpointer FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter,
-                       const char *classw, const char *window);
+gpointer FindWindowExW        (gpointer hwndParent, gpointer hwndChildAfter,
+                              const char *classw, const char *window);
 
-gpointer HeapAlloc (gpointer unused1, gint32 unused2, gint32 nbytes);
-gpointer HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size);
-gboolean HeapSetInformation (gpointer handle, gpointer heap_info_class,
-                               gpointer heap_info, gint32 head_info_length);
+gpointer HeapAlloc            (gpointer unused1, gint32 unused2, gint32 nbytes);
+gpointer HeapCreate           (gint32 flags, gint32 initial_size, gint32 max_size);
+gboolean HeapSetInformation   (gpointer handle, gpointer heap_info_class,
+                              gpointer heap_info, gint32 head_info_length);
 
 gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
-                       gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
+                              gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
 
-gpointer HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes);
-gpointer HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
-gint32 HeapSize (gpointer handle, gint32 flags, gpointer mem);
-gboolean HeapFree (gpointer handle, gint32 flags, gpointer mem);
-gboolean HeapValidate (gpointer handle, gpointer mem);
-gboolean HeapDestroy (gpointer handle);
-gpointer GetProcessHeap (void);
+gpointer HeapAlloc            (gpointer handle, gint32 flags, gint32 nbytes);
+gpointer HeapReAlloc          (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
+gint32   HeapSize             (gpointer handle, gint32 flags, gpointer mem);
+gboolean HeapFree             (gpointer handle, gint32 flags, gpointer mem);
+gboolean HeapValidate         (gpointer handle, gpointer mem);
+gboolean HeapDestroy          (gpointer handle);
+gpointer GetProcessHeap       (void);
 
 static FnPtr functions [] = {
        { "FindWindowExW", NULL }, /* user32 */
@@ -161,70 +172,4 @@ FindWindowExW (gpointer hwndParent, gpointer hwndChildAfter, const char *classw,
        return func (hwndParent, hwndChildAfter, classw, window);
 }
 
-/* begin Heap* functions */
-gpointer
-HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
-{
-       return (gpointer) 0xDEADBEEF;
-}
-
-gboolean
-HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
-                       gint32 head_info_length)
-{
-       return TRUE;
-}
-
-gboolean
-HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
-                       gint32 head_info_length, gint32 *ret_length)
-{
-       *ret_length = 0;
-       return TRUE;
-}
-
-gpointer
-HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
-{
-       return g_malloc0 (nbytes);
-}
-
-gpointer
-HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
-{
-       return g_realloc (mem, nbytes);
-}
-
-gint32
-HeapSize (gpointer handle, gint32 flags, gpointer mem)
-{
-       return 0;
-}
-
-gboolean
-HeapFree (gpointer handle, gint32 flags, gpointer mem)
-{
-       g_free (mem);
-       return TRUE;
-}
-
-gboolean
-HeapValidate (gpointer handle, gpointer mem)
-{
-       return TRUE;
-}
-
-gboolean
-HeapDestroy (gpointer handle)
-{
-       return TRUE;
-}
-
-
-gpointer 
-GetProcessHeap ()
-{
-       return (gpointer) 0xDEADBEEF;
-}
-/* end Heap* functions */