1 Star 0 Fork 26

winting/php-cp

forked from 郭新华/php-cp 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cpMemory.c 2.98 KB
一键复制 编辑 原始数据 按行查看 历史
/*
+----------------------------------------------------------------------+
| common con pool |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Xinhua Guo <woshiguo35@sina.com>|
+----------------------------------------------------------------------+
*/
#include "php_connect_pool.h"
void *cp_mmap_calloc(int size)
{
void *mem;
int tmpfd = -1;
int flag = MAP_SHARED;
#ifdef MAP_ANONYMOUS
flag |= MAP_ANONYMOUS;
#else
char *mapfile = NULL;
if (mapfile == NULL)
{
mapfile = "/dev/zero";
}
if ((tmpfd = open(mapfile, O_RDWR)) < 0)
{
return NULL;
}
strncpy(object->mapfile, mapfile, SW_SHM_MMAP_FILE_LEN);
object->tmpfd = tmpfd;
#endif
mem = mmap(NULL, size, PROT_READ | PROT_WRITE, flag, tmpfd, 0);
#ifdef MAP_FAILED
if (mem == MAP_FAILED)
#else
if (!mem)
#endif
{
cpLog("mmap fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
else
{
bzero(mem, size);
return mem;
}
}
int cp_create_mmap_dir()
{
umask(0);
if (access(CP_FILE_DIR, F_OK) == -1)
{
int ret = mkdir(CP_FILE_DIR, 0777);
if (ret == -1)
{
php_printf("mkdir fail. Error: %s[%d]", strerror(errno), errno);
return -1;
}
}
return 0;
};
int cp_create_mmap_file(cpShareMemory *object)
{
umask(0);
char tmp[100] = {0};
strncpy(tmp, CP_SERVER_MMAP_FILE, strlen(CP_SERVER_MMAP_FILE));
int fd = open(object->mmap_name, O_RDWR | O_CREAT, 0777);
if (fd == -1)
{
php_printf("open fail. Error: %s[%d],%s", strerror(errno), errno, object->mmap_name);
return -1;
}
ftruncate(fd, object->size); //extend 黑洞
close(fd);
return 0;
};
void* cp_mmap_calloc_with_file(cpShareMemory *object)
{
int fd = open(object->mmap_name, O_RDWR);
if (fd == -1)
{
cpLog("open fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
void *mem = mmap(NULL, object->size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
#ifdef MAP_FAILED
if (mem == MAP_FAILED)
#else
if (!mem)
#endif
{
cpLog("mmap fail. Error: %s[%d]", strerror(errno), errno);
return NULL;
}
else
{
// bzero(mem, object->size);
object->mem = mem;
return mem;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/winting/php-cp.git
git@gitee.com:winting/php-cp.git
winting
php-cp
php-cp
master

搜索帮助