1/* 2Copyright (c) 2003-2006 Hewlett-Packard Development Company, L.P. 3Permission is hereby granted, free of charge, to any person 4obtaining a copy of this software and associated documentation 5files (the "Software"), to deal in the Software without 6restriction, including without limitation the rights to use, 7copy, modify, merge, publish, distribute, sublicense, and/or sell 8copies of the Software, and to permit persons to whom the 9Software is furnished to do so, subject to the following 10conditions: 11 12The above copyright notice and this permission notice shall be 13included in all copies or substantial portions of the Software. 14 15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 17OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 18NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 19HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 20WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22OTHER DEALINGS IN THE SOFTWARE. 23*/ 24 25#ifndef _KERNEL 26#include <string.h> 27#endif 28 29#include "uwx_env.h" 30#include "uwx_str.h" 31 32#ifdef _KERNEL 33static struct uwx_str_pool uwx_str_pool; 34#define free(p) /* nullified */ 35#define malloc(sz) ((sz == sizeof(uwx_str_pool)) ? &uwx_str_pool : NULL) 36#endif 37 38/* 39 * uwx_str.c 40 * 41 * This file contains the routines for maintaining a string 42 * pool for the unwind environment. We preallocate enough 43 * space for most purposes so that no memory allocation is 44 * necessary during a normal unwind. If we do need more, 45 * we use the allocate callback, if one is provided. 46 * 47 * The string pool is reused with each call to step(), 48 * and is completely freed when the unwind environment is 49 * freed. 50 */ 51 52 53int uwx_init_str_pool(struct uwx_env *env, struct uwx_str_pool *pool) 54{ 55 if (pool == 0) 56 return UWX_ERR_NOMEM; 57 58 pool->next = 0; 59 pool->size = STRPOOLSIZE; 60 pool->used = 0; 61 62 env->string_pool = pool; 63 64 return UWX_OK; 65} 66 67void uwx_free_str_pool(struct uwx_env *env) 68{ 69 struct uwx_str_pool *pool; 70 struct uwx_str_pool *next; 71 72 /* The first pool is preallocated as part of the uwx_env. Don't free it! */ 73 pool = env->string_pool; 74 if (pool != 0) 75 pool = pool->next; 76 for (; pool != 0; pool = next) { 77 next = pool->next; 78 if (env->free_cb == 0) 79 free(pool); 80 else 81 (*env->free_cb)(pool); 82 } 83} 84 85char *uwx_alloc_str(struct uwx_env *env, char *str) 86{ 87 int len; 88 int size; 89 struct uwx_str_pool *pool; 90 struct uwx_str_pool *prev; 91 char *p; 92 93 len = strlen(str) + 1; 94 prev = 0; 95 for (pool = env->string_pool; pool != 0; pool = pool->next) { 96 prev = pool; 97 if (pool->size - pool->used >= len) 98 break; 99 } 100 if (pool == 0) { 101 size = STRPOOLSIZE; 102 if (len > size) 103 size = len; 104 size += sizeof(struct uwx_str_pool) - STRPOOLSIZE; 105 if (env->allocate_cb == 0) 106 pool = (struct uwx_str_pool *) malloc(size); 107 else 108 pool = (struct uwx_str_pool *) (*env->allocate_cb)(size); 109 if (env->string_pool == 0) 110 return 0; 111 pool->next = 0; 112 pool->size = size; 113 pool->used = 0; 114 prev->next = pool; 115 } 116 p = pool->pool + pool->used; 117 strcpy(p, str); 118 pool->used += len; 119 return p; 120} 121 122void uwx_reset_str_pool(struct uwx_env *env) 123{ 124 struct uwx_str_pool *pool; 125 126 for (pool = env->string_pool; pool != 0; pool = pool->next) 127 pool->used = 0; 128} 129