Tokyo Cabinet的源码看到的

1
2
3
4
5
6
7
/* Get the global memory pool object. */
TCMPOOL *tcmpoolglobal(void){
    if(tcglobalmemorypool) return tcglobalmemorypool;//如果有全局内存池对象就返回对象
    tcglobalmemorypool = tcmpoolnew();//如果没有就创建啊^_^
    atexit(tcmpooldelglobal);
    return tcglobalmemorypool;
}

而这个tcglobalmemorypool,其实是写在全局的,有

1
2
/* Global memory pool object. */
TCMPOOL *tcglobalmemorypool = NULL;

后记:其实只要把tcglobalmemorypool定义为全局变量,是个人都会创建这种模式的吧…

发表评论