1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
/* ``Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* The Initial Developer of the Original Code is Ericsson Utvecklings AB.
* Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
* AB. All Rights Reserved.''
*
* $Id$
*/
#include "testcase_driver.h"
#include "allocator_test.h"
#include <string.h>
#define CEILING(X, U) ((((X)+(U)-1)/(U))*(U))
void
check_ablk(TestCaseState_t *tcs, Allctr_t *a, void *ptr, Ulong umem_sz)
{
Ulong unit_sz = UNIT_SZ;
Block_t *blk = UMEM2BLK(ptr);
Block_t *nxt_blk = NXT_BLK(blk);
Ulong real_sz = ((Ulong) nxt_blk) - ((Ulong) (blk));
ASSERT(tcs, real_sz == BLK_SZ(blk));
ASSERT(tcs, !IS_FREE_BLK(blk));
ASSERT(tcs, real_sz >= CEILING(ABLK_HDR_SZ + umem_sz, unit_sz));
if (real_sz > MIN_BLK_SZ(a)
&& real_sz > CEILING(ABLK_HDR_SZ+umem_sz, unit_sz)) {
ASSERT(tcs,
real_sz <= CEILING(MIN_BLK_SZ(a)+ABLK_HDR_SZ+umem_sz,
unit_sz));
ASSERT(tcs, IS_LAST_BLK(blk) || !IS_FREE_BLK(nxt_blk));
}
}
void
setup_sequence(TestCaseState_t *tcs, Allctr_t *a, Ulong bsz, int no,
void *res[])
{
Carrier_t *c;
Block_t *blk;
int i;
testcase_printf(tcs,
"Setting up a sequence of %d blocks of size %lu\n",
no, bsz);
c = FIRST_MBC(a);
ASSERT(tcs, !NEXT_C(c));
blk = MBC_TO_FIRST_BLK(a, c);
ASSERT(tcs, IS_LAST_BLK(blk));
for (i = 0; i < no; i++)
res[i] = ALLOC(a, bsz);
for (i = 0; i < no; i++)
ASSERT(tcs, res[i]);
testcase_printf(tcs, "Checking that sequence was set up as expected\n");
for (i = 1; i < no; i++)
ASSERT(tcs, NXT_BLK(UMEM2BLK(res[i-1])) == UMEM2BLK(res[i]));
blk = NXT_BLK(UMEM2BLK(res[no-1]));
ASSERT(tcs, IS_LAST_BLK(blk));
testcase_printf(tcs, "Sequence ok\n");
/* If we fail in setup_sequence(), it doesn't mean that something is
wrong. It is just a faulty assumption in setup_sequence() about
how blocks are going to be placed.
Fix setup_sequence()... */
}
static void
test_free(TestCaseState_t *tcs, Allctr_t *a, Ulong bsz)
{
Block_t *blk;
void *p[7];
testcase_printf(tcs," --- Testing free() with block size %lu ---\n",bsz);
setup_sequence(tcs, a, bsz, 7, p);
check_ablk(tcs, a, p[0], bsz);
check_ablk(tcs, a, p[1], bsz);
check_ablk(tcs, a, p[2], bsz);
check_ablk(tcs, a, p[3], bsz);
check_ablk(tcs, a, p[4], bsz);
check_ablk(tcs, a, p[5], bsz);
check_ablk(tcs, a, p[6], bsz);
/* Coalescing with previous block */
FREE(a, p[2]);
FREE(a, p[3]);
blk = NXT_BLK(UMEM2BLK(p[1]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[4]));
/* Coalescing with next block */
FREE(a, p[1]);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[4]));
/* Coalescing with next and previous block */
FREE(a, p[5]);
FREE(a, p[4]);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[6]));
/* Cleanup */
FREE(a, p[0]);
FREE(a, p[6]);
testcase_printf(tcs," --- free() with block size %lu succeded ---\n",bsz);
}
static void
test_realloc(TestCaseState_t *tcs, Allctr_t *a, Ulong bsz)
{
Block_t *blk;
void *ptr;
void *p[3];
Ulong nbsz;
testcase_printf(tcs," --- Testing realloc() with block size %lu ---\n",
bsz);
setup_sequence(tcs, a, bsz, 3, p);
check_ablk(tcs, a, p[0], bsz);
check_ablk(tcs, a, p[1], bsz);
check_ablk(tcs, a, p[2], bsz);
/* Grow to the end of the carrier */
blk = NXT_BLK(UMEM2BLK(p[2]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, IS_LAST_BLK(blk));
nbsz = bsz + BLK_SZ(blk);
ptr = REALLOC(a, p[2], nbsz);
ASSERT(tcs, p[2] == ptr);
check_ablk(tcs, a, p[2], nbsz);
blk = UMEM2BLK(p[2]);
ASSERT(tcs, IS_LAST_BLK(blk));
/* Shrink from the end of the carrier */
ptr = REALLOC(a, p[2], bsz);
ASSERT(tcs, p[2] == ptr);
blk = UMEM2BLK(p[2]);
ASSERT(tcs, !IS_LAST_BLK(blk));
blk = NXT_BLK(blk);
ASSERT(tcs, IS_LAST_BLK(blk));
check_ablk(tcs, a, p[2], bsz);
/* Shrink and coalecse with next free */
FREE(a, p[1]);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
nbsz = bsz/2;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[2]));
/* Grow into next free; but leave free block at end */
nbsz *= 3;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[2]));
/* Grow upto next alloced block by allocating just enough so that no
free block fits between them */
nbsz = BLK_SZ(blk) + UMEM_SZ(UMEM2BLK(p[0]));
nbsz -= MIN_BLK_SZ(a) - 1;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, !IS_FREE_BLK(blk));
ASSERT(tcs, blk == UMEM2BLK(p[2]));
/* Grow into unused part at end */
nbsz += MIN_BLK_SZ(a) - 1;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
ASSERT(tcs, !IS_FREE_BLK(blk));
ASSERT(tcs, blk == UMEM2BLK(p[2]));
/* Shrink *almost* as much so that a free block would fit between the
allocated blocks, and make sure that we don't get a free block
in between */
nbsz -= MIN_BLK_SZ(a) - 1;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, !IS_FREE_BLK(blk));
ASSERT(tcs, blk == UMEM2BLK(p[2]));
/* Shrink just as much so that a free block can fit between
the alloced blocks */
nbsz -= 1;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, blk < UMEM2BLK(p[2]));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[2]));
/* Shrink so little that no free block would fit between allocated
blocks, and make sure that we shrink the allocated block and
coalesce the extra free part with the next free block. */
nbsz -= MIN_BLK_SZ(a) - 1;
ptr = REALLOC(a, p[0], nbsz);
ASSERT(tcs, p[0] == ptr);
check_ablk(tcs, a, p[0], nbsz);
blk = NXT_BLK(UMEM2BLK(p[0]));
ASSERT(tcs, IS_FREE_BLK(blk));
ASSERT(tcs, blk < UMEM2BLK(p[2]));
ASSERT(tcs, NXT_BLK(blk) == UMEM2BLK(p[2]));
/* Cleanup */
FREE(a, p[0]);
FREE(a, p[2]);
testcase_printf(tcs, " --- realloc() with block size %lu succeded ---\n",
bsz);
}
char *
testcase_name(void)
{
return "coalesce";
}
void
testcase_run(TestCaseState_t *tcs)
{
char *argv_org[] = {"-tsmbcs511","-tmmbcs511", "-tsbct512", "-trmbcmt100", "-tas", NULL, NULL};
char *alg[] = {"af", "gf", "bf", "aobf", "aoff", "aoffcbf", "aoffcaobf", NULL};
int i;
for (i = 0; alg[i]; i++) {
Ulong sz;
Allctr_t *a;
char *argv[sizeof(argv_org)/sizeof(argv_org[0])];
memcpy((void *) argv, (void *) argv_org, sizeof(argv_org));
argv[5] = alg[i];
testcase_printf(tcs, " *** Starting \"%s\" allocator *** \n", alg[i]);
a = START_ALC("coalesce_", 0, argv);
ASSERT(tcs, a);
tcs->extra = (void *) a;
sz = MIN_BLK_SZ(a) - ABLK_HDR_SZ;
test_free(tcs, a, sz);
sz += 1;
test_free(tcs, a, sz);
sz *= 4;
test_free(tcs, a, sz);
sz += 1;
test_free(tcs, a, sz);
sz *= 10;
test_free(tcs, a, sz);
sz = MIN_BLK_SZ(a)*4 - ABLK_HDR_SZ;
test_realloc(tcs, a, sz);
sz += 1;
test_realloc(tcs, a, sz);
sz *= 4;
test_realloc(tcs, a, sz);
sz += 1;
test_realloc(tcs, a, sz);
sz *= 10;
test_realloc(tcs, a, sz);
testcase_printf(tcs, " *** Stopping \"%s\" allocator *** \n", alg[i]);
STOP_ALC(a);
tcs->extra = NULL;
}
}
void
testcase_cleanup(TestCaseState_t *tcs)
{
if (tcs->extra)
STOP_ALC((Allctr_t *) tcs->extra);
}
ERL_NIF_INIT(coalesce, testcase_nif_funcs, testcase_nif_init,
NULL, NULL, NULL);
|