aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authorkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-23 16:34:46 +0000
committerkpfleming <kpfleming@f38db490-d61c-443f-a65b-d21fe96a405b>2008-07-23 16:34:46 +0000
commitc21ba73497572490927ca4b65de01845d09cca8c (patch)
treeb1d836e5e06ba91833c1f34d3c0fce3c561f0d1a /main
parent2b145897c6e2f3e65bff61d0c30a1c3543c0e508 (diff)
Merged revisions 132964 via svnmerge from
https://origsvn.digium.com/svn/asterisk/trunk ................ r132964 | kpfleming | 2008-07-23 11:30:18 -0500 (Wed, 23 Jul 2008) | 10 lines Merged revisions 132872 via svnmerge from https://origsvn.digium.com/svn/asterisk/branches/1.4 ........ r132872 | kpfleming | 2008-07-23 06:52:18 -0500 (Wed, 23 Jul 2008) | 2 lines minor optimization for stringfields: when a field is being set to a larger value than it currently contains and it happens to be the most recent field allocated from the currentl pool, it is possible to 'grow' it without having to waste the space it is currently using (or potentially even allocate a new pool) ........ ................ git-svn-id: http://svn.digium.com/svn/asterisk/branches/1.6.0@132965 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/utils.c53
1 files changed, 42 insertions, 11 deletions
diff --git a/main/utils.c b/main/utils.c
index 6a435249e..d7250d960 100644
--- a/main/utils.c
+++ b/main/utils.c
@@ -1318,7 +1318,8 @@ const char __ast_string_field_empty[] = ""; /*!< the empty string */
* fields in *mgr reflect the size of that only.
*/
static int add_string_pool(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head, size_t size)
+ struct ast_string_field_pool **pool_head,
+ size_t size)
{
struct ast_string_field_pool *pool;
@@ -1329,6 +1330,7 @@ static int add_string_pool(struct ast_string_field_mgr *mgr,
*pool_head = pool;
mgr->size = size;
mgr->used = 0;
+ mgr->last_alloc = NULL;
return 0;
}
@@ -1344,14 +1346,16 @@ static int add_string_pool(struct ast_string_field_mgr *mgr,
* This must be done before destroying the object.
*/
int __ast_string_field_init(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head, int size)
+ struct ast_string_field_pool **pool_head,
+ int size)
{
- const char **p = (const char **)pool_head + 1;
+ const char **p = (const char **) pool_head + 1;
struct ast_string_field_pool *cur = *pool_head;
/* clear fields - this is always necessary */
- while ((struct ast_string_field_mgr *)p != mgr)
+ while ((struct ast_string_field_mgr *) p != mgr)
*p++ = __ast_string_field_empty;
+ mgr->last_alloc = NULL;
if (size > 0) { /* allocate the initial pool */
*pool_head = NULL;
return add_string_pool(mgr, pool_head, size);
@@ -1367,16 +1371,19 @@ int __ast_string_field_init(struct ast_string_field_mgr *mgr,
(*pool_head)->prev = NULL;
mgr->used = 0;
}
+
while (cur) {
struct ast_string_field_pool *prev = cur->prev;
+
ast_free(cur);
cur = prev;
}
+
return 0;
}
ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head, size_t needed)
+ struct ast_string_field_pool **pool_head, size_t needed)
{
char *result = NULL;
size_t space = mgr->size - mgr->used;
@@ -1393,17 +1400,41 @@ ast_string_field __ast_string_field_alloc_space(struct ast_string_field_mgr *mgr
result = (*pool_head)->base + mgr->used;
mgr->used += needed;
+ mgr->last_alloc = result;
return result;
}
+int __ast_string_field_ptr_grow(struct ast_string_field_mgr *mgr, size_t needed,
+ const ast_string_field *ptr)
+{
+ int grow = needed - (strlen(*ptr) + 1);
+ size_t space = mgr->size - mgr->used;
+
+ if (grow <= 0) {
+ return 0;
+ }
+
+ if (*ptr != mgr->last_alloc) {
+ return 1;
+ }
+
+ if (space < grow) {
+ return 1;
+ }
+
+ mgr->used += grow;
+
+ return 0;
+}
+
__attribute((format (printf, 4, 0)))
void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head,
- const ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
+ struct ast_string_field_pool **pool_head,
+ const ast_string_field *ptr, const char *format, va_list ap1, va_list ap2)
{
size_t needed;
char *dst = (*pool_head)->base + mgr->used;
- const char **p = (const char **)ptr;
+ const char **p = (const char **) ptr;
size_t space = mgr->size - mgr->used;
/* try to write using available space */
@@ -1424,14 +1455,14 @@ void __ast_string_field_ptr_build_va(struct ast_string_field_mgr *mgr,
vsprintf(dst, format, ap2);
}
- *p = dst;
+ mgr->last_alloc = *p = dst;
mgr->used += needed;
}
__attribute((format (printf, 4, 5)))
void __ast_string_field_ptr_build(struct ast_string_field_mgr *mgr,
- struct ast_string_field_pool **pool_head,
- const ast_string_field *ptr, const char *format, ...)
+ struct ast_string_field_pool **pool_head,
+ const ast_string_field *ptr, const char *format, ...)
{
va_list ap1, ap2;