summaryrefslogtreecommitdiff
path: root/plugins/Db3x_mmap
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2012-07-21 08:10:36 +0000
committerGeorge Hazan <george.hazan@gmail.com>2012-07-21 08:10:36 +0000
commitcbd39236065544d5f5ac914dfd1099f740b27476 (patch)
treee00f408cffb82ab6bb9b15d5051f134d119831b2 /plugins/Db3x_mmap
parent9550917a49f7046389cfdd8158061477106289bd (diff)
no more direct access to critsecs in mmap
git-svn-id: http://svn.miranda-ng.org/main/trunk@1089 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Db3x_mmap')
-rw-r--r--plugins/Db3x_mmap/dbcache.cpp15
-rw-r--r--plugins/Db3x_mmap/dbcontacts.cpp83
-rw-r--r--plugins/Db3x_mmap/dbevents.cpp178
-rw-r--r--plugins/Db3x_mmap/dbsettings.cpp186
4 files changed, 213 insertions, 249 deletions
diff --git a/plugins/Db3x_mmap/dbcache.cpp b/plugins/Db3x_mmap/dbcache.cpp
index 03fcc7d7d0..466aa7a00d 100644
--- a/plugins/Db3x_mmap/dbcache.cpp
+++ b/plugins/Db3x_mmap/dbcache.cpp
@@ -54,7 +54,7 @@ void CDdxMmap::ReMap(DWORD needed)
}
}
else
- m_dwFileSize += m_ChunkSize;
+ m_dwFileSize += m_ChunkSize;
// FlushViewOfFile(m_pDbCache, 0);
UnmapViewOfFile(m_pDbCache);
@@ -68,8 +68,8 @@ void CDdxMmap::DBMoveChunk(DWORD ofsDest,DWORD ofsSource,int bytes)
{
int x = 0;
log3("move %d %08x->%08x",bytes,ofsSource,ofsDest);
- if (ofsDest+bytes>m_dwFileSize) ReMap(ofsDest+bytes-m_dwFileSize);
- if (ofsSource+bytes>m_dwFileSize) {
+ if (ofsDest+bytes > m_dwFileSize) ReMap(ofsDest+bytes-m_dwFileSize);
+ if (ofsSource+bytes > m_dwFileSize) {
x = ofsSource+bytes-m_dwFileSize;
log0("buggy move!");
_ASSERT(0);
@@ -100,7 +100,7 @@ PBYTE CDdxMmap::DBRead(DWORD ofs,int bytesRequired,int *bytesAvail)
void CDdxMmap::DBWrite(DWORD ofs,PVOID pData,int bytes)
{
log2("write %d@%08x",bytes,ofs);
- if (ofs+bytes>m_dwFileSize) ReMap(ofs+bytes-m_dwFileSize);
+ if (ofs+bytes > m_dwFileSize) ReMap(ofs+bytes-m_dwFileSize);
MoveMemory(m_pDbCache+ofs,pData,bytes);
logg();
}
@@ -160,13 +160,12 @@ void CDdxMmap::DBFlush(int setting)
int CDdxMmap::InitCache(void)
{
- DWORD x;
-
m_dwFileSize = GetFileSize(m_hDbFile, NULL);
// Align to chunk
- x = m_dwFileSize % m_ChunkSize;
- if (x) m_dwFileSize += m_ChunkSize - x;
+ DWORD x = m_dwFileSize % m_ChunkSize;
+ if (x)
+ m_dwFileSize += m_ChunkSize - x;
Map();
diff --git a/plugins/Db3x_mmap/dbcontacts.cpp b/plugins/Db3x_mmap/dbcontacts.cpp
index c23db78f30..b8d4a843b8 100644
--- a/plugins/Db3x_mmap/dbcontacts.cpp
+++ b/plugins/Db3x_mmap/dbcontacts.cpp
@@ -39,9 +39,9 @@ DBCachedContactValueList* CDdxMmap::AddToCachedContactList(HANDLE hContact, int
int CDdxMmap::CheckProto(HANDLE hContact, const char *proto)
{
- static char protobuf[MAX_PATH] = {0};
- static DBVARIANT dbv;
- static DBCONTACTGETSETTING sVal = {proto_module,proto_setting,&dbv};
+ char protobuf[MAX_PATH] = {0};
+ DBVARIANT dbv;
+ DBCONTACTGETSETTING sVal = {proto_module,proto_setting,&dbv};
dbv.type = DBVT_ASCIIZ;
dbv.pszVal = protobuf;
@@ -106,35 +106,31 @@ STDMETHODIMP_(HANDLE) CDdxMmap::FindNextContact(HANDLE hContact, const char *szP
STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact)
{
- DWORD ofsThis,ofsNext,ofsFirstEvent;
- DBContactSettings *dbcs;
- DBEvent *dbe;
- int index;
-
if (hContact == NULL)
return 1;
- {
- mir_cslock lck(m_csDbAccess);
- DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE)
- return 1;
+ mir_cslockfull lck(m_csDbAccess);
+ DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
+ if (dbc->signature != DBCONTACT_SIGNATURE)
+ return 1;
- if (hContact == (HANDLE)m_dbHeader.ofsUser) {
- log0("FATAL: del of user chain attempted.");
- return 1;
- }
- log0("del contact");
+ if (hContact == (HANDLE)m_dbHeader.ofsUser) {
+ log0("FATAL: del of user chain attempted.");
+ return 1;
}
+ lck.unlock();
+ log0("del contact");
+
// call notifier while outside mutex
NotifyEventHooks(hContactDeletedEvent, (WPARAM)hContact, 0);
// get back in
- mir_cslock lck(m_csDbAccess);
+ lck.lock();
DBCachedContactValueList VLtemp;
VLtemp.hContact = hContact;
+ int index;
if ((index = m_lContacts.getIndex(&VLtemp)) != -1) {
DBCachedContactValueList *VL = m_lContacts[index];
DBCachedContactValue* V = VL->first;
@@ -151,21 +147,21 @@ STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact)
m_lContacts.remove(index);
}
- DBContact *dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
+ dbc = (DBContact*)DBRead(hContact, sizeof(DBContact), NULL);
//delete settings chain
- ofsThis = dbc->ofsFirstSettings;
- ofsFirstEvent = dbc->ofsFirstEvent;
+ DWORD ofsThis = dbc->ofsFirstSettings;
+ DWORD ofsFirstEvent = dbc->ofsFirstEvent;
while (ofsThis) {
- dbcs = (DBContactSettings*)DBRead(ofsThis,sizeof(DBContactSettings),NULL);
- ofsNext = dbcs->ofsNext;
+ DBContactSettings *dbcs = (DBContactSettings*)DBRead(ofsThis,sizeof(DBContactSettings),NULL);
+ DWORD ofsNext = dbcs->ofsNext;
DeleteSpace(ofsThis,offsetof(DBContactSettings,blob)+dbcs->cbBlob);
ofsThis = ofsNext;
}
//delete event chain
ofsThis = ofsFirstEvent;
while (ofsThis) {
- dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
- ofsNext = dbe->ofsNext;
+ DBEvent *dbe = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
+ DWORD ofsNext = dbe->ofsNext;
DeleteSpace(ofsThis,offsetof(DBEvent,blob)+dbe->cbBlob);
ofsThis = ofsNext;
}
@@ -176,7 +172,7 @@ STDMETHODIMP_(LONG) CDdxMmap::DeleteContact(HANDLE hContact)
DBWrite(0,&m_dbHeader,sizeof(m_dbHeader));
}
else {
- ofsNext = dbc->ofsNext;
+ DWORD ofsNext = dbc->ofsNext;
ofsThis = m_dbHeader.ofsFirstContact;
DBContact *dbcPrev = (DBContact*)DBRead(ofsThis,sizeof(DBContact),NULL);
while (dbcPrev->ofsNext != (DWORD)hContact) {
@@ -230,24 +226,19 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddContact()
STDMETHODIMP_(BOOL) CDdxMmap::IsDbContact(HANDLE hContact)
{
- DBContact *dbc;
- DWORD ofsContact = (DWORD)hContact;
- int ret;
+ mir_cslock lck(m_csDbAccess);
- EnterCriticalSection(&m_csDbAccess);
- {
- int index;
- DBCachedContactValueList VLtemp;
- VLtemp.hContact = hContact;
- if ((index = m_lContacts.getIndex(&VLtemp)) != -1)
- ret = TRUE;
- else {
- dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- ret = dbc->signature == DBCONTACT_SIGNATURE;
- if (ret)
- AddToCachedContactList(hContact, index);
- } }
-
- LeaveCriticalSection(&m_csDbAccess);
- return ret;
+ DBCachedContactValueList VLtemp;
+ VLtemp.hContact = hContact;
+ int index = m_lContacts.getIndex(&VLtemp);
+ if (index != -1)
+ return TRUE;
+
+ DBContact *dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
+ if (dbc->signature == DBCONTACT_SIGNATURE) {
+ AddToCachedContactList(hContact, index);
+ return TRUE;
+ }
+
+ return FALSE;
}
diff --git a/plugins/Db3x_mmap/dbevents.cpp b/plugins/Db3x_mmap/dbevents.cpp
index ba4f496868..22245d4515 100644
--- a/plugins/Db3x_mmap/dbevents.cpp
+++ b/plugins/Db3x_mmap/dbevents.cpp
@@ -48,88 +48,86 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
return 0;
BOOL neednotify;
- DWORD ofsNew;
- {
- mir_cslock lck(m_csDbAccess);
-
- DWORD ofsContact = (hContact == 0) ? m_dbHeader.ofsUser : (DWORD)hContact;
- DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc.signature != DBCONTACT_SIGNATURE)
- return 0;
-
- ofsNew = CreateNewSpace(offsetof(DBEvent,blob) + dbei->cbBlob);
- DWORD ofsModuleName = GetModuleNameOfs(dbei->szModule);
-
- DBEvent dbe;
- dbe.signature = DBEVENT_SIGNATURE;
- dbe.ofsModuleName = ofsModuleName;
- dbe.timestamp = dbei->timestamp;
- dbe.flags = dbei->flags;
- dbe.eventType = dbei->eventType;
- dbe.cbBlob = dbei->cbBlob;
- //find where to put it - sort by timestamp
- if (dbc.eventCount == 0) {
+ mir_cslockfull lck(m_csDbAccess);
+
+ DWORD ofsContact = (hContact == 0) ? m_dbHeader.ofsUser : (DWORD)hContact;
+ DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
+ if (dbc.signature != DBCONTACT_SIGNATURE)
+ return 0;
+
+ DWORD ofsNew = CreateNewSpace(offsetof(DBEvent,blob) + dbei->cbBlob);
+ DWORD ofsModuleName = GetModuleNameOfs(dbei->szModule);
+
+ DBEvent dbe;
+ dbe.signature = DBEVENT_SIGNATURE;
+ dbe.ofsModuleName = ofsModuleName;
+ dbe.timestamp = dbei->timestamp;
+ dbe.flags = dbei->flags;
+ dbe.eventType = dbei->eventType;
+ dbe.cbBlob = dbei->cbBlob;
+ //find where to put it - sort by timestamp
+ if (dbc.eventCount == 0) {
+ dbe.ofsPrev = (DWORD)hContact;
+ dbe.ofsNext = 0;
+ dbe.flags |= DBEF_FIRST;
+ dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew;
+ }
+ else {
+ DBEvent *dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent,sizeof(DBEvent),NULL);
+ // Should new event be placed before first event in chain?
+ if (dbei->timestamp < dbeTest->timestamp) {
dbe.ofsPrev = (DWORD)hContact;
- dbe.ofsNext = 0;
+ dbe.ofsNext = dbc.ofsFirstEvent;
dbe.flags |= DBEF_FIRST;
- dbc.ofsFirstEvent = dbc.ofsLastEvent = ofsNew;
+ dbc.ofsFirstEvent = ofsNew;
+ dbeTest = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
+ dbeTest->flags &= ~DBEF_FIRST;
+ dbeTest->ofsPrev = ofsNew;
+ DBWrite(dbe.ofsNext,dbeTest,sizeof(DBEvent));
}
else {
- DBEvent *dbeTest = (DBEvent*)DBRead(dbc.ofsFirstEvent,sizeof(DBEvent),NULL);
- // Should new event be placed before first event in chain?
- if (dbei->timestamp < dbeTest->timestamp) {
- dbe.ofsPrev = (DWORD)hContact;
- dbe.ofsNext = dbc.ofsFirstEvent;
- dbe.flags |= DBEF_FIRST;
- dbc.ofsFirstEvent = ofsNew;
- dbeTest = (DBEvent*)DBRead(dbe.ofsNext,sizeof(DBEvent),NULL);
- dbeTest->flags &= ~DBEF_FIRST;
- dbeTest->ofsPrev = ofsNew;
- DBWrite(dbe.ofsNext,dbeTest,sizeof(DBEvent));
- }
- else {
- // Loop through the chain, starting at the end
- DWORD ofsThis = dbc.ofsLastEvent;
- dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
- for (;;) {
- // If the new event's timesstamp is equal to or greater than the
- // current dbevent, it will be inserted after. If not, continue
- // with the previous dbevent in chain.
- if (dbe.timestamp >= dbeTest->timestamp) {
- dbe.ofsPrev = ofsThis;
- dbe.ofsNext = dbeTest->ofsNext;
- dbeTest->ofsNext = ofsNew;
- DBWrite(ofsThis, dbeTest, sizeof(DBEvent));
- if (dbe.ofsNext == 0)
- dbc.ofsLastEvent = ofsNew;
- else {
- dbeTest = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
- dbeTest->ofsPrev = ofsNew;
- DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
- }
- break;
+ // Loop through the chain, starting at the end
+ DWORD ofsThis = dbc.ofsLastEvent;
+ dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
+ for (;;) {
+ // If the new event's timesstamp is equal to or greater than the
+ // current dbevent, it will be inserted after. If not, continue
+ // with the previous dbevent in chain.
+ if (dbe.timestamp >= dbeTest->timestamp) {
+ dbe.ofsPrev = ofsThis;
+ dbe.ofsNext = dbeTest->ofsNext;
+ dbeTest->ofsNext = ofsNew;
+ DBWrite(ofsThis, dbeTest, sizeof(DBEvent));
+ if (dbe.ofsNext == 0)
+ dbc.ofsLastEvent = ofsNew;
+ else {
+ dbeTest = (DBEvent*)DBRead(dbe.ofsNext, sizeof(DBEvent), NULL);
+ dbeTest->ofsPrev = ofsNew;
+ DBWrite(dbe.ofsNext, dbeTest, sizeof(DBEvent));
}
- ofsThis = dbeTest->ofsPrev;
- dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
+ break;
}
+ ofsThis = dbeTest->ofsPrev;
+ dbeTest = (DBEvent*)DBRead(ofsThis, sizeof(DBEvent), NULL);
}
}
- dbc.eventCount++;
+ }
+ dbc.eventCount++;
- if (!(dbe.flags&(DBEF_READ|DBEF_SENT))) {
- if (dbe.timestamp<dbc.timestampFirstUnread || dbc.timestampFirstUnread == 0) {
- dbc.timestampFirstUnread = dbe.timestamp;
- dbc.ofsFirstUnreadEvent = ofsNew;
- }
- neednotify = TRUE;
+ if (!(dbe.flags&(DBEF_READ|DBEF_SENT))) {
+ if (dbe.timestamp<dbc.timestampFirstUnread || dbc.timestampFirstUnread == 0) {
+ dbc.timestampFirstUnread = dbe.timestamp;
+ dbc.ofsFirstUnreadEvent = ofsNew;
}
- else neednotify = m_safetyMode;
-
- DBWrite(ofsContact,&dbc,sizeof(DBContact));
- DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob));
- DBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob);
- DBFlush(0);
+ neednotify = TRUE;
}
+ else neednotify = m_safetyMode;
+
+ DBWrite(ofsContact,&dbc,sizeof(DBContact));
+ DBWrite(ofsNew,&dbe,offsetof(DBEvent,blob));
+ DBWrite(ofsNew+offsetof(DBEvent,blob),dbei->pBlob,dbei->cbBlob);
+ DBFlush(0);
+ lck.unlock();
log1("add event @ %08x",ofsNew);
@@ -142,26 +140,25 @@ STDMETHODIMP_(HANDLE) CDdxMmap::AddEvent(HANDLE hContact, DBEVENTINFO *dbei)
STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
{
- DWORD ofsContact,ofsThis;
- {
- mir_cslock lck(m_csDbAccess);
-
- ofsContact = (hContact == 0) ? m_dbHeader.ofsUser : (DWORD)hContact;
- DBContact dbc = *(DBContact*)DBRead(ofsContact, sizeof(DBContact), NULL);
- DBEvent dbe = *(DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
- if (dbc.signature != DBCONTACT_SIGNATURE || dbe.signature != DBEVENT_SIGNATURE)
- return 1;
- }
+ mir_cslockfull lck(m_csDbAccess);
+
+ DWORD ofsContact = (hContact == 0) ? m_dbHeader.ofsUser : (DWORD)hContact;
+ DBContact dbc = *(DBContact*)DBRead(ofsContact, sizeof(DBContact), NULL);
+ DBEvent dbe = *(DBEvent*)DBRead(hDbEvent, sizeof(DBEvent), NULL);
+ if (dbc.signature != DBCONTACT_SIGNATURE || dbe.signature != DBEVENT_SIGNATURE)
+ return 1;
+ lck.unlock();
log1("delete event @ %08x",wParam);
//call notifier while outside mutex
NotifyEventHooks(hEventDeletedEvent,(WPARAM)hContact, (LPARAM)hDbEvent);
//get back in
- mir_cslock lck(m_csDbAccess);
- DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- DBEvent dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+ lck.lock();
+ dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
+ dbe = *(DBEvent*)DBRead(hDbEvent,sizeof(DBEvent),NULL);
+
//check if this was the first unread, if so, recalc the first unread
if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) {
DBEvent *dbeNext = &dbe;
@@ -171,7 +168,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteEvent(HANDLE hContact, HANDLE hDbEvent)
dbc.timestampFirstUnread = 0;
break;
}
- ofsThis = dbeNext->ofsNext;
+ DWORD ofsThis = dbeNext->ofsNext;
dbeNext = (DBEvent*)DBRead(ofsThis,sizeof(DBEvent),NULL);
if ( !(dbeNext->flags & (DBEF_READ | DBEF_SENT))) {
dbc.ofsFirstUnreadEvent = ofsThis;
@@ -259,7 +256,6 @@ STDMETHODIMP_(BOOL) CDdxMmap::GetEvent(HANDLE hDbEvent, DBEVENTINFO *dbei)
STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
{
- INT_PTR ret;
DBEvent *dbe;
DBContact dbc;
DWORD ofsThis;
@@ -272,15 +268,13 @@ STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
if (dbe->signature != DBEVENT_SIGNATURE || dbc.signature != DBCONTACT_SIGNATURE)
return -1;
- if (dbe->flags&DBEF_READ || dbe->flags&DBEF_SENT) {
- ret = (INT_PTR)dbe->flags;
- return ret;
- }
+ if ((dbe->flags & DBEF_READ) || (dbe->flags & DBEF_SENT))
+ return (INT_PTR)dbe->flags;
log1("mark read @ %08x",wParam);
dbe->flags |= DBEF_READ;
DBWrite((DWORD)hDbEvent,dbe,sizeof(DBEvent));
- ret = (int)dbe->flags;
+ BOOL ret = dbe->flags;
if (dbc.ofsFirstUnreadEvent == (DWORD)hDbEvent) {
for (;;) {
if (dbe->ofsNext == 0) {
@@ -297,7 +291,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::MarkEventRead(HANDLE hContact, HANDLE hDbEvent)
}
}
}
- DBWrite((DWORD)hContact,&dbc,sizeof(DBContact));
+ DBWrite((DWORD)hContact, &dbc, sizeof(DBContact));
DBFlush(0);
return ret;
}
diff --git a/plugins/Db3x_mmap/dbsettings.cpp b/plugins/Db3x_mmap/dbsettings.cpp
index 96df28a227..9423d7141c 100644
--- a/plugins/Db3x_mmap/dbsettings.cpp
+++ b/plugins/Db3x_mmap/dbsettings.cpp
@@ -228,7 +228,7 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
return 1;
}
- EnterCriticalSection(&m_csDbAccess);
+ mir_cslock lck(m_csDbAccess);
log3("get [%08p] %s/%s",hContact,dbcgs->szModule,dbcgs->szSetting);
@@ -269,7 +269,6 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
default: log1( "get cached crap: %d", dbcgs->pValue->type ); break;
}
- LeaveCriticalSection(&m_csDbAccess);
return ( pCachedValue->type == DBVT_DELETED ) ? 1 : 0;
} }
@@ -277,10 +276,9 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
if (hContact == NULL) ofsContact = m_dbHeader.ofsUser;
else ofsContact = (DWORD)hContact;
dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (dbc->signature != DBCONTACT_SIGNATURE)
return 1;
- }
+
ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
if (ofsSettingsGroup) {
ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob);
@@ -290,17 +288,15 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
if (pBlob[0] == settingNameLen && !memcmp(pBlob+1,dbcgs->szSetting,settingNameLen)) {
MoveAlong(1+settingNameLen);
NeedBytes(5);
- if (isStatic && pBlob[0]&DBVTF_VARIABLELENGTH && VLT(dbcgs->pValue->type) != VLT(pBlob[0])) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (isStatic && pBlob[0]&DBVTF_VARIABLELENGTH && VLT(dbcgs->pValue->type) != VLT(pBlob[0]))
return 1;
- }
+
dbcgs->pValue->type = pBlob[0];
switch(pBlob[0]) {
- case DBVT_DELETED: { /* this setting is deleted */
+ case DBVT_DELETED: /* this setting is deleted */
dbcgs->pValue->type = DBVT_DELETED;
- LeaveCriticalSection(&m_csDbAccess);
return 2;
- }
+
case DBVT_BYTE: dbcgs->pValue->bVal = pBlob[1]; break;
case DBVT_WORD: dbcgs->pValue->wVal = *(PWORD)(pBlob+1); break;
case DBVT_DWORD: dbcgs->pValue->dVal = *(PDWORD)(pBlob+1); break;
@@ -342,7 +338,6 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
SetCachedVariant(dbcgs->pValue,pCachedValue);
}
- LeaveCriticalSection(&m_csDbAccess);
logg();
return 0;
}
@@ -361,7 +356,6 @@ int CDdxMmap::GetContactSettingWorker(HANDLE hContact,DBCONTACTGETSETTING *dbcgs
pCachedValue->type = DBVT_DELETED;
}
- LeaveCriticalSection(&m_csDbAccess);
logg();
return 1;
}
@@ -486,12 +480,12 @@ STDMETHODIMP_(BOOL) CDdxMmap::FreeVariant(DBVARIANT *dbv)
STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *pszSettingName)
{
size_t cbSettingNameLen = strlen(pszSettingName) + 2;
- if (cbSettingNameLen < 512) {
+ if (cbSettingNameLen < 512) {
char* szSetting;
char szTemp[512];
strcpy( szTemp+1, pszSettingName);
- EnterCriticalSection(&m_csDbAccess);
+ mir_cslock lck(m_csDbAccess);
int idx = m_lSettings.getIndex(szTemp);
if (idx == -1)
szSetting = InsertCachedSetting( szTemp, cbSettingNameLen);
@@ -507,8 +501,6 @@ STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *p
}
else if (!bIsResident)
m_lResidentSettings.remove(idx);
-
- LeaveCriticalSection(&m_csDbAccess);
}
return 0;
}
@@ -516,7 +508,6 @@ STDMETHODIMP_(BOOL) CDdxMmap::SetSettingResident(BOOL bIsResident, const char *p
STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRITESETTING *dbcws)
{
DBCONTACTWRITESETTING tmp;
- DBContact dbc;
DWORD ofsModuleName;
DBContactSettings dbcs;
PBYTE pBlob;
@@ -581,46 +572,42 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT
}
}
- EnterCriticalSection(&m_csDbAccess);
- {
- char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, moduleNameLen, settingNameLen);
- if ( tmp.value.type != DBVT_BLOB ) {
- DBVARIANT* pCachedValue = GetCachedValuePtr(hContact, szCachedSettingName, 1);
- if ( pCachedValue != NULL ) {
- BOOL bIsIdentical = FALSE;
- if ( pCachedValue->type == tmp.value.type ) {
- switch(tmp.value.type) {
- case DBVT_BYTE: bIsIdentical = pCachedValue->bVal == tmp.value.bVal; break;
- case DBVT_WORD: bIsIdentical = pCachedValue->wVal == tmp.value.wVal; break;
- case DBVT_DWORD: bIsIdentical = pCachedValue->dVal == tmp.value.dVal; break;
- case DBVT_UTF8:
- case DBVT_ASCIIZ: bIsIdentical = strcmp( pCachedValue->pszVal, tmp.value.pszVal ) == 0; break;
- }
- if ( bIsIdentical ) {
- LeaveCriticalSection(&m_csDbAccess);
- return 0;
- }
+ mir_cslockfull lck(m_csDbAccess);
+
+ char* szCachedSettingName = GetCachedSetting(tmp.szModule, tmp.szSetting, moduleNameLen, settingNameLen);
+ if ( tmp.value.type != DBVT_BLOB ) {
+ DBVARIANT* pCachedValue = GetCachedValuePtr(hContact, szCachedSettingName, 1);
+ if ( pCachedValue != NULL ) {
+ BOOL bIsIdentical = FALSE;
+ if ( pCachedValue->type == tmp.value.type ) {
+ switch(tmp.value.type) {
+ case DBVT_BYTE: bIsIdentical = pCachedValue->bVal == tmp.value.bVal; break;
+ case DBVT_WORD: bIsIdentical = pCachedValue->wVal == tmp.value.wVal; break;
+ case DBVT_DWORD: bIsIdentical = pCachedValue->dVal == tmp.value.dVal; break;
+ case DBVT_UTF8:
+ case DBVT_ASCIIZ: bIsIdentical = strcmp( pCachedValue->pszVal, tmp.value.pszVal ) == 0; break;
}
- SetCachedVariant(&tmp.value, pCachedValue);
- }
- if ( szCachedSettingName[-1] != 0 ) {
- LeaveCriticalSection(&m_csDbAccess);
- NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
- return 0;
+ if ( bIsIdentical )
+ return 0;
}
+ SetCachedVariant(&tmp.value, pCachedValue);
+ }
+ if ( szCachedSettingName[-1] != 0 ) {
+ lck.unlock();
+ NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
+ return 0;
}
- else GetCachedValuePtr(hContact, szCachedSettingName, -1);
}
+ else GetCachedValuePtr(hContact, szCachedSettingName, -1);
ofsModuleName = GetModuleNameOfs(tmp.szModule);
if (hContact == 0) ofsContact = m_dbHeader.ofsUser;
else ofsContact = (DWORD)hContact;
- dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc.signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&m_csDbAccess);
+ DBContact dbc = *(DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
+ if (dbc.signature != DBCONTACT_SIGNATURE)
return 1;
- }
+
log0("write setting");
//make sure the module group exists
ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(&dbc,ofsModuleName);
@@ -696,7 +683,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT
}
//quit
DBFlush(1);
- LeaveCriticalSection(&m_csDbAccess);
+ lck.unlock();
//notify
NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp);
return 0;
@@ -713,7 +700,7 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT
else bytesRequired = tmp.value.type;
bytesRequired += 2+settingNameLen;
bytesRequired += ofsBlobPtr+1-(ofsSettingsGroup+offsetof(DBContactSettings,blob));
- if ((DWORD)bytesRequired>dbcs.cbBlob) {
+ if ((DWORD)bytesRequired > dbcs.cbBlob) {
//doesn't fit: move entire group
DBContactSettings *dbcsPrev;
DWORD ofsDbcsPrev,ofsNew;
@@ -774,12 +761,14 @@ STDMETHODIMP_(BOOL) CDdxMmap::WriteContactSetting(HANDLE hContact, DBCONTACTWRIT
MoveAlong(2+tmp.value.cpbVal);
break;
}
- { BYTE zero = 0;
- DBWrite(ofsBlobPtr,&zero,1);
- }
+
+ BYTE zero = 0;
+ DBWrite(ofsBlobPtr,&zero,1);
+
//quit
DBFlush(1);
- LeaveCriticalSection(&m_csDbAccess);
+ lck.unlock();
+
//notify
NotifyEventHooks(hSettingChangeEvent, (WPARAM)hContact, (LPARAM)&tmp );
return 0;
@@ -794,42 +783,39 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET
char* szCachedSettingName;
WPARAM saveWparam = (WPARAM)hContact;
- if ((!dbcgs->szModule) || (!dbcgs->szSetting))
+ if ( !dbcgs->szModule || !dbcgs->szSetting)
return 1;
+
// the db format can't tolerate more than 255 bytes of space (incl. null) for settings+module name
settingNameLen = (int)strlen(dbcgs->szSetting);
moduleNameLen = (int)strlen(dbcgs->szModule);
- if ( settingNameLen > 0xFE )
- {
+ if ( settingNameLen > 0xFE ) {
#ifdef _DEBUG
OutputDebugStringA("DeleteContactSetting() got a > 255 setting name length. \n");
#endif
return 1;
}
- if ( moduleNameLen > 0xFE )
- {
+ if ( moduleNameLen > 0xFE ) {
#ifdef _DEBUG
OutputDebugStringA("DeleteContactSetting() got a > 255 module name length. \n");
#endif
return 1;
}
- EnterCriticalSection(&m_csDbAccess);
+ mir_cslockfull lck(m_csDbAccess);
ofsModuleName = GetModuleNameOfs(dbcgs->szModule);
if (hContact == 0)
hContact = (HANDLE)m_dbHeader.ofsUser;
dbc = (DBContact*)DBRead(hContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (dbc->signature != DBCONTACT_SIGNATURE)
return 1;
- }
+
//make sure the module group exists
ofsSettingsGroup = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
- if (ofsSettingsGroup == 0) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (ofsSettingsGroup == 0)
return 1;
- }
+
//find if the setting exists
ofsBlobPtr = ofsSettingsGroup+offsetof(DBContactSettings,blob);
pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
@@ -843,42 +829,40 @@ STDMETHODIMP_(BOOL) CDdxMmap::DeleteContactSetting(HANDLE hContact, DBCONTACTGET
MoveAlong(1+GetSettingValueLength(pBlob));
NeedBytes(1);
}
- if (!pBlob[0]) { //setting didn't exist
- LeaveCriticalSection(&m_csDbAccess);
+ if (!pBlob[0]) //setting didn't exist
return 1;
- }
- { //bin it
- int nameLen,valLen;
- DWORD ofsSettingToCut;
- MoveAlong(1+settingNameLen);
+
+ //bin it
+ int nameLen,valLen;
+ DWORD ofsSettingToCut;
+ MoveAlong(1+settingNameLen);
+ NeedBytes(3);
+ nameLen = 1+settingNameLen;
+ valLen = 1+GetSettingValueLength(pBlob);
+ ofsSettingToCut = ofsBlobPtr-nameLen;
+ MoveAlong(valLen);
+ NeedBytes(1);
+ while (pBlob[0]) {
+ MoveAlong(pBlob[0]+1);
NeedBytes(3);
- nameLen = 1+settingNameLen;
- valLen = 1+GetSettingValueLength(pBlob);
- ofsSettingToCut = ofsBlobPtr-nameLen;
- MoveAlong(valLen);
+ MoveAlong(1+GetSettingValueLength(pBlob));
NeedBytes(1);
- while (pBlob[0]) {
- MoveAlong(pBlob[0]+1);
- NeedBytes(3);
- MoveAlong(1+GetSettingValueLength(pBlob));
- NeedBytes(1);
- }
- DBMoveChunk(ofsSettingToCut,ofsSettingToCut+nameLen+valLen,ofsBlobPtr+1-ofsSettingToCut);
}
+ DBMoveChunk(ofsSettingToCut,ofsSettingToCut+nameLen+valLen,ofsBlobPtr+1-ofsSettingToCut);
szCachedSettingName = GetCachedSetting(dbcgs->szModule,dbcgs->szSetting,moduleNameLen,settingNameLen);
GetCachedValuePtr((HANDLE)saveWparam, szCachedSettingName, -1 );
//quit
DBFlush(1);
- LeaveCriticalSection(&m_csDbAccess);
- { //notify
- DBCONTACTWRITESETTING dbcws = {0};
- dbcws.szModule = dbcgs->szModule;
- dbcws.szSetting = dbcgs->szSetting;
- dbcws.value.type = DBVT_DELETED;
- NotifyEventHooks(hSettingChangeEvent,saveWparam,(LPARAM)&dbcws);
- }
+ lck.unlock();
+
+ //notify
+ DBCONTACTWRITESETTING dbcws = {0};
+ dbcws.szModule = dbcgs->szModule;
+ dbcws.szSetting = dbcgs->szSetting;
+ dbcws.value.type = DBVT_DELETED;
+ NotifyEventHooks(hSettingChangeEvent,saveWparam,(LPARAM)&dbcws);
return 0;
}
@@ -893,27 +877,24 @@ STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUM
if (!dbces->szModule)
return -1;
- EnterCriticalSection(&m_csDbAccess);
+ mir_cslock lck(m_csDbAccess);
ofsModuleName = GetModuleNameOfs(dbces->szModule);
if (hContact == 0) ofsContact = m_dbHeader.ofsUser;
else ofsContact = (DWORD)hContact;
dbc = (DBContact*)DBRead(ofsContact,sizeof(DBContact),NULL);
- if (dbc->signature != DBCONTACT_SIGNATURE) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (dbc->signature != DBCONTACT_SIGNATURE)
return -1;
- }
+
dbces->ofsSettings = GetSettingsGroupOfsByModuleNameOfs(dbc,ofsModuleName);
- if (!dbces->ofsSettings) {
- LeaveCriticalSection(&m_csDbAccess);
+ if ( !dbces->ofsSettings)
return -1;
- }
+
ofsBlobPtr = dbces->ofsSettings+offsetof(DBContactSettings,blob);
pBlob = (PBYTE)DBRead(ofsBlobPtr,1,&bytesRemaining);
- if (pBlob[0] == 0) {
- LeaveCriticalSection(&m_csDbAccess);
+ if (pBlob[0] == 0)
return -1;
- }
+
result = 0;
while (pBlob[0]) {
NeedBytes(1);
@@ -925,7 +906,6 @@ STDMETHODIMP_(BOOL) CDdxMmap::EnumContactSettings(HANDLE hContact, DBCONTACTENUM
MoveAlong(1+GetSettingValueLength(pBlob));
NeedBytes(1);
}
- LeaveCriticalSection(&m_csDbAccess);
return result;
}