upload http
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr-util / test / testrmm.c
1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
2  * applicable.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "apr_shm.h"
18 #include "apr_rmm.h"
19 #include "apr_errno.h"
20 #include "apr_general.h"
21 #include "apr_lib.h"
22 #include "apr_strings.h"
23 #include "apr_time.h"
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #if APR_HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #if APR_HAS_SHARED_MEMORY
32
33 #define FRAG_SIZE 80
34 #define FRAG_COUNT 10
35 #define SHARED_SIZE (apr_size_t)(FRAG_SIZE * FRAG_COUNT * sizeof(char*))
36
37 static apr_status_t test_rmm(apr_pool_t *parpool)
38 {
39     apr_status_t rv;
40     apr_pool_t *pool;
41     apr_shm_t *shm;
42     apr_rmm_t *rmm;
43     apr_size_t size, fragsize;
44     apr_rmm_off_t *off;
45     int i;
46     void *entity;
47
48     rv = apr_pool_create(&pool, parpool);
49     if (rv != APR_SUCCESS) {
50         fprintf(stderr, "Error creating child pool\n");
51         return rv;
52     }
53
54     /* We're going to want 10 blocks of data from our target rmm. */
55     size = SHARED_SIZE + apr_rmm_overhead_get(FRAG_COUNT + 1);
56     printf("Creating anonymous shared memory (%"
57            APR_SIZE_T_FMT " bytes).....", size); 
58     rv = apr_shm_create(&shm, size, NULL, pool);
59     if (rv != APR_SUCCESS) { 
60         fprintf(stderr, "Error allocating shared memory block\n");
61         return rv;
62     }
63     fprintf(stdout, "OK\n");
64
65     printf("Creating rmm segment.............................");
66     rv = apr_rmm_init(&rmm, NULL, apr_shm_baseaddr_get(shm), size,
67                       pool);
68
69     if (rv != APR_SUCCESS) {
70         fprintf(stderr, "Error allocating rmm..............\n");
71         return rv;
72     }
73     fprintf(stdout, "OK\n");
74
75     fragsize = SHARED_SIZE / FRAG_COUNT;
76     printf("Creating each fragment of size %" APR_SIZE_T_FMT "................",
77            fragsize);
78     off = apr_palloc(pool, FRAG_COUNT * sizeof(apr_rmm_off_t));
79     for (i = 0; i < FRAG_COUNT; i++) {
80         off[i] = apr_rmm_malloc(rmm, fragsize);
81     } 
82     fprintf(stdout, "OK\n");
83     
84     printf("Checking for out of memory allocation............");
85     if (apr_rmm_malloc(rmm, FRAG_SIZE * FRAG_COUNT) == 0) {
86         fprintf(stdout, "OK\n");
87     }
88     else {
89         return APR_EGENERAL;  
90     }
91
92     printf("Checking each fragment for address alignment.....");
93     for (i = 0; i < FRAG_COUNT; i++) {
94         char *c = apr_rmm_addr_get(rmm, off[i]);
95         apr_size_t sc = (apr_size_t)c;
96
97         if (off[i] == 0) {
98             printf("allocation failed for offset %d\n", i);
99             return APR_ENOMEM;
100         }
101
102         if (sc & 7) {
103             printf("Bad alignment for fragment %d; %p not %p!\n",
104                    i, c, (void *)APR_ALIGN_DEFAULT((apr_size_t)c));
105             return APR_EGENERAL;
106         }
107     }
108     fprintf(stdout, "OK\n");   
109     
110     printf("Setting each fragment to a unique value..........");
111     for (i = 0; i < FRAG_COUNT; i++) {
112         int j;
113         char **c = apr_rmm_addr_get(rmm, off[i]);
114         for (j = 0; j < FRAG_SIZE; j++, c++) {
115             *c = apr_itoa(pool, i + j);
116         }
117     } 
118     fprintf(stdout, "OK\n");
119
120     printf("Checking each fragment for its unique value......");
121     for (i = 0; i < FRAG_COUNT; i++) {
122         int j;
123         char **c = apr_rmm_addr_get(rmm, off[i]);
124         for (j = 0; j < FRAG_SIZE; j++, c++) {
125             char *d = apr_itoa(pool, i + j);
126             if (strcmp(*c, d) != 0) {
127                 return APR_EGENERAL;
128             }
129         }
130     } 
131     fprintf(stdout, "OK\n");
132
133     printf("Freeing each fragment............................");
134     for (i = 0; i < FRAG_COUNT; i++) {
135         rv = apr_rmm_free(rmm, off[i]);
136         if (rv != APR_SUCCESS) {
137             return rv;
138         }
139     } 
140     fprintf(stdout, "OK\n");
141
142     printf("Creating one large segment.......................");
143     off[0] = apr_rmm_calloc(rmm, SHARED_SIZE);
144     fprintf(stdout, "OK\n");
145
146     printf("Setting large segment............................");
147     for (i = 0; i < FRAG_COUNT * FRAG_SIZE; i++) {
148         char **c = apr_rmm_addr_get(rmm, off[0]);
149         c[i] = apr_itoa(pool, i);
150     }
151     fprintf(stdout, "OK\n");
152
153     printf("Freeing large segment............................");
154     apr_rmm_free(rmm, off[0]);
155     fprintf(stdout, "OK\n");
156
157     printf("Creating each fragment of size %" APR_SIZE_T_FMT " (again)........",
158            fragsize);
159     for (i = 0; i < FRAG_COUNT; i++) {
160         off[i] = apr_rmm_malloc(rmm, fragsize);
161     } 
162     fprintf(stdout, "OK\n");
163
164     printf("Freeing each fragment backwards..................");
165     for (i = FRAG_COUNT - 1; i >= 0; i--) {
166         rv = apr_rmm_free(rmm, off[i]);
167         if (rv != APR_SUCCESS) {
168             return rv;
169         }
170     } 
171     fprintf(stdout, "OK\n");
172
173     printf("Creating one large segment (again)...............");
174     off[0] = apr_rmm_calloc(rmm, SHARED_SIZE);
175     fprintf(stdout, "OK\n");
176
177     printf("Freeing large segment............................");
178     apr_rmm_free(rmm, off[0]);
179     fprintf(stdout, "OK\n");
180
181     printf("Checking realloc.................................");
182     off[0] = apr_rmm_calloc(rmm, SHARED_SIZE - 100);
183     off[1] = apr_rmm_calloc(rmm, 100);
184     if (off[0] == 0 || off[1] == 0) {
185         printf("FAILED\n");
186         return APR_EINVAL;
187     }
188     entity = apr_rmm_addr_get(rmm, off[1]);
189     rv = apr_rmm_free(rmm, off[0]);
190     if (rv != APR_SUCCESS) {
191         printf("FAILED\n");
192         return rv;
193     }
194
195     {
196         unsigned char *c = entity;
197
198         /* Fill in the region; the first half with zereos, which will
199          * likely catch the apr_rmm_realloc offset calculation bug by
200          * making it think the old region was zero length. */
201         for (i = 0; i < 100; i++) {
202             c[i] = (i < 50) ? 0 : i;
203         }
204     }
205
206     /* now we can realloc off[1] and get many more bytes */
207     off[0] = apr_rmm_realloc(rmm, entity, SHARED_SIZE - 100);
208     if (off[0] == 0) {
209         printf("FAILED\n");
210         return APR_EINVAL;
211     }
212
213     {
214         unsigned char *c = apr_rmm_addr_get(rmm, off[0]);
215
216         /* fill in the region */
217         for (i = 0; i < 100; i++) {
218             if (c[i] != (i < 50 ? 0 : i)) {
219                 printf("FAILED at offset %d: %hx\n", i, c[i]);
220                 return APR_EGENERAL;
221             }
222         }
223     }
224
225     fprintf(stdout, "OK\n");
226
227     printf("Destroying rmm segment...........................");
228     rv = apr_rmm_destroy(rmm);
229     if (rv != APR_SUCCESS) {
230         printf("FAILED\n");
231         return rv;
232     }
233     printf("OK\n");
234
235     printf("Destroying shared memory segment.................");
236     rv = apr_shm_destroy(shm);
237     if (rv != APR_SUCCESS) {
238         printf("FAILED\n");
239         return rv;
240     }
241     printf("OK\n");
242
243     apr_pool_destroy(pool);
244
245     return APR_SUCCESS;
246 }
247
248
249 int main(void)
250 {
251     apr_status_t rv;
252     apr_pool_t *pool;
253     char errmsg[200];
254
255     apr_initialize();
256     
257     printf("APR RMM Memory Test\n");
258     printf("======================\n\n");
259
260     printf("Initializing the pool............................"); 
261     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
262         printf("could not initialize pool\n");
263         exit(-1);
264     }
265     printf("OK\n");
266
267     rv = test_rmm(pool);
268     if (rv != APR_SUCCESS) {
269         printf("Anonymous shared memory test FAILED: [%d] %s\n",
270                rv, apr_strerror(rv, errmsg, sizeof(errmsg)));
271         exit(-2);
272     }
273     printf("RMM test passed!\n");
274
275     return 0;
276 }
277
278 #else /* APR_HAS_SHARED_MEMORY */
279 #error shmem is not supported on this platform
280 #endif /* APR_HAS_SHARED_MEMORY */
281