uwx_str.c revision 121642
1115013Smarcel/*
2121642SmarcelCopyright (c) 2003 Hewlett-Packard Development Company, L.P.
3121642SmarcelPermission is hereby granted, free of charge, to any person
4121642Smarcelobtaining a copy of this software and associated documentation
5121642Smarcelfiles (the "Software"), to deal in the Software without
6121642Smarcelrestriction, including without limitation the rights to use,
7121642Smarcelcopy, modify, merge, publish, distribute, sublicense, and/or sell
8121642Smarcelcopies of the Software, and to permit persons to whom the
9121642SmarcelSoftware is furnished to do so, subject to the following
10121642Smarcelconditions:
11115013Smarcel
12121642SmarcelThe above copyright notice and this permission notice shall be
13121642Smarcelincluded in all copies or substantial portions of the Software.
14121642Smarcel
15121642SmarcelTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16121642SmarcelEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17121642SmarcelOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18121642SmarcelNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19121642SmarcelHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20121642SmarcelWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21121642SmarcelFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22121642SmarcelOTHER DEALINGS IN THE SOFTWARE.
23121642Smarcel*/
24121642Smarcel
25115013Smarcel#include "uwx_env.h"
26115013Smarcel#include "uwx_str.h"
27115013Smarcel
28115013Smarcel#ifdef _KERNEL
29115013Smarcel#define	free(p)		/* nullified */
30115013Smarcel#define	malloc(sz)	NULL
31115013Smarcel#endif
32115013Smarcel
33115013Smarcel/*
34115013Smarcel *  uwx_str.c
35115013Smarcel *
36115013Smarcel *  This file contains the routines for maintaining a string
37115013Smarcel *  pool for the unwind environment. We preallocate enough
38115013Smarcel *  space for most purposes so that no memory allocation is
39115013Smarcel *  necessary during a normal unwind. If we do need more,
40115013Smarcel *  we use the allocate callback, if one is provided.
41115013Smarcel *
42115013Smarcel *  The string pool is reused with each call to step(),
43115013Smarcel *  and is completely freed when the unwind environment is
44115013Smarcel *  freed.
45115013Smarcel */
46115013Smarcel
47115013Smarcel
48115013Smarcelint uwx_init_str_pool(struct uwx_env *env)
49115013Smarcel{
50115013Smarcel    if (env->allocate_cb == 0)
51115013Smarcel	env->string_pool = (struct uwx_str_pool *)
52115013Smarcel		malloc(sizeof(struct uwx_str_pool));
53115013Smarcel    else
54115013Smarcel	env->string_pool = (struct uwx_str_pool *)
55115013Smarcel		(*env->allocate_cb)(sizeof(struct uwx_str_pool));
56115013Smarcel
57115013Smarcel    if (env->string_pool == 0)
58115013Smarcel	return UWX_ERR_NOMEM;
59115013Smarcel
60115013Smarcel    env->string_pool->next = 0;
61115013Smarcel    env->string_pool->size = STRPOOLSIZE;
62115013Smarcel    env->string_pool->used = 0;
63115013Smarcel
64115013Smarcel    return UWX_OK;
65115013Smarcel}
66115013Smarcel
67115013Smarcelvoid uwx_free_str_pool(struct uwx_env *env)
68115013Smarcel{
69115013Smarcel    struct uwx_str_pool *pool;
70115013Smarcel    struct uwx_str_pool *next;
71115013Smarcel
72115013Smarcel    for (pool = env->string_pool; pool != 0; pool = next) {
73115013Smarcel	next = pool->next;
74115013Smarcel	if (env->free_cb == 0)
75115013Smarcel	    free(pool);
76115013Smarcel	else
77115013Smarcel	    (*env->free_cb)(pool);
78115013Smarcel    }
79115013Smarcel}
80115013Smarcel
81115013Smarcelchar *uwx_alloc_str(struct uwx_env *env, char *str)
82115013Smarcel{
83115013Smarcel    int len;
84115013Smarcel    int size;
85115013Smarcel    struct uwx_str_pool *pool;
86115013Smarcel    struct uwx_str_pool *prev;
87115013Smarcel    char *p;
88115013Smarcel
89115013Smarcel    len = strlen(str) + 1;
90115013Smarcel    prev = 0;
91115013Smarcel    for (pool = env->string_pool; pool != 0; pool = pool->next) {
92115013Smarcel	prev = pool;
93115013Smarcel	if (pool->size - pool->used >= len)
94115013Smarcel	    break;
95115013Smarcel    }
96115013Smarcel    if (pool == 0) {
97115013Smarcel	size = STRPOOLSIZE;
98115013Smarcel	if (len > size)
99115013Smarcel	    size = len;
100115013Smarcel	size += sizeof(struct uwx_str_pool) - STRPOOLSIZE;
101115013Smarcel	if (env->allocate_cb == 0)
102115013Smarcel	    pool = (struct uwx_str_pool *) malloc(size);
103115013Smarcel	else
104115013Smarcel	    pool = (struct uwx_str_pool *) (*env->allocate_cb)(size);
105115013Smarcel	if (env->string_pool == 0)
106115013Smarcel	    return 0;
107115013Smarcel	pool->next = 0;
108115013Smarcel	pool->size = size;
109115013Smarcel	pool->used = 0;
110115013Smarcel	prev->next = pool;
111115013Smarcel    }
112115013Smarcel    p = pool->pool + pool->used;
113115013Smarcel    strcpy(p, str);
114115013Smarcel    pool->used += len;
115115013Smarcel    return p;
116115013Smarcel}
117115013Smarcel
118115013Smarcelvoid uwx_reset_str_pool(struct uwx_env *env)
119115013Smarcel{
120115013Smarcel    struct uwx_str_pool *pool;
121115013Smarcel
122115013Smarcel    for (pool = env->string_pool; pool != 0; pool = pool->next)
123115013Smarcel	pool->used = 0;
124115013Smarcel}
125