cbuf.c
2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright (C) 2012 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 200112L
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "class.h"
#include "interface/class.h"
#include "cbuf.h"
static void dtor(void*);
static
int
ctor(void * _this, va_list * params)
{
Cbuf this = _this;
char state = 0;
char * shm_name = va_arg(*params, char*);
long psize = sysconf(_SC_PAGESIZE);
size_t size;
int shm;
char * data;
this->shm_name = malloc(strlen(shm_name) + 7 + 2);
sprintf(this->shm_name, "/%06d_%s", getpid(), shm_name);
/**
* align size at page boundary.
* increase as neccessary
*/
size = va_arg(*params, size_t);
size = (0 >= size)? 1 : (0 != size%psize)? (size/psize)+1 : size/psize;
this->bsize = psize * size;
while (0 == state) {
shm = shm_open(this->shm_name, O_RDWR|O_CREAT|O_EXCL, S_IRWXU);
if (-1 == shm) {
break;
}
if (-1 == ftruncate(shm, this->bsize)) {
break;
}
this->data = mmap (0, this->bsize << 1,
PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
if (this->data == MAP_FAILED) {
this->data = NULL;
break;
}
data = mmap (this->data, this->bsize,
PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0);
if (data != this->data) {
break;
}
data = mmap (this->data + this->bsize, this->bsize,
PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0);
if (data != this->data + this->bsize) {
break;
}
state = 1;
}
if (-1 != shm) {
shm_unlink(this->shm_name);
close(shm);
}
if (1 != state) {
dtor(this);
}
return 0;
}
static
void
dtor(void * _this)
{
Cbuf this = _this;
if (NULL != this->shm_name) {
free(this->shm_name);
}
if (NULL != this->data && MAP_FAILED != this->data) {
munmap(this->data, this->bsize << 1);
}
this->shm_name = NULL;
this->data = NULL;
}
INIT_IFACE(Class, ctor, dtor, NULL);
CREATE_CLASS(Cbuf, NULL, IFACE(Class));
// vim: set ts=4 sw=4: