-
Notifications
You must be signed in to change notification settings - Fork 540
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement a maybe workable memory manager
- Loading branch information
1 parent
f9746ae
commit 26aa44c
Showing
10 changed files
with
466 additions
and
74 deletions.
There are no files selected for viewing
132 changes: 132 additions & 0 deletions
132
RoaringBitmap/src/main/java/org/roaringbitmap/AllocationManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package org.roaringbitmap; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Arrays; | ||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
|
||
/** | ||
* Provides A lazily initialised constant allocator. To override allocation, | ||
* registration must take place before first use. | ||
*/ | ||
public final class AllocationManager { | ||
|
||
private static final AllocationManager INSTANCE = new AllocationManager(); | ||
|
||
private static final AtomicReferenceFieldUpdater<AllocationManager, Allocator> UPDATER = | ||
AtomicReferenceFieldUpdater.newUpdater(AllocationManager.class, Allocator.class, "providedAllocator"); | ||
|
||
private volatile Allocator providedAllocator; | ||
|
||
private static final class RegisteredAllocator { | ||
public static final Allocator ALLOCATOR; | ||
static { | ||
Allocator allocator = INSTANCE.providedAllocator; | ||
if (allocator == null) { | ||
allocator = new DefaultAllocator(); | ||
if (!UPDATER.compareAndSet(INSTANCE, null, allocator)) { | ||
allocator = INSTANCE.providedAllocator; | ||
} | ||
} | ||
ALLOCATOR = allocator; | ||
} | ||
} | ||
|
||
public static boolean register(Allocator allocator) { | ||
return UPDATER.compareAndSet(INSTANCE, null, allocator); | ||
} | ||
|
||
public static char[] allocateChars(int size) { | ||
return RegisteredAllocator.ALLOCATOR.allocateChars(size); | ||
} | ||
|
||
public static long[] allocateLongs(int size) { | ||
return RegisteredAllocator.ALLOCATOR.allocateLongs(size); | ||
} | ||
|
||
public static Container[] allocateContainers(int size) { | ||
return RegisteredAllocator.ALLOCATOR.allocateObjects(Container.class, size); | ||
} | ||
|
||
public static char[] copy(char[] chars) { | ||
return RegisteredAllocator.ALLOCATOR.copy(chars); | ||
} | ||
|
||
public static char[] copy(char[] chars, int newSize) { | ||
return RegisteredAllocator.ALLOCATOR.copy(chars, newSize); | ||
} | ||
|
||
public static long[] copy(long[] longs) { | ||
return RegisteredAllocator.ALLOCATOR.copy(longs); | ||
} | ||
|
||
public static Container[] copy(Container[] containers) { | ||
return RegisteredAllocator.ALLOCATOR.copy(containers); | ||
} | ||
|
||
public static char[] extend(char[] chars, int newSize) { | ||
return RegisteredAllocator.ALLOCATOR.extend(chars, newSize); | ||
} | ||
|
||
public static Container[] extend(Container[] containers, int newSize) { | ||
return RegisteredAllocator.ALLOCATOR.extend(containers, newSize); | ||
} | ||
|
||
public static <T> void free(Class<T> clazz, Object object) { | ||
RegisteredAllocator.ALLOCATOR.free(clazz, object); | ||
} | ||
|
||
static final class DefaultAllocator implements Allocator { | ||
|
||
@Override | ||
public char[] allocateChars(int size) { | ||
return new char[size]; | ||
} | ||
|
||
@Override | ||
public long[] allocateLongs(int size) { | ||
return new long[size]; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T[] allocateObjects(Class<T> clazz, int size) { | ||
return (T[]) Array.newInstance(clazz, size); | ||
} | ||
|
||
@Override | ||
public long[] copy(long[] longs) { | ||
return Arrays.copyOf(longs, longs.length); | ||
} | ||
|
||
@Override | ||
public char[] copy(char[] chars) { | ||
return Arrays.copyOf(chars, chars.length); | ||
} | ||
|
||
@Override | ||
public char[] copy(char[] chars, int newSize) { | ||
return Arrays.copyOf(chars, newSize); | ||
} | ||
|
||
@Override | ||
public <T> T[] copy(T[] objects) { | ||
return Arrays.copyOf(objects, objects.length); | ||
} | ||
|
||
@Override | ||
public char[] extend(char[] chars, int newSize) { | ||
return Arrays.copyOf(chars, newSize); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> T[] extend(T[] objects, int newSize) { | ||
return Arrays.copyOf(objects, newSize); | ||
} | ||
|
||
@Override | ||
public <T> void free(Class<T> clazz, Object object) { | ||
// do nothing | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
RoaringBitmap/src/main/java/org/roaringbitmap/Allocator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.roaringbitmap; | ||
|
||
public interface Allocator { | ||
|
||
/** | ||
* allocate a new array | ||
* @param size | ||
* @return | ||
*/ | ||
char[] allocateChars(int size); | ||
|
||
/** | ||
* allocate a new array | ||
* @param size | ||
* @return | ||
*/ | ||
long[] allocateLongs(int size); | ||
|
||
/** | ||
* Allocate a new array with the element type | ||
* @param clazz | ||
* @param size | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> T[] allocateObjects(Class<T> clazz, int size); | ||
|
||
/** | ||
* Copies the array, does not free the input | ||
* @param longs | ||
* @return | ||
*/ | ||
long[] copy(long[] longs); | ||
|
||
/** | ||
* Copies the array, does not free the input | ||
* @param chars | ||
* @return | ||
*/ | ||
char[] copy(char[] chars); | ||
|
||
/** | ||
* Copies the array, does not free the input | ||
* @param chars | ||
* @param newSize | ||
* @return | ||
*/ | ||
char[] copy(char[] chars, int newSize); | ||
|
||
/** | ||
* Copies the array, does not free the input | ||
* @param objects | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> T[] copy(T[] objects); | ||
|
||
/** | ||
* Copy and free the array, return a new array | ||
* @param chars | ||
* @param newSize | ||
* @return | ||
*/ | ||
char[] extend(char[] chars, int newSize); | ||
|
||
/** | ||
* Copy and free the array, return a new array | ||
* @param objects | ||
* @param newSize | ||
* @param <T> | ||
* @return | ||
*/ | ||
<T> T[] extend(T[] objects, int newSize); | ||
|
||
/** | ||
* free the object | ||
* @param clazz | ||
* @param object | ||
* @param <T> | ||
*/ | ||
<T> void free(Class<T> clazz, Object object); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.