1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. 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"
27 #if APR_HAS_SHARED_MEMORY
31 #define SHARED_SIZE (apr_size_t)(FRAG_SIZE * FRAG_COUNT * sizeof(char*))
33 static void test_rmm(abts_case
*tc
, void *data
)
39 apr_size_t size
, fragsize
;
40 apr_rmm_off_t
*off
, off2
;
44 rv
= apr_pool_create(&pool
, p
);
45 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
47 /* We're going to want 10 blocks of data from our target rmm. */
48 size
= SHARED_SIZE
+ apr_rmm_overhead_get(FRAG_COUNT
+ 1);
49 rv
= apr_shm_create(&shm
, size
, NULL
, pool
);
50 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
52 if (rv
!= APR_SUCCESS
)
55 rv
= apr_rmm_init(&rmm
, NULL
, apr_shm_baseaddr_get(shm
), size
, pool
);
56 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
58 if (rv
!= APR_SUCCESS
)
61 /* Creating each fragment of size fragsize */
62 fragsize
= SHARED_SIZE
/ FRAG_COUNT
;
63 off
= apr_palloc(pool
, FRAG_COUNT
* sizeof(apr_rmm_off_t
));
64 for (i
= 0; i
< FRAG_COUNT
; i
++) {
65 off
[i
] = apr_rmm_malloc(rmm
, fragsize
);
68 /* Checking for out of memory allocation */
69 off2
= apr_rmm_malloc(rmm
, FRAG_SIZE
* FRAG_COUNT
);
72 /* Checking each fragment for address alignment */
73 for (i
= 0; i
< FRAG_COUNT
; i
++) {
74 char *c
= apr_rmm_addr_get(rmm
, off
[i
]);
75 apr_size_t sc
= (apr_size_t
)c
;
77 ABTS_TRUE(tc
, !!off
[i
]);
78 ABTS_TRUE(tc
, !(sc
& 7));
81 /* Setting each fragment to a unique value */
82 for (i
= 0; i
< FRAG_COUNT
; i
++) {
84 char **c
= apr_rmm_addr_get(rmm
, off
[i
]);
85 for (j
= 0; j
< FRAG_SIZE
; j
++, c
++) {
86 *c
= apr_itoa(pool
, i
+ j
);
90 /* Checking each fragment for its unique value */
91 for (i
= 0; i
< FRAG_COUNT
; i
++) {
93 char **c
= apr_rmm_addr_get(rmm
, off
[i
]);
94 for (j
= 0; j
< FRAG_SIZE
; j
++, c
++) {
95 char *d
= apr_itoa(pool
, i
+ j
);
96 ABTS_STR_EQUAL(tc
, d
, *c
);
100 /* Freeing each fragment */
101 for (i
= 0; i
< FRAG_COUNT
; i
++) {
102 rv
= apr_rmm_free(rmm
, off
[i
]);
103 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
106 /* Creating one large segment */
107 off
[0] = apr_rmm_calloc(rmm
, SHARED_SIZE
);
109 /* Setting large segment */
110 for (i
= 0; i
< FRAG_COUNT
* FRAG_SIZE
; i
++) {
111 char **c
= apr_rmm_addr_get(rmm
, off
[0]);
112 c
[i
] = apr_itoa(pool
, i
);
115 /* Freeing large segment */
116 rv
= apr_rmm_free(rmm
, off
[0]);
117 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
119 /* Creating each fragment of size fragsize */
120 for (i
= 0; i
< FRAG_COUNT
; i
++) {
121 off
[i
] = apr_rmm_malloc(rmm
, fragsize
);
124 /* Freeing each fragment backwards */
125 for (i
= FRAG_COUNT
- 1; i
>= 0; i
--) {
126 rv
= apr_rmm_free(rmm
, off
[i
]);
127 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
130 /* Creating one large segment (again) */
131 off
[0] = apr_rmm_calloc(rmm
, SHARED_SIZE
);
133 /* Freeing large segment */
134 rv
= apr_rmm_free(rmm
, off
[0]);
135 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
137 /* Checking realloc */
138 off
[0] = apr_rmm_calloc(rmm
, SHARED_SIZE
- 100);
139 off
[1] = apr_rmm_calloc(rmm
, 100);
140 ABTS_TRUE(tc
, !!off
[0]);
141 ABTS_TRUE(tc
, !!off
[1]);
143 entity
= apr_rmm_addr_get(rmm
, off
[1]);
144 rv
= apr_rmm_free(rmm
, off
[0]);
145 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
148 unsigned char *c
= entity
;
150 /* Fill in the region; the first half with zereos, which will
151 * likely catch the apr_rmm_realloc offset calculation bug by
152 * making it think the old region was zero length. */
153 for (i
= 0; i
< 100; i
++) {
154 c
[i
] = (i
< 50) ? 0 : i
;
158 /* now we can realloc off[1] and get many more bytes */
159 off
[0] = apr_rmm_realloc(rmm
, entity
, SHARED_SIZE
- 100);
160 ABTS_TRUE(tc
, !!off
[0]);
163 unsigned char *c
= apr_rmm_addr_get(rmm
, off
[0]);
165 /* fill in the region */
166 for (i
= 0; i
< 100; i
++) {
167 ABTS_TRUE(tc
, c
[i
] == (i
< 50 ? 0 : i
));
171 rv
= apr_rmm_destroy(rmm
);
172 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
174 rv
= apr_shm_destroy(shm
);
175 ABTS_INT_EQUAL(tc
, APR_SUCCESS
, rv
);
177 apr_pool_destroy(pool
);
180 #endif /* APR_HAS_SHARED_MEMORY */
182 abts_suite
*testrmm(abts_suite
*suite
)
184 suite
= ADD_SUITE(suite
);
186 #if APR_HAS_SHARED_MEMORY
187 abts_run_test(suite
, test_rmm
, NULL
);