From 31b7d755e08b5e18926cb0dceb44ecd5327753f0 Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Sun, 19 Feb 2012 15:41:48 +0100 Subject: [PATCH] another try with the shmen trick...this time use MAP_ANONYMOUS ... as GNU extension. --- include/cbuf.h | 1 - src/cbuf.c | 39 +++++++++++++++++---------------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/include/cbuf.h b/include/cbuf.h index 63e2117..4a08154 100644 --- a/include/cbuf.h +++ b/include/cbuf.h @@ -23,7 +23,6 @@ CLASS(Cbuf) { char * shm_name; // shared memory identifier char * data; - char * mirror; size_t bsize; size_t bused; diff --git a/src/cbuf.c b/src/cbuf.c index 6c86e37..80be866 100644 --- a/src/cbuf.c +++ b/src/cbuf.c @@ -1,5 +1,6 @@ #define _POSIX_SOURCE #define _POSIX_C_SOURCE 200112L +#define _GNU_SOURCE #include #include @@ -28,6 +29,7 @@ ctor(void * _this, va_list * params) 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); @@ -51,25 +53,22 @@ ctor(void * _this, va_list * params) } this->data = mmap (0, this->bsize << 1, - PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0); + PROT_NONE, MAP_ANONYMOUS|MAP_PRIVATE, -1, 0); if (this->data == MAP_FAILED) { this->data = NULL; break; } - munmap(this->data + this->bsize, this->bsize); - - this->mirror = mmap (this->data + this->bsize, this->bsize, - PROT_READ|PROT_WRITE, MAP_SHARED, shm, 0); - if (this->mirror != this->data + this->bsize) { - if (this->mirror == this->data - this->bsize) { - this->data = this->mirror; - this->mirror += this->bsize; - } - else { - this->mirror = 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; @@ -93,18 +92,14 @@ dtor(void * _this) if (NULL != this->shm_name) { free(this->shm_name); - this->shm_name = NULL; } - if (NULL != this->data) { - munmap(this->data, this->bsize); - this->data = NULL; + if (NULL != this->data && MAP_FAILED != this->data) { + munmap(this->data, this->bsize << 1); } - if (NULL != this->mirror) { - munmap(this->mirror, this->bsize); - this->mirror = NULL; - } + this->shm_name = NULL; + this->data = NULL; } INIT_IFACE(Class, ctor, dtor, NULL);