pub unsafe fn new_in_shared_mem(name: &str, bytes: usize) -> Result<RingBuffer>{
        if bytes & (bytes - 1) != 0 {
            // We need pages to be a power of 2.
            return Err(InvalidRingSize(bytes).into());
        }
        // // Make sure size is page aligned
        // assert!(bytes & !PAGE_SIZE == 0);

        let name = CString::new(name).unwrap();
        let mut fd = shm_open(
            name.as_ptr(),
            libc::O_CREAT | libc::O_EXCL | libc::O_RDWR,
            0o700,
        );
        if fd == -1 {
            if let Some(e) = Error::last_os_error().raw_os_error() {
                if e == libc::EEXIST {
                    shm_unlink(name.as_ptr());
                    fd = shm_open(
                        name.as_ptr(),
                        libc::O_CREAT | libc::O_EXCL | libc::O_RDWR,
                        0o700,
                    );
                }
            }
        };
        assert!(fd >= 0, "Could not create shared memory segment");
        let ftret = ftruncate(fd, bytes as i64);
        assert!(ftret == 0, "Could not truncate");
        let address = mmap(
            ptr::null_mut(),
            bytes + 16,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_POPULATE | libc::MAP_PRIVATE,
            fd,
            0,
        );
        if address == libc::MAP_FAILED {
            let err_string = CString::new("mmap failed").unwrap();
            libc::perror(err_string.as_ptr());
            panic!("Could not mmap shared region");
        }
        close(fd);

        unsafe{
            *(address as *mut usize) = 0;
            *((address as *mut usize).offset(1)) = 0;
            *((address as *mut usize).offset(2)) = bytes;
            *((address as *mut usize).offset(3)) = bytes - 1;
        }

        Ok(RingBuffer {
            name,
            head: (address as *mut usize),
            tail: (address as *mut usize).offset(1), 
            size: (address as *mut usize).offset(2),
            mask: (address as *mut usize).offset(3),
            vec: (address as *mut usize).offset(4) as *mut u8,
        })
    }
/// Create a new wrapping ring buffer. The ring buffer size is specified in bytes and must be a power of 2. 
/// bytes is the number of bytes of RingBuffer::vec
Create a new wrapping ring buffer. The ring buffer size is specified in bytes and must be a power of 2.
bytes is the number of bytes of RingBuffer::vec
Create a new wrapping ring buffer . The ring buffer size is specified in bytes and must be a power of 2 . bytes is the number of bytes of RingBuffer :: vec
pub unsafe fn new_in_shared_mem(name: &str, bytes: usize) -> Result<RingBuffer>{
        if bytes & (bytes - 1) != 0 {
            // We need pages to be a power of 2.
            return Err(InvalidRingSize(bytes).into());
        }
        // // Make sure size is page aligned
        // assert!(bytes & !PAGE_SIZE == 0);
        let name = CString::new(name).unwrap();
        let mut fd = shm_open(
            name.as_ptr(),
            libc::O_CREAT | libc::O_EXCL | libc::O_RDWR,
            0o700,
        );
        if fd == -1 {
            if let Some(e) = Error::last_os_error().raw_os_error() {
                if e == libc::EEXIST {
                    shm_unlink(name.as_ptr());
                    fd = shm_open(
                        name.as_ptr(),
                        libc::O_CREAT | libc::O_EXCL | libc::O_RDWR,
                        0o700,
                    );
                }
            }
        };
        assert!(fd >= 0, "Could not create shared memory segment");
        let ftret = ftruncate(fd, bytes as i64);
        assert!(ftret == 0, "Could not truncate");
        let address = mmap(
            ptr::null_mut(),
            bytes + 16,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_POPULATE | libc::MAP_PRIVATE,
            fd,
            0,
        );
        if address == libc::MAP_FAILED {
            let err_string = CString::new("mmap failed").unwrap();
            libc::perror(err_string.as_ptr());
            panic!("Could not mmap shared region");
        }
        close(fd);
        unsafe{
            *(address as *mut usize) = 0;
            *((address as *mut usize).offset(1)) = 0;
            *((address as *mut usize).offset(2)) = bytes;
            *((address as *mut usize).offset(3)) = bytes - 1;
        }
        Ok(RingBuffer {
            name,
            head: (address as *mut usize),
            tail: (address as *mut usize).offset(1), 
            size: (address as *mut usize).offset(2),
            mask: (address as *mut usize).offset(3),
            vec: (address as *mut usize).offset(4) as *mut u8,
        })
    }
pub unsafe fn new_in_shared_mem ( name : & str , bytes : usize ) -> Result < RingBuffer > { if bytes & ( bytes - 1 ) != 0 { // We need pages to be a power of 2. return Err ( InvalidRingSize ( bytes ) . into ( ) ) ; } // // Make sure size is page aligned // assert!(bytes & !PAGE_SIZE == 0); let name = CString :: new ( name ) . unwrap ( ) ; let mut fd = shm_open ( name . as_ptr ( ) , libc :: O_CREAT | libc :: O_EXCL | libc :: O_RDWR , 0o700 , ) ; if fd == - 1 { if let Some ( e ) = Error :: last_os_error ( ) . raw_os_error ( ) { if e == libc :: EEXIST { shm_unlink ( name . as_ptr ( ) ) ; fd = shm_open ( name . as_ptr ( ) , libc :: O_CREAT | libc :: O_EXCL | libc :: O_RDWR , 0o700 , ) ; } } } ; assert ! ( fd 0 " " ) ; let ftret = ftruncate ( fd , bytes as i64 ) ; assert ! ( ftret 0 " " ) ; let address = mmap ( ptr :: null_mut ( ) , bytes + 16 , libc :: PROT_READ | libc :: PROT_WRITE , libc :: MAP_POPULATE | libc :: MAP_PRIVATE , fd , 0 , ) ; if address == libc :: MAP_FAILED { let err_string = CString :: new ( " " ) . unwrap ( ) ; libc :: perror ( err_string . as_ptr ( ) ) ; panic ! ( " " ) ; } close ( fd ) ; unsafe { * ( address as * mut usize ) = 0 ; * ( ( address as * mut usize ) . offset ( 1 ) ) = 0 ; * ( ( address as * mut usize ) . offset ( 2 ) ) = bytes ; * ( ( address as * mut usize ) . offset ( 3 ) ) = bytes - 1 ; } Ok ( RingBuffer { name , head : ( address as * mut usize ) , tail : ( address as * mut usize ) . offset ( 1 ) , size : ( address as * mut usize ) . offset ( 2 ) , mask : ( address as * mut usize ) . offset ( 3 ) , vec : ( address as * mut usize ) . offset ( 4 ) as * mut u8 , } ) }
Create a new wrapping ring buffer.
Create a new wrapping ring buffer .
name
&str
bytes
usize
name
&str
bytes
usize