objmemory.cpp (58256B)
1 // 2 // objmemory.cpp 3 // Smalltalk-80 4 // 5 // Created by Dan Banay on 2/20/20. 6 // Copyright © 2020 Dan Banay. All rights reserved. 7 // 8 // MIT License 9 // Permission is hereby granted, free of charge, to any person obtaining a copy 10 // of this software and associated documentation files (the "Software"), to deal 11 // in the Software without restriction, including without limitation the rights 12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 // copies of the Software, and to permit persons to whom the Software is 14 // furnished to do so, subject to the following conditions: 15 // 16 // The above copyright notice and this permission notice shall be included in all 17 // copies or substantial portions of the Software. 18 // 19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 // SOFTWARE. 26 // 27 28 29 #include <cstdint> 30 #include <algorithm> 31 #include "objmemory.h" 32 #include "oops.h" 33 34 #ifndef GC_REF_COUNT 35 #ifndef GC_MARK_SWEEP 36 #error "must define GC_REF_COUNT and/or GC_MARK_SWEEP" 37 #endif 38 #endif 39 40 41 ObjectMemory::ObjectMemory( 42 IHardwareAbstractionLayer *halInterface 43 #ifdef GC_MARK_SWEEP 44 , IGCNotification *notification 45 #endif 46 ) : currentSegment(-1), freeWords(0), freeOops(0) 47 { 48 #ifdef GC_MARK_SWEEP 49 gcNotification = notification; 50 #endif 51 hal = halInterface; 52 } 53 54 55 56 bool ObjectMemory::loadObjectTable(IFileSystem *fileSystem, int fd) 57 { 58 // First two 32-bit values have the object space length and object table lengths in words 59 std::int32_t objectTableLength; 60 61 if (fileSystem->seek_to(fd, 4) == -1) // Skip over object space length 62 return false; 63 if (fileSystem->read(fd, (char *)&objectTableLength, sizeof(objectTableLength)) != sizeof(objectTableLength)) 64 return false; 65 int fileSize = fileSystem->file_size(fd); 66 67 if (fileSystem->seek_to(fd, fileSize - objectTableLength*2) == -1) // Reposition to start of object table 68 return false; 69 70 for(int objectPointer = 0; objectPointer < objectTableLength; objectPointer+=2) 71 { 72 std::uint16_t words[2]; 73 if (fileSystem->read(fd, (char *)&words, sizeof(words)) != sizeof(words)) 74 return false; 75 ot_put(objectPointer, words[0]); 76 locationBitsOf_put(objectPointer, words[1]); 77 } 78 79 headOfFreePointerListPut(NonPointer); 80 81 // Initialize the remaining entries as free 82 for(int objectPointer = objectTableLength; objectPointer < ObjectTableSize; objectPointer += 2) 83 { 84 ot_put(objectPointer, 0); 85 freeBitOf_put(objectPointer, 1); 86 locationBitsOf_put(objectPointer, 0); 87 } 88 89 // Build the OT entry free list, Go backwards so we have lower entries first on free list. 90 // Why? Makes the OT easier to see in debugger. 91 // Note we skip oop 0, which is considered reserved and invalid. (page 2, Xerox Virtual Image booklet) 92 for(int objectPointer = ObjectTableSize-2; objectPointer >= 2; objectPointer -= 2) 93 if (freeBitOf(objectPointer)) 94 toFreePointerListAdd(objectPointer); 95 96 freeOops = auditFreeOops(); 97 98 return true; 99 } 100 101 bool ObjectMemory::loadObjects(IFileSystem *fileSystem, int fd) 102 { 103 static const int SegmentHeapSpaceSize = HeapSpaceStop + 1; 104 105 // Track amount of free space available for objects in each segment 106 int heapSpaceRemaining[HeapSegmentCount]; 107 108 for(int segment = FirstHeapSegment; segment <= LastHeapSegment; segment++) 109 heapSpaceRemaining[segment - FirstHeapSegment] = SegmentHeapSpaceSize; 110 111 // Load objects from the virtual image into the heap segments 112 // being careful to not split an object across a segment boundary 113 int destinationSegment = FirstHeapSegment, destinationWord = 0; 114 115 for(int objectPointer = 2; objectPointer < ObjectTableSize; objectPointer += 2) 116 { 117 if (freeBitOf(objectPointer)) continue; 118 // A free chunk has it's COUNT field set to zero but the free bit is clear 119 assert (countBitsOf(objectPointer) != 0); // SANITY Make sure a freeChunk wasn't saved! 120 121 // On disk objects are stored contiguously as if a large 20-bit WORD addressed space 122 // In this scheme, the OT segment and locations combine to form a WORD address 123 const int objectImageWordAddress = (segmentBitsOf(objectPointer) << 16) 124 + locationBitsOf(objectPointer); 125 126 fileSystem->seek_to(fd, ObjectSpaceBaseInImage + objectImageWordAddress * sizeof(std::uint16_t)); 127 128 std::uint16_t objectSize; 129 fileSystem->read(fd, (char *) &objectSize, sizeof(objectSize)); 130 131 132 // Account for the extra word used by HugeSize objects 133 int extraSpace = objectSize < HugeSize || pointerBitOf(objectPointer) == 0 ? 0 : 1; 134 int space = objectSize + extraSpace; // space in memory 135 136 if (space > heapSpaceRemaining[destinationSegment - FirstHeapSegment]) 137 { 138 // No room left in the current segment, move to next 139 destinationSegment++; 140 if (destinationSegment == HeapSegmentCount) return false; // Full 141 destinationWord = 0; 142 } 143 144 // Update OT entry so that it references the object location in ObjectMemory vs the disk image 145 segmentBitsOf_put(objectPointer, destinationSegment); 146 locationBitsOf_put(objectPointer, destinationWord); 147 148 // Store the object in the image into word memory 149 // First comes the size... 150 151 sizeBitsOf_put(objectPointer, objectSize ); 152 153 // Next is the class... 154 std::uint16_t classBits; 155 fileSystem->read(fd,(char *) &classBits, sizeof(classBits)); 156 157 classBitsOf_put(objectPointer, classBits); 158 159 // Followed by the fields... 160 for(int wordIndex = 0; wordIndex < objectSize-HeaderSize; wordIndex++) 161 { 162 std::uint16_t word; 163 fileSystem->read(fd,(char *) &word, sizeof(word)); 164 // use heap chunk 165 storeWord_ofObject_withValue(wordIndex, objectPointer, word); 166 } 167 168 destinationWord += space; 169 170 heapSpaceRemaining[destinationSegment - FirstHeapSegment] -= space; 171 } 172 173 // Initialize the free chunk lists for each heap segment with the sentinel 174 for(int segment = FirstHeapSegment; segment <= LastHeapSegment; segment++) 175 { 176 for(int size = HeaderSize; size <= BigSize; size++) 177 resetFreeChunkList_inSegment(size, segment); 178 } 179 180 freeWords = 0; 181 // Place any remaining space in each segment onto it's free chunk list, which is 182 // is a linked list of object pointers. 183 // The chunks are linked using the class field of an object. The size field of 184 // the object contains the actual size of the free chunk. 185 for(int segment = FirstHeapSegment; segment <= LastHeapSegment; segment++) 186 { 187 int freeChunkSize = heapSpaceRemaining[segment - FirstHeapSegment]; 188 freeWords += freeChunkSize; 189 if (freeChunkSize >= HeaderSize) 190 { 191 int freeChunkLocation = SegmentHeapSpaceSize - freeChunkSize; 192 // G&R pg 665 - each free chunk has an OT entry 193 currentSegment = segment; // Set special segment register 194 int objectPointer = obtainPointer_location(freeChunkSize, freeChunkLocation); 195 toFreeChunkList_add(std::min(freeChunkSize, (int) BigSize), objectPointer); 196 } 197 } 198 199 currentSegment = FirstHeapSegment; 200 201 return true; 202 203 } 204 205 bool ObjectMemory::loadSnapshot(IFileSystem *fileSystem, const char *fileName) 206 { 207 int fd = fileSystem->open_file(fileName); 208 if (fd == -1) 209 return false; 210 211 bool succeeded = loadObjectTable(fileSystem, fd) && loadObjects(fileSystem, fd); 212 213 fileSystem->close_file(fd); 214 215 return succeeded; 216 } 217 218 219 bool ObjectMemory::padToPage(IFileSystem *fileSystem, int fd) 220 { 221 int pos = fileSystem->tell(fd); 222 int desired = ((pos + 512 - 1) / 512) * 512; 223 std::uint16_t word = 0; 224 int pad = (desired - pos) / sizeof(word); 225 while (pad-- > 0) 226 { 227 if (fileSystem->write(fd, (char *) &word, sizeof(word)) != sizeof(word)) 228 return false; 229 } 230 231 return true; 232 } 233 234 bool ObjectMemory::saveSnapshot(IFileSystem *fileSystem, const char *imageFileName) 235 { 236 237 int fd = fileSystem->create_file(imageFileName); 238 if (fd == -1) 239 return false; 240 bool success = saveObjects(fileSystem, fd); 241 242 fileSystem->close_file(fd); 243 return success; 244 245 } 246 247 bool ObjectMemory::saveObjects(IFileSystem *fileSystem, int fd) 248 { 249 // Avoid dumping out the entire object table -- we only need to write entries up until 250 // the last OT entry that references an object 251 int lastUsedObjectPointer = NonPointer; 252 for(int objectPointer = 2; objectPointer < ObjectTableSize; objectPointer += 2) 253 { 254 if (hasObject(objectPointer)) 255 lastUsedObjectPointer = objectPointer; 256 } 257 258 int storedObjectTableLength = lastUsedObjectPointer + 2; 259 260 std::int32_t placeHolder[2] = {0}; 261 262 // Write place holder value for object space length and object table length 263 if (fileSystem->write(fd, (char *) &placeHolder, sizeof(placeHolder)) != sizeof(placeHolder)) 264 return false; 265 266 // Write two zero bytes indicating interchange format 267 std::uint8_t interchange[2] = {0}; 268 if (fileSystem->write(fd, (char *) &interchange, sizeof(interchange)) != sizeof(interchange)) 269 return false; 270 271 if (!padToPage(fileSystem, fd)) // Advance to next page before writing objects 272 return false; 273 274 // Write objects 275 std::int32_t objectSpaceLength = 0; 276 for(int objectPointer = 2; objectPointer < storedObjectTableLength; objectPointer += 2) 277 { 278 if (!hasObject(objectPointer)) continue; 279 280 // Write object to file... N.B. we do not store the extra word for HugeSize objects 281 std::uint16_t header[2]; 282 std::uint16_t objectSize = sizeBitsOf(objectPointer); 283 header[0] = objectSize; 284 header[1] = (std::uint16_t) fetchClassOf(objectPointer); 285 if (fileSystem->write(fd, (char *) &header, sizeof(header)) != sizeof(header)) 286 return false; 287 int wordLengthOfObject = fetchWordLengthOf(objectPointer); 288 for(int wordIndex = 0; wordIndex < wordLengthOfObject; wordIndex++ ) 289 { 290 std::uint16_t word = (std::uint16_t) fetchWord_ofObject(wordIndex, objectPointer); 291 if (fileSystem->write(fd, (char *) &word, sizeof(word)) != sizeof(word)) 292 return false; 293 } 294 295 objectSpaceLength += objectSize; 296 } 297 298 299 if (!padToPage(fileSystem, fd)) // Advance to next page before writing object table 300 return false; 301 302 // Write object table 303 int objectImageWordAddress = 0; 304 for(int objectPointer = 0; objectPointer < storedObjectTableLength; objectPointer += 2) 305 { 306 std::uint16_t oldOTValue = ot(objectPointer); 307 std::uint16_t oldOTLocation = locationBitsOf(objectPointer); 308 309 if (objectPointer >= 2) 310 { 311 if (!freeBitOf(objectPointer) && countBitsOf(objectPointer) == 0) 312 { 313 // This entry was for a free chunk of memory, but we don't save 314 // free space in the image. Store as a 315 // an available OT entry by setting free bit 316 freeBitOf_put(objectPointer, 1); 317 } 318 319 if (freeBitOf(objectPointer)) 320 { 321 // manual.pdf - page 3: free entries have freeBit set and other bits in both 322 // words are 0. 323 ot_put(objectPointer, 0); 324 freeBitOf_put(objectPointer, 1); 325 locationBitsOf_put(objectPointer, 0); 326 } 327 else 328 { 329 std::uint16_t objectSize = (std::uint16_t) sizeBitsOf(objectPointer); 330 331 // Modify the location of the object table entry... we do this once we no longer 332 // process this object table entry 333 segmentBitsOf_put(objectPointer, objectImageWordAddress >> 16); 334 locationBitsOf_put(objectPointer, objectImageWordAddress & 0xffff); 335 objectImageWordAddress += objectSize; 336 } 337 } 338 339 // Assemble object table entry 340 std::uint16_t words[2]; 341 words[0] = ot(objectPointer); 342 words[1] = locationBitsOf(objectPointer); 343 344 // Restore OT entry 345 ot_put(objectPointer, oldOTValue); 346 locationBitsOf_put(objectPointer, oldOTLocation); 347 348 // Write this entry 349 if (fileSystem->write(fd, (char *) &words, sizeof(words)) != sizeof(words)) 350 return false; 351 352 } 353 354 // Now we can go back fill in the values for the image header. 355 fileSystem->seek_to(fd, 0); 356 fileSystem->write(fd, (char *) &objectSpaceLength, sizeof(objectSpaceLength)); 357 fileSystem->write(fd, (char *) &storedObjectTableLength, sizeof(storedObjectTableLength)); 358 359 return true; 360 } 361 362 // sweepCurrentSegmentFrom: 363 int ObjectMemory::sweepCurrentSegmentFrom(int lowWaterMark) 364 { 365 int si; 366 int di; 367 int objectPointer; 368 int size; 369 370 371 /* "source" 372 "ERROR dbanay - unused local 'space'" 373 si <- di <- lowWaterMark. 374 [si < HeapSpaceStop] 375 whileTrue: "for each object, si" 376 [(wordMemory segment: currentSegment word: si + 1) = NonPointer 377 ifTrue: "unallocated, so skip it" 378 [size <- wordMemory segment: currentSegment word: si. 379 si <- si + size] 380 ifFalse: "allocated, so keep it, but move it to compact storage" 381 [objectPointer 382 <- wordMemory segment: currentSegment word: si. 383 size <- self locationBitsOf: objectPointer. 384 "the reversed size" 385 self locationBitsOf: objectPointer 386 put: di. "point object table at new location" 387 self sizeBitsOf: objectPointer 388 put: size. "restore the size to its proper place" 389 si <- si + 1. "skip the size" 390 di <- di + 1. "skip the size" 391 2 to: (self spaceOccupiedBy: objectPointer) do: 392 "move the rest of the object" 393 [ :i | 394 wordMemory segment: currentSegment 395 word: di 396 put: (wordMemory segment: 397 currentSegment 398 word: si). 399 si <- si + 1. 400 di <- di + 1]]]. 401 ^di 402 */ 403 404 si = di = lowWaterMark; 405 while (si < HeapSpaceStop) // for each object, si 406 { 407 if (wordMemory.segment_word(currentSegment, si + 1) == NonPointer) 408 { 409 // Unallocated, so skip it (see abandonFreeChunksInSegment) 410 size = wordMemory.segment_word(currentSegment, si); 411 si = si + size; 412 } 413 else 414 { 415 // allocated, so keep it, but move it to compact storage 416 objectPointer = wordMemory.segment_word(currentSegment, si); // reversed pointer! 417 size = locationBitsOf(objectPointer); // the reversed size (stored during pointer reversal step) 418 locationBitsOf_put(objectPointer, di); // point object table at new location (di) 419 sizeBitsOf_put(objectPointer, size); // restore the size to its proper place 420 si++; // skip the size header 421 di++; // skip the size header (size we wrote to di via sizeBitsOf_put) 422 int limit = spaceOccupiedBy(objectPointer); 423 for(int i = 2; i <= limit; i++) // move the rest of the object (already wrote size via sizeBitsOf_put) 424 { 425 wordMemory.segment_word_put(currentSegment, di, 426 wordMemory.segment_word(currentSegment, si)); 427 si++; 428 di++; 429 430 } 431 } 432 } 433 434 return di; 435 436 } 437 438 439 // compactCurrentSegment 440 void ObjectMemory::compactCurrentSegment() 441 { 442 int lowWaterMark; 443 int bigSpace; 444 445 /* "source" 446 lowWaterMark <- self abandonFreeChunksInSegment: currentSegment. 447 lowWaterMark < HeapSpaceStop 448 ifTrue: [self reverseHeapPointersAbove: lowWaterMark. 449 bigSpace <- self sweepCurrentSegmentFrom: lowWaterMark. 450 self deallocate: (self obtainPointer: 451 (HeapSpaceStop + 1 - bigSpace) 452 location: bigSpace)] 453 */ 454 455 RUNTIME_CHECK(currentSegment >= FirstHeapSegment && currentSegment <= LastHeapSegment); 456 457 lowWaterMark = abandonFreeChunksInSegment(currentSegment); 458 if (lowWaterMark < HeapSpaceStop) 459 { 460 reverseHeapPointersAbove(lowWaterMark); 461 bigSpace = sweepCurrentSegmentFrom(lowWaterMark); 462 deallocate(obtainPointer_location(HeapSpaceStop + 1 - bigSpace, bigSpace)); 463 } 464 } 465 466 // Force a garbage collection 467 void ObjectMemory::garbageCollect() 468 { 469 #ifdef GC_MARK_SWEEP 470 reclaimInaccessibleObjects(); 471 #endif 472 } 473 474 475 // releasePointer: 476 void ObjectMemory::releasePointer(int objectPointer) 477 { 478 /* "source" 479 self freeBitOf: objectPointer put: 1. 480 self toFreePointerListAdd: objectPointer 481 */ 482 freeBitOf_put(objectPointer, 1); 483 toFreePointerListAdd(objectPointer); 484 } 485 486 487 // reverseHeapPointersAbove: 488 void ObjectMemory::reverseHeapPointersAbove(int lowWaterMark) 489 { 490 int size; 491 492 /* "source" 493 0 to: ObjectTableSize-2 by: 2 do: 494 [ :objectPointer | 495 (self freeBitOf: objectPointer) = 0 496 ifTrue: "the Object Table entry is in use" 497 [(self segmentBitsOf: objectPointer) = currentSegment 498 ifTrue: "the object is in this segment" 499 [(self locationBitsOf: objectPointer) < lowWaterMark 500 ifFalse: "the object will be swept" 501 [size <- self sizeBitsOf: objectPointer. 502 "rescue the size" 503 self sizeBitsOf: objectPointer 504 put: objectPointer. 505 "reverse the pointer" 506 self locationBitsOf: objectPointer 507 put: size "save the size"]]]] 508 */ 509 510 /* pg. 673 G&R */ 511 for(int objectPointer = 0; objectPointer <= ObjectTableSize-2; objectPointer += 2) 512 { 513 if (freeBitOf(objectPointer) == 0) // the Object Table entry is in use 514 { 515 // the object is in this segment 516 if (segmentBitsOf(objectPointer) == currentSegment) 517 { 518 // the object will be swept 519 if (locationBitsOf(objectPointer) >= lowWaterMark) 520 { 521 size = sizeBitsOf(objectPointer); // rescue the size 522 sizeBitsOf_put(objectPointer, objectPointer); // reverse pointer 523 locationBitsOf_put(objectPointer, size); // save the size 524 } 525 } 526 } 527 } 528 } 529 530 531 // abandonFreeChunksInSegment: 532 int ObjectMemory::abandonFreeChunksInSegment(int segment) 533 { 534 int lowWaterMark; // Location in the segment of the first free chunk 535 int objectPointer; 536 int nextPointer; 537 538 /* "source" 539 lowWaterMark <- HeapSpaceStop. "first assume that no chunk is free" 540 HeaderSize to: BigSize do: "for each free-chunk list" 541 [ :size | 542 objectPointer <- self headOfFreeChunkList: size 543 inSegment: segment. 544 [objectPointer = NonPointer] whileFalse: 545 [lowWaterMark <- lowWaterMark min: 546 (self locationBitsOf: objectPointer). 547 nextPointer <- self classBitsOf: objectPointer. 548 "link to next free chunk" 549 self classBitsOf: objectPointer put: NonPointer. 550 "distinguish for sweep" 551 self releasePointer: objectPointer. 552 "add entry to free-pointer list" 553 objectPointer <- nextPointer]. 554 self resetFreeChunkList: size inSegment: segment]. 555 ^lowWaterMark 556 */ 557 558 // This computes the low water mark and sets the class field of free chunks to NonPointer 559 // so that are identified as such and can be combined into one free block after compaction 560 lowWaterMark = HeapSpaceStop; // first assume that no chunk is free 561 for(int size = HeaderSize; size <= BigSize; size++) 562 { 563 objectPointer = headOfFreeChunkList_inSegment(size, segment); 564 while (objectPointer != NonPointer) 565 { 566 lowWaterMark = std::min(lowWaterMark, locationBitsOf(objectPointer)); 567 nextPointer = classBitsOf(objectPointer); // link to next free chunk 568 classBitsOf_put(objectPointer, NonPointer); // distinguish for sweep 569 releasePointer(objectPointer); // Add entry to free (object table) pointer list 570 objectPointer = nextPointer; 571 } 572 resetFreeChunkList_inSegment(size, segment); 573 } 574 return lowWaterMark; 575 } 576 577 578 #ifdef GC_MARK_SWEEP 579 580 // reclaimInaccessibleObjects 581 void ObjectMemory::reclaimInaccessibleObjects() 582 { 583 584 /* "source" 585 self zeroReferenceCounts. 586 self markAccessibleObjects. 587 self rectifyCountsAndDeallocateGarbage 588 */ 589 590 zeroReferenceCounts(); 591 markAccessibleObjects(); 592 rectifyCountsAndDeallocateGarbage(); 593 } 594 595 596 // markObjectsAccessibleFrom: 597 int ObjectMemory::markObjectsAccessibleFrom(int rootObjectPointer) 598 { 599 /* "source" 600 ^self forAllObjectsAccessibleFrom: rootObjectPointer 601 suchThat: "the predicate tests for an unmarked object and marks it" 602 [ :objectPointer | 603 unmarked <- (self countBitsOf: objectPointer) = 0. 604 unmarked ifTrue: [self countBitsOf: objectPointer put: 1]. 605 unmarked] 606 do: "the action restores the mark to count=1" 607 [ :objectPointer | 608 self countBitsOf: objectPointer put: 1] 609 */ 610 return forAllObjectsAccessibleFrom_suchThat_do(rootObjectPointer, 611 [this](int objectPointer) { // the predicate tests for an unmarked object and marks it 612 bool unmarked = countBitsOf(objectPointer) == 0; 613 if (unmarked) 614 { 615 countBitsOf_put(objectPointer, 1); 616 } 617 return unmarked; 618 }, 619 [this](int objectPointer) { // the action restores the mark to count=1 620 countBitsOf_put(objectPointer, 1); 621 }); 622 623 } 624 625 626 // markAccessibleObjects 627 void ObjectMemory::markAccessibleObjects() 628 { 629 /* "source" 630 "ERROR: rootObjectPointers not defined" 631 rootObjectPointers do: 632 [ :rootObjectPointer | 633 self markObjectsAccessibleFrom: rootObjectPointer] 634 */ 635 636 for(int i = 0; i <= LastSpecialOop; i += 2) 637 { 638 addRoot(i); 639 } 640 641 if (gcNotification) 642 gcNotification->prepareForCollection(); 643 644 } 645 #endif 646 647 648 void ObjectMemory::outOfMemoryError() 649 { 650 assert(0); 651 hal->error("Out of memory"); 652 } 653 654 // allocateChunk: 655 int ObjectMemory::allocateChunk(int size) 656 { 657 int objectPointer; 658 659 /* "source" 660 "marking collector" 661 objectPointer <- self attemptToAllocateChunk: size. 662 objectPointer isNil ifFalse: [^objectPointer]. 663 self reclaimInaccessibleObjects. "garbage collect and try again" 664 objectPointer <- self attemptToAllocateChunk: size. 665 objectPointer isNil ifFalse: [^objectPointer]. 666 self outOfMemoryError "give up" 667 */ 668 669 objectPointer = attemptToAllocateChunk(size); 670 #ifdef GC_MARK_SWEEP 671 if (objectPointer == NilPointer) 672 { 673 reclaimInaccessibleObjects(); 674 objectPointer = attemptToAllocateChunk(size); 675 } 676 #endif 677 if (objectPointer != NilPointer) 678 { 679 if (freeWords >= size) 680 freeWords -= size; 681 682 return objectPointer; 683 } 684 outOfMemoryError(); // give up 685 return NilPointer; 686 } 687 688 #ifdef GC_MARK_SWEEP 689 690 // rectifyCountsAndDeallocateGarbage 691 void ObjectMemory::rectifyCountsAndDeallocateGarbage() 692 { 693 int count; 694 695 /* "source" 696 "reset heads of free-chunk lists" 697 FirstHeapSegment to: LastHeapSegment do: "for every segment" 698 [ :segment | 699 HeaderSize to: BigSize do: "for every free chunk list" 700 [ :size | "reset the list head" 701 self resetFreeChunkList: size inSegment: segment]]. 702 "rectify counts, and deallocate garbage" 703 0 to: ObjectTableSize-2 by: 2 do: "for every object table entry" 704 [ :objectPointer | 705 (self freeBitOf: objectPointer) = 0 706 ifTrue: "if it is not a free entry" 707 [(count <- self countBitsOf: objectPointer) = 0 708 ifTrue: "it is unmarked so deallocate it" 709 [self deallocate: objectPointer] 710 ifFalse: "it is marked so rectify reference counts" 711 [count < 128 712 ifTrue: "subtract 1 to compensate for the mark" 713 [self countBitsOf: objectPointer put: count-1]. 714 1 to: (self lastPointerOf: objectPointer)-1 do: 715 [ :offset | "increment the reference count 716 of each pointer" 717 self countUp: 718 (self heapChunkOf: objectPointer 719 word: offset)]]]]. 720 "be sure the root objects don't disappear" 721 "ERROR: rootObjectPointers not defined" 722 rootObjectPointers do: 723 [ :rootObjectPointer | self countUp: rootObjectPointer]. 724 self countBitsOf: NilPointer put: 128 725 */ 726 // reset heads of free-chunk lists 727 for(int segment = FirstHeapSegment; segment <= LastHeapSegment; segment++) 728 { 729 // for every free chunk list 730 for(int size = HeaderSize; size <= BigSize; size++) 731 { 732 // reset the list head 733 resetFreeChunkList_inSegment(size, segment); 734 } 735 } 736 737 // rectify counts, and deallocate garbage 738 for(int objectPointer = 0; objectPointer <= ObjectTableSize-2; objectPointer += 2) 739 { 740 if (freeBitOf(objectPointer) == 0) // if it is not a free entry 741 { 742 count = countBitsOf(objectPointer); 743 if (count == 0) 744 { 745 // unmarked, so deallocate it 746 freeWords += spaceOccupiedBy(objectPointer); //dbanay 747 deallocate(objectPointer); 748 } 749 else 750 { 751 // it is marked so rectify reference counts 752 if (count < 128) 753 { 754 // subtract 1 to compensate for the mark 755 countBitsOf_put(objectPointer, count - 1); 756 } 757 758 int limit = lastPointerOf(objectPointer)-1; 759 // increment the reference count of each pointer 760 // NB start at offset 1, which is the class 761 for(int offset = 1; offset <= limit; offset++) 762 { 763 countUp(heapChunkOf_word(objectPointer, offset)); 764 } 765 } 766 } 767 } 768 769 countBitsOf_put(NilPointer, 128); 770 771 freeOops = auditFreeOops(); 772 773 if (gcNotification) gcNotification->collectionCompleted(); 774 775 776 } 777 778 779 // zeroReferenceCounts 780 void ObjectMemory::zeroReferenceCounts() 781 { 782 /* "source" 783 0 to: ObjectTableSize-2 by: 2 do: 784 [ :objectPointer | 785 self countBitsOf: objectPointer put: 0] 786 */ 787 788 for(int objectPointer = 0; objectPointer <= ObjectTableSize-2; objectPointer += 2) 789 { 790 countBitsOf_put(objectPointer, 0); 791 } 792 } 793 #endif 794 795 // lastPointerOf: 796 // This returns the size of object up to the last pointer in it 797 int ObjectMemory::lastPointerOf(int objectPointer) 798 { 799 // MethodClass is the object table index of CompiledMethod. 800 static const int MethodClass = ClassCompiledMethod; 801 int methodHeader; 802 803 /* "source" 804 "ERROR - dbanay this is WRONG." 805 (self pointerBitOf: objectPointer) = 0 806 ifTrue: [^HeaderSize] 807 "ERROR: MethodClass not defined" 808 ifFalse: [(self classBitsOf: objectPointer) = MethodClass 809 ifTrue: [methodHeader <- self heapChunkOf: objectPointer 810 word: HeaderSize. 811 ^HeaderSize + 1 + ((methodHeader bitAnd: 126) 812 bitShift: -1)] 813 ifFalse: [^self sizeBitsOf: objectPointer]] 814 */ 815 816 /* "source" 817 "Fixed in July 1985 Ed of G&R" 818 (self pointerBitOf: objectPointer) = 0 819 ifTrue: [(self classBitsOf: objectPointer) = MethodClass 820 ifTrue: [methodHeader <- self heapChunkOf: objectPointer 821 word: HeaderSize. 822 ^HeaderSize + 1 + ((methodHeader bitAnd: 126) bitShift: -1)] 823 ifFalse: [^HeaderSize]] 824 ifFalse: 825 [^self sizeBitsOf: objectPointer] 826 827 */ 828 829 if (pointerBitOf(objectPointer) == 0) 830 { // Not pointer object 831 // CompiledMethods are special in that they are marked as having no pointers but actually do 832 if (classBitsOf(objectPointer) == MethodClass) 833 { 834 methodHeader = heapChunkOf_word(objectPointer, HeaderSize); 835 // Header Size + Method Header Size + Literal Count 836 return HeaderSize + 1 + ((methodHeader & 126) >> 1); 837 } 838 else 839 return HeaderSize; // Class field is last pointer in the object 840 } 841 842 return sizeBitsOf(objectPointer); 843 844 } 845 846 847 // spaceOccupiedBy: 848 int ObjectMemory::spaceOccupiedBy(int objectPointer) 849 { 850 int size; 851 852 /* "source" 853 size <- self sizeBitsOf: objectPointer. 854 (size < HugeSize or: [(self pointerBitOf: objectPointer) = 0]) 855 ifTrue: [^size] 856 ifFalse: [^size + 1] 857 */ 858 size = sizeBitsOf(objectPointer); 859 if (size < HugeSize || pointerBitOf(objectPointer) == 0) 860 return size; 861 return size+1; // Account for extra word used for traversal algorithm (G&R pg. 679) 862 } 863 864 865 // allocate:odd:pointer:extra:class: 866 int ObjectMemory::allocate_odd_pointer_extra_class( 867 int size, 868 int oddBit, 869 int pointerBit, 870 int extraWord, 871 int classPointer 872 ) 873 { 874 int objectPointer; 875 int defaultValue; 876 877 /* "source" 878 self countUp: classPointer. 879 objectPointer <- self allocateChunk: size + extraWord. 880 self oddBitOf: objectPointer put: oddBit. 881 self pointerBitOf: objectPointer put: pointerBit. 882 self classBitsOf: objectPointer put: classPointer. 883 default <- pointerBit=0 ifTrue: [0] ifFalse: [NilPointer]. 884 HeaderSize to: size-1 do: 885 [ :i | self heapChunkOf: objectPointer word: i put: default]. 886 self sizeBitsOf: objectPointer put: size. 887 ^objectPointer 888 */ 889 countUp(classPointer); 890 objectPointer = allocateChunk(size + extraWord); 891 oddBitOf_put(objectPointer, oddBit); 892 pointerBitOf_put(objectPointer, pointerBit); 893 classBitsOf_put(objectPointer, classPointer); 894 defaultValue = (pointerBit == 0) ? 0 : NilPointer; 895 for(int i = HeaderSize; i <= size-1; i++) 896 { 897 heapChunkOf_word_put(objectPointer, i, defaultValue); 898 } 899 sizeBitsOf_put(objectPointer, size); 900 freeOops--; // dbanay 901 return objectPointer; 902 } 903 904 // headOfFreePointerList 905 int ObjectMemory::headOfFreePointerList() 906 { 907 /* "source" 908 ^wordMemory segment: ObjectTableSegment 909 word: FreePointerList 910 */ 911 912 return wordMemory.segment_word(ObjectTableSegment, 913 FreePointerList); 914 } 915 916 // toFreeChunkList:add: 917 void ObjectMemory::toFreeChunkList_add(int size, int objectPointer) 918 { 919 int segment; 920 921 /* "source" 922 segment <- self segmentBitsOf: objectPointer. 923 self classBitsOf: objectPointer 924 put: (self headOfFreeChunkList: size inSegment: segment). 925 self headOfFreeChunkList: size 926 inSegment: segment 927 put: objectPointer 928 */ 929 930 segment = segmentBitsOf(objectPointer); 931 classBitsOf_put(objectPointer, 932 headOfFreeChunkList_inSegment(size, segment)); 933 headOfFreeChunkList_inSegment_put(size, segment, objectPointer); 934 935 } 936 937 938 // headOfFreeChunkList:inSegment:put: 939 int ObjectMemory::headOfFreeChunkList_inSegment_put(int size, int segment, int objectPointer) 940 { 941 /* "source" 942 ^wordMemory segment: segment 943 word: FirstFreeChunkList + size 944 put: objectPointer 945 */ 946 947 RUNTIME_CHECK(size >= HeaderSize && size <= BigSize); 948 949 return wordMemory.segment_word_put(segment, 950 FirstFreeChunkList + size, 951 objectPointer); 952 } 953 954 955 // removeFromFreePointerList 956 int ObjectMemory::removeFromFreePointerList() 957 { 958 int objectPointer; 959 960 /* "source" 961 objectPointer <- self headOfFreePointerList. 962 objectPointer = NonPointer ifTrue: [^nil]. 963 self headOfFreePointerListPut: (self locationBitsOf: objectPointer). 964 ^objectPointer 965 */ 966 967 objectPointer = headOfFreePointerList(); 968 if (objectPointer == NonPointer) 969 return NilPointer; 970 headOfFreePointerListPut(locationBitsOf(objectPointer)); 971 return objectPointer; 972 } 973 974 // toFreePointerListAdd: 975 void ObjectMemory::toFreePointerListAdd(int objectPointer) 976 { 977 /* "source" 978 self locationBitsOf: objectPointer 979 put: (self headOfFreePointerList). 980 self headOfFreePointerListPut: objectPointer 981 */ 982 983 locationBitsOf_put(objectPointer, headOfFreePointerList()); 984 headOfFreePointerListPut(objectPointer); 985 986 } 987 988 989 // removeFromFreeChunkList: 990 int ObjectMemory::removeFromFreeChunkList(int size) 991 { 992 int objectPointer; 993 int secondChunk; 994 995 996 /* "source" 997 objectPointer <- self headOfFreeChunkList: size 998 inSegment: currentSegment. 999 objectPointer = NonPointer ifTrue: [^nil]. 1000 secondChunk <- self classBitsOf: objectPointer. 1001 self headOfFreeChunkList: size 1002 inSegment: currentSegment 1003 put: secondChunk. 1004 ^objectPointer 1005 */ 1006 1007 RUNTIME_CHECK(currentSegment >= FirstHeapSegment && currentSegment <= LastHeapSegment); 1008 1009 objectPointer = headOfFreeChunkList_inSegment(size, currentSegment); 1010 if (objectPointer == NonPointer) return NilPointer; 1011 secondChunk = classBitsOf(objectPointer); 1012 headOfFreeChunkList_inSegment_put(size, 1013 currentSegment, 1014 secondChunk); 1015 return objectPointer; 1016 1017 } 1018 1019 1020 // resetFreeChunkList:inSegment: 1021 void ObjectMemory::resetFreeChunkList_inSegment(int size, int segment) 1022 { 1023 /* "source" 1024 self headOfFreeChunkList: size 1025 inSegment: segment 1026 put: NonPointer 1027 */ 1028 RUNTIME_CHECK(size >= 2 && size <= BigSize); 1029 headOfFreeChunkList_inSegment_put(size, segment, NonPointer); 1030 } 1031 1032 1033 // headOfFreeChunkList:inSegment: 1034 int ObjectMemory::headOfFreeChunkList_inSegment(int size, int segment) 1035 { 1036 /* "source" 1037 ^wordMemory segment: segment 1038 word: FirstFreeChunkList + size 1039 */ 1040 RUNTIME_CHECK(size >= 2 && size <= BigSize); 1041 return wordMemory.segment_word(segment, 1042 FirstFreeChunkList + size); 1043 } 1044 1045 1046 // headOfFreePointerListPut: 1047 int ObjectMemory::headOfFreePointerListPut(int objectPointer) 1048 { 1049 /* "source" 1050 ^wordMemory segment: ObjectTableSegment 1051 word: FreePointerList 1052 put: objectPointer 1053 */ 1054 1055 return wordMemory.segment_word_put(ObjectTableSegment, 1056 FreePointerList, 1057 objectPointer); 1058 } 1059 1060 1061 // countDown: 1062 int ObjectMemory::countDown(int rootObjectPointer) 1063 { 1064 1065 /* "source" 1066 (self isIntegerObject: rootObjectPointer) 1067 ifTrue: [^rootObjectPointer] 1068 ifFalse: "this is a pointer, so decrement its reference count" 1069 [^self forAllObjectsAccessibleFrom: rootObjectPointer 1070 suchThat: 1071 "the predicate decrements the count and tests for zero" 1072 [ :objectPointer | 1073 count <- (self countBitsOf: objectPointer) - 1. 1074 count < 127 1075 ifTrue: [self countBitsOf: objectPointer 1076 put: count]. 1077 count = 0] 1078 do: "the action zeroes the count and deallocates the object" 1079 [ :objectPointer | 1080 self countBitsOf: objectPointer put: 0. 1081 self deallocate: objectPointer]] 1082 */ 1083 1084 if (isIntegerObject(rootObjectPointer)) 1085 return rootObjectPointer; 1086 1087 RUNTIME_CHECK(countBitsOf(rootObjectPointer)>0); 1088 1089 // this is a pointer, so decrement its reference count 1090 return forAllObjectsAccessibleFrom_suchThat_do(rootObjectPointer, 1091 [this](int objectPointer) { // predicate 1092 int count = countBitsOf(objectPointer) - 1; 1093 RUNTIME_CHECK(count >= 0); 1094 if (count < 127) 1095 countBitsOf_put(objectPointer, count); 1096 return count == 0; 1097 }, 1098 [this](int objectPointer) { // action 1099 // std::cout << "reference count zero. freeing " << objectPointer << " (" << classNameOfObject(fetchClassOf(objectPointer)) << ") free oops = " << freeOops << "\n"; 1100 countBitsOf_put(objectPointer, 0); 1101 freeWords += spaceOccupiedBy(objectPointer); //dbanay 1102 freeOops++; 1103 deallocate(objectPointer); 1104 }); 1105 1106 } 1107 1108 1109 // countUp: 1110 int ObjectMemory::countUp(int objectPointer) 1111 { 1112 int count; 1113 1114 /* "source" 1115 (self isIntegerObject: objectPointer) 1116 ifFalse: [count <- (self countBitsOf: objectPointer) + 1. 1117 count < 129 ifTrue: [self countBitsOf: objectPointer put: count]]. 1118 ^objectPointer 1119 */ 1120 if (!isIntegerObject(objectPointer)) 1121 { 1122 count = countBitsOf(objectPointer) + 1; 1123 if (count < 129) // Count sticks at 128 1124 countBitsOf_put(objectPointer, count); 1125 } 1126 1127 return objectPointer; 1128 } 1129 1130 1131 // deallocate: 1132 void ObjectMemory::deallocate(int objectPointer) 1133 { 1134 int space; 1135 1136 /* "source" 1137 space <- self spaceOccupiedBy: objectPointer. 1138 self sizeBitsOf: objectPointer put: space. 1139 self toFreeChunkList: (space min: BigSize) add: objectPointer 1140 */ 1141 1142 space = spaceOccupiedBy(objectPointer); 1143 sizeBitsOf_put(objectPointer, space); 1144 toFreeChunkList_add(std::min(space, (int) BigSize), objectPointer); 1145 } 1146 1147 #ifdef RECURSIVE_MARKING 1148 // recursive version -- stack hungry 1149 // forAllOtherObjectsAccessibleFrom:suchThat:do: 1150 int ObjectMemory::forAllOtherObjectsAccessibleFrom_suchThat_do( 1151 int objectPointer, 1152 const std::function <bool (int)>& predicate, 1153 const std::function <void (int)>& action 1154 ) 1155 { 1156 int next; 1157 1158 /* "source" 1159 1 to: (self lastPointerOf: objectPointer) - 1 do: 1160 [ :offset | 1161 next <- self heapChunkOf: objectPointer word: offset. 1162 ((self isIntegerObject: next)==false and: [predicate value: next]) 1163 ifTrue: "it's a non-immediate object and it should be processed" 1164 [self forAllOtherObjectsAccessibleFrom: next 1165 suchThat: predicate 1166 do: action]]. 1167 "all pointers have been followed; now perform the action" 1168 action value: objectPointer. 1169 ^objectPointer 1170 */ 1171 1172 int limit = lastPointerOf(objectPointer) - 1; 1173 //N.B. start at offset 1, which is class field 1174 for(int offset = 1; offset <= limit; offset++) 1175 { 1176 next = heapChunkOf_word(objectPointer, offset); 1177 if (!isIntegerObject(next) && predicate(next)) 1178 { 1179 // it's a non-immediate object and it should be processed 1180 forAllOtherObjectsAccessibleFrom_suchThat_do(next, 1181 predicate, 1182 action); 1183 } 1184 } 1185 1186 // all pointers have been followed; now perform the action 1187 action(objectPointer); 1188 return objectPointer; 1189 } 1190 #else 1191 // forAllOtherObjectsAccessibleFrom:suchThat:do: 1192 int ObjectMemory::forAllOtherObjectsAccessibleFrom_suchThat_do( 1193 int objectPointer, 1194 const std::function <bool (int)>& predicate, 1195 const std::function <void (int)>& action 1196 ) 1197 { 1198 int prior; 1199 int current; 1200 int offset; 1201 int size; 1202 int next; 1203 1204 /* "source" 1205 "compute prior, current, offset, and size to begin processing objectPointer" 1206 prior <- NonPointer. 1207 current <- objectPointer. 1208 offset <- size <- self lastPointerOf: objectPointer. 1209 [true] whileTrue: "for all pointers in all objects traversed" 1210 [(offset <- offset - 1) > 0 "decrement the field index" 1211 ifTrue: "the class hasn't been passed yet" 1212 [next <- self heapChunkOf: current word: offset. 1213 "one of the pointers" 1214 ((self isIntegerObject: next)==false and: [predicate value: next]) 1215 ifTrue: "it's a non-immediate object and it should be processed" 1216 ["reverse the pointer chain" 1217 self heapChunkOf: current word: offset put: prior. 1218 "save the offset either in the count field or in the extra word" 1219 size < HugeSize 1220 ifTrue: [self countBitsOf: current put: offset] 1221 ifFalse: [self heapChunkOf: current 1222 word: size+1 put: offset]. "ERROR dbanay size not size + 1" 1223 "compute prior, current, offset, and size to begin processing next" 1224 prior <- current. 1225 current <- next. 1226 offset <- size <- self lastPointerOf: current]] 1227 ifFalse: ["all pointers have been followed; now perform the action" 1228 action value: objectPointer. "ERROR dbanay - should be current" 1229 "did we get here from another object?" 1230 prior = NonPointer 1231 ifTrue: "this was the root object, so we are done" 1232 [^objectPointer]. 1233 "restore next, current and size to resume processing prior" 1234 next <- current. 1235 current <- prior. 1236 size <- self lastPointerOf: current. 1237 "restore offset either from the count field or from the extra word" 1238 size < HugeSize 1239 ifTrue: [offset <- self countBitsOf: current] 1240 ifFalse: [offset <- self heapChunkOf: current word: size+1]. "ERROR dbanay size not size + 1" 1241 "restore prior from the reversed pointer chain" 1242 prior <- self heapChunkOf: current word: offset. 1243 "restore (un-reverse) the pointer chain" 1244 self heapChunkOf: current word: offset put: next]] 1245 */ 1246 1247 prior = NonPointer; 1248 current = objectPointer; 1249 offset = size = lastPointerOf(objectPointer); 1250 for(;;) 1251 { 1252 // for all pointers in all objects traversed 1253 offset--; // decrement the field index 1254 if (offset > 0) // the class hasn't been passed yet 1255 { 1256 next = heapChunkOf_word(current, offset); // one of the pointers 1257 if (!isIntegerObject(next) && predicate(next)) 1258 { 1259 // it's a non-immediate object and it should be processed 1260 // reverse pointer chain 1261 heapChunkOf_word_put(current, offset, prior); 1262 // save the offset either in the count field or in the extra word 1263 if (size < HugeSize) 1264 countBitsOf_put(current, offset); 1265 else 1266 heapChunkOf_word_put(current, size, offset); 1267 // compute prior, current, offset, and size to begin processing next 1268 prior = current; 1269 current = next; 1270 offset = size = lastPointerOf(current); 1271 } 1272 } 1273 else 1274 { 1275 // all pointers have been followed; now perform the action 1276 action(current); 1277 // did we get here from another object? 1278 if (prior == NonPointer) // this was the root object, so we are done 1279 return objectPointer; 1280 // restore next, current and size to resume processing prior 1281 next = current; 1282 current = prior; 1283 size = lastPointerOf(current); 1284 // restore offset either from the count field or from the extra word 1285 if (size < HugeSize) 1286 offset = countBitsOf(current); 1287 else 1288 offset = heapChunkOf_word(current, size); 1289 // restore prior from the reversed pointer chain 1290 prior = heapChunkOf_word(current, offset); 1291 // restore (un-reverse) the pointer chain 1292 heapChunkOf_word_put(current, offset, next); 1293 } 1294 } 1295 } 1296 #endif 1297 1298 1299 // forAllObjectsAccessibleFrom:suchThat:do: 1300 int ObjectMemory::forAllObjectsAccessibleFrom_suchThat_do(int objectPointer, 1301 const std::function <bool (int)>& predicate, 1302 const std::function <void (int)>& action) 1303 { 1304 /* "source" 1305 (predicate value: objectPointer) 1306 ifTrue: [^self forAllOtherObjectsAccessibleFrom: objectPointer 1307 suchThat: predicate 1308 do: action] 1309 */ 1310 1311 if (predicate(objectPointer)) 1312 { 1313 return forAllOtherObjectsAccessibleFrom_suchThat_do(objectPointer, 1314 predicate, 1315 action); 1316 } 1317 1318 return NilPointer; 1319 1320 } 1321 1322 1323 // storePointer:ofObject:withValue: 1324 int ObjectMemory::storePointer_ofObject_withValue(int fieldIndex, int objectPointer, int valuePointer) 1325 { 1326 int chunkIndex; 1327 1328 /* "source" 1329 chunkIndex <- HeaderSize + fieldIndex. 1330 self countUp: valuePointer. 1331 self countDown: (self heapChunkOf: objectPointer word: chunkIndex). 1332 ^self heapChunkOf: objectPointer word: chunkIndex put: valuePointer 1333 */ 1334 1335 RUNTIME_CHECK(fieldIndex >= 0 && fieldIndex < fetchWordLengthOf(objectPointer)); 1336 RUNTIME_CHECK(valuePointer > 0); 1337 1338 chunkIndex = HeaderSize + fieldIndex; 1339 1340 #ifdef GC_REF_COUNT 1341 countUp(valuePointer); 1342 countDown(heapChunkOf_word(objectPointer, chunkIndex)); 1343 #endif 1344 return heapChunkOf_word_put(objectPointer, chunkIndex, valuePointer); 1345 } 1346 1347 1348 // storeWord:ofObject:withValue: 1349 int ObjectMemory::storeWord_ofObject_withValue(int wordIndex, int objectPointer, int valueWord) 1350 { 1351 /* "source" 1352 ^self heapChunkOf: objectPointer word: HeaderSize + wordIndex 1353 put: valueWord 1354 */ 1355 RUNTIME_CHECK(wordIndex >= 0 && wordIndex < fetchWordLengthOf(objectPointer)); 1356 return heapChunkOf_word_put(objectPointer, HeaderSize + wordIndex, 1357 valueWord); 1358 } 1359 1360 1361 // initialInstanceOf: 1362 int ObjectMemory::initialInstanceOf(int classPointer) 1363 { 1364 // Mario checks for the count bit not being zero. This is necessary 1365 // because an OT entry with a clear free bit but a ZERO count marks 1366 // an allocated chunk of memory available for use 1367 /* "source" 1368 0 to: ObjectTableSize-2 by: 2 do: 1369 [ :pointer | 1370 "ERROR in next line, second part of test omitted" 1371 ((self freeBitOf: pointer) = 0 and: [(self countBitOf: pointer) ~= 0]) 1372 ifTrue: [(self fetchClassOf: pointer) = classPointer 1373 ifTrue: [^pointer]]]. 1374 ^NilPointer 1375 */ 1376 1377 for(int pointer = 0; pointer <= ObjectTableSize-2; pointer += 2) 1378 { 1379 // Only consider non-free entries that are not free chunks 1380 if (freeBitOf(pointer) == 0 && countBitsOf(pointer) != 0) 1381 { 1382 if (fetchClassOf(pointer) == classPointer) 1383 return pointer; 1384 } 1385 } 1386 return NilPointer; 1387 } 1388 1389 1390 1391 // swapPointersOf:and: 1392 void ObjectMemory::swapPointersOf_and(int firstPointer, int secondPointer) 1393 { 1394 1395 int firstSegment; 1396 int firstLocation; 1397 int firstPointerBit; /* dbanay - ERROR was named firstPointer */ 1398 int firstOdd; 1399 1400 /* "source" 1401 "ERROR in next line, firstPointer redefined" 1402 "dbanay - should have been called firstPointerBit (FYI - NOT FIXED IN July 1985 ed)" 1403 firstSegment <- self segmentBitsOf: firstPointer. 1404 firstLocation <- self locationBitsOf: firstPointer. 1405 "dbanay ERROR - next should be: firstPointerBit <- self pointerBitOf: firstPointer. 1406 firstPointer <- self pointerBitOf: firstPointer. 1407 firstOdd <- self oddBitOf: firstPointer. 1408 self segmentBitsOf: firstPointer put: (self segmentBitsOf: secondPointer). 1409 self locationBitsOf: firstPointer put: (self locationBitsOf: secondPointer). 1410 self pointerBitOf: firstPointer put: (self pointerBitOf: secondPointer). 1411 self oddBitOf: firstPointer put: (self oddBitOf: secondPointer). 1412 self segmentBitsOf: secondPointer put: firstSegment. 1413 self locationBitsOf: secondPointer put: firstLocation. 1414 "ERROR dbanay - should be firstPointerBit" 1415 self pointerBitOf: secondPointer put: firstPointer. 1416 self oddBitOf: secondPointer put: firstOdd 1417 */ 1418 1419 firstSegment = segmentBitsOf(firstPointer); 1420 firstLocation = locationBitsOf(firstPointer); 1421 firstPointerBit = pointerBitOf(firstPointer); 1422 firstOdd = oddBitOf(firstPointer); 1423 1424 segmentBitsOf_put(firstPointer, segmentBitsOf(secondPointer)); 1425 locationBitsOf_put(firstPointer, locationBitsOf(secondPointer)); 1426 pointerBitOf_put(firstPointer, pointerBitOf(secondPointer)); 1427 oddBitOf_put(firstPointer, oddBitOf(secondPointer)); 1428 1429 segmentBitsOf_put(secondPointer, firstSegment); 1430 locationBitsOf_put(secondPointer, firstLocation); 1431 pointerBitOf_put(secondPointer, firstPointerBit); 1432 oddBitOf_put(secondPointer, firstOdd); 1433 } 1434 1435 1436 // instantiateClass:withWords: 1437 int ObjectMemory::instantiateClass_withWords(int classPointer, int length) 1438 { 1439 int size; 1440 1441 /* "source" 1442 size <- HeaderSize + length. 1443 ^self allocate: size odd: 0 pointer: 0 extra: 0 class: classPointer 1444 */ 1445 size = HeaderSize + length; 1446 return allocate_odd_pointer_extra_class(size, 0, 0, 0, classPointer); 1447 } 1448 1449 1450 // instantiateClass:withBytes: 1451 int ObjectMemory::instantiateClass_withBytes(int classPointer, int length) 1452 { 1453 int size; 1454 1455 /* "source" 1456 size <- HeaderSize + ((length + 1)/2). 1457 ^self allocate: size odd: length\\2 pointer: 0 extra: 0 class: classPointer 1458 */ 1459 size = HeaderSize + ((length + 1)/2); 1460 return allocate_odd_pointer_extra_class(size, length % 2, 0, 0, classPointer); 1461 } 1462 1463 1464 // hasObject: 1465 bool ObjectMemory::hasObject(int objectPointer) 1466 { 1467 /* "source" 1468 "is objectPointer pointing to a valid object ?" 1469 ^(self freeBitOf: objectPointer) = 0 1470 "not sure that the first clause is necessary" 1471 and: [(self countBitsOf: objectPointer) ~= 0] 1472 */ 1473 cantBeIntegerObject(objectPointer); // dbanay 1474 return freeBitOf(objectPointer) == 0 && countBitsOf(objectPointer) != 0; 1475 } 1476 1477 int ObjectMemory::auditFreeOops() 1478 { 1479 int count = 0; 1480 for(int objectPointer = 2; objectPointer < ObjectTableSize; objectPointer+=2) 1481 { 1482 if (!hasObject(objectPointer)) 1483 count++; 1484 } 1485 1486 return count; 1487 } 1488 1489 1490 1491 // instantiateClass:withPointers: 1492 int ObjectMemory::instantiateClass_withPointers(int classPointer, int length) 1493 { 1494 int size; 1495 int extra; 1496 1497 /* "source" 1498 size <- HeaderSize + length. 1499 extra <- size < HugeSize ifTrue: [0] ifFalse: [1]. 1500 ^self allocate: size odd: 0 pointer: 1 extra: extra class: classPointer 1501 */ 1502 1503 size = HeaderSize + length; 1504 extra = size < HugeSize ? 0 : 1; 1505 return allocate_odd_pointer_extra_class(size, 0, 1, extra, classPointer); 1506 } 1507 1508 1509 // instanceAfter: 1510 int ObjectMemory::instanceAfter(int objectPointer) 1511 { 1512 int classPointer; 1513 1514 /* "source" 1515 "July 1985 ed also got this wrong" 1516 "ERROR: next line omitted by G&R" 1517 classPointer <- self fetchClassOf: objectPointer. 1518 "dbanay - ERROR - was 'objectPointer to: ObjectTableSize-2 by: 2 do:' should start at objectPointer+2'" 1519 "dbanay - ERROR - should also check count bits to skip free Chunk entries" 1520 objectPointer to: ObjectTableSize-2 by: 2 do: 1521 [ :pointer | 1522 (self freeBitOf: pointer) = 0 1523 ifTrue: [(self fetchClassOf: pointer) = classPointer 1524 ifTrue: [^pointer]]]. 1525 ^NilPointer 1526 */ 1527 1528 classPointer = fetchClassOf(objectPointer); 1529 1530 for(int pointer = objectPointer+2; pointer <= ObjectTableSize-2; pointer += 2) 1531 { 1532 if (hasObject(pointer)) 1533 { 1534 if (fetchClassOf(pointer) == classPointer) 1535 return pointer; 1536 } 1537 } 1538 return NilPointer; 1539 1540 } 1541 1542 1543 1544 // cantBeIntegerObject: 1545 void ObjectMemory::cantBeIntegerObject(int objectPointer) 1546 { 1547 /* "source" 1548 (self isIntegerObject: objectPointer) 1549 "ERROR: Sensor and notify: not defined" 1550 ifTrue: [Sensor notify: 'A small integer has no object table entry'] 1551 */ 1552 1553 assert(!isIntegerObject(objectPointer)); 1554 if (isIntegerObject(objectPointer)) 1555 hal->error("A small integer has no object table entry"); 1556 } 1557 1558 1559 1560 // obtainPointer:location: 1561 int ObjectMemory::obtainPointer_location(int size, int location) 1562 { 1563 int objectPointer; 1564 1565 /* "source" 1566 objectPointer <- self removeFromFreePointerList. 1567 objectPointer isNil ifTrue: [^nil]. 1568 self ot: objectPointer put: 0. 1569 self segmentBitsOf: objectPointer put: currentSegment. 1570 self locationBitsOf: objectPointer put: location. 1571 self sizeBitsOf: objectPointer put: size. 1572 ^objectPointer 1573 */ 1574 RUNTIME_CHECK(currentSegment >= FirstHeapSegment && currentSegment <= LastHeapSegment); 1575 objectPointer = removeFromFreePointerList(); 1576 if (objectPointer == NilPointer) 1577 return NilPointer; 1578 ot_put(objectPointer, 0); 1579 segmentBitsOf_put(objectPointer, currentSegment); 1580 locationBitsOf_put(objectPointer, location); 1581 sizeBitsOf_put(objectPointer, size); 1582 return objectPointer; 1583 } 1584 1585 1586 // attemptToAllocateChunk: 1587 int ObjectMemory::attemptToAllocateChunk(int size) 1588 { 1589 int objectPointer; 1590 1591 /* "source" 1592 objectPointer <- self attemptToAllocateChunkInCurrentSegment: size. 1593 objectPointer isNil ifFalse: [^objectPointer]. 1594 1 to: HeapSegmentCount do: 1595 [ :i | 1596 currentSegment <- currentSegment + 1. 1597 currentSegment > LastHeapSegment 1598 ifTrue: [currentSegment <- FirstHeapSegment]. 1599 self compactCurrentSegment. 1600 objectPointer 1601 <- self attemptToAllocateChunkInCurrentSegment: size. 1602 objectPointer isNil ifFalse: [^objectPointer]]. 1603 ^nil 1604 */ 1605 1606 RUNTIME_CHECK(currentSegment >= FirstHeapSegment && currentSegment <= LastHeapSegment); 1607 objectPointer = attemptToAllocateChunkInCurrentSegment(size); 1608 if (objectPointer != NilPointer) return objectPointer; 1609 for(int i = 1; i <= HeapSegmentCount; i++) 1610 { 1611 currentSegment++; 1612 if (currentSegment > LastHeapSegment) 1613 currentSegment = FirstHeapSegment; 1614 compactCurrentSegment(); 1615 objectPointer = attemptToAllocateChunkInCurrentSegment(size); 1616 if (objectPointer != NilPointer) 1617 return objectPointer; 1618 1619 } 1620 return NilPointer; 1621 } 1622 1623 1624 // attemptToAllocateChunkInCurrentSegment: 1625 int ObjectMemory::attemptToAllocateChunkInCurrentSegment(int size) 1626 { 1627 int objectPointer = NilPointer; 1628 int predecessor; 1629 int next; 1630 int availableSize; 1631 int excessSize; 1632 int newPointer; 1633 1634 /* "source" 1635 ERROR dbanay - LastFreeChunkList is a LOCATION and not a size. Should use BigSize 1636 size < BigSize 1637 ifTrue: [objectPointer <- self removeFromFreeChunkList: size]. 1638 objectPointer notNil 1639 ifTrue: [^objectPointer]. "small chunk of exact size handy so use it" 1640 predecessor <- NonPointer. 1641 "remember predecessor of chunk under consideration" 1642 objectPointer <- self headOfFreeChunkList: LastFreeChunkList 1643 inSegment: currentSegment. 1644 "the search loop stops when the end of the linked list is encountered" 1645 [objectPointer = NonPointer] whileFalse: 1646 [availableSize <- self sizeBitsOf: objectPointer. 1647 availableSize = size 1648 ifTrue: "exact fit - remove from free chunk list and return" 1649 [next <- self classBitsOf: objectPointer. 1650 "the link to the next chunk" 1651 predecessor = NonPointer 1652 ifTrue: "it was the head of the list; make the next item the head" 1653 [self headOfFreeChunkList: LastFreeChunkList 1654 inSegment: currentSegment 1655 put: next] 1656 ifFalse: "it was between two chunks; link them together" 1657 [self classBitsOf: predecessor 1658 put: next]. 1659 ^objectPointer]. 1660 "this chunk was either too big or too small; inspect the amount of variance" 1661 excessSize <- availableSize - size. 1662 excessSize >= HeaderSize 1663 ifTrue: "can be broken into two usable parts: return the second part" 1664 ["obtain an object table entry for the second part" 1665 newPointer <- self obtainPointer: size 1666 location: (self locationBitsOf: objectPointer) 1667 + excessSize. 1668 newPointer isNil ifTrue: [^nil]. 1669 "correct the size of the first part (which remains on the free list)" 1670 self sizeBitsOf: objectPointer put: excessSize. 1671 ^newPointer] 1672 ifFalse: "not big enough to use; try the next chunk on the list" 1673 [predecessor <- objectPointer. 1674 objectPointer <- self classBitsOf: objectPointer]]. 1675 ^nil "the end of the linked list was reached and no fit was found" 1676 */ 1677 RUNTIME_CHECK(currentSegment >= FirstHeapSegment && currentSegment <= LastHeapSegment); 1678 if (size < BigSize) 1679 objectPointer = removeFromFreeChunkList(size); 1680 1681 if (objectPointer != NilPointer) 1682 { 1683 return objectPointer; // small chunk of exact size handy so use it 1684 } 1685 predecessor = NonPointer; // remember predecessor of chunk under consideration 1686 objectPointer = headOfFreeChunkList_inSegment(BigSize, currentSegment); 1687 1688 // the search loop stops when the end of the linked list is encountered 1689 while (objectPointer != NonPointer) 1690 { 1691 availableSize = sizeBitsOf(objectPointer); 1692 if (availableSize == size) 1693 { 1694 // exact fit - remove from free chunk list and return 1695 next = classBitsOf(objectPointer); // the link to the next chunk 1696 if (predecessor == NonPointer) 1697 { 1698 // it was the head of the list; make the next item the head 1699 headOfFreeChunkList_inSegment_put(BigSize, currentSegment, next); 1700 } 1701 else 1702 { 1703 // it was between two chunks; link them together 1704 classBitsOf_put(predecessor, next); 1705 } 1706 return objectPointer; 1707 } 1708 1709 // this chunk was either too big or too small; inspect the amount of variance 1710 excessSize = availableSize - size; 1711 if (excessSize >= HeaderSize) 1712 { 1713 // can be broken into two usable parts: return the second part 1714 // obtain an object table entry for the second part 1715 newPointer = obtainPointer_location(size, locationBitsOf(objectPointer) + excessSize); 1716 if (newPointer == NilPointer) 1717 return NilPointer; 1718 // correct the size of the first part (which remains on the free list) 1719 sizeBitsOf_put(objectPointer, excessSize); 1720 return newPointer; 1721 } 1722 else 1723 { 1724 // not big enough to use; try the next chunk on the list 1725 predecessor = objectPointer; 1726 objectPointer = classBitsOf(objectPointer); 1727 } 1728 } 1729 1730 return NilPointer; // the end of the linked list was reached and no fit was found 1731 } 1732