]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
vector: Add AST_VECTOR_COMPACT() to reclaim wasted space
authorSean Bright <sean.bright@gmail.com>
Thu, 14 Mar 2019 14:55:30 +0000 (10:55 -0400)
committerSean Bright <sean.bright@gmail.com>
Fri, 15 Mar 2019 20:03:03 +0000 (16:03 -0400)
This might be useful in situations where you are loading an undetermined number
of items into a vector and don't want to keep (potentially) 2x the necessary
memory around indefinitely.

Change-Id: I9711daa0fe01783fc6f04c5710eba84f2676d7b9

include/asterisk/vector.h

index 2ee5e29a6a0ee3c275f87ed9b9004a70ab7c67d3..0e33590560a3c1e72b74f76ed7e6ef3d8b179283 100644 (file)
@@ -589,6 +589,34 @@ AST_VECTOR(ast_vector_string, char *);
        (vec)->current = 0; \
 })
 
+/*!
+ * \brief Resize a vector so that its capacity is the same as its size.
+ *
+ * \param vec Vector to compact.
+ *
+ * \return 0 on success.
+ * \return Non-zero on failure.
+ */
+#define AST_VECTOR_COMPACT(vec) ({ \
+       int res = 0;                                                            \
+       do {                                                                                                            \
+               if ((vec)->max > (vec)->current) {                                              \
+                       size_t new_max = (vec)->current;                                \
+                       typeof((vec)->elems) new_elems = ast_realloc(           \
+                               (vec)->elems,                                                                   \
+                               new_max * sizeof(*new_elems));                                  \
+                       if (new_elems || (vec)->current == 0) {                         \
+                               (vec)->elems = new_elems;                                               \
+                               (vec)->max = new_max;                                                   \
+                       } else {                                                                                        \
+                               res = -1;                                                                               \
+                               break;                                                                                  \
+                       }                                                                                                       \
+               }                                                                                                               \
+       } while(0);                                                                                                     \
+       res;                                                                                                            \
+})
+
 /*!
  * \brief Get an address of element in a vector.
  *