Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

109
LINES

< > BotCompany Repo | #1029336 // ManagedIntObjects64_Collector [dev., compacting collector for managed memory]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 11335K of libraries. Compilation Failed (4531L/29K).

!include once #1027304 // Eclipse Collections

sclass ManagedIntObjects64_Collector implements IManagedObjectCollector64 {
  ManagedIntObjects64 mem;
  CompactTreeSet<LongRange> objects = new(longRangeComparatorByStart());
  new LongHashSet pointers;
  new CompactHashSet<LongRange> pointerRanges;
  new LongLongHashMap relocationMap;
  long[] newMem;
  
  new Map<Long, IVF1<Long>> relocationCallbacks;

  bool discardFreeListEarly; // saves memory, but ironically makes original memory unusable when compacting fails because of OOM
  bool verbose = true;
  
  *(ManagedIntObjects64 *mem) {}
  
  // notify collector that there is an object in a location
  public void noteObject(long start, long size, IVF1<Long> updateAddress default null) {
    objects.add(longRange(start, start+size));
    if (updateAddress != null)
      relocationCallbacks.put(start, updateAddress);
  }
  
  // notify collector that there is a pointer in a location
  public void notePointer(long addr) {
    if (addr != 0)
      pointers.add(addr);
  }
  
  // notify collector that there is a pointer array in a location
  public void notePointerArray(long start) {
    if (start == 0) ret;
    long n = mem.get(start);
    noteObject(start, n+1);
    notePointerRange(LongRange(start+1, start+1+n));
  }
  
  void notePointerRange(LongRange r) {
    pointerRanges.add(r);
  }
  
  int sizeNeeded() {
    ret toInt(mem.reservedSpaceAtBeginning()+totalLongRangesLength(objects));
  }
  
  void collectAndCompact() {
    int sizeNeeded = sizeNeeded();
    if (mem.size() == sizeNeeded) ret;
    
    if (discardFreeListEarly) mem.freeList.clear();
    
    // reserve new memory
    print("Reserving " + toM(sizeNeeded*(long) mem.wordSizeInBytes()) + " MB");
    newMem = new long[sizeNeeded];
    
    // copy all the objects, fill relocationMap
    print("Copying " + nObjects(objects) + " (" + n2(relocationCallbacks, "callback") + ")");
    long freePtr = mem.reservedSpaceAtBeginning();
    for (LongRange r : objects) {
      long oldPtr = r.start, newPtr = freePtr;
      if (oldPtr != newPtr) {
        relocationMap.put(oldPtr, newPtr);
        callF(relocationCallbacks.get(oldPtr), freePtr);
      }
      arraycopy(mem.mem, r.start, newMem, freePtr, r.length());
      freePtr += r.length();
    }
    relocationCallbacks.clear();
    
    // update the pointers
    print("Updating " + n2(pointers.size(), "pointer"));
    LongIterator itPointers = pointers.longIterator();
    while (itPointers.hasNext())
      movePointer(itPointers.next());
      
    print("Updating " + n2(pointerRanges.size(), "pointer range"));
    for (LongRange r : pointerRanges) {
      for (long i = r.start; i < r.end; i++)
        movePointer(i);
    }

    print("Managed GC done");
    mem.mem = newMem;
    if (!discardFreeListEarly) mem.freeList.clear();
  }
  
  // call after collectAndCompact to get new location of objects
  public long getNewLocation(long addr) {
    ret relocationMap.getIfAbsent(addr, addr);
  }
  
  void movePointer(long pointerAddr) {
    // find enclosing object
    LongRange obj = objects.floor(LongRange(pointerAddr, pointerAddr));
    if (obj == null) ret with print("Pointer without object");
    
    long newObjAddr = relocationMap.getIfAbsent(obj.start, obj.start);
    long newPointerAddr = newObjAddr-obj.start+pointerAddr;
    
    // find target, update pointer
    long target = mem.get(pointerAddr);
    if (target == 0) ret;
    
    long newTarget = relocationMap.getIfAbsent(target, -1);
    if (newTarget >= 0)
      newMem[newPointerAddr] = newTarget;
  }
}

Author comment

Began life as a copy of #1029237

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

-
Snippet ID: #1029336
Snippet name: ManagedIntObjects64_Collector [dev., compacting collector for managed memory]
Eternal ID of this version: #1029336/8
Text MD5: 07b8534b74feb110a978ccc6eadb5acf
Transpilation MD5: 92addced1d789cf393ead2ef85bd8a8d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-08-01 12:53:44
Source code size: 3688 bytes / 109 lines
Pitched / IR pitched: No / No
Views / Downloads: 252 / 305
Version history: 7 change(s)
Referenced in: