/* --------------------------------------------------------------------- */ /* BEGIN COPYRIGHT AND LICENSE NOTICE */ /* --------------------------------------------------------------------- */ /* ** Copyright (c) 2008, 2009 by Richard Harter. ** ** Permission is hereby granted, free of charge, to any person ** obtaining a copy of this software and associated documentation ** files , to deal in the Software without ** restriction, including without limitation the rights to use, ** copy, modify, merge, publish, distribute, sublicense, and/or ** sell copies of the Software, and to permit persons to whom the ** Software is furnished to do so, subject to the following ** conditions: ** ** The above copyright notice and this permission notice shall be ** included in copies of this software and in copies of substantial ** portions, whether or not the software has been modified. ** ** Derivative works shall include a notice that the software is a ** modified version of the copyrighted software. ** ** There is no guarantee that this software is useful for anything ** or that it is any way correct or of value. The author disclaims ** any responsibility for the consequences of using this software. ** */ /* --------------------------------------------------------------------- */ /* END COPYRIGHT AND LICENSE NOTICE */ /* --------------------------------------------------------------------- */ #include #include "trace.h" #include "errmgr.h" #include "getspace.h" #include "stgpool.h" #define DEFAULT_SIZE 96 #define NABITS 3 #define ASIZE (1<pos + rsz; if (newpos >= pool->sp_lim) { newpool = getspace(sizeof(stgpool_s),LINELOC); *newpool = *pool; newpos = rsz; pool->link = newpool; pool->pos = 0; pool->sp_lim += pool->sp_lim/2; if (rsz > pool->sp_lim) pool->sp_lim = rsz; pool->space = getspace(pool->sp_lim,loc); } loc_ptr = pool->pos; pool->pos = newpos; return pool->space + loc_ptr; } stgpool_s * stgpool_open(void) { stgpool_s * pool; trace("stgpool_open"); pool = getspace(sizeof(stgpool_s),LINELOC); pool->link = 0; pool->pos = 0; pool->sp_lim = DEFAULT_SIZE; pool->space = getspace(pool->sp_lim,LINELOC); poolcount++; return pool; } void stgpool_close(stgpool_s * pool) { stgpool_s * next; trace("stgpool_close"); while (pool) { next = pool->link; freespace(pool->space,LINELOC); freespace(pool,LINELOC); pool = next; } if (flag) poolcount--; } void stgpool_clear(stgpool_s * pool) { trace("stgpool_clear"); if (!pool) return; flag = 0; if (pool->link) stgpool_close(pool->link); flag = 1; pool->pos = 0; pool->link = 0; } void stgpool_stats() { printf("# of pools = %ld\n",poolcount); } void * exparray(void *a, size_t *a_sz, size_t d_sz,stgpool_s *pool, char * loc) { size_t keep; size_t newb; size_t sz; trace("exparray"); sz = *a_sz; keep = sz*d_sz; if (sz <4) sz = 4; else if (sz < 32) sz *= 2; else sz += sz/2; newb = sz*d_sz; *a_sz = sz; if (!pool) a = morespace(a,keep,newb,loc); else { char * temp; temp = a; a = stgpool_get(pool,newb,loc); if (keep) memcpy((char *)a,temp,keep); } return a; }