ucx
UAP Common Extensions
Loading...
Searching...
No Matches
buffer.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
46#ifndef UCX_BUFFER_H
47#define UCX_BUFFER_H
48
49#include "common.h"
50#include "allocator.h"
51
52#ifdef __cplusplus
53extern "C" {
54#endif
55
59#define CX_BUFFER_DEFAULT 0x00
60
67#define CX_BUFFER_FREE_CONTENTS 0x01
68
72#define CX_BUFFER_AUTO_EXTEND 0x02
73
80#define CX_BUFFER_COPY_ON_WRITE 0x04
81
89#define CX_BUFFER_COPY_ON_EXTEND 0x08
90
95#define cxBufferWriteFunc ((cx_write_func) cxBufferWrite)
100#define cxBufferReadFunc ((cx_read_func) cxBufferRead)
101
140
155
159 union {
163 char *space;
167 unsigned char *bytes;
168 };
178 size_t pos;
180 size_t capacity;
182 size_t size;
190 int flags;
191};
192
196typedef struct cx_buffer_s CxBuffer;
197
232 CxBuffer *buffer,
233 void *space,
234 size_t capacity,
235 const CxAllocator *allocator,
236 int flags
237);
238
256 CxBuffer *buffer,
258);
259
272
286void cxBufferFree(CxBuffer *buffer);
287
317 void *space,
318 size_t capacity,
319 const CxAllocator *allocator,
320 int flags
321);
322
362 CxBuffer *buffer,
363 off_t shift
364);
365
379 CxBuffer *buffer,
380 size_t shift
381);
382
396 CxBuffer *buffer,
397 size_t shift
398);
399
400
424 CxBuffer *buffer,
425 off_t offset,
426 int whence
427);
428
444
457
469bool cxBufferEof(const CxBuffer *buffer);
470
471
485 CxBuffer *buffer,
486 size_t capacity
487);
488
533 const void *ptr,
534 size_t size,
535 size_t nitems,
536 CxBuffer *buffer
537);
538
561 const void *ptr,
562 size_t size,
563 size_t nitems,
564 CxBuffer *buffer
565);
566
622size_t cxBufferFlush(CxBuffer *buffer);
623
642 void *ptr,
643 size_t size,
644 size_t nitems,
645 CxBuffer *buffer
646);
647
670 CxBuffer *buffer,
671 int c
672);
673
689
703 CxBuffer *buffer,
704 const char *str
705);
706
718
719#ifdef __cplusplus
720}
721#endif
722
723#endif // UCX_BUFFER_H
Interface for custom allocators.
int cxBufferEnableFlushing(CxBuffer *buffer, CxBufferFlushConfig config)
Configures the buffer for flushing.
void cxBufferReset(CxBuffer *buffer)
Resets the buffer by resetting the position and size to zero.
size_t cxBufferWrite(const void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Writes data to a CxBuffer.
bool cxBufferEof(const CxBuffer *buffer)
Tests, if the buffer position has exceeded the buffer size.
size_t cxBufferFlush(CxBuffer *buffer)
Performs a single flush-run on the specified buffer.
void cxBufferClear(CxBuffer *buffer)
Clears the buffer by resetting the position and deleting the data.
int cxBufferShift(CxBuffer *buffer, off_t shift)
Shifts the contents of the buffer by the given offset.
size_t cxBufferAppend(const void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Appends data to a CxBuffer.
void cxBufferFree(CxBuffer *buffer)
Deallocates the buffer.
int cxBufferShiftLeft(CxBuffer *buffer, size_t shift)
Shifts the buffer to the left.
int cxBufferGet(CxBuffer *buffer)
Gets a character from a buffer.
int cxBufferMinimumCapacity(CxBuffer *buffer, size_t capacity)
Ensures that the buffer has a minimum capacity.
void cxBufferDestroy(CxBuffer *buffer)
Destroys the buffer contents.
int cxBufferShiftRight(CxBuffer *buffer, size_t shift)
Shifts the buffer to the right.
int cxBufferTerminate(CxBuffer *buffer)
Writes a terminating zero to a buffer at the current position.
int cxBufferSeek(CxBuffer *buffer, off_t offset, int whence)
Moves the position of the buffer.
size_t cxBufferPutString(CxBuffer *buffer, const char *str)
Writes a string to a buffer.
int cxBufferPut(CxBuffer *buffer, int c)
Writes a character to a buffer.
size_t cxBufferRead(void *ptr, size_t size, size_t nitems, CxBuffer *buffer)
Reads data from a CxBuffer.
int cxBufferInit(CxBuffer *buffer, void *space, size_t capacity, const CxAllocator *allocator, int flags)
Initializes a fresh buffer.
CxBuffer * cxBufferCreate(void *space, size_t capacity, const CxAllocator *allocator, int flags)
Allocates and initializes a fresh buffer.
Common definitions and feature checks.
#define cx_attr_export
Only used for building Windows DLLs.
Definition common.h:285
#define cx_attr_nonnull
All pointer arguments must be non-NULL.
Definition common.h:136
size_t(* cx_write_func)(const void *, size_t, size_t, void *)
Function pointer compatible with fwrite-like functions.
Definition common.h:295
#define cx_attr_nodiscard
Warn about discarded return value.
Definition common.h:265
#define cx_attr_malloc
The attributed function always returns freshly allocated memory.
Definition common.h:151
#define cx_attr_nonnull_arg(...)
The specified pointer arguments must be non-NULL.
Definition common.h:141
#define cx_attr_cstr_arg(idx)
No support for null_terminated_string_arg in clang or GCC below 14.
Definition common.h:206
#define cx_attr_dealloc(...)
Not supported in clang.
Definition common.h:167
Structure holding the data for an allocator.
Definition allocator.h:84
Configuration for automatic flushing.
Definition buffer.h:105
cx_write_func wfunc
The write-function used for flushing.
Definition buffer.h:138
size_t blkmax
The maximum number of blocks to flush in one cycle.
Definition buffer.h:127
size_t threshold
The buffer may not extend beyond this threshold before starting to flush.
Definition buffer.h:113
void * target
The target for write function.
Definition buffer.h:132
size_t blksize
The block size for the elements to flush.
Definition buffer.h:117
Structure for the UCX buffer data.
Definition buffer.h:157
char * space
Data is interpreted as text.
Definition buffer.h:163
size_t capacity
Current capacity (i.e.
Definition buffer.h:180
unsigned char * bytes
Data is interpreted as binary.
Definition buffer.h:167
size_t pos
Current position of the buffer.
Definition buffer.h:178
int flags
Flag register for buffer features.
Definition buffer.h:190
size_t size
Current size of the buffer content.
Definition buffer.h:182
const CxAllocator * allocator
The allocator to use for automatic memory management.
Definition buffer.h:170
CxBufferFlushConfig * flush
Optional flush configuration.
Definition buffer.h:176