uwx_str.c revision 115013
1254721Semaste/*
2254721Semaste * Copyright (c) 2002,2003 Hewlett-Packard Company
3254721Semaste *
4254721Semaste * Permission is hereby granted, free of charge, to any person obtaining a
5254721Semaste * copy of this software and associated documentation files (the "Software"),
6254721Semaste * to deal in the Software without restriction, including without limitation
7254721Semaste * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8254721Semaste * and/or sell copies of the Software, and to permit persons to whom the
9254721Semaste * Software is furnished to do so, subject to the following conditions:
10254721Semaste *
11254721Semaste * The above copyright notice and this permission notice shall be included
12254721Semaste * in all copies or substantial portions of the Software.
13254721Semaste *
14254721Semaste * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15254721Semaste * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16276479Sdim * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17276479Sdim * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18254721Semaste * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19254721Semaste * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20254721Semaste * DEALINGS IN THE SOFTWARE.
21254721Semaste */
22254721Semaste
23254721Semaste#include "uwx_env.h"
24254721Semaste#include "uwx_str.h"
25254721Semaste
26254721Semaste#ifdef _KERNEL
27254721Semaste#define	free(p)		/* nullified */
28254721Semaste#define	malloc(sz)	NULL
29254721Semaste#endif
30254721Semaste
31254721Semaste/*
32254721Semaste *  uwx_str.c
33254721Semaste *
34254721Semaste *  This file contains the routines for maintaining a string
35254721Semaste *  pool for the unwind environment. We preallocate enough
36254721Semaste *  space for most purposes so that no memory allocation is
37254721Semaste *  necessary during a normal unwind. If we do need more,
38254721Semaste *  we use the allocate callback, if one is provided.
39254721Semaste *
40254721Semaste *  The string pool is reused with each call to step(),
41254721Semaste *  and is completely freed when the unwind environment is
42254721Semaste *  freed.
43254721Semaste */
44254721Semaste
45254721Semaste
46254721Semasteint uwx_init_str_pool(struct uwx_env *env)
47254721Semaste{
48254721Semaste    if (env->allocate_cb == 0)
49254721Semaste	env->string_pool = (struct uwx_str_pool *)
50254721Semaste		malloc(sizeof(struct uwx_str_pool));
51254721Semaste    else
52254721Semaste	env->string_pool = (struct uwx_str_pool *)
53254721Semaste		(*env->allocate_cb)(sizeof(struct uwx_str_pool));
54254721Semaste
55276479Sdim    if (env->string_pool == 0)
56254721Semaste	return UWX_ERR_NOMEM;
57276479Sdim
58276479Sdim    env->string_pool->next = 0;
59254721Semaste    env->string_pool->size = STRPOOLSIZE;
60276479Sdim    env->string_pool->used = 0;
61254721Semaste
62254721Semaste    return UWX_OK;
63254721Semaste}
64254721Semaste
65254721Semastevoid uwx_free_str_pool(struct uwx_env *env)
66254721Semaste{
67254721Semaste    struct uwx_str_pool *pool;
68254721Semaste    struct uwx_str_pool *next;
69254721Semaste
70276479Sdim    for (pool = env->string_pool; pool != 0; pool = next) {
71254721Semaste	next = pool->next;
72254721Semaste	if (env->free_cb == 0)
73254721Semaste	    free(pool);
74254721Semaste	else
75254721Semaste	    (*env->free_cb)(pool);
76254721Semaste    }
77254721Semaste}
78276479Sdim
79254721Semastechar *uwx_alloc_str(struct uwx_env *env, char *str)
80254721Semaste{
81254721Semaste    int len;
82254721Semaste    int size;
83254721Semaste    struct uwx_str_pool *pool;
84254721Semaste    struct uwx_str_pool *prev;
85254721Semaste    char *p;
86254721Semaste
87254721Semaste    len = strlen(str) + 1;
88254721Semaste    prev = 0;
89254721Semaste    for (pool = env->string_pool; pool != 0; pool = pool->next) {
90254721Semaste	prev = pool;
91254721Semaste	if (pool->size - pool->used >= len)
92254721Semaste	    break;
93254721Semaste    }
94254721Semaste    if (pool == 0) {
95254721Semaste	size = STRPOOLSIZE;
96254721Semaste	if (len > size)
97276479Sdim	    size = len;
98254721Semaste	size += sizeof(struct uwx_str_pool) - STRPOOLSIZE;
99254721Semaste	if (env->allocate_cb == 0)
100254721Semaste	    pool = (struct uwx_str_pool *) malloc(size);
101254721Semaste	else
102254721Semaste	    pool = (struct uwx_str_pool *) (*env->allocate_cb)(size);
103254721Semaste	if (env->string_pool == 0)
104	    return 0;
105	pool->next = 0;
106	pool->size = size;
107	pool->used = 0;
108	prev->next = pool;
109    }
110    p = pool->pool + pool->used;
111    strcpy(p, str);
112    pool->used += len;
113    return p;
114}
115
116void uwx_reset_str_pool(struct uwx_env *env)
117{
118    struct uwx_str_pool *pool;
119
120    for (pool = env->string_pool; pool != 0; pool = pool->next)
121	pool->used = 0;
122}
123