2 * reactos Cache Manager (Cc*) SharedCacheMap structure of libcaptive
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 "sharedcachemap.h" /* self */
23 #include "sharedcachemap-priv.h" /* self */
25 #include "marshallers.h"
26 #include <glib-object.h>
27 #include "privatebcbpin.h"
28 #include "captive/macros.h"
30 #include "reactos/ddk/obfuncs.h"
33 static GHashTable *CaptiveSharedCacheMapObject_hash;
35 static void CaptiveSharedCacheMapObject_hash_init(void)
37 if (CaptiveSharedCacheMapObject_hash)
39 CaptiveSharedCacheMapObject_hash=g_hash_table_new(
40 g_direct_hash, /* hash_func */
41 g_direct_equal); /* key_equal_func */
45 static gpointer captive_shared_cache_map_object_parent_class=NULL;
48 static void captive_shared_cache_map_object_finalize(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
50 static const CC_FILE_SIZES FileSizes_zero;
53 CaptiveSharedCacheMapObject_page *page;
55 g_return_if_fail(captive_shared_cache_map_object!=NULL);
57 for (offset=0;offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);offset+=PAGE_SIZE) {
58 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
59 if (!page->data_valid)
61 g_assert(!page->dirty); /* FIXME: Is it allowed by W32? */
64 CaptiveSharedCacheMapObject_hash_init();
65 errbool=g_hash_table_remove(CaptiveSharedCacheMapObject_hash,captive_shared_cache_map_object);
66 g_assert(errbool==TRUE);
68 captive_shared_cache_map_FileSizes_set(captive_shared_cache_map_object,&FileSizes_zero);
69 g_assert(captive_shared_cache_map_object->buffer==NULL);
70 g_assert(captive_shared_cache_map_object->pages==NULL);
72 if (captive_shared_cache_map_object->pin_hash) {
73 captive_private_bcb_pin_object_hash_destroy(captive_shared_cache_map_object->pin_hash);
74 captive_shared_cache_map_object->pin_hash=NULL;
76 g_assert(captive_shared_cache_map_object->map==NULL);
77 if (captive_shared_cache_map_object->SectionObjectPointers) {
78 g_assert(captive_shared_cache_map_object==captive_shared_cache_map_object->SectionObjectPointers->SharedCacheMap);
79 captive_shared_cache_map_object->SectionObjectPointers->SharedCacheMap=NULL;
82 if (captive_shared_cache_map_object->FileObject) {
83 /* W32 dereferences twice. */
84 ObDereferenceObject(captive_shared_cache_map_object->FileObject);
85 captive_shared_cache_map_object->FileObject=NULL;
88 G_OBJECT_CLASS(captive_shared_cache_map_object_parent_class)->finalize((GObject *)captive_shared_cache_map_object);
91 static guint FileSizes_changed_signal;
92 static guint purge_signal;
94 static void captive_shared_cache_map_object_FileSizes_changed(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
95 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength);
96 static void captive_shared_cache_map_object_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object);
98 static void captive_shared_cache_map_object_class_init(CaptiveSharedCacheMapObjectClass *class)
100 GObjectClass *gobject_class=G_OBJECT_CLASS(class);
102 captive_shared_cache_map_object_parent_class=g_type_class_ref(g_type_parent(G_TYPE_FROM_CLASS(class)));
103 gobject_class->finalize=(void (*)(GObject *object))captive_shared_cache_map_object_finalize;
105 class->FileSizes_changed=captive_shared_cache_map_object_FileSizes_changed;
106 class->purge=captive_shared_cache_map_object_purge;
108 FileSizes_changed_signal=g_signal_new("FileSizes_changed",
109 G_TYPE_FROM_CLASS(gobject_class),
111 G_STRUCT_OFFSET(CaptiveSharedCacheMapObjectClass,FileSizes_changed),
113 captive_cc_VOID__UINT64_UINT64_UINT64,
114 G_TYPE_NONE,3,G_TYPE_UINT64,G_TYPE_UINT64,G_TYPE_UINT64);
115 purge_signal=g_signal_new("purge",
116 G_TYPE_FROM_CLASS(gobject_class),
118 G_STRUCT_OFFSET(CaptiveSharedCacheMapObjectClass,purge),
120 captive_cc_VOID__VOID,
125 static void captive_shared_cache_map_object_init(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
127 captive_shared_cache_map_object->pin_hash=captive_private_bcb_pin_object_hash_new();
129 captive_shared_cache_map_w32_ref(captive_shared_cache_map_object);
130 g_object_unref(captive_shared_cache_map_object); /* ==sink */
132 CaptiveSharedCacheMapObject_hash_init();
133 g_hash_table_insert(CaptiveSharedCacheMapObject_hash,
134 captive_shared_cache_map_object,captive_shared_cache_map_object);
138 GType captive_shared_cache_map_object_get_type(void)
140 static GType captive_shared_cache_map_object_type=0;
142 if (!captive_shared_cache_map_object_type) {
143 static const GTypeInfo captive_shared_cache_map_object_info={
144 sizeof(CaptiveSharedCacheMapObjectClass),
145 NULL, /* base_init */
146 NULL, /* base_finalize */
147 (GClassInitFunc)captive_shared_cache_map_object_class_init,
148 NULL, /* class_finalize */
149 NULL, /* class_data */
150 sizeof(CaptiveSharedCacheMapObject),
152 (GInstanceInitFunc)captive_shared_cache_map_object_init,
155 captive_shared_cache_map_object_type=g_type_register_static(G_TYPE_OBJECT,
156 "CaptiveSharedCacheMapObject",&captive_shared_cache_map_object_info,0);
159 return captive_shared_cache_map_object_type;
162 static void captive_shared_cache_map_object_FileSizes_changed(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
163 guint64 AllocationSize,guint64 FileSize,guint64 ValidDataLength)
165 size_t size_old,size_new;
166 guint64 size64_old,size64_new;
168 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
170 g_assert((!captive_shared_cache_map_object->buffer)==(!captive_shared_cache_map_object->AllocationSize));
171 g_assert((!captive_shared_cache_map_object->pages)==(!captive_shared_cache_map_object->AllocationSize));
173 size64_old=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);
175 g_assert(size_old==size64_old);
176 size64_new=CAPTIVE_ROUND_UP64(AllocationSize,PAGE_SIZE);
178 if (size_new!=size64_new) {
180 g_error("Mapped size %" G_GUINT64_FORMAT " is too big; FIXME: non-mmap(2)-able areas not yet supported",size64_new);
181 g_assert_not_reached();
184 if (size_old!=size_new) {
187 /* These two assertions should be already catched by pin/map signal handlers. */
188 g_assert(!captive_shared_cache_map_object->map);
189 g_assert(!g_hash_table_size(captive_shared_cache_map_object->pin_hash));
191 if (AllocationSize) {
197 PAGE_SIZE+size_new+PAGE_SIZE, /* length; leading and trailing boundary check pages */
198 PROT_READ|PROT_WRITE, /* prot; read/write must be possible although write is not guaranteed to be flushed yet */
199 MAP_PRIVATE|MAP_ANONYMOUS, /* flags */
200 -1, /* fd; ignored due to MAP_ANONYMOUS */
201 0); /* offset; ignored due to MAP_ANONYMOUS */
202 if (base==MAP_FAILED)
204 g_assert(base!=NULL);
207 errint=munmap(base-PAGE_SIZE,PAGE_SIZE); /* unmap leading boundary check page */
209 errint=munmap(base+size_new,PAGE_SIZE); /* unmap trailing boundary check page */
217 memcpy(buffer_new,captive_shared_cache_map_object->buffer,
218 MIN(AllocationSize,captive_shared_cache_map_object->AllocationSize));
220 if (captive_shared_cache_map_object->AllocationSize) {
223 errint=munmap(captive_shared_cache_map_object->buffer,size_old);
227 captive_shared_cache_map_object->buffer=buffer_new;
229 #if 0 /* It appears it is valid to squeeze out 'dirty' blocks. FIXME: Flush them? */
230 if (size_old>size_new) {
233 for (now=size_new;now<size_old;now+=PAGE_SIZE) {
234 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
236 g_assert(!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty);
240 captive_shared_cache_map_object->pages=g_realloc(captive_shared_cache_map_object->pages,
241 size_new/PAGE_SIZE*sizeof(*captive_shared_cache_map_object->pages));
242 if (size_new>size_old) /* prevent 'size_new-size_old' as it is unsigned! */
243 memset(captive_shared_cache_map_object->pages+(size_old/PAGE_SIZE),0,
244 (size_new-size_old)/PAGE_SIZE*sizeof(*captive_shared_cache_map_object->pages));
247 captive_shared_cache_map_object->AllocationSize=AllocationSize;
248 captive_shared_cache_map_object->FileSize=FileSize;
249 captive_shared_cache_map_object->ValidDataLength=ValidDataLength;
251 g_assert((!captive_shared_cache_map_object->buffer)==(!captive_shared_cache_map_object->AllocationSize));
252 g_assert((!captive_shared_cache_map_object->pages)==(!captive_shared_cache_map_object->AllocationSize));
255 static void captive_shared_cache_map_object_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
257 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
259 /* NOP; just to provide slot for checking by Bcbs */
262 CaptiveSharedCacheMapObject *captive_shared_cache_map_get_ref(FILE_OBJECT *FileObject,
263 const CC_FILE_SIZES *FileSizes,BOOLEAN PinAccess,const CACHE_MANAGER_CALLBACKS *CallBacks,VOID *LazyWriterContext)
265 CaptiveSharedCacheMapObject *captive_shared_cache_map_object;
267 g_return_val_if_fail(FileObject!=NULL,NULL);
268 g_return_val_if_fail(FileObject->SectionObjectPointers!=NULL,NULL);
269 g_return_val_if_fail(FileSizes!=NULL,NULL);
270 g_return_val_if_fail(CallBacks!=NULL,NULL);
272 if ((captive_shared_cache_map_object=FileObject->SectionObjectPointers->SharedCacheMap)) {
273 captive_shared_cache_map_w32_ref(captive_shared_cache_map_object);
276 captive_shared_cache_map_object=g_object_new(
277 CAPTIVE_SHARED_CACHE_MAP_TYPE_OBJECT, /* object_type */
278 NULL); /* first_property_name; FIXME: support properties */
280 /* FIXME: When to drop SharedCacheMap?
281 * Currently we never close it.
282 * Fix also CcZeroData() workaround.
284 g_object_ref(captive_shared_cache_map_object);
286 /* W32 references twice. */
287 ObReferenceObject(FileObject);
288 captive_shared_cache_map_object->FileObject=FileObject;
289 captive_shared_cache_map_object->SectionObjectPointers=FileObject->SectionObjectPointers;
290 captive_shared_cache_map_object->AllocationSize=0;
291 captive_shared_cache_map_object->FileSize=0;
292 captive_shared_cache_map_object->ValidDataLength=0;
293 captive_shared_cache_map_object->PinAccess=PinAccess;
294 captive_shared_cache_map_object->CallBacks=*CallBacks;
295 captive_shared_cache_map_object->LazyWriterContext=LazyWriterContext;
297 FileObject->SectionObjectPointers->SharedCacheMap=captive_shared_cache_map_object;
300 g_assert(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
301 /* FileObject may differ - we can have a different reference to the same FCB. */
302 g_assert(FileObject->SectionObjectPointers==captive_shared_cache_map_object->SectionObjectPointers);
303 g_assert(PinAccess==captive_shared_cache_map_object->PinAccess);
304 g_assert(CallBacks->AcquireForLazyWrite==captive_shared_cache_map_object->CallBacks.AcquireForLazyWrite);
305 g_assert(CallBacks->ReleaseFromLazyWrite==captive_shared_cache_map_object->CallBacks.ReleaseFromLazyWrite);
306 g_assert(CallBacks->AcquireForReadAhead==captive_shared_cache_map_object->CallBacks.AcquireForReadAhead);
307 g_assert(CallBacks->ReleaseFromReadAhead==captive_shared_cache_map_object->CallBacks.ReleaseFromReadAhead);
308 g_assert(LazyWriterContext==captive_shared_cache_map_object->LazyWriterContext);
310 captive_shared_cache_map_FileSizes_set(captive_shared_cache_map_object,FileSizes);
312 return captive_shared_cache_map_object;
315 void captive_shared_cache_map_FileSizes_set(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
316 const CC_FILE_SIZES *FileSizes)
318 guint64 AllocationSize,FileSize,ValidDataLength;
320 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
321 g_return_if_fail(FileSizes!=NULL);
323 AllocationSize=FileSizes->AllocationSize.QuadPart;
324 FileSize=FileSizes->FileSize.QuadPart;
325 ValidDataLength=FileSizes->ValidDataLength.QuadPart;
327 if (ValidDataLength==G_MAXINT64)
328 ValidDataLength=FileSize;
330 g_assert(AllocationSize>=0);
331 g_assert(FileSize>=0);
332 g_assert(ValidDataLength>=0);
334 g_assert(ValidDataLength<=FileSize);
335 /* Do not: g_assert(0==(AllocationSize%0x200));
336 * as it is true for ntfs.sys of NT-5.1sp1 but it fails for fastfat.sys of NT-5.1sp1.
338 /* AllocationSize can be much higher: */
339 g_assert(FileSize<=AllocationSize);
341 /* Prevent signalling if not needed. */
343 || captive_shared_cache_map_object->AllocationSize!=AllocationSize
344 || captive_shared_cache_map_object->FileSize!=FileSize
345 || captive_shared_cache_map_object->ValidDataLength!=ValidDataLength) {
346 /* Signalling is forbidden in captive_shared_cache_map_object_finalize(). */
347 if (G_OBJECT(captive_shared_cache_map_object)->ref_count==0) {
348 (*CAPTIVE_SHARED_CACHE_MAP_OBJECT_GET_CLASS(captive_shared_cache_map_object)->FileSizes_changed)
349 (captive_shared_cache_map_object,AllocationSize,FileSize,ValidDataLength);
352 g_signal_emit(captive_shared_cache_map_object,FileSizes_changed_signal,0,
353 AllocationSize,FileSize,ValidDataLength);
357 g_assert(captive_shared_cache_map_object->AllocationSize==AllocationSize);
358 g_assert(captive_shared_cache_map_object->FileSize==FileSize);
359 g_assert(captive_shared_cache_map_object->ValidDataLength==ValidDataLength);
362 CaptiveSharedCacheMapObject *captive_SectionObjectPointers_to_SharedCacheMap(SECTION_OBJECT_POINTERS *SectionObjectPointers)
364 g_return_val_if_fail(SectionObjectPointers!=NULL,NULL);
365 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(SectionObjectPointers->SharedCacheMap),NULL);
367 return SectionObjectPointers->SharedCacheMap;
370 CaptiveSharedCacheMapObject *captive_FileObject_to_SharedCacheMap(FILE_OBJECT *FileObject)
372 g_return_val_if_fail(FileObject!=NULL,NULL);
374 return captive_SectionObjectPointers_to_SharedCacheMap(FileObject->SectionObjectPointers);
377 void captive_shared_cache_map_w32_ref(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
379 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
381 g_object_ref(captive_shared_cache_map_object);
382 captive_shared_cache_map_object->w32_ref_count++;
385 void captive_shared_cache_map_w32_unref(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
387 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
388 g_return_if_fail(G_OBJECT(captive_shared_cache_map_object)->ref_count>0);
390 captive_shared_cache_map_object->w32_ref_count--;
391 g_object_unref(captive_shared_cache_map_object);
394 void captive_shared_cache_map_data_validate_read(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
395 FILE_OBJECT *FileObject,guint64 start,guint64 end)
398 gboolean after_eof=FALSE; /* Did we reached the end of file already? */
400 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
401 g_return_if_fail(captive_shared_cache_map_object==captive_FileObject_to_SharedCacheMap(FileObject));
402 g_return_if_fail(start<=end);
403 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
405 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
406 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
408 for (now=start;now<end;now+=PAGE_SIZE) {
409 LARGE_INTEGER now_LargeInteger;
412 if (captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
414 now_LargeInteger.QuadPart=now;
415 got=captive_Cc_IoPageRead(FileObject,captive_shared_cache_map_object->buffer+now,PAGE_SIZE,&now_LargeInteger);
419 g_assert(got<=PAGE_SIZE);
420 after_eof=(got<PAGE_SIZE);
421 captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid=TRUE;
425 void captive_shared_cache_map_data_validate_noread(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
426 guint64 start,guint64 end)
430 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
431 g_return_if_fail(start<=end);
432 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
434 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
435 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
437 for (now=start;now<end;now+=PAGE_SIZE) {
438 g_assert(captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid);
442 void captive_shared_cache_map_set_data_valid(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
443 guint64 start,guint64 end)
447 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
448 g_return_if_fail(start<=end);
449 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
451 start=CAPTIVE_ROUND_UP64(start,PAGE_SIZE);
452 if (end<captive_shared_cache_map_object->FileSize)
453 end=CAPTIVE_ROUND_DOWN64(end,PAGE_SIZE);
455 /* We can validate the last page of the file
456 * even if it does not end on PAGE_SIZE boundary.
458 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
460 /* We may get end<start here - it is valid to not to validate anything. */
462 for (now=start;now<end;now+=PAGE_SIZE) {
463 captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid=TRUE;
467 void captive_shared_cache_map_set_dirty(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
468 guint64 start,guint64 end)
471 CaptiveSharedCacheMapObject_page *page;
473 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
474 g_return_if_fail(end<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
476 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
477 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
479 for (now=start;now<end;now+=PAGE_SIZE) {
480 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
481 g_assert(page->data_valid);
490 gboolean captive_shared_cache_map_is_page_dirty(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
493 CaptiveSharedCacheMapObject_page *page;
495 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),FALSE);
496 g_return_val_if_fail(offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE),FALSE);
497 g_return_val_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE),FALSE);
498 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
499 g_return_val_if_fail(page->data_valid,FALSE);
504 void captive_shared_cache_map_page_set_lsn(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
505 guint64 offset,gint64 lsn)
507 CaptiveSharedCacheMapObject_page *page;
509 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
510 g_return_if_fail(offset<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
511 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
512 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
513 g_return_if_fail(page->data_valid);
514 g_return_if_fail(page->dirty);
515 g_return_if_fail(page->lsn_oldest<=page->lsn_newest);
516 g_return_if_fail(!page->lsn_newest || lsn>=page->lsn_newest);
517 g_return_if_fail(captive_shared_cache_map_object->LogHandle_set);
518 g_return_if_fail(captive_shared_cache_map_object->FlushToLsnRoutine_set);
520 if (!page->lsn_oldest)
521 page->lsn_oldest=lsn;
522 page->lsn_newest=lsn;
525 void captive_shared_cache_map_purge(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
528 CaptiveSharedCacheMapObject_page *page;
530 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
532 g_signal_emit(captive_shared_cache_map_object,purge_signal,0);
534 for (offset=0;offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE);offset+=PAGE_SIZE) {
535 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
536 if (!page->data_valid)
538 g_assert(!page->dirty); /* FIXME: Is it allowed by W32? */
539 page->data_valid=FALSE;
543 static VOID *captive_LogHandle;
544 static PFLUSH_TO_LSN captive_FlushToLsnRoutine;
546 void captive_shared_cache_map_set_LogHandle(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,VOID *LogHandle)
548 g_return_if_fail(!captive_shared_cache_map_object || CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
549 /* FIXME: 'captive_shared_cache_map_object->LogHandle_set' may be set.
550 * Does it mean 'LogHandle_set' is 'FileObject'-based instead of 'SharedCacheMap'-based?
555 g_assert(!captive_LogHandle || captive_LogHandle==LogHandle);
556 captive_LogHandle=LogHandle;
557 if (LogHandle && captive_shared_cache_map_object)
558 captive_shared_cache_map_object->LogHandle_set=TRUE;
561 void captive_shared_cache_map_set_FlushToLsnRoutine
562 (CaptiveSharedCacheMapObject *captive_shared_cache_map_object,PFLUSH_TO_LSN FlushToLsnRoutine)
564 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
565 /* FIXME: 'captive_shared_cache_map_object->FlushToLsnRoutine_set' may be set.
566 * Does it mean 'FlushToLsnRoutine_set' is 'FileObject'-based instead of 'SharedCacheMap'-based?
569 if (!FlushToLsnRoutine)
571 g_assert(!captive_FlushToLsnRoutine || captive_FlushToLsnRoutine==FlushToLsnRoutine);
572 captive_FlushToLsnRoutine=FlushToLsnRoutine;
573 if (FlushToLsnRoutine)
574 captive_shared_cache_map_object->FlushToLsnRoutine_set=TRUE;
577 static void captive_shared_cache_map_page_write(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
580 LARGE_INTEGER offset_LargeInteger;
581 static gint64 lsn_last;
582 CaptiveSharedCacheMapObject_page *page;
584 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
585 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
586 g_return_if_fail(offset<=CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
587 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
588 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
589 g_return_if_fail(page->data_valid);
590 g_return_if_fail(page->dirty);
592 if (page->lsn_newest) {
593 LARGE_INTEGER lsn_newest_LargeInteger;
596 g_assert(!lsn_last || lsn_last<=page->lsn_newest);
597 lsn_last=page->lsn_newest;
599 lsn_newest_LargeInteger.QuadPart=page->lsn_newest;
600 (*captive_FlushToLsnRoutine)(captive_LogHandle,lsn_newest_LargeInteger);
603 offset_LargeInteger.QuadPart=offset;
604 captive_Cc_IoPageWrite(captive_shared_cache_map_object->FileObject,
605 captive_shared_cache_map_object->buffer+offset,PAGE_SIZE,&offset_LargeInteger);
612 typedef struct _captive_shared_cache_map_page_write_lsn_foreach_param captive_shared_cache_map_page_write_lsn_foreach_param;
613 struct _captive_shared_cache_map_page_write_lsn_foreach_param {
616 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_best;
620 static void captive_shared_cache_map_page_write_lsn_foreach(
621 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
622 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
623 captive_shared_cache_map_page_write_lsn_foreach_param *param) /* user_data */
626 CaptiveSharedCacheMapObject_page *page;
628 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
629 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
630 g_return_if_fail(param!=NULL);
632 for (now=0;now<captive_shared_cache_map_object->AllocationSize;now+=PAGE_SIZE) {
633 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
634 if (!page->data_valid)
638 if (!page->lsn_newest)
640 if (page->lsn_newest>=param->lsn_target)
642 if (param->lsn_best && page->lsn_newest>param->lsn_best)
644 param->lsn_best=page->lsn_newest;
645 param->captive_shared_cache_map_object_best=captive_shared_cache_map_object;
646 param->offset_best=now;
650 static void captive_shared_cache_map_page_write_lsn(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
653 CaptiveSharedCacheMapObject_page *page;
655 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
656 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
657 g_return_if_fail(offset<CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
658 g_return_if_fail(0==CAPTIVE_ROUND_DOWN_EXCEEDING64(offset,PAGE_SIZE));
659 page=captive_shared_cache_map_object->pages+offset/PAGE_SIZE;
660 g_return_if_fail(page->data_valid);
661 g_return_if_fail(page->dirty);
663 if (page->lsn_newest) {
664 CaptiveSharedCacheMapObject_hash_init();
666 captive_shared_cache_map_page_write_lsn_foreach_param param;
668 param.lsn_target=page->lsn_newest;
670 g_hash_table_foreach(
671 CaptiveSharedCacheMapObject_hash, /* hash_table */
672 (GHFunc)captive_shared_cache_map_page_write_lsn_foreach, /* func */
673 ¶m); /* user_data */
676 captive_shared_cache_map_page_write(param.captive_shared_cache_map_object_best,param.offset_best);
680 captive_shared_cache_map_page_write(captive_shared_cache_map_object,offset);
683 guint64 captive_shared_cache_map_flush(CaptiveSharedCacheMapObject *captive_shared_cache_map_object,
684 guint64 start,guint64 end)
689 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),0);
690 g_return_val_if_fail(start<=end,0);
692 end=MIN(end,CAPTIVE_ROUND_UP64(captive_shared_cache_map_object->AllocationSize,PAGE_SIZE));
694 start=CAPTIVE_ROUND_DOWN64(start,PAGE_SIZE);
695 end=CAPTIVE_ROUND_UP64(end,PAGE_SIZE);
698 for (now=start;now<end;now+=PAGE_SIZE) {
699 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
701 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty)
703 captive_shared_cache_map_page_write_lsn(captive_shared_cache_map_object,now);
707 /* We were calling W32 code - recheck our task completion. */
708 for (now=start;now<end;now+=PAGE_SIZE) {
709 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
711 g_assert(!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty);
717 static void captive_shared_cache_map_is_any_dirty_foreach(
718 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
719 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
720 gboolean *dirty_foundp) /* user_data */
724 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
725 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
726 g_return_if_fail(dirty_foundp!=NULL);
728 for (now=0;now<captive_shared_cache_map_object->AllocationSize;now+=PAGE_SIZE) {
729 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].data_valid)
731 if (!captive_shared_cache_map_object->pages[now/PAGE_SIZE].dirty)
733 *dirty_foundp=TRUE; /* FIXME: stop the traversal. */
739 gboolean captive_shared_cache_map_is_any_dirty(void)
741 gboolean dirty_found;
743 CaptiveSharedCacheMapObject_hash_init();
745 g_hash_table_foreach(
746 CaptiveSharedCacheMapObject_hash, /* hash_table */
747 (GHFunc)captive_shared_cache_map_is_any_dirty_foreach, /* func */
748 &dirty_found); /* user_data */
754 typedef struct _captive_shared_cache_map_CcGetDirtyPages_foreach_param captive_shared_cache_map_CcGetDirtyPages_foreach_param;
755 struct _captive_shared_cache_map_CcGetDirtyPages_foreach_param {
756 PDIRTY_PAGE_ROUTINE DirtyPageRoutine;
762 static void captive_shared_cache_map_CcGetDirtyPages_foreach(
763 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
764 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
765 captive_shared_cache_map_CcGetDirtyPages_foreach_param *param) /* user_data */
768 CaptiveSharedCacheMapObject_page *page;
770 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
771 g_return_if_fail(captive_shared_cache_map_object->FileObject!=NULL);
772 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
773 g_return_if_fail(param!=NULL);
775 for (now=CAPTIVE_ROUND_DOWN64(captive_shared_cache_map_object->AllocationSize-1,PAGE_SIZE);now>=0;now-=PAGE_SIZE) {
776 LARGE_INTEGER now_LargeInteger,lsn_oldest_LargeInteger,lsn_newest_LargeInteger;
778 page=captive_shared_cache_map_object->pages+now/PAGE_SIZE;
779 if (!page->data_valid)
783 if (page->lsn_oldest && (!param->result || param->result>page->lsn_oldest))
784 param->result=page->lsn_oldest;
786 now_LargeInteger.QuadPart=now;
787 lsn_oldest_LargeInteger.QuadPart=page->lsn_oldest;
788 lsn_newest_LargeInteger.QuadPart=page->lsn_newest;
789 (*param->DirtyPageRoutine)(
790 captive_shared_cache_map_object->FileObject, /* FileObject */
791 &now_LargeInteger, /* FileOffset */
792 PAGE_SIZE, /* Length */
793 &lsn_oldest_LargeInteger, /* OldestLsn */
794 &lsn_newest_LargeInteger, /* NewestLsn */
795 param->Context1, /* Context1 */
796 param->Context2); /* Context2 */
800 gint64 captive_shared_cache_map_CcGetDirtyPages(PDIRTY_PAGE_ROUTINE DirtyPageRoutine,VOID *Context1,VOID *Context2)
802 captive_shared_cache_map_CcGetDirtyPages_foreach_param param;
804 g_return_val_if_fail(DirtyPageRoutine!=NULL,0);
806 param.DirtyPageRoutine=DirtyPageRoutine;
807 param.Context1=Context1;
808 param.Context2=Context2;
811 CaptiveSharedCacheMapObject_hash_init();
812 g_hash_table_foreach(
813 CaptiveSharedCacheMapObject_hash, /* hash_table */
814 (GHFunc)captive_shared_cache_map_CcGetDirtyPages_foreach, /* func */
815 ¶m); /* user_data */
820 gpointer captive_shared_cache_map_get_buffer(CaptiveSharedCacheMapObject *captive_shared_cache_map_object)
822 g_return_val_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object),NULL);
823 g_return_val_if_fail(captive_shared_cache_map_object->buffer!=NULL,NULL);
825 return captive_shared_cache_map_object->buffer;
828 static void captive_cc_FileObject_delete_foreach(
829 CaptiveSharedCacheMapObject *captive_shared_cache_map_object, /* key */
830 CaptiveSharedCacheMapObject *captive_shared_cache_map_object_value, /* value */
831 FILE_OBJECT *FileObject) /* user_data */
833 g_return_if_fail(CAPTIVE_SHARED_CACHE_MAP_IS_OBJECT(captive_shared_cache_map_object));
834 g_return_if_fail(captive_shared_cache_map_object==captive_shared_cache_map_object_value);
835 g_return_if_fail(FileObject!=NULL);
837 g_assert(FileObject!=captive_shared_cache_map_object->FileObject);
840 BOOLEAN captive_cc_FileObject_delete(FILE_OBJECT *FileObject)
842 g_return_val_if_fail(FileObject!=NULL,FALSE);
844 CaptiveSharedCacheMapObject_hash_init();
845 g_hash_table_foreach(
846 CaptiveSharedCacheMapObject_hash, /* hash_table */
847 (GHFunc)captive_cc_FileObject_delete_foreach, /* func */
848 FileObject); /* user_data */
850 return FALSE; /* FIXME: remove useless return code. */