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