pub unsafe fn new_in_heap(ring_size: usize, name: &str, shm_master: bool) -> Result<RingBuffer, Error>{
        if ring_size & (ring_size - 1) != 0 {
            // We need pages to be a power of 2.
            return Err(InvalidRingSize(ring_size).into());
        }
        let size = ring_size * 8 + 16;

        // let temp_vec: Vec<u8> = vec![0; ring_size * 8 + 16];
        // let mut boxed: SuperBox = SuperBox{ my_box: temp_vec.into_boxed_slice(), }; // Box<[u8]> is just like &[u8];
        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) = IOError::last_os_error().raw_os_error() {
                if e == libc::EEXIST {
                    // println!("unlink previous shm");
                    // shm_unlink(name.as_ptr());

                    // if already exist, we just attach to it, instead of unlinking it. 
                    println!("attach to previous shm");
                    fd = shm_open(
                        name.as_ptr(),
                        libc::O_CREAT | libc::O_RDWR,
                        0o700,
                    );
                }
            }
        };
        assert!(fd >= 0, "Could not create shared memory segment");
        let ftret = ftruncate(fd, size as i64);
        assert!(ftret == 0, "Could not truncate");
        let address = mmap(
            ptr::null_mut(),
            size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_POPULATE | libc::MAP_SHARED,
            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);

        let address = address as *mut u8;
        // let address = &mut boxed.my_box[0] as *mut u8;
        unsafe{
            *(address as *mut usize) = 0;
            *((address as *mut usize).offset(1)) = 0;
            *((address as *mut usize).offset(2)) = ring_size;
            *((address as *mut usize).offset(3)) = ring_size - 1;
        }

        Ok(RingBuffer {
            name, 
            size_shm: size, 
            mem: address, 
            shm_master, 
            head: SuperUsize{ my_usize: (address as *mut usize) },
            tail: SuperUsize{ my_usize: (address as *mut usize).offset(1) },
            size: SuperUsize{ my_usize: (address as *mut usize).offset(2) },
            mask: SuperUsize{ my_usize: (address as *mut usize).offset(3) },
            vec: SuperVec{ my_vec: (address as *mut usize).offset(4) as (*mut u64) },
        })
    }
/// Create/attach a new wrapping ring buffer. 
/// The ring buffer size is specified in bytes and must be a power of 2. 
/// we will require additional 16 bytes to store the meta-data for this ring.
Create/attach a new wrapping ring buffer.
The ring buffer size is specified in bytes and must be a power of 2.
we will require additional 16 bytes to store the meta-data for this ring.
Create / attach a new wrapping ring buffer . The ring buffer size is specified in bytes and must be a power of 2 . we will require additional 16 bytes to store the meta - data for this ring .
pub unsafe fn new_in_heap(ring_size: usize, name: &str, shm_master: bool) -> Result<RingBuffer, Error>{
        if ring_size & (ring_size - 1) != 0 {
            // We need pages to be a power of 2.
            return Err(InvalidRingSize(ring_size).into());
        }
        let size = ring_size * 8 + 16;
        // let temp_vec: Vec<u8> = vec![0; ring_size * 8 + 16];
        // let mut boxed: SuperBox = SuperBox{ my_box: temp_vec.into_boxed_slice(), }; // Box<[u8]> is just like &[u8];
        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) = IOError::last_os_error().raw_os_error() {
                if e == libc::EEXIST {
                    // println!("unlink previous shm");
                    // shm_unlink(name.as_ptr());
                    // if already exist, we just attach to it, instead of unlinking it. 
                    println!("attach to previous shm");
                    fd = shm_open(
                        name.as_ptr(),
                        libc::O_CREAT | libc::O_RDWR,
                        0o700,
                    );
                }
            }
        };
        assert!(fd >= 0, "Could not create shared memory segment");
        let ftret = ftruncate(fd, size as i64);
        assert!(ftret == 0, "Could not truncate");
        let address = mmap(
            ptr::null_mut(),
            size,
            libc::PROT_READ | libc::PROT_WRITE,
            libc::MAP_POPULATE | libc::MAP_SHARED,
            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);
        let address = address as *mut u8;
        // let address = &mut boxed.my_box[0] as *mut u8;
        unsafe{
            *(address as *mut usize) = 0;
            *((address as *mut usize).offset(1)) = 0;
            *((address as *mut usize).offset(2)) = ring_size;
            *((address as *mut usize).offset(3)) = ring_size - 1;
        }
        Ok(RingBuffer {
            name, 
            size_shm: size, 
            mem: address, 
            shm_master, 
            head: SuperUsize{ my_usize: (address as *mut usize) },
            tail: SuperUsize{ my_usize: (address as *mut usize).offset(1) },
            size: SuperUsize{ my_usize: (address as *mut usize).offset(2) },
            mask: SuperUsize{ my_usize: (address as *mut usize).offset(3) },
            vec: SuperVec{ my_vec: (address as *mut usize).offset(4) as (*mut u64) },
        })
    }
pub unsafe fn new_in_heap ( ring_size : usize , name : & str , shm_master : bool ) -> Result < RingBuffer , Error > { if ring_size & ( ring_size - 1 ) != 0 { // We need pages to be a power of 2. return Err ( InvalidRingSize ( ring_size ) . into ( ) ) ; } let size = ring_size * 8 + 16 ; // let temp_vec: Vec<u8> = vec![0; ring_size * 8 + 16]; // let mut boxed: SuperBox = SuperBox{ my_box: temp_vec.into_boxed_slice(), }; // Box<[u8]> is just like &[u8]; 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 ) = IOError :: last_os_error ( ) . raw_os_error ( ) { if e == libc :: EEXIST { // println!("unlink previous shm"); // shm_unlink(name.as_ptr()); // if already exist, we just attach to it, instead of unlinking it.  println ! ( " " ) ; fd = shm_open ( name . as_ptr ( ) , libc :: O_CREAT | libc :: O_RDWR , 0o700 , ) ; } } } ; assert ! ( fd 0 " " ) ; let ftret = ftruncate ( fd , size as i64 ) ; assert ! ( ftret 0 " " ) ; let address = mmap ( ptr :: null_mut ( ) , size , libc :: PROT_READ | libc :: PROT_WRITE , libc :: MAP_POPULATE | libc :: MAP_SHARED , fd , 0 , ) ; if address == libc :: MAP_FAILED { let err_string = CString :: new ( " " ) . unwrap ( ) ; libc :: perror ( err_string . as_ptr ( ) ) ; panic ! ( " " ) ; } close ( fd ) ; let address = address as * mut u8 ; // let address = &mut boxed.my_box[0] as *mut u8; unsafe { * ( address as * mut usize ) = 0 ; * ( ( address as * mut usize ) . offset ( 1 ) ) = 0 ; * ( ( address as * mut usize ) . offset ( 2 ) ) = ring_size ; * ( ( address as * mut usize ) . offset ( 3 ) ) = ring_size - 1 ; } Ok ( RingBuffer { name , size_shm : size , mem : address , shm_master , head : SuperUsize { my_usize : ( address as * mut usize ) } , tail : SuperUsize { my_usize : ( address as * mut usize ) . offset ( 1 ) } , size : SuperUsize { my_usize : ( address as * mut usize ) . offset ( 2 ) } , mask : SuperUsize { my_usize : ( address as * mut usize ) . offset ( 3 ) } , vec : SuperVec { my_vec : ( address as * mut usize ) . offset ( 4 ) as ( * mut u64 ) } , } ) }
Create/attach a new wrapping ring buffer.
Create / attach a new wrapping ring buffer .
ring_size
usize
name
&str
shm_master
bool
ring_size
usize
name
&str
shm_master
bool