Add GNU LGPL headers to all .c .C and .h files
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / storage / 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: pool.cc /main/3 1996/06/11 17:34:18 cde-hal $
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 "storage/memory_pool.h"
52 #include "utility/xtime.h"
53
54 #define NUMS 100000
55 #define SIZE 20
56
57 pool_test1()
58 {
59 MESSAGE(cerr, "pool alloc/free test1");
60    memory_pool x;
61
62    char* ptrs[NUMS];
63
64    for ( int i=0; i<NUMS; i++ ) {
65        ptrs[i] = x.alloc(SIZE);
66    }
67
68 MESSAGE(cerr, "alloc done!");
69   
70    for ( i=0; i<NUMS; i++ ) {
71        x.free(ptrs[i]);
72    }
73 MESSAGE(cerr, "free done!");
74 }
75
76 pool_test2()
77 {
78 MESSAGE(cerr, "pool alloc/free test2");
79    memory_pool x;
80
81    char* ptrs[100];
82
83    for ( int i=0; i<NUMS/100; i++ ) {
84
85       for ( int j=0; j<100; j++ ) {
86           ptrs[j] = x.alloc(SIZE);
87       }
88
89
90       for ( j=0; j<100; j++ ) {
91           x.free(ptrs[j]);
92       }
93    }
94
95 }
96
97
98 new_delete_test1()
99 {
100 MESSAGE(cerr, "new/delete test1");
101    char* ptrs[NUMS];
102
103    for ( int i=0; i<NUMS; i++ ) {
104        ptrs[i] = new char[SIZE];
105    }
106
107 MESSAGE(cerr, "alloc done!");
108  
109    for ( i=NUMS-1; i>=0; i-- ) {
110        delete ptrs[i];
111    }
112 MESSAGE(cerr, "free done!");
113 }
114
115 new_delete_test2()
116 {
117 MESSAGE(cerr, "new/delete test2");
118    char* ptrs[100];
119
120    for ( int i=0; i<NUMS/100; i++ ) {
121
122       for ( int j=0; j<100; j++ ) {
123           ptrs[j] = new char[SIZE];
124       }
125
126
127       for ( j=0; j<100; j++ ) {
128           delete ptrs[j];
129       }
130    }
131 }
132
133
134 main( int argc, char** argv )
135 {
136    assert ( argc == 2 );
137
138    int ok;
139    float cpul
140    long elap;
141    xtime tm;
142
143    tm.snap_shot();
144
145    if ( strcmp(argv[1], "-pool1") == 0 ) {
146       pool_test1();
147       ok = 0;
148    } else
149    if ( strcmp(argv[1], "-pool2") == 0 ) {
150       pool_test2();
151       ok = 0;
152    } else
153    if ( strcmp(argv[1], "-new1") == 0 ) {
154       new_delete_test1();
155       ok = 0;
156    } else
157    if ( strcmp(argv[1], "-new2") == 0 ) {
158       new_delete_test2();
159       ok = 0;
160    }
161    else {
162       MESSAGE(cerr, "apply a param: -pool[1|2] or -new[1|2]");
163       ok = -1;
164    }
165
166    tm.duration(cpu, elap);
167    debug(cerr, cpu);
168    debug(cerr, elap);
169
170    return ok;
171 }