taskrambler  0.1.9
Web server and task management solution.
cbuf.c File Reference
#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 "utils/memory.h"
#include "cbuf.h"
+ Include dependency graph for cbuf.c:

Go to the source code of this file.

Macros

#define _POSIX_SOURCE
 
#define _POSIX_C_SOURCE   200112L
 
#define _GNU_SOURCE
 

Functions

static void cbufDtor (void *)
 
static int cbufCtor (void *_this, va_list *params)
 
 INIT_IFACE (Class, cbufCtor, cbufDtor, NULL)
 
 CREATE_CLASS (Cbuf, NULL, IFACE(Class))
 

Detailed Description

Author
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/.

Definition in file cbuf.c.

Macro Definition Documentation

#define _GNU_SOURCE

Definition at line 25 of file cbuf.c.

#define _POSIX_C_SOURCE   200112L

Definition at line 24 of file cbuf.c.

#define _POSIX_SOURCE

Definition at line 23 of file cbuf.c.

Function Documentation

static int cbufCtor ( void *  _this,
va_list *  params 
)
static

align size at page boundary. increase as neccessary

Definition at line 46 of file cbuf.c.

References element::data, memMalloc(), and size.

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 }
void * memMalloc(size_t)
Definition: memory.c:783
static size_t size
int data
Definition: binarytree.c:7

+ Here is the call graph for this function:

static void cbufDtor ( void *  _this)
static

Definition at line 109 of file cbuf.c.

References element::data, and MEM_FREE.

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 }
#define MEM_FREE(seg)
Definition: memory.h:28
int data
Definition: binarytree.c:7
CREATE_CLASS ( Cbuf  ,
NULL  ,
IFACE(Class)   
)
INIT_IFACE ( Class  ,
cbufCtor  ,
cbufDtor  ,
NULL   
)