Rewritten Cache Manager to better match its W32 original behaviour
[captive.git] / src / libcaptive / cc / privatebcbpin.c
1 /* $Id$
2  * reactos Cache Manager (Cc*) PrivateBcb Map type structure of libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include "privatebcbpin.h"      /* self */
23 #include "privatebcb.h"
24 #include "privatebcb-priv.h"
25 #include "sharedcachemap-priv.h"
26 #include <glib-object.h>
27
28
29 struct _CaptivePrivateBcbPinObject {
30         CaptivePrivateBcbObject parent_instance;
31
32         gulong FileSizes_changed_handler_id;
33         gulong FileSizes_changed_after_handler_id;
34         gulong purge_handler_id;
35         guint64 offset;
36         guint32 length;
37         gpointer buffer;
38         };
39 struct _CaptivePrivateBcbPinObjectClass {
40         CaptivePrivateBcbObjectClass parent_class;
41         };
42
43
44 static gpointer captive_private_bcb_pin_object_parent_class=NULL;
45
46
47 static void captive_private_bcb_pin_object_finalize(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
48 {
49 CaptivePrivateBcbObject *captive_private_bcb_object;
50 CaptiveSharedCacheMapObject *SharedCacheMap;
51 GHashTable *pin_hash;
52
53         g_return_if_fail(captive_private_bcb_pin_object!=NULL);
54
55         captive_private_bcb_object=CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object);
56
57         if (captive_private_bcb_pin_object->FileSizes_changed_handler_id) {
58                 g_assert(captive_private_bcb_object->SharedCacheMap!=NULL);
59                 g_signal_handler_disconnect(
60                                 captive_private_bcb_object->SharedCacheMap,     /* instance */
61                                 captive_private_bcb_pin_object->FileSizes_changed_handler_id);
62                 captive_private_bcb_pin_object->FileSizes_changed_handler_id=0;
63                 }
64         if (captive_private_bcb_pin_object->FileSizes_changed_after_handler_id) {
65                 g_assert(captive_private_bcb_object->SharedCacheMap!=NULL);
66                 g_signal_handler_disconnect(
67                                 captive_private_bcb_object->SharedCacheMap,     /* instance */
68                                 captive_private_bcb_pin_object->FileSizes_changed_after_handler_id);
69                 captive_private_bcb_pin_object->FileSizes_changed_after_handler_id=0;
70                 }
71         if (captive_private_bcb_pin_object->purge_handler_id) {
72                 g_assert(captive_private_bcb_object->SharedCacheMap!=NULL);
73                 g_signal_handler_disconnect(
74                                 captive_private_bcb_object->SharedCacheMap,     /* instance */
75                                 captive_private_bcb_pin_object->purge_handler_id);
76                 captive_private_bcb_pin_object->purge_handler_id=0;
77                 }
78         if ((SharedCacheMap=captive_private_bcb_object->SharedCacheMap)) {
79                 if ((pin_hash=SharedCacheMap->pin_hash)) {
80                         /* Do not: g_assert(g_hash_table_lookup(pin_hash,&captive_private_bcb_object->offset));
81                          * as we may be captive_private_bcb_pin_object_detach_pin()ed.
82                          */
83                         if (captive_private_bcb_object==g_hash_table_lookup(pin_hash,&captive_private_bcb_pin_object->offset))
84                                 g_hash_table_remove(pin_hash,&captive_private_bcb_pin_object->offset);
85                         }
86                 }
87
88         G_OBJECT_CLASS(captive_private_bcb_pin_object_parent_class)->finalize((GObject *)captive_private_bcb_pin_object);
89 }
90
91
92 static void captive_private_bcb_pin_object_class_init(CaptivePrivateBcbPinObjectClass *class)
93 {
94 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
95
96         captive_private_bcb_pin_object_parent_class=g_type_class_ref(G_TYPE_OBJECT);
97         gobject_class->finalize=(void (*)(GObject *object))captive_private_bcb_pin_object_finalize;
98 }
99
100 static void captive_private_bcb_pin_object_init(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
101 {
102 }
103
104 GType captive_private_bcb_pin_object_get_type(void)
105 {
106 static GType captive_private_bcb_pin_object_type=0;
107
108         if (!captive_private_bcb_pin_object_type) {
109 static const GTypeInfo captive_private_bcb_pin_object_info={
110                                 sizeof(CaptivePrivateBcbPinObjectClass),
111                                 NULL,   /* base_init */
112                                 NULL,   /* base_finalize */
113                                 (GClassInitFunc)captive_private_bcb_pin_object_class_init,
114                                 NULL,   /* class_finalize */
115                                 NULL,   /* class_data */
116                                 sizeof(CaptivePrivateBcbPinObject),
117                                 5,      /* n_preallocs */
118                                 (GInstanceInitFunc)captive_private_bcb_pin_object_init,
119                                 };
120
121                 captive_private_bcb_pin_object_type=g_type_register_static(CAPTIVE_PRIVATE_BCB_TYPE_OBJECT,
122                                 "CaptivePrivateBcbPinObject",&captive_private_bcb_pin_object_info,0);
123                 }
124
125         return captive_private_bcb_pin_object_type;
126 }
127
128
129 static guint captive_private_bcb_pin_object_hash_new_hash_func(const guint64 *offsetp)
130 {
131         return (*offsetp)^((*offsetp)>>32);
132 }
133
134 static gint captive_private_bcb_pin_object_hash_new_key_compare_func(const guint64 *offset_ap,const guint64 *offset_bp)
135 {
136         return (*offset_bp>*offset_ap)-(*offset_bp<*offset_ap);
137 }
138
139 GHashTable *captive_private_bcb_pin_object_hash_new(void)
140 {
141         return g_hash_table_new(
142                         (GHashFunc)captive_private_bcb_pin_object_hash_new_hash_func,
143                         (GEqualFunc)captive_private_bcb_pin_object_hash_new_key_compare_func);
144 }
145
146 void captive_private_bcb_pin_object_hash_destroy(GHashTable *pin_hash)
147 {
148         g_return_if_fail(pin_hash!=NULL);
149
150         g_hash_table_destroy(pin_hash);
151 }
152
153 static void captive_private_bcb_pin_object_validate(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
154 {
155 guint64 start,end;
156
157         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
158         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(
159                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap));
160
161         start=captive_private_bcb_pin_object->offset;
162         end  =captive_private_bcb_pin_object->offset+captive_private_bcb_pin_object->length;
163
164         g_assert(end<=CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap->ValidDataLength);
165 }
166
167 static void captive_private_bcb_pin_object_FileSizes_changed(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
168                 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength,
169                 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object /* user_data */)
170 {
171         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
172         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
173         g_return_if_fail(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap
174                         ==captive_shared_cache_map_object);
175
176         /* 'AllocationSize' must not change if any map/pin Bcbs exist. */
177         g_assert(AllocationSize==captive_shared_cache_map_object->AllocationSize);
178 }
179
180 static void captive_private_bcb_pin_object_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
181                 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object /* user_data */)
182 {
183         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
184         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
185         g_return_if_fail(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap
186                         ==captive_shared_cache_map_object);
187
188         g_assert(!captive_shared_cache_map_is_page_dirty(captive_shared_cache_map_object,
189                         captive_private_bcb_pin_object->offset));
190 }
191
192 static void captive_private_bcb_pin_object_FileSizes_changed_after(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
193                 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength,
194                 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object /* user_data */)
195 {
196         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
197         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
198         g_return_if_fail(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap
199                         ==captive_shared_cache_map_object);
200
201         /* we are 'g_signal_connect_after' */
202         g_assert(AllocationSize ==captive_shared_cache_map_object->AllocationSize);
203         g_assert(FileSize       ==captive_shared_cache_map_object->FileSize);
204         g_assert(ValidDataLength==captive_shared_cache_map_object->ValidDataLength);
205
206         captive_private_bcb_pin_object_validate(captive_private_bcb_pin_object);
207 }
208
209 void _captive_private_bcb_pin_object_connect_SharedCacheMap
210                 (CaptivePrivateBcbPinObject *captive_private_bcb_pin_object,CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
211 {
212         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
213         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
214         g_return_if_fail(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap==NULL);
215
216         _captive_private_bcb_object_connect_SharedCacheMap(
217                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object),captive_shared_cache_map_object);
218
219         captive_private_bcb_pin_object->FileSizes_changed_handler_id=g_signal_connect(
220                         captive_shared_cache_map_object,"FileSizes_changed",
221                         G_CALLBACK(captive_private_bcb_pin_object_FileSizes_changed),
222                         captive_private_bcb_pin_object);
223         g_assert(captive_private_bcb_pin_object->FileSizes_changed_handler_id>=1);
224         captive_private_bcb_pin_object->FileSizes_changed_after_handler_id=g_signal_connect_after(
225                         captive_shared_cache_map_object,"FileSizes_changed",
226                         G_CALLBACK(captive_private_bcb_pin_object_FileSizes_changed_after),
227                         captive_private_bcb_pin_object);
228         g_assert(captive_private_bcb_pin_object->FileSizes_changed_after_handler_id>=1);
229         captive_private_bcb_pin_object->purge_handler_id=g_signal_connect(
230                         captive_shared_cache_map_object,"purge",
231                         G_CALLBACK(captive_private_bcb_pin_object_purge),
232                         captive_private_bcb_pin_object);
233         g_assert(captive_private_bcb_pin_object->purge_handler_id>=1);
234 }
235
236 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object_new(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
237                 guint64 offset)
238 {
239 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
240
241         g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),NULL);
242
243         g_assert(captive_shared_cache_map_object->PinAccess);
244
245         captive_private_bcb_pin_object=g_object_new(
246                         CAPTIVE_PRIVATE_BCB_PIN_TYPE_OBJECT,    /* object_type */
247                         NULL);  /* first_property_name; FIXME: support properties */
248
249         captive_private_bcb_pin_object->offset=offset;
250         captive_private_bcb_pin_object->length=PAGE_SIZE;
251
252         _captive_private_bcb_pin_object_connect_SharedCacheMap(captive_private_bcb_pin_object,
253                         captive_shared_cache_map_object);
254
255         captive_private_bcb_pin_object->buffer=captive_shared_cache_map_object->buffer+offset;
256
257         g_assert(captive_shared_cache_map_object->pin_hash!=NULL);
258         g_assert(!g_hash_table_lookup(captive_shared_cache_map_object->pin_hash,
259                         &captive_private_bcb_pin_object->offset));
260         g_hash_table_insert(captive_shared_cache_map_object->pin_hash,
261                         &captive_private_bcb_pin_object->offset,
262                         captive_private_bcb_pin_object);
263
264         return captive_private_bcb_pin_object;
265 }
266
267 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object_get_ref(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
268                 guint64 offset)
269 {
270 CaptivePrivateBcbPinObject *captive_private_bcb_pin_object;
271
272         g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),NULL);
273
274         if ((captive_private_bcb_pin_object=g_hash_table_lookup(captive_shared_cache_map_object->pin_hash,
275                         &offset))) {
276                 g_assert(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
277                 g_object_ref(captive_private_bcb_pin_object);
278                 return captive_private_bcb_pin_object;
279                 }
280         return captive_private_bcb_pin_object_new(captive_shared_cache_map_object,offset);
281 }
282
283 gboolean captive_private_bcb_pin_object_is_dirty(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
284 {
285         g_return_val_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object),FALSE);
286         g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(
287                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap),FALSE);
288
289         return captive_shared_cache_map_is_page_dirty(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap,
290                         captive_private_bcb_pin_object->offset);
291 }
292
293 void captive_private_bcb_pin_object_flush(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
294 {
295         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
296         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(
297                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap));
298         g_return_if_fail(captive_private_bcb_pin_object_is_dirty(captive_private_bcb_pin_object));
299
300         /* Flush synchronously here. */
301         captive_shared_cache_map_flush(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap,
302                         captive_private_bcb_pin_object->offset,
303                         captive_private_bcb_pin_object->offset+captive_private_bcb_pin_object->length);
304 }
305
306 void captive_private_bcb_pin_object_set_dirty(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
307 {
308         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
309         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(
310                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap));
311
312         captive_shared_cache_map_set_dirty(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap,
313                         captive_private_bcb_pin_object->offset,
314                         captive_private_bcb_pin_object->offset+captive_private_bcb_pin_object->length);
315 }
316
317 void captive_private_bcb_pin_object_set_lsn(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object,gint64 lsn)
318 {
319         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
320         g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(
321                         CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap));
322
323         captive_shared_cache_map_page_set_lsn(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object)->SharedCacheMap,
324                         captive_private_bcb_pin_object->offset,lsn);
325 }
326
327 void captive_private_bcb_pin_object_detach_pin(CaptivePrivateBcbPinObject *captive_private_bcb_pin_object)
328 {
329 CaptiveSharedCacheMapObject *SharedCacheMap;
330
331         g_return_if_fail(CAPTIVE_PRIVATE_BCB_PIN_IS_OBJECT(captive_private_bcb_pin_object));
332
333         SharedCacheMap=captive_private_bcb_object_get_SharedCacheMap(CAPTIVE_PRIVATE_BCB_OBJECT(captive_private_bcb_pin_object));
334         g_assert(SharedCacheMap->pin_hash!=NULL);
335         g_assert(captive_private_bcb_pin_object==g_hash_table_lookup(SharedCacheMap->pin_hash,
336                         &captive_private_bcb_pin_object->offset));
337         g_hash_table_remove(SharedCacheMap->pin_hash,&captive_private_bcb_pin_object->offset);
338 }