diff options
Diffstat (limited to 'gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc')
-rw-r--r-- | gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc | 165 |
1 files changed, 79 insertions, 86 deletions
diff --git a/gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc b/gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc index c3681280d6..7f1ccd2d7b 100644 --- a/gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc +++ b/gnuradio-runtime/lib/vmcircbuf_sysv_shm.cc @@ -44,9 +44,8 @@ namespace gr { - vmcircbuf_sysv_shm::vmcircbuf_sysv_shm(int size) - : gr::vmcircbuf(size) - { +vmcircbuf_sysv_shm::vmcircbuf_sysv_shm(int size) : gr::vmcircbuf(size) +{ #if !defined(HAVE_SYS_SHM_H) fprintf(stderr, "gr::vmcircbuf_sysv_shm: sysv shared memory is not available\n"); throw std::runtime_error("gr::vmcircbuf_sysv_shm"); @@ -56,46 +55,48 @@ namespace gr { int pagesize = gr::pagesize(); - if(size <= 0 || (size % pagesize) != 0) { - fprintf(stderr, "gr::vmcircbuf_sysv_shm: invalid size = %d\n", size); - throw std::runtime_error("gr::vmcircbuf_sysv_shm"); + if (size <= 0 || (size % pagesize) != 0) { + fprintf(stderr, "gr::vmcircbuf_sysv_shm: invalid size = %d\n", size); + throw std::runtime_error("gr::vmcircbuf_sysv_shm"); } // Attempt to allocate buffers (handle bad_alloc errors) int attempts_remain(MAX_SYSV_SHM_ATTEMPTS); - while(attempts_remain-- > 0){ + while (attempts_remain-- > 0) { int shmid_guard = -1; int shmid1 = -1; int shmid2 = -1; - // We use this as a guard page. We'll map it read-only on both ends of the buffer. - // Ideally we'd map it no access, but I don't think that's possible with SysV - if((shmid_guard = shmget(IPC_PRIVATE, pagesize, IPC_CREAT | 0400)) == -1) { - perror("gr::vmcircbuf_sysv_shm: shmget (0)"); - continue; + // We use this as a guard page. We'll map it read-only on both ends of the + // buffer. Ideally we'd map it no access, but I don't think that's possible with + // SysV + if ((shmid_guard = shmget(IPC_PRIVATE, pagesize, IPC_CREAT | 0400)) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmget (0)"); + continue; } - if((shmid2 = shmget(IPC_PRIVATE, 2 * size + 2 * pagesize, IPC_CREAT | 0700)) == -1) { - perror("gr::vmcircbuf_sysv_shm: shmget (1)"); - shmctl(shmid_guard, IPC_RMID, 0); - continue; + if ((shmid2 = shmget(IPC_PRIVATE, 2 * size + 2 * pagesize, IPC_CREAT | 0700)) == + -1) { + perror("gr::vmcircbuf_sysv_shm: shmget (1)"); + shmctl(shmid_guard, IPC_RMID, 0); + continue; } - if((shmid1 = shmget(IPC_PRIVATE, size, IPC_CREAT | 0700)) == -1) { - perror("gr::vmcircbuf_sysv_shm: shmget (2)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid2, IPC_RMID, 0); - continue; + if ((shmid1 = shmget(IPC_PRIVATE, size, IPC_CREAT | 0700)) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmget (2)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid2, IPC_RMID, 0); + continue; } - void *first_copy = shmat (shmid2, 0, 0); - if(first_copy == (void *) -1) { - perror("gr::vmcircbuf_sysv_shm: shmat (1)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid2, IPC_RMID, 0); - shmctl(shmid1, IPC_RMID, 0); - continue; + void* first_copy = shmat(shmid2, 0, 0); + if (first_copy == (void*)-1) { + perror("gr::vmcircbuf_sysv_shm: shmat (1)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid2, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + continue; } shmctl(shmid2, IPC_RMID, 0); @@ -110,40 +111,41 @@ namespace gr { shmdt(first_copy); // first read-only guard page - if(shmat(shmid_guard, first_copy, SHM_RDONLY) == (void *) -1) { - perror("gr::vmcircbuf_sysv_shm: shmat (2)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid1, IPC_RMID, 0); - continue; + if (shmat(shmid_guard, first_copy, SHM_RDONLY) == (void*)-1) { + perror("gr::vmcircbuf_sysv_shm: shmat (2)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + continue; } // first copy - if(shmat (shmid1, (char*)first_copy + pagesize, 0) == (void *) -1) { - perror("gr::vmcircbuf_sysv_shm: shmat (3)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid1, IPC_RMID, 0); - shmdt(first_copy); - continue; + if (shmat(shmid1, (char*)first_copy + pagesize, 0) == (void*)-1) { + perror("gr::vmcircbuf_sysv_shm: shmat (3)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt(first_copy); + continue; } // second copy - if(shmat (shmid1, (char*)first_copy + pagesize + size, 0) == (void *) -1) { - perror("gr::vmcircbuf_sysv_shm: shmat (4)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid1, IPC_RMID, 0); - shmdt((char *)first_copy + pagesize); - continue; + if (shmat(shmid1, (char*)first_copy + pagesize + size, 0) == (void*)-1) { + perror("gr::vmcircbuf_sysv_shm: shmat (4)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt((char*)first_copy + pagesize); + continue; } // second read-only guard page - if(shmat(shmid_guard, (char*)first_copy + pagesize + 2 * size, SHM_RDONLY) == (void *) -1) { - perror("gr::vmcircbuf_sysv_shm: shmat (5)"); - shmctl(shmid_guard, IPC_RMID, 0); - shmctl(shmid1, IPC_RMID, 0); - shmdt(first_copy); - shmdt((char *)first_copy + pagesize); - shmdt((char *)first_copy + pagesize + size); - continue; + if (shmat(shmid_guard, (char*)first_copy + pagesize + 2 * size, SHM_RDONLY) == + (void*)-1) { + perror("gr::vmcircbuf_sysv_shm: shmat (5)"); + shmctl(shmid_guard, IPC_RMID, 0); + shmctl(shmid1, IPC_RMID, 0); + shmdt(first_copy); + shmdt((char*)first_copy + pagesize); + shmdt((char*)first_copy + pagesize + size); + continue; } shmctl(shmid1, IPC_RMID, 0); @@ -154,57 +156,48 @@ namespace gr { d_size = size; break; } - if(attempts_remain<0){ + if (attempts_remain < 0) { throw std::runtime_error("gr::vmcircbuf_sysv_shm"); } #endif - } +} - vmcircbuf_sysv_shm::~vmcircbuf_sysv_shm() - { +vmcircbuf_sysv_shm::~vmcircbuf_sysv_shm() +{ #if defined(HAVE_SYS_SHM_H) gr::thread::scoped_lock guard(s_vm_mutex); - if(shmdt(d_base - gr::pagesize()) == -1 - || shmdt(d_base) == -1 - || shmdt(d_base + d_size) == -1 - || shmdt(d_base + 2 * d_size) == -1){ - perror("gr::vmcircbuf_sysv_shm: shmdt (2)"); + if (shmdt(d_base - gr::pagesize()) == -1 || shmdt(d_base) == -1 || + shmdt(d_base + d_size) == -1 || shmdt(d_base + 2 * d_size) == -1) { + perror("gr::vmcircbuf_sysv_shm: shmdt (2)"); } #endif - } +} - // ---------------------------------------------------------------- - // The factory interface - // ---------------------------------------------------------------- +// ---------------------------------------------------------------- +// The factory interface +// ---------------------------------------------------------------- - gr::vmcircbuf_factory *vmcircbuf_sysv_shm_factory::s_the_factory = 0; +gr::vmcircbuf_factory* vmcircbuf_sysv_shm_factory::s_the_factory = 0; - gr::vmcircbuf_factory * - vmcircbuf_sysv_shm_factory::singleton() - { - if(s_the_factory) - return s_the_factory; +gr::vmcircbuf_factory* vmcircbuf_sysv_shm_factory::singleton() +{ + if (s_the_factory) + return s_the_factory; s_the_factory = new gr::vmcircbuf_sysv_shm_factory(); return s_the_factory; - } +} - int - vmcircbuf_sysv_shm_factory::granularity() - { - return gr::pagesize(); - } +int vmcircbuf_sysv_shm_factory::granularity() { return gr::pagesize(); } - gr::vmcircbuf * - vmcircbuf_sysv_shm_factory::make(int size) - { +gr::vmcircbuf* vmcircbuf_sysv_shm_factory::make(int size) +{ try { - return new vmcircbuf_sysv_shm(size); - } - catch (...) { - return 0; + return new vmcircbuf_sysv_shm(size); + } catch (...) { + return 0; } - } +} } /* namespace gr */ |