2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
24 * $XConsortium: memory_pool.cc /main/4 1996/07/18 14:29:52 drk $
26 * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27 * All rights reserved. Unpublished -- rights reserved under
28 * the Copyright Laws of the United States. USE OF A COPYRIGHT
29 * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
32 * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33 * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD. USE,
34 * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35 * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
38 * RESTRICTED RIGHTS LEGEND
39 * Use, duplication, or disclosure by the Government is subject
40 * to the restrictions as set forth in subparagraph (c)(l)(ii)
41 * of the Rights in Technical Data and Computer Software clause
42 * at DFARS 252.227-7013.
44 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
51 #include "dstr/memory_pool.h"
56 memory_pool* g_memory_pool_ptr = 0;
59 ///////////////////////////////////////////////////
62 ///////////////////////////////////////////////////
66 chunk_carrier::chunk_carrier(int chk_sz, int chks ) :
67 chunk_sz(ll4(chk_sz)), max_chunks(chks)
70 debug(cerr, chunk_sz);
71 debug(cerr, max_chunks);
72 debug(cerr, int(this));
75 alloc_sz = max_chunks * (chunk_sz + sizeof(chunk_manage_record_t));
76 g_carrier += alloc_sz;
77 carrier_ptr = new char[alloc_sz];
80 chunk_carrier::~chunk_carrier()
85 dlist* chunk_carrier::init_ptrs()
90 for ( int i=0; i<max_chunks; i++ ) {
92 chunk_manage_record_t* x =
93 (chunk_manage_record_t*)(carrier_ptr + offset);
95 x -> chunk_carrier_ptr = this;
97 w -> insert_as_tail(x);
99 offset += (chunk_sz + sizeof(chunk_manage_record_t));
106 ///////////////////////////////////////////////////
109 ///////////////////////////////////////////////////
111 fix_chunk_pool::fix_chunk_pool(int x) : chunk_sz(x), chunks(1)
113 init_one_chunk_carrier();
116 void fix_chunk_pool::init_one_chunk_carrier()
118 chunk_carrier* x = new chunk_carrier(chunk_sz, NUM_CHUNKS);
120 chunk_carrier_list.insert_as_tail(new dlist_void_ptr_cell(x));
122 dlist* z = x -> init_ptrs() ;
124 free_chunk_list.append( z );
129 fix_chunk_pool::~fix_chunk_pool()
131 free_chunk_list.empty_list();
133 long x = chunk_carrier_list.first();
136 chunk_carrier *y = (chunk_carrier*)
137 (((dlist_void_ptr_cell*)x) -> void_ptr());
139 chunk_carrier_list.next(x);
142 //debug(cerr, chunk_sz);
143 //debug(cerr, chunks);
146 char* fix_chunk_pool::alloc()
148 if ( free_chunk_list.count() == 0 ) {
150 init_one_chunk_carrier();
153 chunk_manage_record_t *x =
154 (chunk_manage_record_t*)free_chunk_list.get_head();
156 free_chunk_list.delete_cell(x);
158 return (char*)( ((char*)x) + sizeof(chunk_manage_record_t) );
161 void fix_chunk_pool::free(char* x)
164 chunk_manage_record_t* chk_mng_ptr =
165 (chunk_manage_record_t*)
166 (x - sizeof(chunk_manage_record_t));
169 free_chunk_list.insert_as_tail(
170 (chunk_manage_record_t*)
171 (x - sizeof(chunk_manage_record_t))
175 // chunk_carrier* z = chk_mng_ptr -> chunk_carrier_ptr;
178 // int delta = x - z -> carrier_ptr;
180 // if ( INRANGE(delta, 0, z -> alloc_sz) )
182 // if ( ((char*)chk_mng_ptr - z -> carrier_ptr) %
183 // (z -> chunk_sz + sizeof(chunk_manage_record_t))
188 // free_chunk_list.insert_as_tail(chk_mng_ptr);
190 // MESSAGE(cerr, "fix_chunk_pool::free(): bad ptr");
191 // debug(cerr, int(x));
193 // debug(cerr, int((char*)chk_mng_ptr - z -> carrier_ptr));
194 // debug(cerr, z ->chunk_sz + sizeof(chunk_manage_record_t));
196 // debug(cerr, delta);
197 // debug(cerr, int(z -> carrier_ptr));
203 ///////////////////////////////////////////////////
206 ///////////////////////////////////////////////////
208 memory_pool::memory_pool(int x) :
209 max_alloc_size_from_pool(x), vm_pool_vector(MAX_CHUNK_SZ)
211 //MESSAGE(cerr, "memory_pool cstr");
212 //debug(cerr, int(this));
215 int g_memory_size = 0;
217 memory_pool::~memory_pool()
219 for ( int i=4; i<MAX_CHUNK_SZ; i++ ) {
220 delete (fix_chunk_pool*)vm_pool_vector[i];
222 //cerr << "g_memory_size = " << g_memory_size << "\n";
223 //cerr << "g_carrier= " << g_carrier << "\n";
227 char* memory_pool::alloc(size_t sz)
230 MESSAGE(cerr, "=======");
231 debug(cerr, int(this));
235 //g_memory_size += sz;
237 if ( INRANGE(sz, 4, MAX_CHUNK_SZ) ) {
240 (fix_chunk_pool*)vm_pool_vector[sz];
243 //MESSAGE(cerr, "calling new fix_chunk_pool");
244 x = new fix_chunk_pool(sz);
245 vm_pool_vector.insert(x, sz);
253 //MESSAGE(cerr, "=======");
256 void memory_pool::free(char* str)
258 int sz = ((chunk_manage_record_t*)
259 (str - sizeof(chunk_manage_record_t))) ->
260 chunk_carrier_ptr -> chunk_sz;
262 if ( INRANGE(sz, 4, MAX_CHUNK_SZ) ) {
264 fix_chunk_pool* x = (fix_chunk_pool*)vm_pool_vector[sz];
268 throw(stringException(
269 "memory_pool::free(): fix_chunk_pool missing"