diff options
author | Caolán McNamara <caolanm@redhat.com> | 2012-03-23 08:53:25 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2012-03-23 10:20:47 +0000 |
commit | 76876513479522b37c9047a418521624e27afff6 (patch) | |
tree | 8fe5cd5f92d80440fe427b5ec8dbf8a5f416f077 /desktop | |
parent | f0a2c790192130c0ebf6937673552237c33d4b21 (diff) |
Take a meg off our memory footprint
We create 6 berkleydb backed databases. If no DB_ENV is provided for a
database, then berkleydb creates one for each database. Each
environment has a memory footprint of about 200k. It appears to be
legal to share an environment, which shaves about 1M off our
permanant footprint.
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/source/deployment/misc/db.cxx | 49 |
1 files changed, 48 insertions, 1 deletions
diff --git a/desktop/source/deployment/misc/db.cxx b/desktop/source/deployment/misc/db.cxx index 45f91c7e4cf0..98cfc0189887 100644 --- a/desktop/source/deployment/misc/db.cxx +++ b/desktop/source/deployment/misc/db.cxx @@ -30,6 +30,7 @@ #include <db.hxx> #include <rtl/alloc.h> +#include <rtl/instance.hxx> #include <cstring> #include <errno.h> @@ -54,12 +55,57 @@ char *DbEnv::strerror(int error) return (db_strerror(error)); } +namespace +{ + class theDbEnvMutex + : public rtl::Static<osl::Mutex, theDbEnvMutex> {}; + + class SharedDbEnv : private boost::noncopyable + { + public: + static DB_ENV* getInstance(); + static void releaseInstance(); + private: + SharedDbEnv(); + ~SharedDbEnv(); + static DB_ENV* pSharedEnv; + static int nSharedEnv; + }; + + DB_ENV* SharedDbEnv::pSharedEnv = NULL; + int SharedDbEnv::nSharedEnv = 0; + + DB_ENV* SharedDbEnv::getInstance() + { + ::osl::MutexGuard aGuard(theDbEnvMutex::get()); + if (pSharedEnv == NULL) + { + db_env_create(&pSharedEnv, 0); + pSharedEnv->open(pSharedEnv, NULL, DB_CREATE | DB_INIT_MPOOL | DB_PRIVATE | DB_THREAD, 0); + } + ++nSharedEnv; + return pSharedEnv; + } + + void SharedDbEnv::releaseInstance() + { + ::osl::MutexGuard aGuard(theDbEnvMutex::get()); + --nSharedEnv; + if (0 == nSharedEnv) + { + pSharedEnv->close(pSharedEnv, 0); + pSharedEnv = NULL; + } + } +} + //---------------------------------------------------------------------------- Db::Db(u_int32_t flags) : m_pDBP(0) { - db_internal::check_error( db_create(&m_pDBP, NULL, flags),"Db::Db" ); + DB_ENV *pSharedDbEnv = SharedDbEnv::getInstance(); + db_internal::check_error( db_create(&m_pDBP, pSharedDbEnv, flags),"Db::Db" ); } @@ -78,6 +124,7 @@ int Db::close(u_int32_t flags) { int error = m_pDBP->close(m_pDBP,flags); m_pDBP = 0; + SharedDbEnv::releaseInstance(); return db_internal::check_error(error,"Db::close"); } |