Skip to content

data structures

Sudheer edited this page Mar 11, 2023 · 7 revisions

Description

Implementation of datastructures supporting lock-free and wait-free constructs. The implementations are helpful for an application which deals with shared data across threads.

ev_queue

#include <ev_queue.h>

Exposes the Type definitions
typedef struct ev_queue_s *ev_queue_type;
typedef void (*print_qnode_func_type)(void*);

ev_queue_type create_ev_queue();

Creates a new ev_queue and initializes it

Parameters:
    none
Return:
    queue: ev_queue_type

void destroy_ev_queue(ev_queue_type);

Empties the queue and frees up the memory.

Parameters:
    queue: ev_queue_type
Return:
    none

void enqueue(ev_queue_type ,void * data);

Enqueues data at the tail of the datastructure

Parameters:
    queue: ev_queue_type
    data: void*
Return:
    none

void * dequeue(ev_queue_type );

Dequeues one element from the head of the datastructure

Parameters:
    queue: ev_queue_type
Return:
    data: void \*, NULL if empty

chunked_memory_stream

C++ Class that implements the chunked memory stream

chunked_memory_stream is a datastructure suitable for transmission of large streams of data like, video/audio buffer etc... It provides a mechanism by which the sending side can push parts of memory stream as chunks and the receiving side can dequeue the chunks and push the data over a socket.

The sending and the receiving threads can operate concurrently on the data structure without the need to synchronize their operations.

Methods

size_t push(void * buffer, size_t bytes);

Transfers 'bytes' number of bytes to the chunked_memory_stream.
From the memory buffer.
The caller is expected to manage the memory for buffer.
Returns the number of bytes transferred.

Parameters:
    buffer: void*
    bytes: size_t
Return:
    bytes_transferred: size_t

ssize_t copy(size_t start_pos, void *buffer, size_t bytes);

Copies 'bytes' number of bytes from the chunked_memory_stream,
the data is copied starting at offset '0 + start_pos'.
If there is less data, as many bytes as there are are copied
to the location pointed by buffer.
The caller is expected to allocate memory to the buffer.

Differences between this and pull_out are,
This method only copies the data and leaves the source unaltered
This method has the ability to copy from any offset location.

Returns the number of bytes copied, or 0 if no data is available
or -1 if there is any error.

Parameters:
    start_pos: size_t
    buffer: void*
    bytes: size_t
Return:
    bytes: ssize_t

size_t read(void *buffer, size_t bytes);

Copies 'bytes' number of bytes from the chunked_memory_stream,
from the start position 0.
If there is less data, as many bytes as there are are copied
to the location pointed by buffer.
The caller is expected to allocate memory to the buffer.

Unlike the other version of read, this copies the data to the buffer
and erases from the source.

The index start_pos is C style, i.e. first character is at 0th
position.

Differences between this and pull_out are,
This method only copies the data and leaves the source unaltered
This method has the ability to copy from any offset location.

Returns the number of bytes copied, or 0 if no data is available
or -1 if there is any error.

Parameters:
    buffer: void*
    bytes: size_t
Return:
    bytes: size_t

size_t erase(size_t bytes);

Moves the head of the data stream to the offset 0 + bytes
Memory holding the data is freed.
Returns the number of bytes erased.

Parameters:
    bytes: size_t
Return:
    bytes: size_t

void * get_buffer();

Gets the available allocated, buffer at the head.
This way of accessing data will help to avoid
a memcpy

Parameters:
    none
Return:
    buffer: void*

size_t get_buffer_len();

Gets length of the available allocated buffer at the head.
This way of accessing data will help to avoid

Parameters:
    none
Return:
    none

size_t get_buffer_len(void * node);

Gets the length of the buffer of the given node.

Parameters:
    node: void*
Return:
    bytes: size_t

void * get_buffer(void * node);

Gets the data buffer in the given node

Parameters:
    node: void*
Return:
    buffer: void*

void * get_next(void * node);

Gets the next node in the list of nodes, given the current node

Parameters:
    node: void*
Return:
    nex_node: void*

ev_buffered_stream

C++ Class that implements the chunked memory stream

Exposes chunked_memory_stream as a a std::basic_streambuf The user has to implement a concrete class inheriting ev_buffered_stream. Further they have to expose the stream by classes inheriting std::ios, std::istream and std::ostream for management of the buffer, as input stream and as output stream respectively.

Some of the methods that are distinct from a usual implementation of std::basic_streambuf are

The construction of std::ios , std::istream and std::ostream implementations should inialize the base class ev_buffered_stream with chunked_memory_stream *cms

consume_all_of_max_len()

Ensures that the whole input buffer (of max_len) is consumed before the buffer is destroyed.

Parameters:
    none
Return:
    none

virtual void get_prefix(char* buffer, std::streamsize bytes, char *prefix, size_t bytes_ptr);

The inheriting class can override the implementation to get an actual prefix in a chunk of multiple chunks of the stream.
Used in chuncked stream encoding of HTTP Request and HTTP response

Parameters:
    buffer: char*
    prefix: char*
    ptr_bytes: size_t
Return:
    none

virtual void get_prefix(char* buffer, std::streamsize bytes, char *prefix, size_t bytes_ptr);

The inheriting class can override the implementation to get an actual suffix in a chunk of multiple chunks of the stream.
Used in chuncked stream encoding of HTTP Request and HTTP response

Parameters:
    buffer: char*
    prefix: char*
    ptr_bytes: size_t
Return:
    none

size_t read_from_source(std::streamsize length);

Reads memory from source, can be overriden by the inheriting class to manage the reading process.
Used in chuncked stream encoding of HTTP Request and HTTP response

Parameters:
    buffer: char*
    prefix: char*
    ptr_bytes: size_t
Return:
    none

void set_prefix_len(size_t bytes);
void set_suffix_len(size_t bytes);

Used to set prefix and siffix lengths
Used in chuncked stream encoding of HTTP Request and HTTP response

Parameters:
    length: size_t
Return:
    none

size_t write_to_sync(char * , std::streamsize);

Writes data to output buffer (sync). Can be overriden in the inherited class
Used in chuncked stream encoding of HTTP Request and HTTP response

Parameters:
    buffer: char*
    size: std::streamsize
Return:
    size: size_t

int low_readch();

Reads one character from source and moves the pointer by one char

Parameters:
    none
Return:
    ch: int

Clone this wiki locally