st80

Smalltalk-80 (image Xerox de 1983) dans le navigateur, VM du livre bleu en WebAssembly
git clone https://git.schurger.org/st80.git
Log | Files | Refs | README

objmemory.h (26269B)


      1 //
      2 //  objmemory.h
      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 #pragma once
     29 
     30 #include <cstdint>
     31 #include <cassert>
     32 #include <functional>
     33 #include "hal.h"
     34 #include "filesystem.h"
     35 #include "realwordmemory.h"
     36 #include "oops.h"
     37 
     38 // The Smalltalk-80 VM generates a tremendous amount of circular references as it runs
     39 //  -- primarily a MethodContext that references a BlockContext (from a temp field) that
     40 // has a back reference to that MethodContext (the sender field). If a reference counting only
     41 // scheme is used, then free object table entries will eventually be consumed. If, on the other hand,
     42 // a GC only approach is used then memory will fill up with contexts and GC will happen fairly
     43 // frequently. Therefore, the hybrid reference counting approach with full garbage collection
     44 // when too much circular garbage accumulates is recommended.
     45 
     46 // GM_MARK_SWEEP and GC_REF_COUNT are not mutually exclusive!
     47 // You can define *BOTH* for a hybrid collector which ref counts until
     48 // memory is exhausted (cyclical data) and then does a full GC
     49 
     50 // Mark and sweep collection when memory full
     51 #define GC_MARK_SWEEP
     52 
     53 // Ref counting
     54 #define GC_REF_COUNT
     55 
     56 
     57 // Define to use recursive marking for ref counting/GC
     58 // If undefined the stack space efficient pointer reversal approach described
     59 // on page 678 of G&R is used. Not recommended, and only included for completeness.
     60 //#define RECURSIVE_MARKING
     61 
     62 // Perform range checks etc. at runtime
     63 #define RUNTIME_CHECKING
     64 
     65 #ifdef RUNTIME_CHECKING
     66 #define RUNTIME_CHECK2(c,f,l) runtime_check(c, "RUNTIME ERROR: (" #c ") at: " f "(" #l ")")
     67 #define RUNTIME_CHECK1(c,f,l) RUNTIME_CHECK2(c,f,l)
     68 #define RUNTIME_CHECK(cond) RUNTIME_CHECK1(cond,  __FILE__, __LINE__)
     69 #else
     70 #define RUNTIME_CHECK(cond) ((void)0)
     71 #endif
     72 
     73 #ifdef GC_MARK_SWEEP
     74 class IGCNotification
     75 {
     76 public:
     77     // About to garbage collect. Client should call addRoot to specify roots of the world
     78     virtual void prepareForCollection() = 0;
     79     // Garbage collection has been completed
     80     virtual void collectionCompleted() = 0;
     81 };
     82 
     83 #endif
     84 
     85 class ObjectMemory
     86 {
     87 public:
     88 #ifdef GC_MARK_SWEEP
     89     ObjectMemory(IHardwareAbstractionLayer *halInterface, IGCNotification *notification = 0);
     90 #else
     91     ObjectMemory(IHardwareAbstractionLayer *halInterface);
     92 #endif
     93     
     94     bool loadSnapshot(IFileSystem *fileSystem, const char *imageFileName);
     95     bool saveSnapshot(IFileSystem *fileSystem, const char *imageFileName);
     96         
     97     
     98     // --- BCIInterface ---
     99 
    100     inline int oopsLeft()
    101     {
    102         return freeOops;
    103     }
    104 
    105     inline std::uint32_t coreLeft()
    106     {
    107         return freeWords;
    108     }
    109 
    110 
    111     void garbageCollect();
    112     
    113     // storePointer:ofObject:withValue:
    114     int storePointer_ofObject_withValue(int fieldIndex, int objectPointer, int valuePointer);
    115     
    116     // storeWord:ofObject:withValue:
    117     int storeWord_ofObject_withValue(int wordIndex, int objectPointer, int valueWord);
    118     
    119     // increaseReferencesTo:
    120     inline void increaseReferencesTo(int objectPointer)
    121     {
    122         /* "source"
    123          self countUp: objectPointer
    124         */
    125 #ifdef GC_REF_COUNT
    126          countUp(objectPointer);
    127 #endif
    128     }
    129     
    130     // initialInstanceOf:
    131     int initialInstanceOf(int classPointer);
    132     
    133     // decreaseReferencesTo:
    134     inline void decreaseReferencesTo(int objectPointer)
    135     {
    136        /* "source"
    137         self countDown: objectPointer
    138        */
    139 #ifdef GC_REF_COUNT
    140         countDown(objectPointer);
    141 #endif
    142     }
    143 
    144     
    145     // isIntegerValue:
    146     inline bool isIntegerValue(int valueWord)
    147     {
    148        /* "source"
    149         "ERROR: G&R really cock this up"
    150         "dbanay - still broken in July 1985 ed!"
    151         ^valueWord >= -16384 and: [valueWord <= 16383]
    152        */
    153        
    154         return valueWord >= -16384 && valueWord <= 16383;
    155     }
    156 
    157     
    158     // fetchWord:ofObject:
    159     inline int fetchWord_ofObject(int wordIndex, int objectPointer)
    160     {
    161        /* "source"
    162         ^self heapChunkOf: objectPointer word: HeaderSize + wordIndex
    163        */
    164        
    165         RUNTIME_CHECK(wordIndex >= 0 && wordIndex < fetchWordLengthOf(objectPointer));
    166         return heapChunkOf_word(objectPointer, HeaderSize + wordIndex);
    167     }
    168 
    169     
    170     // integerValueOf:
    171     inline int integerValueOf(int objectPointer)
    172     {
    173        /* "source"
    174            ^objectPointer/2
    175        */
    176      
    177         
    178         return (std::int16_t)(objectPointer & 0xfffe)/2;
    179         // Right shifting a negative number is undefined according to the standard.
    180         // return ((std::int16_t) objectPointer) >> 1;
    181     }
    182     
    183     // swapPointersOf:and:
    184     void swapPointersOf_and(int firstPointer, int secondPointer);
    185     
    186     // fetchWordLengthOf:
    187     inline int fetchWordLengthOf(int objectPointer)
    188     {
    189        /* "source"
    190         ^(self sizeBitsOf: objectPointer) - HeaderSize
    191        */
    192 
    193         return sizeBitsOf(objectPointer) - HeaderSize;
    194     }
    195     
    196     // instantiateClass:withWords:
    197     int instantiateClass_withWords(int classPointer, int length);
    198     
    199     // isIntegerObject:
    200     inline bool isIntegerObject(int objectPointer)
    201     {
    202        /* "source"
    203         ^(objectPointer bitAnd: 1) = 1
    204        */
    205        
    206         return (objectPointer & 1) == 1;
    207     }
    208 
    209     
    210     // instantiateClass:withBytes:
    211     int instantiateClass_withBytes(int classPointer, int length);
    212     
    213     // hasObject:
    214     bool hasObject(int objectPointer);
    215     
    216     // instantiateClass:withPointers:
    217     int instantiateClass_withPointers(int classPointer, int length);
    218     
    219     // fetchByte:ofObject:
    220     inline int fetchByte_ofObject(int byteIndex, int objectPointer)
    221     {
    222         /* "source"
    223          ^self heapChunkOf: objectPointer byte: (HeaderSize*2 + byteIndex)
    224          */
    225         return heapChunkOf_byte(objectPointer, (HeaderSize*2 + byteIndex));
    226     }
    227     
    228     // fetchPointer:ofObject:
    229     inline int fetchPointer_ofObject(int fieldIndex, int objectPointer)
    230     {
    231         /* "source"
    232          ^self heapChunkOf: objectPointer word: HeaderSize + fieldIndex
    233          */
    234         RUNTIME_CHECK(fieldIndex >= 0 && fieldIndex < fetchWordLengthOf(objectPointer));
    235         return heapChunkOf_word(objectPointer, HeaderSize + fieldIndex);
    236     }
    237     
    238     // fetchClassOf:
    239     inline int fetchClassOf(int objectPointer)
    240     {
    241         /* Note that fetchClassOf:objectPointer returns IntegerClass (the object table index of SmallInteger)
    242             if its argument is an immediate integer. G&R pg 686 */
    243        /* "source"
    244         (self isIntegerObject: objectPointer)
    245             ifTrue: [^IntegerClass] "ERROR IntegerClass not defined"
    246             ifFalse: [^self classBitsOf: objectPointer]
    247        */
    248         
    249         if (isIntegerObject(objectPointer))
    250             return ClassSmallInteger;
    251         
    252         return classBitsOf(objectPointer);
    253 
    254     }
    255     
    256     // integerObjectOf:
    257     inline int integerObjectOf(int value)
    258     {
    259        /* "source"
    260         ^(value bitShift: 1) + 1
    261        */
    262         return (std::uint16_t) ((value << 1) | 1);
    263     }
    264     
    265     // fetchByteLengthOf:
    266     inline int fetchByteLengthOf(int objectPointer)
    267     {
    268        /* "source"
    269         "ERROR in selector of next line"
    270         ^(self fetchWordLengthOf: objectPointer)*2 - (self oddBitOf: objectPointer)
    271        */
    272         return fetchWordLengthOf(objectPointer)*2 - oddBitOf(objectPointer);
    273     }
    274 
    275     
    276     // instanceAfter:
    277     int instanceAfter(int objectPointer);
    278     
    279     // storeByte:ofObject:withValue:
    280     inline int storeByte_ofObject_withValue(int byteIndex, int objectPointer, int valueByte)
    281     {
    282        /* "source"
    283         ^self heapChunkOf: objectPointer
    284             byte: (HeaderSize*2 + byteIndex)
    285             put: valueByte
    286        */
    287        
    288         return heapChunkOf_byte_put(objectPointer,
    289                 HeaderSize*2 + byteIndex,
    290                 valueByte);
    291     }
    292     
    293     
    294     // --- ObjectPointers ---
    295     
    296     // cantBeIntegerObject:
    297     void cantBeIntegerObject(int objectPointer);
    298         
    299     #ifdef GC_MARK_SWEEP
    300         void addRoot(int rootObjectPointer) //dbanay
    301         {
    302             markObjectsAccessibleFrom(rootObjectPointer);
    303         }
    304     #endif
    305     
    306 private:
    307     
    308     // --- Compaction ---
    309     
    310     // sweepCurrentSegmentFrom:
    311     int sweepCurrentSegmentFrom(int lowWaterMark);
    312     
    313     // compactCurrentSegment
    314     void compactCurrentSegment();
    315     
    316     // releasePointer:
    317     void releasePointer(int objectPointer);
    318     
    319     // reverseHeapPointersAbove:
    320     void reverseHeapPointersAbove(int lowWaterMark);
    321     
    322     // abandonFreeChunksInSegment:
    323     int abandonFreeChunksInSegment(int segment);
    324     
    325     // allocateChunk:
    326     int allocateChunk(int size);
    327     
    328 #ifdef GC_MARK_SWEEP
    329     // --- MarkingGarbage ---
    330     
    331     // reclaimInaccessibleObjects
    332     void reclaimInaccessibleObjects();
    333     
    334     // markObjectsAccessibleFrom:
    335     int markObjectsAccessibleFrom(int rootObjectPointer);
    336     
    337     // markAccessibleObjects
    338     void markAccessibleObjects();
    339     
    340     // rectifyCountsAndDeallocateGarbage
    341     void rectifyCountsAndDeallocateGarbage();
    342     
    343     // zeroReferenceCounts
    344     void zeroReferenceCounts();
    345     
    346 #endif
    347     
    348     // --- NonpointerObjs ---
    349     
    350     // lastPointerOf:
    351     int lastPointerOf(int objectPointer);
    352     
    353     // spaceOccupiedBy:
    354     int spaceOccupiedBy(int objectPointer);
    355     
    356     // allocate:odd:pointer:extra:class:
    357     int allocate_odd_pointer_extra_class(
    358                                          int size,
    359                                          int oddBit,
    360                                          int pointerBit,
    361                                          int extraWord,
    362                                          int classPointer
    363                                          );
    364     
    365     
    366     // --- UnallocatedSpc ---
    367     
    368     // headOfFreePointerList
    369     int headOfFreePointerList();
    370     
    371     // toFreeChunkList:add:
    372     void toFreeChunkList_add(int size, int objectPointer);
    373     
    374     // headOfFreeChunkList:inSegment:put:
    375     int headOfFreeChunkList_inSegment_put(int size, int segment, int objectPointer);
    376     
    377     // removeFromFreePointerList
    378     int removeFromFreePointerList();
    379     
    380     // toFreePointerListAdd:
    381     void toFreePointerListAdd(int objectPointer);
    382     
    383     // removeFromFreeChunkList:
    384     int removeFromFreeChunkList(int size);
    385     
    386     // resetFreeChunkList:inSegment:
    387     void resetFreeChunkList_inSegment(int size, int segment);
    388     
    389     // headOfFreeChunkList:inSegment:
    390     int headOfFreeChunkList_inSegment(int size, int segment);
    391     
    392     // headOfFreePointerListPut:
    393     int headOfFreePointerListPut(int objectPointer);
    394     
    395     
    396     // --- RefCntGarbage ---
    397     
    398     // countDown:
    399     int countDown(int rootObjectPointer);
    400     
    401     // countUp:
    402     int countUp(int objectPointer);
    403     
    404     // deallocate:
    405     void deallocate(int objectPointer);
    406     
    407     // forAllOtherObjectsAccessibleFrom:suchThat:do:
    408     int forAllOtherObjectsAccessibleFrom_suchThat_do(
    409                                                      int objectPointer,
    410                                                      const std::function <bool (int)>& predicate,
    411                                                      const std::function <void (int)>& action
    412                                                      );
    413     
    414     // forAllObjectsAccessibleFrom:suchThat:do:
    415     int forAllObjectsAccessibleFrom_suchThat_do(int objectPointer,
    416                                                 const std::function <bool (int)>& predicate,
    417                                                 const std::function <void (int)>& action);
    418 
    419     
    420     // --- ObjectTableEnt ---
    421     
    422     // segmentBitsOf:
    423     inline int segmentBitsOf(int objectPointer)
    424     {
    425        /* "source"
    426         ^self ot: objectPointer bits: 12 to: 15
    427        */
    428        
    429         return ot_bits_to(objectPointer, 12, 15);
    430     }
    431     
    432     // heapChunkOf:byte:put:
    433     inline int heapChunkOf_byte_put(int objectPointer, int offset, int value)
    434     {
    435        /* "source"
    436         ^wordMemory segment: (self segmentBitsOf: objectPointer)
    437             word: ((self locationBitsOf: objectPointer) + (offset//2))
    438             byte: (offset\\2) put: value
    439        */
    440        
    441         return wordMemory.segment_word_byte_put(segmentBitsOf(objectPointer),
    442                 locationBitsOf(objectPointer) + (offset/2),
    443                 offset % 2, value);
    444     }
    445 
    446     
    447     // pointerBitOf:put:
    448     inline int pointerBitOf_put(int objectPointer, int value)
    449     {
    450        /* "source"
    451         ^self ot: objectPointer bits: 9 to: 9 put: value
    452        */
    453        
    454         return ot_bits_to_put(objectPointer, 9, 9, value);
    455     }
    456 
    457     
    458     // heapChunkOf:word:
    459     inline int heapChunkOf_word(int objectPointer, int offset)
    460     {
    461        /* "source"
    462         ^wordMemory segment: (self segmentBitsOf: objectPointer)
    463             word: ((self locationBitsOf: objectPointer) + offset)
    464        */
    465         return wordMemory.segment_word(segmentBitsOf(objectPointer),
    466                 locationBitsOf(objectPointer) + offset);
    467     }
    468     
    469     // segmentBitsOf:put:
    470     inline int segmentBitsOf_put(int objectPointer, int value)
    471     {
    472         /* "source"
    473          ^self ot: objectPointer bits: 12 to: 15 put: value
    474          */
    475         
    476         return ot_bits_to_put(objectPointer, 12, 15, value);
    477     }
    478     
    479     // heapChunkOf:word:put:
    480     inline int heapChunkOf_word_put(int objectPointer, int offset, int value)
    481     {
    482        /* "source"
    483         ^wordMemory segment: (self segmentBitsOf: objectPointer)
    484             word: ((self locationBitsOf: objectPointer) + offset)
    485             put: value
    486        */
    487         return wordMemory.segment_word_put(segmentBitsOf(objectPointer),
    488                 locationBitsOf(objectPointer) + offset,
    489                 value);
    490     }
    491     
    492     // oddBitOf:
    493     inline int oddBitOf(int objectPointer)
    494     {
    495         /* "source"
    496          ^self ot: objectPointer bits: 8 to: 8
    497          */
    498         
    499         return ot_bits_to(objectPointer, 8, 8);
    500     }
    501 
    502     
    503     // freeBitOf:
    504     inline int freeBitOf(int objectPointer)
    505     {
    506        /* "source"
    507         ^self ot: objectPointer bits: 10 to: 10
    508        */
    509        
    510         return ot_bits_to(objectPointer, 10, 10);
    511     }
    512     
    513     // locationBitsOf:
    514     inline int locationBitsOf(int objectPointer)
    515     {
    516        /* "source"
    517         self cantBeIntegerObject: objectPointer.
    518         ^wordMemory segment: ObjectTableSegment
    519             word: ObjectTableStart + objectPointer + 1
    520        */
    521         cantBeIntegerObject(objectPointer);
    522         return wordMemory.segment_word(ObjectTableSegment,
    523             ObjectTableStart + objectPointer + 1);
    524     }
    525     
    526     // ot:
    527     inline int ot(int objectPointer)
    528     {
    529 
    530        /* "source"
    531         self cantBeIntegerObject: objectPointer.
    532         ^wordMemory segment: ObjectTableSegment
    533             word: ObjectTableStart + objectPointer
    534        */
    535        
    536         cantBeIntegerObject(objectPointer);
    537         return wordMemory.segment_word(ObjectTableSegment,
    538             ObjectTableStart + objectPointer);
    539     }
    540     
    541     // freeBitOf:put:
    542     inline int freeBitOf_put(int objectPointer, int value)
    543     {
    544        /* "source"
    545         ^self ot: objectPointer bits: 10 to: 10 put: value
    546        */
    547        
    548         return ot_bits_to_put(objectPointer, 10, 10, value);
    549     }
    550     
    551     // classBitsOf:put:
    552     inline int classBitsOf_put(int objectPointer, int value)
    553     {
    554        /* "source"
    555         ^self heapChunkOf: objectPointer word: 1 put: value
    556        */
    557        
    558         return heapChunkOf_word_put(objectPointer, 1, value);
    559     }
    560     
    561     // heapChunkOf:byte:
    562     inline int heapChunkOf_byte(int objectPointer, int offset)
    563     {
    564        /* "source"
    565         ^wordMemory segment: (self segmentBitsOf: objectPointer)
    566             word: ((self locationBitsOf: objectPointer) + (offset//2))
    567             byte: (offset\\2)
    568        */
    569 
    570         return wordMemory.segment_word_byte(segmentBitsOf(objectPointer),
    571                         locationBitsOf(objectPointer) + offset/2,
    572                         offset % 2);
    573     }
    574 
    575     
    576     // locationBitsOf:put:
    577     inline int locationBitsOf_put(int objectPointer, int value)
    578     {
    579        /* "source"
    580         self cantBeIntegerObject: objectPointer.
    581         ^wordMemory segment: ObjectTableSegment
    582             word: ObjectTableStart + objectPointer + 1
    583             put: value
    584        */
    585         cantBeIntegerObject(objectPointer);
    586         return wordMemory.segment_word_put(ObjectTableSegment,
    587             ObjectTableStart + objectPointer + 1,
    588             value);
    589     }
    590 
    591     
    592     // sizeBitsOf:
    593     inline int sizeBitsOf(int objectPointer)
    594     {
    595        /* "source"
    596         ^self heapChunkOf: objectPointer word: 0
    597        */
    598         
    599         return heapChunkOf_word(objectPointer, 0);
    600     }
    601 
    602     
    603     // oddBitOf:put:
    604     inline int oddBitOf_put(int objectPointer, int value)
    605     {
    606        /* "source"
    607         ^self ot: objectPointer bits: 8 to: 8 put: value
    608        */
    609        return ot_bits_to_put(objectPointer, 8, 8, value);
    610     }
    611     
    612     // ot:put:
    613     inline int ot_put(int objectPointer, int value)
    614     {
    615        /* "source"
    616         self cantBeIntegerObject: objectPointer.
    617         ^wordMemory segment: ObjectTableSegment
    618             word: ObjectTableStart + objectPointer
    619             put: value
    620        */
    621        
    622         cantBeIntegerObject(objectPointer);
    623         return wordMemory.segment_word_put(ObjectTableSegment,
    624                 ObjectTableStart + objectPointer,
    625                 value);
    626     }
    627 
    628     // countBitsOf:put:
    629     inline int countBitsOf_put(int objectPointer, int value)
    630     {
    631        /* "source"
    632         ^self ot: objectPointer bits: 0 to: 7 put: value
    633        */
    634        
    635       return ot_bits_to_put(objectPointer, 0, 7, value);
    636     }
    637     
    638     // classBitsOf:
    639     inline int classBitsOf(int objectPointer)
    640     {
    641         /* "source"
    642          ^self heapChunkOf: objectPointer word: 1
    643          */
    644         return heapChunkOf_word(objectPointer, 1);
    645         
    646     }
    647     
    648     // countBitsOf:
    649     inline int countBitsOf(int objectPointer)
    650     {
    651        /* "source"
    652         ^self ot: objectPointer bits: 0 to: 7
    653        */
    654        
    655         return ot_bits_to(objectPointer, 0, 7);
    656     }
    657     
    658     // ot:bits:to:put:
    659     inline int ot_bits_to_put(
    660                        int objectPointer,
    661                        int firstBitIndex,
    662                        int lastBitIndex,
    663                        int value
    664                        )
    665     {
    666        /* "source"
    667         self cantBeIntegerObject: objectPointer.
    668         ^wordMemory segment: ObjectTableSegment
    669             word: ObjectTableStart + objectPointer
    670             bits: firstBitIndex
    671             to: lastBitIndex
    672             put: value
    673        */
    674        
    675         cantBeIntegerObject(objectPointer);
    676         return wordMemory.segment_word_bits_to_put(ObjectTableSegment,
    677                     ObjectTableStart + objectPointer,
    678                     firstBitIndex,
    679                     lastBitIndex,
    680                     value);
    681     }
    682     
    683     // sizeBitsOf:put:
    684     inline int sizeBitsOf_put(int objectPointer, int value)
    685     {
    686         /* "source"
    687          ^self heapChunkOf: objectPointer word: 0 put: value
    688          */
    689         
    690         return heapChunkOf_word_put(objectPointer, 0, value);
    691     }
    692     
    693     // ot:bits:to:
    694     inline int ot_bits_to(int objectPointer, int firstBitIndex, int lastBitIndex)
    695     {
    696        /* "source"
    697         self cantBeIntegerObject: objectPointer.
    698         ^wordMemory segment: ObjectTableSegment
    699             word: ObjectTableStart + objectPointer
    700             bits: firstBitIndex
    701             to: lastBitIndex
    702        */
    703        
    704         cantBeIntegerObject(objectPointer);
    705         return wordMemory.segment_word_bits_to(ObjectTableSegment,
    706                  ObjectTableStart + objectPointer,
    707                  firstBitIndex, lastBitIndex);
    708 
    709     }
    710     
    711     // pointerBitOf:
    712     inline int pointerBitOf(int objectPointer)
    713     {
    714        /* "source"
    715         ^self ot: objectPointer bits: 9 to: 9
    716        */
    717        
    718         return ot_bits_to(objectPointer, 9, 9);
    719     }
    720 
    721     
    722     // --- Allocation ---
    723     
    724     // obtainPointer:location:
    725     int obtainPointer_location(int size, int location);
    726     
    727     // attemptToAllocateChunk:
    728     int attemptToAllocateChunk(int size);
    729     
    730     // attemptToAllocateChunkInCurrentSegment:
    731     int attemptToAllocateChunkInCurrentSegment(int size);
    732     
    733     void outOfMemoryError();
    734     
    735     int auditFreeOops();
    736     
    737 #ifdef RUNTIME_CHECKING
    738     inline void runtime_check(bool condition, const char *errorMessage)
    739     {
    740         if (!condition)
    741         {
    742             assert(0);
    743             hal->error(errorMessage);
    744         }
    745     }
    746 #endif
    747     
    748 private:
    749     RealWordMemory wordMemory;
    750     // Special Register G&R pg. 667
    751     int currentSegment; // The index of the heap segment currently being used for allocation
    752 
    753     int freeWords; // free words remaining (make primitiveFreeCore "fast")
    754     
    755     // An a table entry with a free bit set OR that contains a reference to a free chunk
    756     // (free bit clear but count field zero) of memory is counted as a free oop
    757     int freeOops;  // free OT entries (make primitiveFreeOops "fast")
    758     
    759     
    760     // G&R pg. 664 - Object Table Related Constants
    761     // Object Table Segment (last segment) contains the Object Table followed by the
    762     // head of the OT free pointer list
    763     // +-------------------------+
    764     // |                         | <--- ObjectTableStart
    765     // |                         |
    766     // |                         |
    767     // |      Object Table       |
    768     // |                         |
    769     // |                         |
    770     // +-------------------------+
    771     // |     FreePointerList     |
    772     // +-------------------------+
    773     // |////// UNUSED WORD //////|
    774     // +-------------------------+
    775     //
    776     
    777     static const int ObjectTableSegment = RealWordMemory::SegmentCount-1;
    778     static const int ObjectTableStart = 0;
    779     static const int ObjectTableSize = RealWordMemory::SegmentSize - 2;
    780     // The smallest number that is too large to represent in an eight-bit count field; that is, 256.
    781     static const int HugeSize = 256; // G&R pg 661
    782     
    783     // The location of the head of the linked list of free object table entries
    784     static const int FreePointerList = ObjectTableStart + ObjectTableSize; // G&R pg. 664
    785 
    786     // G&R pg. 664 - Object Table Related Constants
    787     // The smallest size of chunk that is not stored on a list whose chunk share the same size.
    788     // (Theindex of the last free chunk list).
    789     static const int BigSize = 20;
    790     static const int FirstFreeChunkListSize = BigSize+1;
    791 
    792     
    793     // Heap Constants G&R pg. 658
    794     
    795     // The number of heaps segments used in the implementation.
    796     // We reserve the last segment for the Object Table and use the remaining for the heap
    797     static const int HeapSegmentCount = RealWordMemory::SegmentCount - 1;
    798     
    799     // Each heap segment is organized as follows:
    800     //
    801     // +-------------------------+
    802     // |                         |
    803     // |                         |
    804     // |     Object Storage      |
    805     // |                         |
    806     // |                         |<--- HeapSpaceStop (last word)
    807     // +-------------------------+
    808     // |   Array of BigSize+1    |<--- FirstFreeChunkList
    809     // |   Free Chunks Linked    |
    810     // |   List Heads            |
    811     // |                         |<--- LastFreeChunkList
    812     // +-------------------------+
    813     //
    814     
    815     // The index of the first memory segmentused to store the heap
    816     static const int FirstHeapSegment = 0;
    817     static const int LastHeapSegment = FirstHeapSegment + HeapSegmentCount - 1;
    818     
    819     // The address of the last location used in each heap segment.
    820     static const int HeapSpaceStop = RealWordMemory::SegmentSize - FirstFreeChunkListSize - 1;
    821     static const int HeaderSize = 2; // The number of words in an object header(2).
    822     // If HeaderSize changes, revisit forAllOtherObjectsAccessibleFrom_suchThat_do
    823     // where we test if the offset passes the class field...
    824     
    825     // The location of the head of the linked list of free chunks of size zero. Comes right
    826     // after the last word for object storage.
    827     static const int FirstFreeChunkList = HeapSpaceStop + 1;
    828     
    829     // The bluebook incorrectly uses LastFreeChunkList in all places it is used! The
    830     // headOfFreeChunkList:inSegment: and headOfFreeChunkList:inSegment:put methods take
    831     // a SIZE as the first parameter not a location.
    832     // The location of the head of the linked list of free chunks of size BigSize or larger.
    833     // static const int LastFreeChunkList =  FirstFreeChunkList + BigSize;
    834     
    835     // Any sixteen-bit value that cannot be an object table index, e.g.,2**16~1.
    836     static const int NonPointer = 65535;
    837     // Last special oop
    838     // (See SystemTracer in Smalltalk.sources)
    839     static const int LastSpecialOop = 52;
    840 
    841     
    842     // Snapshots
    843     // Object space starts at offset 512 in the image
    844     static const int ObjectSpaceBaseInImage = 512;
    845 
    846     bool loadObjectTable( IFileSystem *fileSystem, int fd);
    847     static bool padToPage(IFileSystem *fileSystem, int fd);
    848     bool loadObjects(IFileSystem *fileSystem, int fd);
    849     bool saveObjects(IFileSystem *fileSystem, int fd);
    850 
    851 #ifdef GC_MARK_SWEEP
    852     IGCNotification *gcNotification;
    853 #endif
    854     
    855     // Interface to the host operating system
    856     IHardwareAbstractionLayer *hal;
    857 };
    858