#171 const qualifier and nonnull attributes

Sun, 24 Apr 2022 14:54:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 24 Apr 2022 14:54:50 +0200
changeset 529
814d51173f20
parent 528
4fbfac557df8
child 530
e866516cac17

#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
--- a/src/buffer.c	Mon Apr 18 17:26:21 2022 +0200
+++ b/src/buffer.c	Sun Apr 24 14:54:50 2022 +0200
@@ -36,7 +36,7 @@
         CxBuffer *buffer,
         void *space,
         size_t capacity,
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         int flags
 ) {
     buffer->allocator = allocator;
@@ -65,33 +65,6 @@
     }
 }
 
-CxBuffer *cxBufferExtract(
-        CxBuffer *src,
-        size_t start,
-        size_t length,
-        int flags
-) {
-    if (src->size == 0 || length == 0 ||
-        ((size_t) -1) - start < length || start + length > src->capacity) {
-        return NULL;
-    }
-
-    CxBuffer *dst = (CxBuffer *) malloc(sizeof(cx_buffer_s));
-    if (dst) {
-        dst->bytes = malloc(length);
-        if (!dst->bytes) {
-            free(dst);
-            return NULL;
-        }
-        dst->capacity = length;
-        dst->size = length;
-        dst->flags = flags | CX_BUFFER_FREE_CONTENTS;
-        dst->pos = 0;
-        memcpy(dst->bytes, src->bytes + start, length);
-    }
-    return dst;
-}
-
 int cxBufferSeek(
         CxBuffer *buffer,
         off_t offset,
@@ -128,7 +101,13 @@
 
 }
 
-int cxBufferEof(CxBuffer *buffer) {
+void cxBufferClear(CxBuffer *buffer) {
+    memset(buffer->bytes, 0, buffer->size);
+    buffer->size = 0;
+    buffer->pos = 0;
+}
+
+int cxBufferEof(CxBuffer const *buffer) {
     return buffer->pos >= buffer->size;
 }
 
--- a/src/cx/buffer.h	Mon Apr 18 17:26:21 2022 +0200
+++ b/src/cx/buffer.h	Sun Apr 24 14:54:50 2022 +0200
@@ -84,7 +84,7 @@
         unsigned char *bytes;
     };
     /** The allocator to use for automatic memory management. */
-    CxAllocator *allocator;
+    CxAllocator const *allocator;
     /** Current position of the buffer. */
     size_t pos;
     /** Current capacity (i.e. maximum size) of the buffer. */
@@ -120,11 +120,12 @@
  * @param flags buffer features (see cx_buffer_s.flags)
  * @return zero on success, non-zero if a required allocation failed
  */
+__attribute__((__nonnull__(1, 4)))
 int cxBufferInit(
         CxBuffer *buffer,
         void *space,
         size_t capacity,
-        CxAllocator *allocator,
+        CxAllocator const *allocator,
         int flags
 );
 
@@ -135,37 +136,10 @@
  *
  * @param buffer the buffer which contents shall be destroyed
  */
+__attribute__((__nonnull__))
 void cxBufferDestroy(CxBuffer *buffer);
 
 /**
- * Creates a new buffer and fills it with content copied from another buffer.
- *
- * \note The #CX_BUFFER_FREE_CONTENTS feature is enforced for the new buffer.
- *
- * @param src the source buffer
- * @param start the start position of extraction
- * @param length the count of bytes to extract (must not be zero)
- * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
- * @return a new buffer containing the extraction
- */
-CxBuffer *cxBufferExtract(
-        CxBuffer *src,
-        size_t start,
-        size_t length,
-        int flags
-);
-
-/**
- * A shorthand macro for copying an entire buffer.
- *
- * @param src the source buffer
- * @param flags features for the new buffer (#CX_BUFFER_FREE_CONTENTS will always be enabled)
- * @return a new buffer with the copied content
- */
-#define cxBufferClone(src, flags) cxBufferExtract(src, 0, (src)->capacity, flags)
-
-
-/**
  * Shifts the contents of the buffer by the given offset.
  *
  * If the offset is positive, the contents are shifted to the right.
@@ -198,6 +172,7 @@
  * @param shift the shift offset (negative means left shift)
  * @return 0 on success, non-zero if a required auto-extension fails
  */
