1 Star 3 Fork 2

liexusong / unid

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
shm.c 1001 Bytes
一键复制 编辑 原始数据 按行查看 历史
liexusong 提交于 2016-07-16 00:00 . first commit
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include "shm.h"
#ifdef MAP_ANON
int
shm_alloc(struct shm *shm)
{
shm->addr = (void *) mmap(NULL, shm->size,
PROT_READ|PROT_WRITE,
MAP_ANON|MAP_SHARED, -1, 0);
if (shm->addr == NULL) {
return -1;
}
return 0;
}
void
shm_free(struct shm *shm)
{
if (shm->addr) {
munmap((void *) shm->addr, shm->size);
}
}
#else
int
shm_alloc(struct shm *shm)
{
ngx_fd_t fd;
fd = open("/dev/zero", O_RDWR);
if (fd == -1) {
return -1;
}
shm->addr = (void *) mmap(NULL, shm->size,
PROT_READ|PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
if (shm->addr == NULL) {
return -1;
}
return 0;
}
void
shm_free(struct shm *shm)
{
if (shm->addr) {
munmap((void *) shm->addr, shm->size);
}
}
#endif
C
1
https://gitee.com/liexusong/unid.git
git@gitee.com:liexusong/unid.git
liexusong
unid
unid
master

搜索帮助