--------------------- PatchSet 379 Date: 2002/12/14 10:48:16 Author: rbcollins Branch: unify-io Tag: (none) Log: start factoring the rebuild logic Members: src/ufscommon.cc:1.4.10.30->1.4.10.31 Index: squid3/src/ufscommon.cc =================================================================== RCS file: /cvsroot/squid-sf//squid3/src/Attic/ufscommon.cc,v retrieving revision 1.4.10.30 retrieving revision 1.4.10.31 diff -u -r1.4.10.30 -r1.4.10.31 --- squid3/src/ufscommon.cc 14 Dec 2002 10:01:10 -0000 1.4.10.30 +++ squid3/src/ufscommon.cc 14 Dec 2002 10:48:16 -0000 1.4.10.31 @@ -1,5 +1,5 @@ /* - * $Id: ufscommon.cc,v 1.4.10.30 2002/12/14 10:01:10 rbcollins Exp $ + * $Id: ufscommon.cc,v 1.4.10.31 2002/12/14 10:48:16 rbcollins Exp $ * * DEBUG: section 47 Store Directory Routines * AUTHOR: Duane Wessels @@ -35,11 +35,17 @@ #include "ufscommon.h" #include "Store.h" #include "SwapDir.h" +#include "RefCount.h" -class RebuildState { +class RebuildState : public RefCountable { public: + void *operator new(size_t); + void operator delete(void *); + void deleteSelf() const; static EVH RebuildFromDirectory; static EVH RebuildFromSwapLog; + + ~RebuildState(); UFSSwapDir *sd; int n_read; FILE *log; @@ -60,11 +66,48 @@ char fullfilename[SQUID_MAXPATHLEN]; struct _store_rebuild_data counts; private: + CBDATA_CLASS(RebuildState); void rebuildFromDirectory(); void rebuildFromSwapLog(); int getNextFile(sfileno *, int *size); }; +CBDATA_CLASS_INIT(RebuildState); + +void * +RebuildState::operator new (size_t size) +{ + assert (size == sizeof(RebuildState)); + CBDATA_INIT_TYPE(RebuildState); + RebuildState *result = cbdataAlloc(RebuildState); + /* Mark result as being owned - we want the refcounter to do the delete + * call */ + cbdataReference(result); + return result; +} + +void +RebuildState::operator delete (void *address) +{ + RebuildState *t = static_cast(address); + cbdataFree(address); + /* And allow the memory to be freed */ + cbdataReferenceDone (t); +} + +void +RebuildState::deleteSelf() const +{ + delete this; +} + +RebuildState::~RebuildState() +{ + store_dirs_rebuilding--; + sd->closeTmpSwapLog(); + storeRebuildComplete(&counts); +} + static int n_dirs = 0; static int *dir_index = NULL; static int commonUfsFilenoBelongsHere(int fn, int F0, int F1, int F2); @@ -311,7 +354,6 @@ StoreEntry *e = NULL; StoreEntry tmpe; cache_key key[MD5_DIGEST_CHARS]; - int count; struct stat sb; int swap_hdr_len; int fd = -1; @@ -319,7 +361,7 @@ tlv *t; assert(this != NULL); debug(47, 3) ("commonUfsDirRebuildFromDirectory: DIR #%d\n", sd->index); - for (count = 0; count < speed; count++) { + for (int count = 0; count < speed; count++) { assert(fd == -1); sfileno filn = 0; int size; @@ -327,11 +369,7 @@ if (fd == -2) { debug(47, 1) ("Done scanning %s swaplog (%d entries)\n", sd->path, n_read); - store_dirs_rebuilding--; - sd->closeTmpSwapLog(); - storeRebuildComplete(&counts); - RebuildState *anLvalue = this; - cbdataFree(anLvalue); + deleteSelf(); return; } else if (fd < 0) { continue; @@ -455,24 +493,17 @@ RebuildState::rebuildFromSwapLog() { StoreEntry *e = NULL; - storeSwapLogData s; - size_t ss = sizeof(storeSwapLogData); - int count; - int used; /* is swapfile already in use? */ - int disk_entry_newer; /* is the log entry newer than current entry? */ double x; /* load a number of objects per invocation */ - for (count = 0; count < speed; count++) { + for (int count = 0; count < speed; count++) { + storeSwapLogData s; + size_t ss = sizeof(storeSwapLogData); if (fread(&s, ss, 1, log) != 1) { debug(47, 1) ("Done reading %s swaplog (%d entries)\n", sd->path, n_read); fclose(log); log = NULL; - store_dirs_rebuilding--; - sd->closeTmpSwapLog(); - storeRebuildComplete(&counts); - RebuildState *anLvalue = this; - cbdataFree(anLvalue); + delete this; return; } n_read++; @@ -540,11 +571,13 @@ continue; } e = storeGet(s.key); + int used; /* is swapfile already in use? */ used = sd->mapBitTest(s.swap_filen); /* If this URL already exists in the cache, does the swap log * appear to have a newer entry? Compare 'lastref' from the * swap log to e->lastref. */ - disk_entry_newer = e ? (s.lastref > e->lastref ? 1 : 0) : 0; + /* is the log entry newer than current entry? */ + int disk_entry_newer = e ? (s.lastref > e->lastref ? 1 : 0) : 0; if (used && !disk_entry_newer) { /* log entry is old, ignore it */ counts.clashcount++; @@ -630,7 +663,6 @@ { squidufsinfo_t *ioinfo = (squidufsinfo_t *) sd->fsdata; int fd = -1; - int used = 0; int dirs_opened = 0; debug(47, 3) ("commonUfsDirGetNextFile: flag=%d, %d: /%02X/%02X\n", flags.init, @@ -680,8 +712,7 @@ fn, sd->index, curlvl1, curlvl2); continue; } - used = sd->mapBitTest(fn); - if (used) { + if (sd->mapBitTest(fn)) { debug(47, 3) ("commonUfsDirGetNextFile: Locked, continuing with next.\n"); continue; } @@ -754,8 +785,6 @@ return e; } -CBDATA_TYPE(RebuildState); - void UFSSwapDir::rebuild() { @@ -763,8 +792,7 @@ int zero = 0; FILE *fp; EVH *func = NULL; - CBDATA_INIT_TYPE(RebuildState); - RebuildState *rb = cbdataAlloc(RebuildState); + RebuildState *rb = new RebuildState; rb->sd = this; rb->speed = opt_foreground_rebuild ? 1 << 30 : 50; /*