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 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
|
/* nailboard.c: NAILBOARD IMPLEMENTATION
*
* $Id$
* Copyright (c) 2014-2020 Ravenbrook Limited. See end of file for license.
*
* .sources: <design/nailboard>.
*/
#include "bt.h"
#include "check.h"
#include "mpm.h"
#include "nailboard.h"
SRCID(nailboard, "$Id$");
/* Log2 of scale factor between levels. <design/nailboard#.impl.scale>. */
#define LEVEL_SHIFT MPS_WORD_SHIFT
/* nailboardLevels -- return the number of levels in a nailboard with
* the given number of nails.
*
* <design/nailboard#.impl.table.last>
*/
static Count nailboardLevels(Count nails)
{
return (SizeFloorLog2((Size)nails) + LEVEL_SHIFT) / LEVEL_SHIFT;
}
/* nailboardNails -- return the total number of nails in the board */
static Count nailboardNails(Nailboard board)
{
return RangeSize(&board->range) >> board->alignShift;
}
/* nailboardLevelBits -- return the number of bits in the bit table
* for the given level.
*/
static Count nailboardLevelBits(Count nails, Index level)
{
Shift shift = (Shift)(level * LEVEL_SHIFT);
return (nails + ((Count)1 << shift) - 1) >> shift;
}
Bool NailboardCheck(Nailboard board)
{
Index i;
Count nails;
CHECKS(Nailboard, board);
CHECKL(RangeCheck(&board->range));
CHECKL(0 < board->levels);
nails = nailboardNails(board);
CHECKL(board->levels == nailboardLevels(nails));
CHECKL(nails == nailboardLevelBits(nails, 0));
CHECKL(nailboardLevelBits(nails, board->levels - 1) != 0);
CHECKL(nailboardLevelBits(nails, board->levels) == 1);
CHECKL(BoolCheck(board->newNails));
for (i = 0; i < board->levels; ++i) {
CHECKL(board->level[i] != NULL);
}
return TRUE;
}
/* nailboardStructSize -- return the size of the nailboard structure,
* plus the array of pointers to levels.
*/
static Size nailboardStructSize(Count levels)
{
return offsetof(NailboardStruct, level) + sizeof(BT *) * levels;
}
/* nailboardSize -- return the total size of the nailboard
*
* This is the size of the nailboard structure plus the combined sizes
* of the bit tables.
*/
static Size nailboardSize(Count nails, Count levels)
{
Index i;
Size size;
size = nailboardStructSize(levels);
for (i = 0; i < levels; ++i) {
size += BTSize(nailboardLevelBits(nails, i));
}
return size;
}
/* NailboardCreate -- allocate a nailboard
*
* Allocate a nailboard in the control pool for arena, to cover the
* range of addresses from base to limit (which must be non-empty). If
* successful, set *boardReturn to point to the nailboard and return
* ResOK. Otherwise, return a result code to indicate failure.
*
* alignment specifies the granularity of the nails: that is, the
* number of bytes covered by each nail.
*/
Res NailboardCreate(Nailboard *boardReturn, Arena arena, Align alignment,
Addr base, Addr limit)
{
void *p;
Nailboard board;
Shift alignShift;
Count nails, levels;
Index i;
Res res;
AVER(boardReturn != NULL);
AVERT(Arena, arena);
AVERT(Align, alignment);
AVER(base < limit);
AVER(AddrIsAligned(base, alignment));
AVER(AddrIsAligned(limit, alignment));
alignShift = SizeLog2((Size)alignment);
nails = AddrOffset(base, limit) >> alignShift;
levels = nailboardLevels(nails);
res = ControlAlloc(&p, arena, nailboardSize(nails, levels));
if (res != ResOK)
return res;
board = p;
RangeInit(&board->range, base, limit);
board->levels = levels;
board->alignShift = alignShift;
board->newNails = FALSE;
p = PointerAdd(p, nailboardStructSize(levels));
for (i = 0; i < levels; ++i) {
Count levelBits = nailboardLevelBits(nails, i);
AVER(levelBits > 0);
board->level[i] = p;
BTResRange(board->level[i], 0, levelBits);
p = PointerAdd(p, BTSize(levelBits));
}
board->sig = NailboardSig;
AVERT(Nailboard, board);
*boardReturn = board;
return ResOK;
}
/* NailboardDestroy -- destroy a nailboard */
void NailboardDestroy(Nailboard board, Arena arena)
{
Count nails;
Size size;
AVERT(Nailboard, board);
AVERT(Arena, arena);
nails = nailboardNails(board);
size = nailboardSize(nails, board->levels);
board->sig = SigInvalid;
ControlFree(arena, board, size);
}
/* NailboardClearNewNails -- clear the "new nails" flag */
void (NailboardClearNewNails)(Nailboard board)
{
AVERT(Nailboard, board);
NailboardClearNewNails(board);
}
/* NailboardNewNails -- return the "new nails" flag
*
* Return TRUE if any new nails have been set in the nailboard since
* the last call to NailboardClearNewNails (or since the nailboard was
* created, if there have never been any such calls), FALSE otherwise.
*/
Bool (NailboardNewNails)(Nailboard board)
{
AVERT(Nailboard, board);
return NailboardNewNails(board);
}
/* nailboardIndex -- return the index of the nail corresponding to
* addr in the given level.
*/
static Index nailboardIndex(Nailboard board, Index level, Addr addr)
{
Index i = AddrOffset(RangeBase(&board->range), addr)
>> (board->alignShift + level * LEVEL_SHIFT);
AVER_CRITICAL(i < nailboardLevelBits(nailboardNails(board), level));
return i;
}
/* nailboardAddr -- return the address corresponding to the index in
* the given level.
*/
static Addr nailboardAddr(Nailboard board, Index level, Index index)
{
return AddrAdd(RangeBase(&board->range),
index << (board->alignShift + level * LEVEL_SHIFT));
}
/* nailboardIndexRange -- update *ibaseReturn and *ilimitReturn to be
* the interval of indexes of nails in the given level, corresponding
* to the interval of addresses base and limit. See
* <design/nailboard/#.impl.isresrange.alignment>.
*/
static void nailboardIndexRange(Index *ibaseReturn, Index *ilimitReturn,
Nailboard board, Index level,
Addr base, Addr limit)
{
*ibaseReturn = nailboardIndex(board, level, base);
*ilimitReturn = nailboardIndex(board, level, AddrSub(limit, 1)) + 1;
}
/* NailboardGet -- return nail corresponding to address
*
* Return the nail in the nailboard corresponding to the address addr.
* It is an error if addr does not lie in the range of addresses
* covered by the nailboard.
*/
Bool NailboardGet(Nailboard board, Addr addr)
{
AVERT(Nailboard, board);
AVER(RangeContains(&board->range, addr));
return BTGet(board->level[0], nailboardIndex(board, 0, addr));
}
/* NailboardSet -- set nail corresponding to address
*
* Set the nail in the nailboard corresponding to the address addr.
* Return the old nail at that position. It is an error if addr does
* not lie in the range of addresses covered by the nailboard.
*
* This function is on the critical path because it is called for
* every fix of an ambiguous reference to an address in an AMC pool.
*/
Bool NailboardSet(Nailboard board, Addr addr)
{
Index i, j;
AVERT_CRITICAL(Nailboard, board);
AVER_CRITICAL(RangeContains(&board->range, addr));
j = nailboardIndex(board, 0, addr);
if (BTGet(board->level[0], j))
return TRUE;
board->newNails = TRUE;
BTSet(board->level[0], j);
for (i = 1; i < board->levels; ++i) {
j = nailboardIndex(board, i, addr);
if (BTGet(board->level[i], j))
break;
BTSet(board->level[i], j);
}
return FALSE;
}
/* NailboardSetRange -- set all nails in range
*
* Set all nails in the nailboard corresponding to the range between
* base and limit. It is an error if any part of the range is not
* covered by the nailboard, or if any nail in the range is set.
*/
void NailboardSetRange(Nailboard board, Addr base, Addr limit)
{
Index i, ibase, ilimit;
nailboardIndexRange(&ibase, &ilimit, board, 0, base, limit);
AVER(BTIsResRange(board->level[0], ibase, ilimit));
BTSetRange(board->level[0], ibase, ilimit);
for (i = 1; i < board->levels; ++i) {
nailboardIndexRange(&ibase, &ilimit, board, i, base, limit);
BTSetRange(board->level[i], ibase, ilimit);
}
}
/* NailboardIsSetRange -- test if all nails are set in a range
*
* Return TRUE if all nails are set in the range between base and
* limit, or FALSE if any nail is unset. It is an error if any part of
* the range is not covered by the nailboard.
*
* This function is not expected to be efficient because it is only
* used in an AVER in AMCWhiten to check that the unused part of the
* buffer for a nailboarded segment has in fact been nailed.
*/
Bool NailboardIsSetRange(Nailboard board, Addr base, Addr limit)
{
Index ibase, ilimit;
AVERT(Nailboard, board);
nailboardIndexRange(&ibase, &ilimit, board, 0, base, limit);
return BTIsSetRange(board->level[0], ibase, ilimit);
}
/* NailboardIsResRange -- test if all nails are reset in a range
*
* Return TRUE if no nails are set in the range between base and
* limit, or FALSE if any nail is set. It is an error if any part of
* the range is not covered by the nailboard.
*
* This function is on the critical path as it is called for every
* object in every nailed segment. It must take time that is no more
* than logarithmic in the size of the range.
*
* <design/nailboard#.impl.isresrange>.
*/
Bool NailboardIsResRange(Nailboard board, Addr base, Addr limit)
{
Index i, ibase, ilimit;
Index j, jbase, jlimit;
Addr leftLimit, rightBase;
AVERT_CRITICAL(Nailboard, board);
/* Descend levels until ibase and ilimit are two or more bits apart:
* that is, until there is an "inner" part to the range. */
i = board->levels;
do {
-- i;
nailboardIndexRange(&ibase, &ilimit, board, i, base, limit);
if (BTIsResRange(board->level[i], ibase, ilimit))
/* The entire range was clear. This is expected to be the common
* case. <design/nailboard#.impl.isresrange.empty> */
return TRUE;
if (i == 0)
/* At level 0 there is only one nail per bit so the set bit is known
* to be within the range. <design/nailboard#.impl.isresrange.level0> */
return FALSE;
} while (ibase + 1 >= ilimit - 1);
/* At this point we know there is an "inner" part. Are there any
* bits set in it? <design/nailboard#impl.isresrange.inner> */
if (!BTIsResRange(board->level[i], ibase + 1, ilimit - 1))
return FALSE;
/* At this point we know that in level i, there is is a bit set at
* ibase or at ilimit - 1 (or both), and everything between them is
* reset. */
AVER_CRITICAL(BTGet(board->level[i], ibase)
|| BTGet(board->level[i], ilimit - 1));
/* Left splinter */
j = i;
jbase = ibase;
for (;;) {
leftLimit = nailboardAddr(board, j, jbase + 1);
AVER_CRITICAL(base < leftLimit);
AVER_CRITICAL(leftLimit < limit);
-- j;
nailboardIndexRange(&jbase, &jlimit, board, j, base, leftLimit);
if (jbase + 1 < jlimit && !BTIsResRange(board->level[j], jbase + 1, jlimit))
return FALSE; /* <design/nailboard#.impl.isresrange.inner> */
if (!BTGet(board->level[j], jbase))
break;
if (j == 0)
return FALSE;
}
/* Right splinter */
j = i;
jlimit = ilimit;
for (;;) {
rightBase = nailboardAddr(board, j, jlimit - 1);
AVER_CRITICAL(base < rightBase);
AVER_CRITICAL(rightBase < limit);
-- j;
nailboardIndexRange(&jbase, &jlimit, board, j, rightBase, limit);
if (jbase < jlimit - 1 && !BTIsResRange(board->level[j], jbase, jlimit - 1))
return FALSE; /* <design/nailboard#.impl.isresrange.inner> */
if (!BTGet(board->level[j], jlimit - 1))
break;
if (j == 0)
return FALSE;
}
return TRUE;
}
Res NailboardDescribe(Nailboard board, mps_lib_FILE *stream, Count depth)
{
Index i, j;
Res res;
if (!TESTT(Nailboard, board))
return ResFAIL;
if (stream == NULL)
return ResFAIL;
res = WriteF(stream, depth,
"Nailboard $P {\n", (WriteFP)board,
" levels: $U\n", (WriteFU)board->levels,
" newNails: $S\n", WriteFYesNo(board->newNails),
" alignShift: $U\n", (WriteFU)board->alignShift,
NULL);
if (res != ResOK)
return res;
res = RangeDescribe(&board->range, stream, depth + 2);
if (res != ResOK)
return res;
for(i = 0; i < board->levels; ++i) {
Count levelNails = nailboardLevelBits(nailboardNails(board), i);
Count resetNails = BTCountResRange(board->level[i], 0, levelNails);
res = WriteF(stream, depth + 2, "Level $U ($U bits, $U set): ",
(WriteFU)i, (WriteFU)levelNails,
(WriteFU)(levelNails - resetNails),
NULL);
if (res != ResOK)
return res;
for (j = 0; j < levelNails; ++j) {
char c = BTGet(board->level[i], j) ? '*' : '.';
res = WriteF(stream, 0, "$C", (WriteFC)c, NULL);
if (res != ResOK)
return res;
}
res = WriteF(stream, 0, "\n", NULL);
if (res != ResOK)
return res;
}
res = WriteF(stream, depth, "} Nailboard $P\n", (WriteFP)board, NULL);
if (res != ResOK)
return res;
return ResOK;
}
/* C. COPYRIGHT AND LICENSE
*
* Copyright (C) 2014-2020 Ravenbrook Limited <https://www.ravenbrook.com/>.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
|