Implement finalize() method for wmem strbuf.

Change-Id: Ib7a2b0d348b3624f41253e2d0995a4a38a9fe45d
Reviewed-on: https://code.wireshark.org/review/859
Reviewed-by: Evan Huus <eapache@gmail.com>
This commit is contained in:
Evan Huus 2014-03-28 16:28:53 -04:00
parent 7785dd00cd
commit 48fc43baad
3 changed files with 30 additions and 0 deletions

View File

@ -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
*

View File

@ -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);
/** @}
* @} */

View File

@ -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");