Kaydet (Commit) 76876513 authored tarafından Caolán McNamara's avatar Caolán McNamara

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.
üst f0a2c790
......@@ -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");
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment