ring-buffer/queue.h
/** Initialize a blocking queue *q* of size *s* */
Initialize a blocking queue *q* of size *s
Initialize a blocking queue * q * of size * s
void queue_init(queue_t *q, size_t s)
{
    if (s % getpagesize() != 0) {
        queue_error(
            "Requested size (%lu) is not a multiple of the page size (%d)", s,
            getpagesize());
    }
    if ((q->fd = memfd_create("queue_region", 0)) == -1)
        queue_error_errno("Could not obtain anonymous file");
    if (ftruncate(q->fd, s) != 0)
        queue_error_errno("Could not set size of anonymous file");
    if ((q->buffer = mmap(NULL, 2 * s, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
                          -1, 0)) == MAP_FAILED)
        queue_error_errno("Could not allocate virtual memory");
    if (mmap(q->buffer, s, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
             q->fd, 0) == MAP_FAILED)
        queue_error_errno("Could not map buffer into virtual memory");
    if (mmap(q->buffer + s, s, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
             q->fd, 0) == MAP_FAILED)
        queue_error_errno("Could not map buffer into virtual memory");
    if (pthread_mutex_init(&q->lock, NULL) != 0)
        queue_error_errno("Could not initialize mutex");
    if (pthread_cond_init(&q->readable, NULL) != 0)
        queue_error_errno("Could not initialize condition variable");
    if (pthread_cond_init(&q->writeable, NULL) != 0)
        queue_error_errno("Could not initialize condition variable");
    q->size = s;
    q->head = q->tail = 0;
    q->head_seq = q->tail_seq = 0;
}
void queue_init ( queue_t * q , size_t s ) { if ( s % getpagesize ( ) != 0 ) { queue_error ( " " , s , getpagesize ( ) ) ; } if ( ( q -> fd = memfd_create ( " " , 0 ) ) == -1 ) queue_error_errno ( " " ) ; if ( ftruncate ( q -> fd , s ) != 0 ) queue_error_errno ( " " ) ; if ( ( q -> buffer = mmap ( NULL , 2 * s , PROT_NONE , MAP_PRIVATE | MAP_ANONYMOUS , -1 , 0 ) ) == MAP_FAILED ) queue_error_errno ( " " ) ; if ( mmap ( q -> buffer , s , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_FIXED , q -> fd , 0 ) == MAP_FAILED ) queue_error_errno ( " " ) ; if ( mmap ( q -> buffer + s , s , PROT_READ | PROT_WRITE , MAP_SHARED | MAP_FIXED , q -> fd , 0 ) == MAP_FAILED ) queue_error_errno ( " " ) ; if ( pthread_mutex_init ( & q -> lock , NULL ) != 0 ) queue_error_errno ( " " ) ; if ( pthread_cond_init ( & q -> readable , NULL ) != 0 ) queue_error_errno ( " " ) ; if ( pthread_cond_init ( & q -> writeable , NULL ) != 0 ) queue_error_errno ( " " ) ; q -> size = s ; q -> head = q -> tail = 0 ; q -> head_seq = q -> tail_seq = 0 ; }
Initialize a blocking queue *q* of size *s
Initialize a blocking queue * q * of size * s
/* We mmap two adjacent pages (in virtual memory) that point to the same
     * physical memory. This lets us optimize memory access, so that we don't
     * need to even worry about wrapping our pointers around until we go
     * through the entire buffer.
     */ // Check that the requested size is a multiple of a page. If it isn't, we're // in trouble. // Create an anonymous file backed by memory // Set buffer size // Ask mmap for a good address // Mmap first region // Mmap second region, with exact address // Initialize synchronization primitives // Initialize remaining members