diff mbox series

[ovs-dev,1/1] util: helper function for freeing allocated array of pointers

Message ID 1582544283-5783-1-git-send-email-damjan.skvarc@gmail.com
State Changes Requested
Headers show
Series [ovs-dev,1/1] util: helper function for freeing allocated array of pointers | expand

Commit Message

Damijan Skvarc Feb. 24, 2020, 11:38 a.m. UTC
OVS code provides useful functionality svec, which deals with dynamical
vector of strings. This module already provides functionality for adding, 
removing, modifying... strings in this vector. While working on reducing memory
leaks in OVN code I found ovs_cmdl_env_parse_all() function, which allocates
array of strings with the help of svec. Resulting array represents memory
leak (both in ovn-nctl and ovn-sctl) code, however since it is detached for 
svec entity apparent functionality for releasing this memory could not be used.

Instead of assigning allocated memory back to svec (with a purpose to deallocate it
with appropriate function) I was looking for yet another function in OVS which would 
do the same. Since I didn't find it I am proposing the following function. 
I think the most apropriate location would be in util.c module, however if someone
could advice for some other possibility I would be thankful.



Signed-off-by: Damijan Skvarc <damjan.skvarc@gmail.com>
---
 lib/util.c | 13 +++++++++++++
 lib/util.h |  1 +
 2 files changed, 14 insertions(+)

Comments

Ben Pfaff Feb. 24, 2020, 7:11 p.m. UTC | #1
On Mon, Feb 24, 2020 at 12:38:03PM +0100, Damijan Skvarc wrote:
> OVS code provides useful functionality svec, which deals with dynamical
> vector of strings. This module already provides functionality for adding, 
> removing, modifying... strings in this vector. While working on reducing memory
> leaks in OVN code I found ovs_cmdl_env_parse_all() function, which allocates
> array of strings with the help of svec. Resulting array represents memory
> leak (both in ovn-nctl and ovn-sctl) code, however since it is detached for 
> svec entity apparent functionality for releasing this memory could not be used.
> 
> Instead of assigning allocated memory back to svec (with a purpose to deallocate it
> with appropriate function) I was looking for yet another function in OVS which would 
> do the same. Since I didn't find it I am proposing the following function. 
> I think the most apropriate location would be in util.c module, however if someone
> could advice for some other possibility I would be thankful.
> 
> 
> 
> Signed-off-by: Damijan Skvarc <damjan.skvarc@gmail.com>

Thanks for working on making OVS better.

The 'x' prefix usually means that a function aborts when a memory
allocation fails.  This function doesn't allocate memory, so I would
omit it.

I think I'd name this function something like free_strings().
diff mbox series

Patch

diff --git a/lib/util.c b/lib/util.c
index 830e145..a588c8e 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -346,6 +346,19 @@  xasprintf(const char *format, ...)
     return s;
 }
 
+/* free array of char pointers and array itself */
+void
+xstrsfree(char **strs, size_t size)
+{
+    size_t i;
+
+    for (i = 0; i < size; i++) {
+        free(strs[i]);
+    }
+    free(strs);
+}
+
+
 /* Similar to strlcpy() from OpenBSD, but it never reads more than 'size - 1'
  * bytes from 'src' and doesn't return anything. */
 void
diff --git a/lib/util.h b/lib/util.h
index 7ad8758..284973a 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -159,6 +159,7 @@  bool nullable_string_is_equal(const char *a, const char *b);
 char *xasprintf(const char *format, ...) OVS_PRINTF_FORMAT(1, 2) MALLOC_LIKE;
 char *xvasprintf(const char *format, va_list) OVS_PRINTF_FORMAT(1, 0) MALLOC_LIKE;
 void *x2nrealloc(void *p, size_t *n, size_t s);
+void  xstrsfree(char **, size_t);
 
 void *xmalloc_cacheline(size_t) MALLOC_LIKE;
 void *xzalloc_cacheline(size_t) MALLOC_LIKE;