2 * cabextract memory allocation for acquiration installation utility
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "cabinet-memory.h" /* self */
23 #include <glib/gmessages.h>
24 #include <glib/ghash.h>
25 #include <glib/gtypes.h>
26 #include <glib/gslist.h>
29 struct _CaptiveAcquireCabinetMemoryObject {
30 GObject parent_instance;
33 GHashTable *memory_hash;
37 struct _CaptiveAcquireCabinetMemoryObjectClass {
38 GObjectClass parent_class;
41 static CaptiveAcquireCabinetMemoryObject *memory_object;
42 static GSList *memory_object_slist;
44 static gpointer captive_acquire_cabinet_memory_object_parent_class=NULL;
46 static void captive_acquire_cabinet_memory_object_finalize_foreach
47 (gpointer mem /* key */,gpointer mem_val,gpointer user_data /* unused */)
49 g_return_if_fail(mem!=NULL);
50 g_return_if_fail(mem_val!=NULL);
51 g_return_if_fail(mem==mem_val);
56 static void captive_acquire_cabinet_memory_object_finalize(CaptiveAcquireCabinetMemoryObject *memory)
58 g_return_if_fail(memory!=NULL);
59 g_return_if_fail(memory!=memory_object);
61 g_hash_table_foreach(memory->memory_hash,
62 captive_acquire_cabinet_memory_object_finalize_foreach, /* func */
63 NULL); /* user_data; unused */
65 g_hash_table_destroy(memory->memory_hash);
71 G_OBJECT_CLASS(captive_acquire_cabinet_memory_object_parent_class)->finalize((GObject *)memory);
74 static void captive_acquire_cabinet_memory_object_class_init(CaptiveAcquireCabinetMemoryObjectClass *class)
76 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
78 captive_acquire_cabinet_memory_object_parent_class=g_type_class_ref(g_type_parent(G_TYPE_FROM_CLASS(class)));
79 gobject_class->finalize=(void (*)(GObject *object))captive_acquire_cabinet_memory_object_finalize;
82 static void captive_acquire_cabinet_memory_object_init(CaptiveAcquireCabinetMemoryObject *memory_object)
84 memory_object->memory_hash=g_hash_table_new(g_direct_hash,g_direct_equal);
87 GType captive_acquire_cabinet_memory_object_get_type(void)
89 static GType captive_acquire_cabinet_memory_object_type=0;
91 if (!captive_acquire_cabinet_memory_object_type) {
92 static const GTypeInfo captive_acquire_cabinet_memory_object_info={
93 sizeof(CaptiveAcquireCabinetMemoryObjectClass),
95 NULL, /* base_finalize */
96 (GClassInitFunc)captive_acquire_cabinet_memory_object_class_init,
97 NULL, /* class_finalize */
98 NULL, /* class_data */
99 sizeof(CaptiveAcquireCabinetMemoryObject),
101 (GInstanceInitFunc)captive_acquire_cabinet_memory_object_init,
104 captive_acquire_cabinet_memory_object_type=g_type_register_static(G_TYPE_OBJECT,
105 "CaptiveAcquireCabinetMemoryObject",&captive_acquire_cabinet_memory_object_info,0);
108 return captive_acquire_cabinet_memory_object_type;
111 CaptiveAcquireCabinetMemoryObject *acquire_cabinet_memory_object_new(void)
114 CAPTIVE_ACQUIRE_CABINET_MEMORY_TYPE_OBJECT, /* object_type */
115 NULL); /* first_property_name */
118 void acquire_cabinet_memory_object_push(CaptiveAcquireCabinetMemoryObject *memory)
120 g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory));
123 memory_object_slist=g_slist_prepend(memory_object_slist,memory_object);
124 memory_object=memory;
127 /* 'memory' is just a sanity check */
128 void acquire_cabinet_memory_object_pop(CaptiveAcquireCabinetMemoryObject *memory)
130 g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory));
131 g_return_if_fail(memory==memory_object);
133 if (!memory_object_slist)
136 memory_object=memory_object_slist->data;
137 g_assert(memory_object!=NULL);
138 memory_object_slist=g_slist_delete_link(memory_object_slist,memory_object_slist);
142 gpointer acquire_cabinet_memory_malloc(gulong n_bytes)
146 g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
149 g_assert((!r)==(!n_bytes));
151 g_hash_table_insert(memory_object->memory_hash,r,r);
156 gpointer acquire_cabinet_memory_malloc0(gulong n_bytes)
160 g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
162 r=g_malloc0(n_bytes);
163 g_assert((!r)==(!n_bytes));
165 g_hash_table_insert(memory_object->memory_hash,r,r);
170 gpointer acquire_cabinet_memory_realloc(gpointer mem,gulong n_bytes)
175 g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
178 errbool=g_hash_table_remove(memory_object->memory_hash,mem);
179 g_assert(errbool==TRUE);
181 r=g_realloc(mem,n_bytes);
182 g_assert((!r)==(!n_bytes));
184 g_hash_table_insert(memory_object->memory_hash,r,r);
189 void acquire_cabinet_memory_free(gpointer mem)
193 g_return_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object));
196 errbool=g_hash_table_remove(memory_object->memory_hash,mem);
197 g_assert(errbool==TRUE);
202 /* UGLY HACK for 'cabextract/cabextract.c/decomp_state' */
203 gpointer acquire_cabinet_memory_data_get(gsize size)
205 g_return_val_if_fail(size>0,NULL);
206 g_return_val_if_fail(CAPTIVE_ACQUIRE_CABINET_MEMORY_IS_OBJECT(memory_object),NULL);
208 if (!memory_object->data_size) {
209 memory_object->data_size=size;
210 memory_object->data=g_malloc0(memory_object->data_size);
213 g_assert(memory_object->data_size==size);
214 g_assert(memory_object->data!=NULL);
216 return memory_object->data;