1 /* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "apr_errno.h"
20 #include "apr_general.h"
22 #include "apr_strings.h"
31 #if APR_HAS_SHARED_MEMORY
35 #define SHARED_SIZE (apr_size_t)(FRAG_SIZE * FRAG_COUNT * sizeof(char*))
37 static apr_status_t
test_rmm(apr_pool_t
*parpool
)
43 apr_size_t size
, fragsize
;
48 rv
= apr_pool_create(&pool
, parpool
);
49 if (rv
!= APR_SUCCESS
) {
50 fprintf(stderr
, "Error creating child pool\n");
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");
63 fprintf(stdout
, "OK\n");
65 printf("Creating rmm segment.............................");
66 rv
= apr_rmm_init(&rmm
, NULL
, apr_shm_baseaddr_get(shm
), size
,
69 if (rv
!= APR_SUCCESS
) {
70 fprintf(stderr
, "Error allocating rmm..............\n");
73 fprintf(stdout
, "OK\n");
75 fragsize
= SHARED_SIZE
/ FRAG_COUNT
;
76 printf("Creating each fragment of size %" APR_SIZE_T_FMT
"................",
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
);
82 fprintf(stdout
, "OK\n");
84 printf("Checking for out of memory allocation............");
85 if (apr_rmm_malloc(rmm
, FRAG_SIZE
* FRAG_COUNT
) == 0) {
86 fprintf(stdout
, "OK\n");
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
;
98 printf("allocation failed for offset %d\n", i
);
103 printf("Bad alignment for fragment %d; %p not %p!\n",
104 i
, c
, (void *)APR_ALIGN_DEFAULT((apr_size_t
)c
));
108 fprintf(stdout
, "OK\n");
110 printf("Setting each fragment to a unique value..........");
111 for (i
= 0; i
< FRAG_COUNT
; i
++) {
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
);
118 fprintf(stdout
, "OK\n");
120 printf("Checking each fragment for its unique value......");
121 for (i
= 0; i
< FRAG_COUNT
; i
++) {
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) {
131 fprintf(stdout
, "OK\n");
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
) {
140 fprintf(stdout
, "OK\n");
142 printf("Creating one large segment.......................");
143 off
[0] = apr_rmm_calloc(rmm
, SHARED_SIZE
);
144 fprintf(stdout
, "OK\n");
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
);
151 fprintf(stdout
, "OK\n");
153 printf("Freeing large segment............................");
154 apr_rmm_free(rmm
, off
[0]);
155 fprintf(stdout
, "OK\n");
157 printf("Creating each fragment of size %" APR_SIZE_T_FMT
" (again)........",
159 for (i
= 0; i
< FRAG_COUNT
; i
++) {
160 off
[i
] = apr_rmm_malloc(rmm
, fragsize
);
162 fprintf(stdout
, "OK\n");
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
) {
171 fprintf(stdout
, "OK\n");
173 printf("Creating one large segment (again)...............");
174 off
[0] = apr_rmm_calloc(rmm
, SHARED_SIZE
);
175 fprintf(stdout
, "OK\n");
177 printf("Freeing large segment............................");
178 apr_rmm_free(rmm
, off
[0]);
179 fprintf(stdout
, "OK\n");
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) {
188 entity
= apr_rmm_addr_get(rmm
, off
[1]);
189 rv
= apr_rmm_free(rmm
, off
[0]);
190 if (rv
!= APR_SUCCESS
) {
196 unsigned char *c
= entity
;
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
;
206 /* now we can realloc off[1] and get many more bytes */
207 off
[0] = apr_rmm_realloc(rmm
, entity
, SHARED_SIZE
- 100);
214 unsigned char *c
= apr_rmm_addr_get(rmm
, off
[0]);
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
]);
225 fprintf(stdout
, "OK\n");
227 printf("Destroying rmm segment...........................");
228 rv
= apr_rmm_destroy(rmm
);
229 if (rv
!= APR_SUCCESS
) {
235 printf("Destroying shared memory segment.................");
236 rv
= apr_shm_destroy(shm
);
237 if (rv
!= APR_SUCCESS
) {
243 apr_pool_destroy(pool
);
257 printf("APR RMM Memory Test\n");
258 printf("======================\n\n");
260 printf("Initializing the pool............................");
261 if (apr_pool_create(&pool
, NULL
) != APR_SUCCESS
) {
262 printf("could not initialize pool\n");
268 if (rv
!= APR_SUCCESS
) {
269 printf("Anonymous shared memory test FAILED: [%d] %s\n",
270 rv
, apr_strerror(rv
, errmsg
, sizeof(errmsg
)));
273 printf("RMM test passed!\n");
278 #else /* APR_HAS_SHARED_MEMORY */
279 #error shmem is not supported on this platform
280 #endif /* APR_HAS_SHARED_MEMORY */