Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / dstr / memory_pool.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /*
24  * $XConsortium: memory_pool.cc /main/4 1996/07/18 14:29:52 drk $
25  *
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
30  * OR DISCLOSURE.
31  * 
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
36  * INTERNATIONAL, LTD.
37  * 
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.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "dstr/memory_pool.h"
52
53 #define NUM_CHUNKS 50
54
55 #ifdef C_API
56 memory_pool* g_memory_pool_ptr = 0;
57 #endif
58
59 ///////////////////////////////////////////////////
60 //
61 //
62 ///////////////////////////////////////////////////
63
64 int g_carrier = 0;
65
66 chunk_carrier::chunk_carrier(int chk_sz, int chks ) :
67 chunk_sz(ll4(chk_sz)), max_chunks(chks)
68 {
69 /*
70 debug(cerr, chunk_sz);
71 debug(cerr, max_chunks);
72 debug(cerr, int(this));
73 */
74
75    alloc_sz = max_chunks * (chunk_sz + sizeof(chunk_manage_record_t));
76 g_carrier += alloc_sz;
77    carrier_ptr = new char[alloc_sz];
78 }
79
80 chunk_carrier::~chunk_carrier()
81 {
82    delete carrier_ptr;
83 }
84
85 dlist* chunk_carrier::init_ptrs()
86 {
87    dlist* w = new dlist;
88
89    int offset = 0;
90    for ( int i=0; i<max_chunks; i++ ) {
91
92       chunk_manage_record_t* x = 
93         (chunk_manage_record_t*)(carrier_ptr + offset);
94
95       x -> chunk_carrier_ptr = this;
96
97       w -> insert_as_tail(x);
98
99       offset += (chunk_sz + sizeof(chunk_manage_record_t));
100    }
101
102    return w;
103 }
104
105
106 ///////////////////////////////////////////////////
107 //
108 //
109 ///////////////////////////////////////////////////
110
111 fix_chunk_pool::fix_chunk_pool(int x) : chunk_sz(x), chunks(1)
112 {
113    init_one_chunk_carrier();
114 }
115
116 void fix_chunk_pool::init_one_chunk_carrier()
117 {
118    chunk_carrier* x = new chunk_carrier(chunk_sz, NUM_CHUNKS);
119
120    chunk_carrier_list.insert_as_tail(new dlist_void_ptr_cell(x));
121
122    dlist* z = x -> init_ptrs() ;
123
124    free_chunk_list.append( z );
125
126    delete z;
127 }
128
129 fix_chunk_pool::~fix_chunk_pool()
130 {
131    free_chunk_list.empty_list();
132
133    long x = chunk_carrier_list.first();
134
135    while ( x ) {
136       chunk_carrier *y = (chunk_carrier*)
137               (((dlist_void_ptr_cell*)x) -> void_ptr());
138       delete y;
139       chunk_carrier_list.next(x); 
140    }
141
142 //debug(cerr, chunk_sz);
143 //debug(cerr, chunks);
144 }
145
146 char* fix_chunk_pool::alloc()
147 {
148    if ( free_chunk_list.count() == 0 ) {
149 //chunks++;
150       init_one_chunk_carrier();
151    }
152
153    chunk_manage_record_t *x = 
154       (chunk_manage_record_t*)free_chunk_list.get_head();
155
156    free_chunk_list.delete_cell(x);
157
158    return (char*)( ((char*)x) + sizeof(chunk_manage_record_t) );
159 }
160
161 void fix_chunk_pool::free(char* x)
162 {
163 /*
164    chunk_manage_record_t* chk_mng_ptr = 
165       (chunk_manage_record_t*)
166          (x - sizeof(chunk_manage_record_t));
167 */
168
169    free_chunk_list.insert_as_tail(
170       (chunk_manage_record_t*)
171          (x - sizeof(chunk_manage_record_t))
172    );
173
174
175 //   chunk_carrier* z = chk_mng_ptr -> chunk_carrier_ptr;
176 //
177 //
178 //   int delta = x -  z -> carrier_ptr;
179 //
180 //   if ( INRANGE(delta, 0, z -> alloc_sz) )
181 ///*
182 //   if ( ((char*)chk_mng_ptr - z -> carrier_ptr) % 
183 //        (z -> chunk_sz + sizeof(chunk_manage_record_t)) 
184 //           == 
185 //        0 
186 //      )
187 //*/
188 //     free_chunk_list.insert_as_tail(chk_mng_ptr);
189 //   else {
190 //     MESSAGE(cerr, "fix_chunk_pool::free(): bad ptr");
191 //     debug(cerr, int(x));
192 ///*
193 //     debug(cerr, int((char*)chk_mng_ptr - z -> carrier_ptr));
194 //     debug(cerr, z ->chunk_sz + sizeof(chunk_manage_record_t));
195 //*/
196 //     debug(cerr, delta);
197 //     debug(cerr, int(z -> carrier_ptr));
198 //     abort();
199 //   }
200 }
201
202
203 ///////////////////////////////////////////////////
204 //
205 //
206 ///////////////////////////////////////////////////
207
208 memory_pool::memory_pool(int x) : 
209 max_alloc_size_from_pool(x), vm_pool_vector(MAX_CHUNK_SZ)
210 {
211 //MESSAGE(cerr, "memory_pool cstr");
212 //debug(cerr, int(this));
213 }
214
215 int g_memory_size = 0;
216
217 memory_pool::~memory_pool()
218 {
219    for ( int i=4; i<MAX_CHUNK_SZ; i++ ) {
220       delete (fix_chunk_pool*)vm_pool_vector[i];
221    }
222 //cerr << "g_memory_size = " << g_memory_size << "\n";
223 //cerr << "g_carrier= " << g_carrier << "\n";
224 }
225
226
227 char* memory_pool::alloc(size_t sz)
228 {
229 /*
230 MESSAGE(cerr, "=======");
231 debug(cerr, int(this));
232 debug(cerr, sz);
233 */
234
235 //g_memory_size += sz;
236
237    if ( INRANGE(sz, 4, MAX_CHUNK_SZ) ) {
238
239        fix_chunk_pool *x = 
240           (fix_chunk_pool*)vm_pool_vector[sz];
241
242        if ( x == 0 ) {
243 //MESSAGE(cerr, "calling new fix_chunk_pool");
244           x = new fix_chunk_pool(sz);
245           vm_pool_vector.insert(x, sz);
246        }
247        
248        return x -> alloc();
249       
250    } else {
251        return new char[sz];
252    }
253 //MESSAGE(cerr, "=======");
254 }
255
256 void memory_pool::free(char* str)
257 {
258    int sz = ((chunk_manage_record_t*)
259       (str - sizeof(chunk_manage_record_t))) ->
260           chunk_carrier_ptr -> chunk_sz;
261
262    if ( INRANGE(sz, 4, MAX_CHUNK_SZ) ) {
263
264        fix_chunk_pool* x = (fix_chunk_pool*)vm_pool_vector[sz];
265
266        if ( x == 0 ) {
267           debug(cerr, sz);
268           throw(stringException(
269                 "memory_pool::free(): fix_chunk_pool missing"
270                                )
271                );
272        } else
273           x -> free(str);
274       
275    } else {
276        delete str;
277    }
278 }
279