📄 secdb.c
字号:
#include"secdb.h"
#include"stdio.h"
#include"SITable.h"
#include"string.h"
#include"PrMem.h"
SECTIONDB*CreateSectionDB(SectionCompareProc fun,PRMutex lock)
{
SECTIONDB*db=(SECTIONDB*)PrMalloc(sizeof(SECTIONDB));
db->CmpFun=fun;
db->sections=NULL;
db->Count=0;
db->version=0;
db->PrivateLock=(lock==NULL);
if(db->PrivateLock)
lock=PrMutexCreate();
db->lock=lock;
return db;
}
static UINT dbsize=0;
static UINT alloccnt=0,freecnt=0;
void FreeDBSections(SECTIONDB*secDB)
{
if(secDB){
SECTIONDATA*sd;
PrMutexLock(secDB->lock,-1);
sd=secDB->sections;
while(sd){
SECTIONDATA*tmp=sd->next;
dbsize-=GetSectionLength(sd->Data)+3;
PrFree(sd->Data);
freecnt++;
PrFree(sd);
sd=tmp;
}
secDB->sections=NULL;
secDB->Count=0;
secDB->version=0;
PrMutexUnlock(secDB->lock);
}
}
void DestroySectionDB(SECTIONDB*secDB)
{
if(secDB){
FreeDBSections(secDB);
if(secDB->PrivateLock)
PrMutexDestroy(secDB->lock);
PrFree(secDB);
}
}
SECTIONDATA*FindSection(SECTIONDB*secDB,UCHAR*section)
{
if(secDB){
SECTIONDATA*sd=secDB->sections;
while(sd){
if(secDB->CmpFun(sd->Data,section)==0){
return sd;
}
sd=sd->next;
}
}
return NULL;
}
void DumpSectionDB(SECTIONDB*sDB)
{
int cc=0;
SECTIONDATA*sd;
if(sDB==NULL)return;
sd=sDB->sections;
PrDbgPrintf("SectionDB %p section count=%d\n",sDB,sDB->Count);
while(sd){
cc++;
sd=sd->next;
}
PrDbgPrintf("SectionDB %p COUNT=%d\n",sDB,cc);
}
static int SortDB(SECTIONDB*db)
{
SECTIONDATA*s1,*s2;
int count=0;
s1=db->sections;
while(s1->next){
s2=s1->next;
while(s2){
if(db->CmpFun(s1->Data,s2->Data)>0){
UCHAR*dt=s1->Data;
s1->Data=s2->Data;
s2->Data=dt;
count++;
}
s2=s2->next;
}
s1=s1->next;
}
return count;
}
SECTIONDATA*AddSection(SECTIONDB*secDB,UCHAR*section)
{
SECTIONDATA *node=NULL;
int slen=GetSectionLength(section)+3;
int needsort=0;
#if 0
SECTIONDATA*p;
if(secDB->sections==NULL){
node=(SECTIONDATA*)PrMalloc(sizeof(SECTIONDATA));
node->next=NULL; node->prev=NULL;
secDB->sections=node; secDB->Count++;
}else{
p=secDB->sections;
while(p){
int rc=secDB->CmpFun(p->Data,section);
if(rc==0){
node=p;
PrFree(node->Data);
break;
}else if(rc>0){
node=(SECTIONDATA*)PrMalloc(sizeof(SECTIONDATA));
if(p==secDB->sections){
node->next=secDB->sections; node->prev=NULL;
secDB->sections->prev=node; secDB->sections=node;
}else{
node->next=p; p->prev->next=node;
node->prev=p->prev; p->prev=node;
}
secDB->Count++;
break;
}else {//rc<0
if(p->next==NULL){
node=(SECTIONDATA*)PrMalloc(sizeof(SECTIONDATA));
node->next=NULL; node->prev=p;
p->next=node; secDB->Count++;
break;
}
p=p->next;
}
}
}
#else
{
static UINT dbcc=0;
if(dbcc++%600==0)
PrDbgPrintf("Section DB Used %d bytes alloc %d / free %d times\r\n",
dbsize,alloccnt,freecnt);
}
PrMutexLock(secDB->lock,-1);
node=FindSection(secDB,section);
if(node==NULL){
node=(SECTIONDATA*)PrMalloc(sizeof(SECTIONDATA));
node->prev=NULL;
node->Data=NULL;
node->next=secDB->sections;
if(secDB->sections){
secDB->sections->prev=node;
secDB->sections=node;
}else
secDB->sections=node;
secDB->Count++;
needsort=1;
}else{
if( (GetSectionVersion(node->Data)==GetSectionVersion(section) )
||(GetSectionCRC(node->Data)==GetSectionCRC(section) ) ){
PrMutexUnlock(secDB->lock);
return node;
}
if(GetSectionLength(node->Data)+3<slen){
dbsize-=GetSectionLength(node->Data)+3;
PrFree(node->Data);
freecnt++;
node->Data=NULL;
}
}
secDB->version++;
#endif
if(node->Data==NULL)
node->Data=(UCHAR*)PrMalloc(slen);
dbsize+=slen;
alloccnt++;
memcpy(node->Data,section,slen);
if(needsort)SortDB(secDB);
PrMutexUnlock(secDB->lock);
return node;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -