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

128
LINES

< > BotCompany Repo | #1029330 // ManagedIntObjects64 [dev., manage our own objects in an int array, allow > 16GB]

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

Libraryless. Compilation Failed (3907L/25K).

1  
// all sizes+addresses are in longs (8 bytes, 2 words)
2  
// longs are stored big-endian (like twoIntsToLong)
3  
asclass ManagedIntObjects64 {
4  
  replace Addr with long.
5  
  replace Addr2 with Long.
6  
  
7  
  new FreeList64 freeList;
8  
  
9  
  *() {
10  
    alloc(reservedSpaceAtBeginning()); // so 0 is usable as the null pointer
11  
  }
12  
  
13  
  // overridable
14  
  public abstract long size();
15  
  public abstract int get(Addr i);
16  
  public abstract void set(Addr i, int val);
17  
  
18  
  public void setLong(Addr i, long val) {
19  
    set(i, firstIntFromLong(val));
20  
    set(i+1, secondIntFromLong(val));
21  
  }
22  
  
23  
  public long getLong(Addr i) {
24  
    ret twoIntsToLong(get(i), get(i+1));
25  
  }
26  
  
27  
  swappable void onMemorySizeChanged() {}
28  
  
29  
  void ensureCapacity(long size) {
30  
    if (memorySize() < size)
31  
      setMemorySize(roundUpToPowerOfTwo(size));
32  
  }
33  
  
34  
  abstract void setMemorySize(long newSize);
35  
36  
  void growMemory() {
37  
    setMemorySize(max(1, memorySize()*2));
38  
  }
39  
  
40  
  // all allocated ranges are filled with zero
41  
  long alloc(long length) {
42  
    LongRange r;
43  
    while ((r = freeList.findFreeSpace(length)) == null)
44  
      growMemory();
45  
    
46  
    freeList.remove(LongRange(r.start, r.start+length));
47  
    ret r.start;
48  
  }
49  
  
50  
  void free(long start, long length) {
51  
    if (length == 0) ret;
52  
    freeList.add(LongRange(start, start+length));
53  
    clearMemory(start, length);
54  
  }
55  
  
56  
  abstract void clearMemory(long start, long length);
57  
  
58  
  abstract long memorySize();
59  
  final int read(Addr i) { ret get(i); }
60  
  final long readLong(Addr i) { ret getLong(i); }
61  
  final void write(Addr i, int val) { set(i, val); }
62  
  final void writeLong(Addr i, long val) { setLong(i, val); }
63  
  
64  
  // words a pointer requires
65  
  long sizeOfPointer() { ret 2; }
66  
  int wordSizeInBytes() { ret 4; }
67  
  
68  
  public Addr nullPtr() { ret 0; }
69  
  
70  
  // convenience pointer array handling (memory layout: length + data)
71  
  
72  
  // it's a List, so max size 31 bit
73  
  L<Addr2> pointerArray(Addr addr) {
74  
    if (addr == 0) null;
75  
    int size = get(addr);
76  
    ret new RandomAccessAbstractList<Addr2>() {
77  
      public int size() { ret size; }
78  
      public Addr2 get(int i) { rangeCheck(i, size); ret readLong(addr+1+i*sizeOfPointer()); }
79  
      public Addr2 set(int i, Int val) { rangeCheck(i, size); Addr2 old = get(i); writeLong(addr+1+i*sizeOfPointer(), val); ret old; }
80  
    };
81  
  }
82  
  
83  
  Addr newPointerArray(int size) {
84  
    Addr a = alloc(size*sizeOfPointer()+1);
85  
    set(a, size);
86  
    ret a;
87  
  }
88  
  
89  
  void freePointerArray(Addr a) {
90  
    if (a != 0) free(a, read(a)+1);
91  
  }
92  
  
93  
  // a can be nullPtr(), then we just make a new array
94  
  Addr resizePointerArray(Addr a, int newSize) {
95  
    ifdef ManagedIntObjects_v1_debug
96  
    printFunctionCall resizePointerArray(a, newSize);
97  
    endifdef
98  
    if (a == 0) ret newPointerArray(newSize);
99  
    int oldSize = get(a);
100  
    if (oldSize == newSize) ret a;
101  
    Addr a2 = newPointerArray(newSize);
102  
    memCopy(a+1, a2+1, min(oldSize, newSize));
103  
    ret a2;
104  
  }
105  
  
106  
  abstract void memCopy(long from, long to, long length);
107  
  
108  
  long totalFree() { ret freeList.totalFree(); }
109  
  
110  
  long reservedSpaceAtBeginning() { ret 2; }
111  
  
112  
  void collectAndCompact(IManagedObject64 rootObject) {
113  
    ManagedIntObjects64_Collector collector = new(this);
114  
    if (rootObject != null)
115  
      rootObject.scanForCollection(collector);
116  
    collector.collectAndCompact();
117  
  }
118  
  
119  
  void printStats {
120  
    print("Managed memory size: " + toM(size()*(long) wordSizeInBytes()) + " MB, free: " + ratioToIntPercent(freeList.totalFree(), size()) + "%");
121  
  }
122  
  
123  
  // +1, actually
124  
  long highestUsedWord() {
125  
    LongRange r = freeList.highestFreeRange();
126  
    ret r != null && r.end == size() ? r.start : size();
127  
  }
128  
}

Author comment

Began life as a copy of #1029226

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: #1029330
Snippet name: ManagedIntObjects64 [dev., manage our own objects in an int array, allow > 16GB]
Eternal ID of this version: #1029330/13
Text MD5: acb82cf70097a1ad089ebd9c36d622f6
Transpilation MD5: baae09c46d5651699369d65d0bc7ce21
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:43:24
Source code size: 3763 bytes / 128 lines
Pitched / IR pitched: No / No
Views / Downloads: 306 / 602
Version history: 12 change(s)
Referenced in: [show references]