diff --git a/network/cache/nu/include/nsCacheTrace.h b/network/cache/nu/include/nsCacheTrace.h index 6977bb53e72c..fe544af928ce 100644 --- a/network/cache/nu/include/nsCacheTrace.h +++ b/network/cache/nu/include/nsCacheTrace.h @@ -28,23 +28,8 @@ # define CRLF "\r\n" #endif -#include -#include - -#define APPEND(x) \ - if (total) { \ - total = (char*) realloc (total, strlen(total) + strlen(x) +1); \ - if (total == 0) \ - return 0; \ - strcat(total, x); \ - } \ - else { \ - total = (char*) malloc (strlen(x)+1); \ - if (total == 0) \ - return 0; \ - strcpy(total, x); } - -#include +#include +#include class nsCacheTrace { diff --git a/network/cache/nu/nsGeneric.h b/network/cache/nu/nsGeneric.h index 6d6fe04f4fbb..5f74a936c338 100644 --- a/network/cache/nu/nsGeneric.h +++ b/network/cache/nu/nsGeneric.h @@ -38,6 +38,7 @@ protected: private: nsGeneric(const nsGeneric& o); nsGeneric& operator=(const nsGeneric& o); -} +}; + #endif // nsGeneric_h__ diff --git a/network/cache/nu/public/nsCacheManager.h b/network/cache/nu/public/nsCacheManager.h index c1fced131eae..2c46730fa545 100644 --- a/network/cache/nu/public/nsCacheManager.h +++ b/network/cache/nu/public/nsCacheManager.h @@ -32,31 +32,53 @@ #include "nsCacheModule.h" #include "nsCacheObject.h" +class nsMemModule; +class nsDiskModule; +class nsCachePref; + class nsCacheManager //: public nsISupports { - + /* Change entries from 32 to 16 bit */ public: nsCacheManager(); ~nsCacheManager(); PRInt32 AddModule(nsCacheModule* i_cacheModule); + PRBool Contains(const char* i_url) const; + + /* Number of modules in the cache manager */ PRInt32 Entries() const; - /* Singleton */ + + /* Singleton */ static nsCacheManager* GetInstance(); + nsCacheObject* GetObject(const char* i_url) const; nsCacheModule* GetModule(PRInt32 i_index) const; + + nsMemModule* GetMemModule() const; + nsDiskModule* GetDiskModule() const; + const char* Trace() const; + /* Performance measure- microseconds */ - PRUint32 WorstCaseTime() const; + PRUint32 WorstCaseTime() const; + protected: - nsCacheModule* + void Init(); + nsCacheModule* LastModule() const; + enum modules + { + MEM =0, + DISK=1 + }; private: nsCacheModule* m_pFirstModule; + nsCachePref* m_pPref; nsCacheManager(const nsCacheManager& cm); nsCacheManager& operator=(const nsCacheManager& cm); diff --git a/network/cache/nu/public/nsCacheObject.h b/network/cache/nu/public/nsCacheObject.h index 15ef6085f1eb..3e7eebe3096c 100644 --- a/network/cache/nu/public/nsCacheObject.h +++ b/network/cache/nu/public/nsCacheObject.h @@ -70,6 +70,9 @@ public: LastModified(void) const; void LastModified(PRIntervalTime i_lastModified); + PRInt16 Module(void) const; + void Module(PRUint16 i_m); + PRUint32 Size(void) const; void Size(PRUint32 s); @@ -93,6 +96,8 @@ protected: PRUint32 m_Size; char* m_Url; + PRInt16 m_Module; + private: nsCacheObject& operator=(const nsCacheObject& x); }; @@ -148,6 +153,16 @@ inline void nsCacheObject::LastModified(PRIntervalTime i_LastModified) m_LastModified = i_LastModified; } +inline PRInt16 nsCacheObject::Module(void) const +{ + return m_Module; +} + +inline void nsCacheObject::Module(PRUint16 i_Module) +{ + m_Module = i_Module; +} + inline PRUint32 nsCacheObject::Size(void) const { return m_Size; diff --git a/network/cache/nu/src/makefile.win b/network/cache/nu/src/makefile.win index 5fb6014c68d5..6638c0bfb0fe 100644 --- a/network/cache/nu/src/makefile.win +++ b/network/cache/nu/src/makefile.win @@ -23,10 +23,10 @@ DLL = .\$(OBJDIR)\$(DLLNAME).dll LIBRARY_NAME=cachelib -MISCDEP = \ +MISCDEP = \ + $(LIBNSPR) \ $(DIST)\lib\xpcom32.lib \ - $(LIBNSPR) \ - $(NULL) + $(NULL) MODULE = cache @@ -37,6 +37,7 @@ OBJS = \ .\$(OBJDIR)\nsDiskModule.obj \ .\$(OBJDIR)\nsCacheTrace.obj \ .\$(OBJDIR)\nsCacheManager.obj \ + .\$(OBJDIR)\nsCachePref.obj \ .\$(OBJDIR)\nsMemCacheObject.obj \ $(NULL) @@ -51,9 +52,9 @@ LCFLAGS = \ -DNS_DLLNAME=$(DLLNAME).dll \ $(NULL) -LLIBS = \ - $(DIST)\lib\xpcom32.lib \ - $(LIBNSPR) \ +LLIBS = \ + $(DIST)\lib\xpcom32.lib \ + $(LIBNSPR) \ $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/network/cache/nu/src/nsCacheManager.cpp b/network/cache/nu/src/nsCacheManager.cpp index 50edac9e295d..df7f861564db 100644 --- a/network/cache/nu/src/nsCacheManager.cpp +++ b/network/cache/nu/src/nsCacheManager.cpp @@ -23,17 +23,23 @@ #include #include + +#include +#include +#include + #include "nsCacheManager.h" #include "nsCacheTrace.h" +#include "nsCachePref.h" #include "nsCacheModule.h" - -#include // TODO change -#include //TODO change to PR_ +#include "nsMemModule.h" +#include "nsDiskModule.h" static nsCacheManager TheManager; -nsCacheManager::nsCacheManager(): m_pFirstModule(0) +nsCacheManager::nsCacheManager(): m_pFirstModule(0), m_pPref(new nsCachePref()) { + Init(); } nsCacheManager::~nsCacheManager() @@ -43,6 +49,7 @@ nsCacheManager::~nsCacheManager() if (m_pFirstModule) delete m_pFirstModule; #endif + delete m_pPref; } nsCacheManager* nsCacheManager::GetInstance() @@ -50,15 +57,17 @@ nsCacheManager* nsCacheManager::GetInstance() return &TheManager; } +/* Caller must free returned char* */ const char* nsCacheManager::Trace() const { char linebuffer[128]; - char* total = 0; - - sprintf(linebuffer, "nsCacheManager: Modules = %d\n", Entries()); - APPEND(linebuffer); + char* total; + + sprintf(linebuffer, "nsCacheManager: Modules = %d\n", Entries()); + total = new char[strlen(linebuffer) + 1]; + strcpy(total, linebuffer); return total; } @@ -133,12 +142,31 @@ nsCacheModule* nsCacheManager::GetModule(PRInt32 i_index) const nsCacheModule* pModule = m_pFirstModule; for (PRInt32 i=0; i<=i_index; pModule = pModule->Next()) i++; -#ifdef DEBUG - assert(pModule); -#endif + PR_ASSERT(pModule); return pModule; } +nsMemModule* nsCacheManager::GetMemModule() const +{ + return (nsMemModule*) m_pFirstModule; +} + +nsDiskModule* nsCacheManager::GetDiskModule() const +{ + return (m_pFirstModule) ? (nsDiskModule*) m_pFirstModule->Next() : NULL; +} + + +void nsCacheManager::Init() +{ + if (m_pFirstModule) + delete m_pFirstModule; + + m_pFirstModule = new nsMemModule(m_pPref->MemCacheSize()); + // m_pFirstModule->Next((new nsDiskModule(m_pPref->DiskCacheSize())); + +} + nsCacheModule* nsCacheManager::LastModule() const { if (m_pFirstModule) diff --git a/network/cache/nu/src/nsCacheModule.cpp b/network/cache/nu/src/nsCacheModule.cpp index 067b38dd123f..f66c245f6b4c 100644 --- a/network/cache/nu/src/nsCacheModule.cpp +++ b/network/cache/nu/src/nsCacheModule.cpp @@ -16,9 +16,13 @@ * Reserved. */ +#include +#include + #include "nsCacheModule.h" #include "nsCacheTrace.h" + /* * nsCacheModule * @@ -55,10 +59,12 @@ nsCacheModule::~nsCacheModule() const char* nsCacheModule::Trace() const { char linebuffer[128]; - char* total = 0; + char* total; sprintf(linebuffer, "nsCacheModule: Objects = %d\n", Entries()); - APPEND(linebuffer); + + total = new char[strlen(linebuffer) + 1]; + strcpy(total, linebuffer); return total; } \ No newline at end of file diff --git a/network/cache/nu/src/nsCacheObject.cpp b/network/cache/nu/src/nsCacheObject.cpp index 2facb96da357..cd643fad0ca0 100644 --- a/network/cache/nu/src/nsCacheObject.cpp +++ b/network/cache/nu/src/nsCacheObject.cpp @@ -15,8 +15,9 @@ * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ -#include -#include +#include +#include +#include #include "nsCacheObject.h" #include "nsCacheTrace.h" @@ -33,7 +34,8 @@ static const PRIntervalTime DEFAULT_EXPIRES = PR_SecondsToInterval(86400); nsCacheObject::nsCacheObject(): m_Flags(INIT), m_Url(new char[1]), - m_Etag(new char[1]) + m_Etag(new char[1]), + m_Module(-1) { Init(); *m_Url = '\0'; @@ -67,14 +69,14 @@ nsCacheObject::nsCacheObject(const char* i_url): m_Etag(new char[1]) { Init(); - assert(i_url); + PR_ASSERT(i_url); strcpy(m_Url, i_url); *m_Etag = '\0'; } void nsCacheObject::Address(const char* i_url) { - assert(i_url); + PR_ASSERT(i_url && *i_url); if (m_Url) delete[] m_Url; m_Url = new char[strlen(i_url) + 1]; @@ -83,7 +85,7 @@ void nsCacheObject::Address(const char* i_url) void nsCacheObject::Etag(const char* i_etag) { - assert(i_etag && *i_etag); + PR_ASSERT(i_etag && *i_etag); if (m_Etag) delete[] m_Etag; m_Etag = new char[strlen(i_etag) + 1]; @@ -96,10 +98,11 @@ void nsCacheObject::Init() m_Hits = 0; } +/* Caller must free returned string */ const char* nsCacheObject::Trace() const { char linebuffer[256]; - char* total = 0; + char* total; sprintf(linebuffer, "nsCacheObject:URL=%s,SIZE=%d,ET=%s,\n\tLM=%d,LA=%d,EXP=%d,HITS=%d\n", m_Url, @@ -109,7 +112,9 @@ const char* nsCacheObject::Trace() const m_LastAccessed, m_Expires, m_Hits); - APPEND(linebuffer); + + total = new char[strlen(linebuffer) +1]; + strcpy(total, linebuffer); return total; } \ No newline at end of file diff --git a/network/cache/nu/src/nsMemModule.cpp b/network/cache/nu/src/nsMemModule.cpp index 943625a5f849..af14e7ac3f70 100644 --- a/network/cache/nu/src/nsMemModule.cpp +++ b/network/cache/nu/src/nsMemModule.cpp @@ -50,6 +50,7 @@ nsMemModule::~nsMemModule() } } +/* TODO dont make copies */ PRBool nsMemModule::AddObject(nsCacheObject* i_pObject) { if (i_pObject) diff --git a/network/cache/nu/tests/cb/cb.mak b/network/cache/nu/tests/cb/cb.mak index 0688e81617c0..3cc04f5746ee 100644 --- a/network/cache/nu/tests/cb/cb.mak +++ b/network/cache/nu/tests/cb/cb.mak @@ -380,6 +380,16 @@ SOURCE=.\cbView.cpp !IF "$(CFG)" == "cb - Win32 Release" DEP_CPP_CBVIE=\ + "..\..\..\..\..\dist\public\cache\nsCacheManager.h"\ + "..\..\..\..\..\dist\public\cache\nsCacheModule.h"\ + "..\..\..\..\..\dist\public\cache\nsCacheObject.h"\ + "..\..\..\..\..\dist\public\cache\nsDiskModule.h"\ + "..\..\..\..\..\dist\public\cache\nsMemCacheObject.h"\ + "..\..\..\..\..\dist\public\cache\nsMemModule.h"\ + "..\..\..\..\..\dist\win32_d.obj\include\obsolete\protypes.h"\ + "..\..\..\..\..\dist\win32_d.obj\include\prcpucfg.h"\ + "..\..\..\..\..\dist\win32_d.obj\include\prinrval.h"\ + "..\..\..\..\..\dist\win32_d.obj\include\prtypes.h"\ ".\cb.h"\ ".\cbDoc.h"\ ".\cbView.h"\ @@ -387,10 +397,8 @@ DEP_CPP_CBVIE=\ ".\StdAfx.h"\ NODEP_CPP_CBVIE=\ - ".\nsCacheManager.h"\ - ".\nsCacheObject.h"\ - ".\nsDiskModule.h"\ - ".\nsMemModule.h"\ + "..\..\..\..\..\dist\public\cache\nsISupports.h"\ + "..\..\..\..\..\dist\win32_d.obj\include\protypes.h"\ "$(INTDIR)\cbView.obj" : $(SOURCE) $(DEP_CPP_CBVIE) "$(INTDIR)"\ diff --git a/network/cache/nu/tests/cb/cb.mdp b/network/cache/nu/tests/cb/cb.mdp index 26585e6dc6cb..666530937fe5 100644 Binary files a/network/cache/nu/tests/cb/cb.mdp and b/network/cache/nu/tests/cb/cb.mdp differ diff --git a/network/cache/nu/tests/cb/cbView.cpp b/network/cache/nu/tests/cb/cbView.cpp index e4eb4548bf28..2751b56d2f18 100644 --- a/network/cache/nu/tests/cb/cbView.cpp +++ b/network/cache/nu/tests/cb/cbView.cpp @@ -13,6 +13,7 @@ #include "nsMemModule.h" #include "nsDiskModule.h" #include "nsCacheObject.h" +#include "nsTimeIt.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -58,9 +59,11 @@ void CCbView::OnDraw(CDC* pDC) { CRect rect; GetClientRect(&rect); + + pDC->SetBkColor(RGB(192,192,192)); pDC->FillSolidRect(rect, RGB(192,192,192)); - rect.DeflateRect(10,10); + rect.DeflateRect(10,10); pDC->DrawText(m_Mesg.GetBuffer(m_Mesg.GetLength()), -1, rect, DT_WORDBREAK | DT_EXPANDTABS); } @@ -123,6 +126,13 @@ void CCbView::OnInitialUpdate() pCO->Size(1230); pCO->Etag("Another"); + int j = 1000; + char tmpBuff[10]; + while (j--) + { + pMM->AddObject(pCO); + pCO->Address(itoa(j,tmpBuff,10)); + } if (pMM->AddObject(pCO)) { char buffer[10]; @@ -137,17 +147,30 @@ void CCbView::OnInitialUpdate() m_Mesg += pMM->Trace(); m_Mesg += "-----------------\n"; - for (int i = pMM->Entries(); i>0 ; i--) + /* + for (int i = pMM->Entries(); i>0 ; i--) { m_Mesg += pMM->GetObject(i-1)->Trace(); } m_Mesg += "-----------------\n"; + */ char buffer[10]; m_Mesg += "Worst case time= "; m_Mesg += itoa(pCM->WorstCaseTime(), buffer, 10); m_Mesg += " microsec.\n"; + PRUint32 t; + { + nsTimeIt tmp(t); + if (pMM->Contains("999")) + m_Mesg += "!"; + } + m_Mesg += "-----------------\n"; + m_Mesg += "Found in "; + m_Mesg += itoa(t, buffer, 10); + m_Mesg += " microsec.\n"; + delete pMM; pMM = 0; delete pCO;