[0b990d] | 1 | //
|
---|
| 2 | // pooltest.cc
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
| 5 | //
|
---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
| 7 | // Maintainer: LPS
|
---|
| 8 | //
|
---|
| 9 | // This file is part of the SC Toolkit.
|
---|
| 10 | //
|
---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
| 12 | // it under the terms of the GNU Library General Public License as published by
|
---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
| 14 | // any later version.
|
---|
| 15 | //
|
---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU Library General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU Library General Public License
|
---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | //
|
---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
| 26 | //
|
---|
| 27 |
|
---|
| 28 | #include <stdlib.h>
|
---|
| 29 | #include <math.h>
|
---|
| 30 |
|
---|
| 31 | #include <util/group/pool.h>
|
---|
| 32 | #include <util/misc/formio.h>
|
---|
| 33 |
|
---|
| 34 | using namespace std;
|
---|
| 35 | using namespace sc;
|
---|
| 36 |
|
---|
| 37 | class Double {
|
---|
| 38 | private:
|
---|
| 39 | static Pool* pool_;
|
---|
| 40 | static Double* list;
|
---|
| 41 | Double* next;
|
---|
| 42 | Double* prev;
|
---|
| 43 | double* d;
|
---|
| 44 | int size;
|
---|
| 45 | static void zaplist(Double*);
|
---|
| 46 | public:
|
---|
| 47 | Double(size_t size);
|
---|
| 48 | ~Double();
|
---|
| 49 | void zap();
|
---|
| 50 | static void zapall();
|
---|
| 51 | void clear();
|
---|
| 52 | static void pool(Pool*);
|
---|
| 53 | };
|
---|
| 54 |
|
---|
| 55 | Double* Double::list = 0;
|
---|
| 56 | Pool* Double::pool_ = 0;
|
---|
| 57 |
|
---|
| 58 | void
|
---|
| 59 | Double::clear()
|
---|
| 60 | {
|
---|
| 61 | if (d) pool_->release(d);
|
---|
| 62 | d = 0;
|
---|
| 63 | size = 0;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | void
|
---|
| 67 | Double::zapall()
|
---|
| 68 | {
|
---|
| 69 | zaplist(list);
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | void
|
---|
| 73 | Double::zaplist(Double*l)
|
---|
| 74 | {
|
---|
| 75 | for (Double* i=l; i; i = i->next) {
|
---|
| 76 | i->zap();
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | Double::Double(size_t s):
|
---|
| 81 | size(s)
|
---|
| 82 | {
|
---|
| 83 | if (!pool_) {
|
---|
| 84 | cerr << scprintf("Double::Double: Pool not initialized\n");
|
---|
| 85 | abort();
|
---|
| 86 | }
|
---|
| 87 | d = pool_->allocate_double(size);
|
---|
| 88 | if (!d) {
|
---|
| 89 | //cout << scprintf("\nDouble::Double allocation of size %d failed\n",
|
---|
| 90 | // size);
|
---|
| 91 | cout << "F" << endl;
|
---|
| 92 | size = 0;
|
---|
| 93 | }
|
---|
| 94 | next = list;
|
---|
| 95 | prev = 0;
|
---|
| 96 | list = this;
|
---|
| 97 | if (next) next->prev = this;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | Double::~Double()
|
---|
| 101 | {
|
---|
| 102 | clear();
|
---|
| 103 | if (next) next->prev = prev;
|
---|
| 104 | if (prev) prev->next = next;
|
---|
| 105 | else list = next;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | void
|
---|
| 109 | Double::zap()
|
---|
| 110 | {
|
---|
| 111 | int i;
|
---|
| 112 | int* x = (int*)d;
|
---|
| 113 | for (i=0; i<size*2; i++) {
|
---|
| 114 | if (x[i] == PoolData::magic) {
|
---|
| 115 | cerr << scprintf("Double::zap: tried to zap a magic number\n");
|
---|
| 116 | abort();
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | for (i=0; i<size; i++) d[i] = 0.0;
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | void
|
---|
| 123 | Double::pool(Pool*p)
|
---|
| 124 | {
|
---|
| 125 | if (pool_ && list) {
|
---|
| 126 | cerr << scprintf("Double::pool: cannot reinitialize pool\n");
|
---|
| 127 | abort();
|
---|
| 128 | }
|
---|
| 129 | pool_ = p;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void test1(Pool*);
|
---|
| 133 | void test2(Pool*);
|
---|
| 134 |
|
---|
| 135 | int
|
---|
| 136 | main()
|
---|
| 137 | {
|
---|
| 138 | const int poolsize = 4000000;
|
---|
| 139 | Pool *pool = new(malloc(poolsize)) Pool(poolsize);
|
---|
| 140 |
|
---|
| 141 | Double::pool(pool);
|
---|
| 142 |
|
---|
| 143 | srand48(100);
|
---|
| 144 |
|
---|
| 145 | cout << "test1:" << endl;
|
---|
| 146 | test1(pool);
|
---|
| 147 | cout << "test2:" << endl;
|
---|
| 148 | test2(pool);
|
---|
| 149 |
|
---|
| 150 | return 0;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 |
|
---|
| 154 | void
|
---|
| 155 | test1(Pool*pool)
|
---|
| 156 | {
|
---|
| 157 | pool->check();
|
---|
| 158 |
|
---|
| 159 | pool->print();
|
---|
| 160 | cout << endl;
|
---|
| 161 |
|
---|
| 162 | Double t1(10);
|
---|
| 163 |
|
---|
| 164 | Double::zapall();
|
---|
| 165 |
|
---|
| 166 | pool->check();
|
---|
| 167 |
|
---|
| 168 | pool->print();
|
---|
| 169 | cout << endl;
|
---|
| 170 |
|
---|
| 171 | Double t2(10000);
|
---|
| 172 |
|
---|
| 173 | Double::zapall();
|
---|
| 174 |
|
---|
| 175 | pool->check();
|
---|
| 176 |
|
---|
| 177 | Double t3(100);
|
---|
| 178 |
|
---|
| 179 | Double::zapall();
|
---|
| 180 |
|
---|
| 181 | pool->check();
|
---|
| 182 |
|
---|
| 183 | pool->print();
|
---|
| 184 | cout << endl;
|
---|
| 185 |
|
---|
| 186 | Double::zapall();
|
---|
| 187 |
|
---|
| 188 | pool->check();
|
---|
| 189 |
|
---|
| 190 | pool->print();
|
---|
| 191 | cout << endl;
|
---|
| 192 |
|
---|
| 193 | t2.clear();
|
---|
| 194 |
|
---|
| 195 | pool->check();
|
---|
| 196 |
|
---|
| 197 | pool->print();
|
---|
| 198 | cout << endl;
|
---|
| 199 |
|
---|
| 200 | Double t4(100);
|
---|
| 201 |
|
---|
| 202 | pool->check();
|
---|
| 203 |
|
---|
| 204 | pool->print();
|
---|
| 205 | cout << endl;
|
---|
| 206 |
|
---|
| 207 | t1.clear();
|
---|
| 208 | t4.clear();
|
---|
| 209 | t3.clear();
|
---|
| 210 |
|
---|
| 211 | pool->check();
|
---|
| 212 |
|
---|
| 213 | pool->print();
|
---|
| 214 | cout << endl;
|
---|
| 215 |
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | void
|
---|
| 219 | test2(Pool*pool)
|
---|
| 220 | {
|
---|
| 221 | int i, ii;
|
---|
| 222 |
|
---|
| 223 | const int npass = 10;
|
---|
| 224 | const int nd = 512;
|
---|
| 225 | Double* d[nd];
|
---|
| 226 |
|
---|
| 227 | for (i=0; i<nd; i++) d[i] = 0;
|
---|
| 228 |
|
---|
| 229 | for (ii=0; ii<npass; ii++) {
|
---|
| 230 | for (i=0; i<nd;) {
|
---|
| 231 | if (mrand48() > 0) {
|
---|
| 232 | // allocate data
|
---|
| 233 | size_t size = lrand48() & 0x03ff;
|
---|
| 234 | d[i] = new Double(size);
|
---|
| 235 | cerr.flush();
|
---|
| 236 | cout << "a" << flush;
|
---|
| 237 | i++;
|
---|
| 238 | }
|
---|
| 239 | else {
|
---|
| 240 | // deallocate data
|
---|
| 241 | int loc = (int) (drand48()*i);
|
---|
| 242 | if (loc >= nd) loc = nd-1;
|
---|
| 243 | if (loc < 0) loc = 0;
|
---|
| 244 | if (d[loc]) {
|
---|
| 245 | cerr.flush();
|
---|
| 246 | cout << "d" << flush;
|
---|
| 247 | delete d[loc];
|
---|
| 248 | d[loc] = 0;
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 | //pool->print();
|
---|
| 252 | //pool->check();
|
---|
| 253 | //Double::zapall();
|
---|
| 254 | }
|
---|
| 255 | for (i=0; i<nd; i++) {
|
---|
| 256 | if (d[i]) {
|
---|
| 257 | cerr.flush();
|
---|
| 258 | cout << "d" << flush;
|
---|
| 259 | delete d[i];
|
---|
| 260 | d[i] = 0;
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 | cout << endl;
|
---|
| 264 | pool->print();
|
---|
| 265 | //pool->check();
|
---|
| 266 | //Double::zapall();
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 271 |
|
---|
| 272 | // Local Variables:
|
---|
| 273 | // mode: c++
|
---|
| 274 | // c-file-style: "CLJ"
|
---|
| 275 | // End:
|
---|