⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shm.c

📁 一种用于编译时头文件的缓存处理缓存源代码,使用起来就像C/C++编译器的缓存预处理器,简化该功能相应的环境改变时对内核的影响。
💻 C
字号:
/********************************************************************
	created:	2008/01/24
	filename: 	shm.c
	author:		Lichuang
                
	purpose:    
*********************************************************************/

#include "shm.h"
#include "node.h"
#include "hash.h"
#include "lock.h"
#include "operator.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>

static int countsize(int nodenum, int nodesize, int hashitemnum);

ccache_t* create_shm(int nodenum, int datasize, int hashitemnum, int keysize, const char* mapfilename, int init)
{
    int nodesize = sizeof(struct node_t) + sizeof(char) * (keysize + datasize);
    int mapfilesize = countsize(nodenum, nodesize, hashitemnum);
    int fd;

    fd = open(mapfilename, O_RDWR);
    if (0 > fd)
    {
        return NULL; 
    }

    struct stat st;
    if (0 != fstat(fd, &st))
    {
        return NULL;
    }

    if (st.st_size != mapfilesize)
    {
        if (0 != unlink(mapfilename))
        {
            return NULL;
        }
        close(fd);

        fd = open(mapfilename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
        if (0 > fd)
        {
            return NULL;
        }

        init = 1;
    }        

    if (init)
    {
        if (0 > lseek(fd, mapfilesize - 1, SEEK_SET))
        {
            return NULL;
        }

        if (0 > write(fd, " ", 1))
        {
            return NULL;
        }
    }

    ccache_t* cache = mmap(0, mapfilesize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (MAP_FAILED == cache)
    {
        close(fd);
        return NULL;
    }

    close(fd);

    if (init)
    {
        cache->mapfilesize = mapfilesize;
        cache->hashitemnum = hashitemnum;
        cache->nodenum = nodenum;
        cache->usednodenum = 0;
        cache->firstfreenode = 0;
        cache->lrufirst = cache->lrulast = -1;

        cache->nodesize = nodesize;
        cache->keysize = keysize;
        cache->datasize = datasize;
        cache->hashitemtolsize = hashitemnum * sizeof(struct hashitem_t);

        if (0 > initthreadmutex(&(cache->mutex)))
        {
            return NULL;
        }
        if (0 > inithashitem(cache))
        {
            return NULL;
        }

        INIT_NODES(cache);
    }

    return cache;        
}

int destroy_shm(ccache_t* cache)
{
    return munmap((void*)cache, cache->mapfilesize);
}

int countsize(int nodenum, int nodesize, int hashitemnum)
{
    return (sizeof(struct ccache_t)
            + nodenum * nodesize
            + hashitemnum * sizeof(struct hashitem_t));
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -