support-heap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Emulates the Heap* routines.
  3. *
  4. * Authors:
  5. * Gonzalo Paniagua ([email protected])
  6. * Miguel de Icaza ([email protected])
  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. gpointer HeapAlloc (gpointer unused1, gint32 unused2, gint32 nbytes);
  17. gpointer HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size);
  18. gboolean HeapSetInformation (gpointer handle, gpointer heap_info_class,
  19. gpointer heap_info, gint32 head_info_length);
  20. gboolean HeapQueryInformation (gpointer handle, gpointer heap_info_class,
  21. gpointer heap_info, gint32 head_info_length, gint32 *ret_length);
  22. gpointer HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes);
  23. gpointer HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes);
  24. gint32 HeapSize (gpointer handle, gint32 flags, gpointer mem);
  25. gboolean HeapFree (gpointer handle, gint32 flags, gpointer mem);
  26. gboolean HeapValidate (gpointer handle, gpointer mem);
  27. gboolean HeapDestroy (gpointer handle);
  28. typedef struct _HeapInfo {
  29. gint32 flags;
  30. gint32 initial_size;
  31. gint32 max_size;
  32. GHashTable *hash;
  33. } HeapInfo;
  34. /* Some initial value for the process heap */
  35. HeapInfo *process_heap;
  36. static GHashTable *heaps;
  37. gpointer
  38. HeapCreate (gint32 flags, gint32 initial_size, gint32 max_size)
  39. {
  40. HeapInfo *hi;
  41. if (heaps == NULL)
  42. heaps = g_hash_table_new (g_direct_hash, g_direct_equal);
  43. if (flags != 0)
  44. g_warning ("Flags for HeapCreate are the unsupported value non-zero");
  45. hi = g_new (HeapInfo, 1);
  46. hi->flags = flags;
  47. hi->initial_size = initial_size;
  48. hi->max_size = max_size;
  49. hi->hash = g_hash_table_new (g_direct_hash, g_direct_equal);
  50. g_hash_table_insert (heaps, hi, hi);
  51. return hi;
  52. }
  53. gboolean
  54. HeapSetInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
  55. gint32 head_info_length)
  56. {
  57. return TRUE;
  58. }
  59. gboolean
  60. HeapQueryInformation (gpointer handle, gpointer heap_info_class, gpointer heap_info,
  61. gint32 head_info_length, gint32 *ret_length)
  62. {
  63. *ret_length = 0;
  64. return TRUE;
  65. }
  66. gpointer
  67. HeapAlloc (gpointer handle, gint32 flags, gint32 nbytes)
  68. {
  69. HeapInfo *heap = (HeapInfo *) handle;
  70. void *ptr;
  71. ptr = g_malloc0 (nbytes);
  72. g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
  73. return ptr;
  74. }
  75. gpointer
  76. HeapReAlloc (gpointer handle, gint32 flags, gpointer mem, gint32 nbytes)
  77. {
  78. HeapInfo *heap = (HeapInfo *) handle;
  79. void *ptr;
  80. g_hash_table_remove (heap->hash, mem);
  81. ptr = g_realloc (mem, nbytes);
  82. g_hash_table_insert (heap->hash, ptr, GINT_TO_POINTER (nbytes));
  83. return ptr;
  84. }
  85. gint32
  86. HeapSize (gpointer handle, gint32 flags, gpointer mem)
  87. {
  88. HeapInfo *heap = (HeapInfo *) handle;
  89. gint32 size = GPOINTER_TO_INT (g_hash_table_lookup (heap->hash, mem));
  90. return size;
  91. }
  92. gboolean
  93. HeapFree (gpointer handle, gint32 flags, gpointer mem)
  94. {
  95. HeapInfo *heap = (HeapInfo *) handle;
  96. g_hash_table_remove (heap->hash, GINT_TO_POINTER (mem));
  97. g_free (mem);
  98. return TRUE;
  99. }
  100. gboolean
  101. HeapValidate (gpointer handle, gpointer mem)
  102. {
  103. return TRUE;
  104. }
  105. static void
  106. free_handles (gpointer key, gpointer value, gpointer user_data)
  107. {
  108. g_free (key);
  109. }
  110. gboolean
  111. HeapDestroy (gpointer handle)
  112. {
  113. HeapInfo *heap = (HeapInfo *) handle;
  114. /* Failure is zero */
  115. if (handle == process_heap)
  116. return 0;
  117. g_hash_table_foreach (heap->hash, free_handles, NULL);
  118. g_hash_table_destroy (heap->hash);
  119. g_hash_table_remove (heaps, handle);
  120. g_free (heap);
  121. return 1;
  122. }
  123. gpointer GetProcessHeap (void);
  124. gpointer
  125. GetProcessHeap (void)
  126. {
  127. if (process_heap == NULL){
  128. process_heap = g_new (HeapInfo, 1);
  129. process_heap->flags = 0;
  130. process_heap->initial_size = 1024;
  131. process_heap->max_size = 1024*1024*1024;
  132. }
  133. return process_heap;
  134. }
  135. /* end Heap* functions */