diff options
author | Evan Huus <eapache@gmail.com> | 2014-03-28 16:28:53 -0400 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2014-03-29 12:11:21 +0000 |
commit | 48fc43baadae5d82b6a7969fef03d305c2c40a31 (patch) | |
tree | a9a95e1212bc713dfb947fe2410e5c1e6e2c81f9 /epan/wmem | |
parent | 7785dd00cd37df9c3296b36db4424ce4c8616657 (diff) |
Implement finalize() method for wmem strbuf.
Change-Id: Ib7a2b0d348b3624f41253e2d0995a4a38a9fe45d
Reviewed-on: https://code.wireshark.org/review/859
Reviewed-by: Evan Huus <eapache@gmail.com>
Diffstat (limited to 'epan/wmem')
-rw-r--r-- | epan/wmem/wmem_strbuf.c | 16 | ||||
-rw-r--r-- | epan/wmem/wmem_strbuf.h | 9 | ||||
-rw-r--r-- | epan/wmem/wmem_test.c | 5 |
3 files changed, 30 insertions, 0 deletions
diff --git a/epan/wmem/wmem_strbuf.c b/epan/wmem/wmem_strbuf.c index d8ced61377..5e973464e9 100644 --- a/epan/wmem/wmem_strbuf.c +++ b/epan/wmem/wmem_strbuf.c @@ -232,6 +232,22 @@ wmem_strbuf_get_len(wmem_strbuf_t *strbuf) return strbuf->len; } +/* Truncates the allocated memory down to the minimal amount, frees the header + * structure, and returns a non-const pointer to the raw string. The + * wmem_strbuf_t structure cannot be used after this is called. + */ +char * +wmem_strbuf_finalize(wmem_strbuf_t *strbuf) +{ + char *ret; + + ret = (char *)wmem_realloc(strbuf->allocator, strbuf->str, strbuf->len+1); + + wmem_free(strbuf->allocator, strbuf); + + return ret; +} + /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * diff --git a/epan/wmem/wmem_strbuf.h b/epan/wmem/wmem_strbuf.h index 50e1e8defd..4645785016 100644 --- a/epan/wmem/wmem_strbuf.h +++ b/epan/wmem/wmem_strbuf.h @@ -89,6 +89,15 @@ WS_DLL_PUBLIC gsize wmem_strbuf_get_len(wmem_strbuf_t *strbuf); +/** Truncates the allocated memory down to the minimal amount, frees the header + * structure, and returns a non-const pointer to the raw string. The + * wmem_strbuf_t structure cannot be used after this is called. Basically a + * destructor for when you still need the underlying C-string. + */ +WS_DLL_PUBLIC +char * +wmem_strbuf_finalize(wmem_strbuf_t *strbuf); + /** @} * @} */ diff --git a/epan/wmem/wmem_test.c b/epan/wmem/wmem_test.c index 2aac1b36d0..247ac25876 100644 --- a/epan/wmem/wmem_test.c +++ b/epan/wmem/wmem_test.c @@ -677,6 +677,7 @@ wmem_test_strbuf(void) wmem_allocator_t *allocator; wmem_strbuf_t *strbuf; int i; + char *str; allocator = wmem_allocator_new(WMEM_ALLOCATOR_STRICT); @@ -737,6 +738,10 @@ wmem_test_strbuf(void) g_assert_cmpstr(wmem_strbuf_get_str(strbuf), ==, "FUZZ3abcd"); g_assert(wmem_strbuf_get_len(strbuf) == 9); + str = wmem_strbuf_finalize(strbuf); + g_assert_cmpstr(str, ==, "FUZZ3abcd"); + g_assert(strlen(str) == 9); + wmem_free_all(allocator); strbuf = wmem_strbuf_new(allocator, "TEST"); |