+__attribute__((__nonnull__))
 int cxBufferShift(
         CxBuffer *buffer,
         off_t shift
@@ -212,6 +187,7 @@
  * @return 0 on success, non-zero if a required auto-extension fails
  * @see cxBufferShift()
  */
+__attribute__((__nonnull__))
 int cxBufferShiftRight(
         CxBuffer *buffer,
         size_t shift
@@ -229,6 +205,7 @@
  * @return always zero
  * @see cxBufferShift()
  */
+__attribute__((__nonnull__))
 int cxBufferShiftLeft(
         CxBuffer *buffer,
         size_t shift
@@ -254,6 +231,7 @@
  * @return 0 on success, non-zero if the position is invalid
  *
  */
+__attribute__((__nonnull__))
 int cxBufferSeek(
         CxBuffer *buffer,
         off_t offset,
@@ -267,8 +245,8 @@
  *
  * @param buffer the buffer to be cleared
  */
-#define cxBufferClear(buffer) memset((buffer)->bytes, 0, (buffer)->size); \
-        (buffer)->size = 0; (buffer)->pos = 0;
+__attribute__((__nonnull__))
+void cxBufferClear(CxBuffer *buffer);
 
 /**
  * Tests, if the buffer position has exceeded the buffer capacity.
@@ -277,7 +255,8 @@
  * @return non-zero, if the current buffer position has exceeded the last
  * available byte of the buffer.
  */
-int cxBufferEof(CxBuffer *buffer);
+__attribute__((__nonnull__))
+int cxBufferEof(CxBuffer const *buffer);
 
 
 /**
@@ -289,6 +268,7 @@
  * @param capacity the minimum required capacity for this buffer
  * @return 0 on success or a non-zero value on failure
  */
+__attribute__((__nonnull__))
 int cxBufferMinimumCapacity(
         CxBuffer *buffer,
         size_t capacity
@@ -307,6 +287,7 @@
  * @param buffer the CxBuffer to write to
  * @return the total count of bytes written
  */
+__attribute__((__nonnull__))
 size_t cxBufferWrite(
         void const *ptr,
         size_t size,
@@ -327,6 +308,7 @@
  * @param buffer the CxBuffer to read from
  * @return the total number of elements read
  */
+__attribute__((__nonnull__))
 size_t cxBufferRead(
         void *ptr,
         size_t size,
@@ -349,12 +331,26 @@
  * @return the byte that has bean written or \c EOF when the end of the stream is
  * reached and automatic extension is not enabled or not possible
  */
+__attribute__((__nonnull__))
 int cxBufferPut(
         CxBuffer *buffer,
         int c
 );
 
 /**
+ * Writes a string to a buffer.
+ *
+ * @param buffer the buffer
+ * @param str the zero-terminated string
+ * @return the number of bytes written
+ */
+__attribute__((__nonnull__))
+size_t cxBufferPutString(
+        CxBuffer *buffer,
+        const char *str
+);
+
+/**
  * Gets a character from a buffer.
  *
  * The current position of the buffer is increased after a successful read.
@@ -362,20 +358,9 @@
  * @param buffer the buffer to read from
  * @return the character or \c EOF, if the end of the buffer is reached
  */
+__attribute__((__nonnull__))
 int cxBufferGet(CxBuffer *buffer);
 
-/**
- * Writes a string to a buffer.
- *
- * @param buffer the buffer
- * @param str the zero-terminated string
- * @return the number of bytes written
- */
-size_t cxBufferPutString(
-        CxBuffer *buffer,
-        const char *str
-);
-
 #ifdef __cplusplus
 }
 #endif

mercurial