Skip to content

Instantly share code, notes, and snippets.

@edenhill
Created May 25, 2017 11:22
Show Gist options
  • Save edenhill/c917068881403579de4c11f85926a395 to your computer and use it in GitHub Desktop.
Save edenhill/c917068881403579de4c11f85926a395 to your computer and use it in GitHub Desktop.
buffers?
/**
* @brief Buffer segment
*/
typedef struct rd_segment_s {
TAILQ_ENTRY(rd_segment_s) seg_link; /*<< rbuf_segments Link */
char *seg_p; /**< Backing-store memory */
size_t seg_of; /**< Current relative write-position
* (length of payload in this segment) */
size_t seg_size; /**< Allocated size of seg_p */
size_t seg_absof; /**< Absolute offset of this segment's
* beginning in the grand rd_buf_t */
void (*seg_free) (void *p); /**< Optional free function for seg_p */
int seg_flags; /**< Segment flags */
#define RD_SEGMENT_F_RDONLY 0x1 /**< Read-only segment */
#define RD_SEGMENT_F_FREE 0x2 /**< Free segment on destroy,
* e.g, not a fixed segment. */
} rd_segment_t;
TAILQ_HEAD(rd_segment_head,rd_segment_s);
/**
* @brief Buffer, containing a list of segments.
*/
typedef struct rd_buf_s {
struct rd_segment_head rbuf_segments; /**< TAILQ list of segments */
size_t rbuf_segment_cnt; /**< Number of segments */
rd_segment_t *rbuf_wpos; /**< Current write position seg */
size_t rbuf_len; /**< Current (written) length */
size_t rbuf_size; /**< Total allocated size of
* all segments. */
rd_segment_t *rbuf_fixed_segs; /**< Pre-allocated segments */
size_t rbuf_fixed_seg_cnt; /**< Pre-allocated segment count*/
size_t rbuf_fixed_seg_usecnt; /**< Number of used
* pre-allocated segments. */
} rd_buf_t;
/**
* @name Buffer read operations
* @{
*/
/**
* @brief Buffer read position, pointing to a segment and the relative
* offset within that segment.
*
* @warning A read buffer position will not be valid/safe after the buffer or
* segments have been modified by a buf write / update / seek.
*/
typedef struct rd_buf_pos_s {
const rd_buf_t *buf; /**< Pointer to buffer */
rd_segment_t *seg; /**< Current read position segment */
size_t rof; /**< Relative read offset in segment */
} rd_buf_pos_t;
typedef struct rd_buf_slice_s {
const rd_buf_t *buf; /**< Pointer to buffer */
rd_segment_t *seg; /**< Current read position segment */
size_t rof; /**< Relative read offset in segment */
size_t size; /**< Remaining slice size, will be reduced as
* the reader progresses. */
} rd_buf_slice_t;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment