Commit 31b7d755e08b5e18926cb0dceb44ecd5327753f0

Authored by Georg Hopp
1 parent 5c3928d4

another try with the shmen trick...this time use MAP_ANONYMOUS ... as GNU extension.

Showing 2 changed files with 17 additions and 23 deletions
... ... @@ -23,7 +23,6 @@ CLASS(Cbuf) {
23 23 char * shm_name; // shared memory identifier
24 24
25 25 char * data;
26   - char * mirror;
27 26
28 27 size_t bsize;
29 28 size_t bused;
... ...
1 1 #define _POSIX_SOURCE
2 2 #define _POSIX_C_SOURCE 200112L
  3 +#define _GNU_SOURCE
3 4
4 5 #include <sys/types.h>
5 6 #include <sys/stat.h>
... ... @@ -28,6 +29,7 @@ ctor(void * _this, va_list * params)
28 29 long psize = sysconf(_SC_PAGESIZE);
29 30 size_t size;
30 31 int shm;
  32 + char * data;
31 33
32 34 this->shm_name = malloc(strlen(shm_name) + 7 + 2);
33 35 sprintf(this->shm_name, "/%06d_%s", getpid(), shm_name);
... ... @@ -51,25 +53,22 @@ ctor(void * _this, va_list * params)
51 53 }
52 54
53 55 this->data = mmap (0, this->bsize << 1,
54   - PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0);
  56 + PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
55 57 if (this->data == MAP_FAILED) {
56 58 this->data = NULL;
57 59 break;
58 60 }
59 61
60   - munmap(this->data + this->bsize, this->bsize);
61   -
62   - this->mirror = mmap (this->data + this->bsize, this->bsize,
63   - PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0);
64   - if (this->mirror != this->data + this->bsize) {
65   - if (this->mirror == this->data - this->bsize) {
66   - this->data = this->mirror;
67   - this->mirror += this->bsize;
68   - }
69   - else {
70   - this->mirror = NULL;
71   - break;
72   - }
  62 + data = mmap (this->data, this->bsize,
  63 + PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0);
  64 + if (data != this->data) {
  65 + break;
  66 + }
  67 +
  68 + data = mmap (this->data + this->bsize, this->bsize,
  69 + PROT_READ|PROT_WRITE, MAP_FIXED|MAP_SHARED, shm, 0);
  70 + if (data != this->data + this->bsize) {
  71 + break;
73 72 }
74 73
75 74 state = 1;
... ... @@ -93,18 +92,14 @@ dtor(void * _this)
93 92
94 93 if (NULL != this->shm_name) {
95 94 free(this->shm_name);
96   - this->shm_name = NULL;
97 95 }
98 96
99   - if (NULL != this->data) {
100   - munmap(this->data, this->bsize);
101   - this->data = NULL;
  97 + if (NULL != this->data && MAP_FAILED != this->data) {
  98 + munmap(this->data, this->bsize << 1);
102 99 }
103 100
104   - if (NULL != this->mirror) {
105   - munmap(this->mirror, this->bsize);
106   - this->mirror = NULL;
107   - }
  101 + this->shm_name = NULL;
  102 + this->data = NULL;
108 103 }
109 104
110 105 INIT_IFACE(Class, ctor, dtor, NULL);
... ...
Please register or login to post a comment