changeset 92:7e376bff2e91

base/vec: Clarify need for initialization and format code
author Lewin Bormann <lbo@spheniscida.de>
date Mon, 26 Aug 2019 16:58:20 +0200
parents 046d0c66e3bb
children aadc11bdfdc4
files src/base/vec.h src/base/vec_test.c
diffstat 2 files changed, 12 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/base/vec.h	Mon Aug 26 16:57:43 2019 +0200
+++ b/src/base/vec.h	Mon Aug 26 16:58:20 2019 +0200
@@ -17,6 +17,9 @@
  *
  * At the cost of access performance we optimize for memory by fitting yvec_t
  * into 16 bytes.
+ *
+ * All yvec_t values must be initialized using YVEC_INIT, YVEC_NEW, yvec_init,
+ * or yvec_new before use; even before calling functions like yvec_copy().
  */
 typedef struct {
     /// Start of vector.
@@ -69,13 +72,14 @@
  * Elements are appended at `vec[vec.len]`. Use `yvec_resize()` to change the
  * length.
  */
-size_t yvec_push(yvec_t* vec, const void *element);
+size_t yvec_push(yvec_t *vec, const void *element);
 
 /**
- * @brief Remove an element and returns true if the vector had at least one element. The element is stored into `dst`, which must point to
- * a memory area of at least `vec->size` bytes.
+ * @brief Remove an element and returns true if the vector had at least one
+ * element. The element is stored into `dst`, which must point to a memory area
+ * of at least `vec->size` bytes.
  */
-bool yvec_pop(yvec_t* vec, void* dst);
+bool yvec_pop(yvec_t *vec, void *dst);
 
 /**
  * @brief Append several elements from a raw array. The size of elements must
@@ -104,14 +108,15 @@
 void *yvec_at(yvec_t *vec, size_t index);
 
 /**
- * @brief Copy the entire contents to a new yvec. `dst` is destroyed before copying.
+ * @brief Copy the entire contents to a new yvec. `dst` is destroyed before
+ * copying.
  */
 void yvec_copy(yvec_t *src, yvec_t *dst);
 
 /**
  * @brief Return a copy of `src` (the full buffer is copied).
  */
-yvec_t yvec_clone(yvec_t* src);
+yvec_t yvec_clone(yvec_t *src);
 
 /**
  * @brief Deallocate a vector. Resets all fields to 0.
--- a/src/base/vec_test.c	Mon Aug 26 16:57:43 2019 +0200
+++ b/src/base/vec_test.c	Mon Aug 26 16:58:20 2019 +0200
@@ -36,6 +36,7 @@
 
     // Copy vector.
     yvec_t other;
+    YVEC_INIT(&other, 16, int);
     yvec_copy(&vec, &other);
 
     assert(98 == *YVEC_AT(&other, 98, int));