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).

1  
!include once #1027304 // Eclipse Collections
2  
3  
sclass ManagedIntObjects64_Collector implements IManagedObjectCollector64 {
4  
  ManagedIntObjects64 mem;
5  
  CompactTreeSet<LongRange> objects = new(longRangeComparatorByStart());
6  
  new LongHashSet pointers;
7  
  new CompactHashSet<LongRange> pointerRanges;
8  
  new LongLongHashMap relocationMap;
9  
  long[] newMem;
10  
  
11  
  new Map<Long, IVF1<Long>> relocationCallbacks;
12  
13  
  bool discardFreeListEarly; // saves memory, but ironically makes original memory unusable when compacting fails because of OOM
14  
  bool verbose = true;
15  
  
16  
  *(ManagedIntObjects64 *mem) {}
17  
  
18  
  // notify collector that there is an object in a location
19  
  public void noteObject(long start, long size, IVF1<Long> updateAddress default null) {
20  
    objects.add(longRange(start, start+size));
21  
    if (updateAddress != null)
22  
      relocationCallbacks.put(start, updateAddress);
23  
  }
24  
  
25  
  // notify collector that there is a pointer in a location
26  
  public void notePointer(long addr) {
27  
    if (addr != 0)
28  
      pointers.add(addr);
29  
  }
30  
  
31  
  // notify collector that there is a pointer array in a location
32  
  public void notePointerArray(long start) {
33  
    if (start == 0) ret;
34  
    long n = mem.get(start);
35  
    noteObject(start, n+1);
36  
    notePointerRange(LongRange(start+1, start+1+n));
37  
  }
38  
  
39  
  void notePointerRange(LongRange r) {
40  
    pointerRanges.add(r);
41  
  }
42  
  
43  
  int sizeNeeded() {
44  
    ret toInt(mem.reservedSpaceAtBeginning()+totalLongRangesLength(objects));
45  
  }
46  
  
47  
  void collectAndCompact() {
48  
    int sizeNeeded = sizeNeeded();
49  
    if (mem.size() == sizeNeeded) ret;
50  
    
51  
    if (discardFreeListEarly) mem.freeList.clear();
52  
    
53  
    // reserve new memory
54  
    print("Reserving " + toM(sizeNeeded*(long) mem.wordSizeInBytes()) + " MB");
55  
    newMem = new long[sizeNeeded];
56  
    
57  
    // copy all the objects, fill relocationMap
58  
    print("Copying " + nObjects(objects) + " (" + n2(relocationCallbacks, "callback") + ")");
59  
    long freePtr = mem.reservedSpaceAtBeginning();
60  
    for (LongRange r : objects) {
61  
      long oldPtr = r.start, newPtr = freePtr;
62  
      if (oldPtr != newPtr) {
63  
        relocationMap.put(oldPtr, newPtr);
64  
        callF(relocationCallbacks.get(oldPtr), freePtr);
65  
      }
66  
      arraycopy(mem.mem, r.start, newMem, freePtr, r.length());
67  
      freePtr += r.length();
68  
    }
69  
    relocationCallbacks.clear();
70  
    
71  
    // update the pointers
72  
    print("Updating " + n2(pointers.size(), "pointer"));
73  
    LongIterator itPointers = pointers.longIterator();
74  
    while (itPointers.hasNext())
75  
      movePointer(itPointers.next());
76  
      
77  
    print("Updating " + n2(pointerRanges.size(), "pointer range"));
78  
    for (LongRange r : pointerRanges) {
79  
      for (long i = r.start; i < r.end; i++)
80  
        movePointer(i);
81  
    }
82  
83  
    print("Managed GC done");
84  
    mem.mem = newMem;
85  
    if (!discardFreeListEarly) mem.freeList.clear();
86  
  }
87  
  
88  
  // call after collectAndCompact to get new location of objects
89  
  public long getNewLocation(long addr) {
90  
    ret relocationMap.getIfAbsent(addr, addr);
91  
  }
92  
  
93  
  void movePointer(long pointerAddr) {
94  
    // find enclosing object
95  
    LongRange obj = objects.floor(LongRange(pointerAddr, pointerAddr));
96  
    if (obj == null) ret with print("Pointer without object");
97  
    
98  
    long newObjAddr = relocationMap.getIfAbsent(obj.start, obj.start);
99  
    long newPointerAddr = newObjAddr-obj.start+pointerAddr;
100  
    
101  
    // find target, update pointer
102  
    long target = mem.get(pointerAddr);
103  
    if (target == 0) ret;
104  
    
105  
    long newTarget = relocationMap.getIfAbsent(target, -1);
106  
    if (newTarget >= 0)
107  
      newMem[newPointerAddr] = newTarget;
108  
  }
109  
}

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: 253 / 306
Version history: 7 change(s)
Referenced in: [show references]