Sun, 24 Apr 2022 14:54:50 +0200
#171 const qualifier and nonnull attributes
Also removes cxBufferExtract temporarily.
src/buffer.c | file | annotate | diff | comparison | revisions | |
src/cx/buffer.h | file | annotate | diff | comparison | revisions |
1.1 --- a/src/buffer.c Mon Apr 18 17:26:21 2022 +0200 1.2 +++ b/src/buffer.c Sun Apr 24 14:54:50 2022 +0200 1.3 @@ -36,7 +36,7 @@ 1.4 CxBuffer *buffer, 1.5 void *space, 1.6 size_t capacity, 1.7 - CxAllocator *allocator, 1.8 + CxAllocator const *allocator, 1.9 int flags 1.10 ) { 1.11 buffer->allocator = allocator; 1.12 @@ -65,33 +65,6 @@ 1.13 } 1.14 } 1.15 1.16 -CxBuffer *cxBufferExtract( 1.17 - CxBuffer *src, 1.18 - size_t start, 1.19 - size_t length, 1.20 - int flags 1.21 -) { 1.22 - if (src->size == 0 || length == 0 || 1.23 - ((size_t) -1) - start < length || start + length > src->capacity) { 1.24 - return NULL; 1.25 - } 1.26 - 1.27 - CxBuffer *dst = (CxBuffer *) malloc(sizeof(cx_buffer_s)); 1.28 - if (dst) { 1.29 - dst->bytes = malloc(length); 1.30 - if (!dst->bytes) { 1.31 - free(dst); 1.32 - return NULL; 1.33 - } 1.34 - dst->capacity = length; 1.35 - dst->size = length; 1.36 - dst->flags = flags | CX_BUFFER_FREE_CONTENTS; 1.37 - dst->pos = 0; 1.38 - memcpy(dst->bytes, src->bytes + start, length); 1.39 - } 1.40 - return dst; 1.41 -} 1.42 - 1.43 int cxBufferSeek( 1.44 CxBuffer *buffer, 1.45 off_t offset, 1.46 @@ -128,7 +101,13 @@ 1.47 1.48 } 1.49 1.50 -int cxBufferEof(CxBuffer *buffer) { 1.51 +void cxBufferClear(CxBuffer *buffer) { 1.52 + memset(buffer->bytes, 0, buffer->size); 1.53 + buffer->size = 0; 1.54 + buffer->pos = 0; 1.55 +} 1.56 + 1.57 +int cxBufferEof(CxBuffer const *buffer) { 1.58 return buffer->pos >= buffer->size; 1.59 } 1.60
2.1 --- a/src/cx/buffer.h Mon Apr 18 17:26:21 2022 +0200 2.2 +++ b/src/cx/buffer.h Sun Apr 24 14:54:50 2022 +0200 2.3 @@ -84,7 +84,7 @@ 2.4 unsigned char *bytes; 2.5 }; 2.6 /** The allocator to use for automatic memory management. */ 2.7 - CxAllocator *allocator; 2.8 + CxAllocator const *allocator; 2.9 /** Current position of the buffer. */ 2.10 size_t pos; 2.11 /** Current capacity (i.e. maximum size) of the buffer. */ 2.12 @@ -120,11 +120,12 @@ 2.13 * @param flags buffer features (see cx_buffer_s.flags) 2.14 * @return zero on success, non-zero if a required allocation failed 2.15 */ 2.16 +__attribute__((__nonnull__(1, 4))) 2.17 int cxBufferInit( 2.18 CxBuffer *buffer, 2.19 void *space, 2.20 size_t capacity, 2.21 - CxAllocator *allocator, 2.22 + CxAllocator const *allocator, 2.23 int flags 2.24 ); 2.25 2.26 @@ -135,37 +136,10 @@ 2.27 * 2.28 * @param buffer the buffer which contents shall be destroyed 2.29 */ 2.30 +__attribute__((__nonnull__)) 2.31 void cxBufferDestroy(CxBuffer *buffer); 2.32 2.33 /** 2.34 - * Creates a new buffer and fills it with content copied from another buffer. 2.35 - * 2.36 - * \note The #CX_BUFFER_FREE_CONTENTS feature is enforced for the new buffer. 2.37 - * 2.38 - * @param src the source buffer 2.39 - * @param start the start position of extraction 2.40 - * @param length the count of bytes to extract (must not be zero) 2.41 - * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled) 2.42 - * @return a new buffer containing the extraction 2.43 - */ 2.44 -CxBuffer *cxBufferExtract( 2.45 - CxBuffer *src, 2.46 - size_t start, 2.47 - size_t length, 2.48 - int flags 2.49 -); 2.50 - 2.51 -/** 2.52 - * A shorthand macro for copying an entire buffer. 2.53 - * 2.54 - * @param src the source buffer 2.55 - * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled) 2.56 - * @return a new buffer with the copied content 2.57 - */ 2.58 -#define cxBufferClone(src, flags) cxBufferExtract(src, 0, (src)->capacity, flags) 2.59 - 2.60 - 2.61 -/** 2.62 * Shifts the contents of the buffer by the given offset. 2.63 * 2.64 * If the offset is positive, the contents are shifted to the right. 2.65 @@ -198,6 +172,7 @@ 2.66 * @param shift the shift offset (negative means left shift) 2.67 * @return 0 on success, non-zero if a required auto-extension fails 2.68 */ 2.69 +__attribute__((__nonnull__)) 2.70 int cxBufferShift( 2.71 CxBuffer *buffer, 2.72 off_t shift 2.73 @@ -212,6 +187,7 @@ 2.74 * @return 0 on success, non-zero if a required auto-extension fails 2.75 * @see cxBufferShift() 2.76 */ 2.77 +__attribute__((__nonnull__)) 2.78 int cxBufferShiftRight( 2.79 CxBuffer *buffer, 2.80 size_t shift 2.81 @@ -229,6 +205,7 @@ 2.82 * @return always zero 2.83 * @see cxBufferShift() 2.84 */ 2.85 +__attribute__((__nonnull__)) 2.86 int cxBufferShiftLeft( 2.87 CxBuffer *buffer, 2.88 size_t shift 2.89 @@ -254,6 +231,7 @@ 2.90 * @return 0 on success, non-zero if the position is invalid 2.91 * 2.92 */ 2.93 +__attribute__((__nonnull__)) 2.94 int cxBufferSeek( 2.95 CxBuffer *buffer, 2.96 off_t offset, 2.97 @@ -267,8 +245,8 @@ 2.98 * 2.99 * @param buffer the buffer to be cleared 2.100 */ 2.101 -#define cxBufferClear(buffer) memset((buffer)->bytes, 0, (buffer)->size); \ 2.102 - (buffer)->size = 0; (buffer)->pos = 0; 2.103 +__attribute__((__nonnull__)) 2.104 +void cxBufferClear(CxBuffer *buffer); 2.105 2.106 /** 2.107 * Tests, if the buffer position has exceeded the buffer capacity. 2.108 @@ -277,7 +255,8 @@ 2.109 * @return non-zero, if the current buffer position has exceeded the last 2.110 * available byte of the buffer. 2.111 */ 2.112 -int cxBufferEof(CxBuffer *buffer); 2.113 +__attribute__((__nonnull__)) 2.114 +int cxBufferEof(CxBuffer const *buffer); 2.115 2.116 2.117 /** 2.118 @@ -289,6 +268,7 @@ 2.119 * @param capacity the minimum required capacity for this buffer 2.120 * @return 0 on success or a non-zero value on failure 2.121 */ 2.122 +__attribute__((__nonnull__)) 2.123 int cxBufferMinimumCapacity( 2.124 CxBuffer *buffer, 2.125 size_t capacity 2.126 @@ -307,6 +287,7 @@ 2.127 * @param buffer the CxBuffer to write to 2.128 * @return the total count of bytes written 2.129 */ 2.130 +__attribute__((__nonnull__)) 2.131 size_t cxBufferWrite( 2.132 void const *ptr, 2.133 size_t size, 2.134 @@ -327,6 +308,7 @@ 2.135 * @param buffer the CxBuffer to read from 2.136 * @return the total number of elements read 2.137 */ 2.138 +__attribute__((__nonnull__)) 2.139 size_t cxBufferRead( 2.140 void *ptr, 2.141 size_t size, 2.142 @@ -349,12 +331,26 @@ 2.143 * @return the byte that has bean written or \c EOF when the end of the stream is 2.144 * reached and automatic extension is not enabled or not possible 2.145 */ 2.146 +__attribute__((__nonnull__)) 2.147 int cxBufferPut( 2.148 CxBuffer *buffer, 2.149 int c 2.150 ); 2.151 2.152 /** 2.153 + * Writes a string to a buffer. 2.154 + * 2.155 + * @param buffer the buffer 2.156 + * @param str the zero-terminated string 2.157 + * @return the number of bytes written 2.158 + */ 2.159 +__attribute__((__nonnull__)) 2.160 +size_t cxBufferPutString( 2.161 + CxBuffer *buffer, 2.162 + const char *str 2.163 +); 2.164 + 2.165 +/** 2.166 * Gets a character from a buffer. 2.167 * 2.168 * The current position of the buffer is increased after a successful read. 2.169 @@ -362,20 +358,9 @@ 2.170 * @param buffer the buffer to read from 2.171 * @return the character or \c EOF, if the end of the buffer is reached 2.172 */ 2.173 +__attribute__((__nonnull__)) 2.174 int cxBufferGet(CxBuffer *buffer); 2.175 2.176 -/** 2.177 - * Writes a string to a buffer. 2.178 - * 2.179 - * @param buffer the buffer 2.180 - * @param str the zero-terminated string 2.181 - * @return the number of bytes written 2.182 - */ 2.183 -size_t cxBufferPutString( 2.184 - CxBuffer *buffer, 2.185 - const char *str 2.186 -); 2.187 - 2.188 #ifdef __cplusplus 2.189 } 2.190 #endif