| [6d7779d] | 1 | /* pool.cpp: This file implements a simple pool of objects. |
|---|
| 2 | GNU/TOLBase4 Program |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2001, 2002, 2003 - Bayes Decisión, SL |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | This program is distributed in the hope that it will be useful, |
|---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | GNU General Public License for more details. |
|---|
| 15 | |
|---|
| 16 | You should have received a copy of the GNU General Public License |
|---|
| 17 | along with this program; if not, write to the Free Software |
|---|
| 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|---|
| 19 | USA. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | #include <tt_pool.h> |
|---|
| 23 | #include <tcl.h> |
|---|
| 24 | #include <string.h> |
|---|
| 25 | |
|---|
| 26 | ToltclPool::ToltclPool( int ini, int grow ) |
|---|
| 27 | { |
|---|
| 28 | m_last = -1; |
|---|
| 29 | if ( grow <= 0 ) |
|---|
| 30 | grow = 5; |
|---|
| 31 | if ( ini <= 0 ) { |
|---|
| 32 | ini = grow; |
|---|
| 33 | } |
|---|
| 34 | m_size = ini; |
|---|
| 35 | m_grow = grow; |
|---|
| 36 | m_pool = (void**)Tcl_Alloc( ini * sizeof(void*) ); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | ToltclPool::ToltclPool( const ToltclPool & pool ) |
|---|
| 40 | { |
|---|
| 41 | m_grow = pool.m_grow; |
|---|
| 42 | m_size = pool.m_size; |
|---|
| 43 | m_last = pool.m_last; |
|---|
| 44 | m_pool = (void**)Tcl_Alloc( m_size * sizeof(void*) ); |
|---|
| 45 | |
|---|
| 46 | /* only copy the void* used */ |
|---|
| 47 | |
|---|
| 48 | memcpy( m_pool, pool.m_pool, (m_last+1)*sizeof(void*) ); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | ToltclPool::~ToltclPool() |
|---|
| 52 | { |
|---|
| 53 | if ( m_pool ) |
|---|
| 54 | Tcl_Free( (char*)m_pool ); |
|---|
| 55 | m_size = 0; |
|---|
| 56 | m_last = -1; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | int ToltclPool::AppendObject( void * ptr ) { |
|---|
| 60 | ++m_last; |
|---|
| 61 | if ( m_last == m_size ) { |
|---|
| 62 | m_size += m_grow; |
|---|
| 63 | m_pool = (void**)Tcl_Realloc( (char*)m_pool, sizeof(void*)*m_size ); |
|---|
| 64 | } |
|---|
| 65 | m_pool[m_last] = ptr; |
|---|
| 66 | return m_last; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void ToltclPool::PrependObject( void * ptr ) { |
|---|
| 70 | ++m_last; |
|---|
| 71 | if ( m_last == m_size ) { |
|---|
| 72 | m_size += m_grow; |
|---|
| 73 | m_pool = (void**)Tcl_Realloc( (char*)m_pool, sizeof(void*)*m_size ); |
|---|
| 74 | } |
|---|
| 75 | if ( m_last ) { |
|---|
| 76 | /* make room for first ptr */ |
|---|
| 77 | memmove( m_pool+1, m_pool, sizeof(void*) * m_last ); |
|---|
| 78 | } |
|---|
| 79 | m_pool[0] = ptr; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | void * ToltclPool::_RemoveObject( int i ) |
|---|
| 83 | { |
|---|
| 84 | void * iObj = m_pool[i]; |
|---|
| 85 | int n = m_last - i; |
|---|
| 86 | |
|---|
| 87 | if ( n ) { |
|---|
| 88 | } |
|---|
| 89 | memmove( m_pool+i, m_pool+i+1, n * sizeof(m_pool[0]) ); |
|---|
| 90 | --m_last; |
|---|
| 91 | return iObj; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | void * ToltclPool::RemoveLast() |
|---|
| 95 | { |
|---|
| 96 | void * lastPtr = NULL; |
|---|
| 97 | |
|---|
| 98 | if ( m_last >= 0 ) { |
|---|
| 99 | lastPtr = m_pool[m_last]; |
|---|
| 100 | --m_last; |
|---|
| 101 | } |
|---|
| 102 | return lastPtr; |
|---|
| 103 | } |
|---|