public class MemoryDaemon extends java.lang.Object implements LowLevelBuffer
MemoryManager
static functions.Modifier and Type | Field and Description |
---|---|
private java.util.Map<java.lang.Long,java.lang.Integer> |
allocatedAddresses
All currently allocated addresses mapped to their usage counters.
|
private static java.lang.Object |
modToken
Token used to authenticate with the dispatcher when registering APIs.
|
Constructor and Description |
---|
MemoryDaemon(boolean single)
Create an instance and export its API to the network.
|
Modifier and Type | Method and Description |
---|---|
long |
allocate(long len)
Allocate the given number of bytes as a new memory block and return the 64-bit address.
|
void |
cleanup()
Deallocate all memory allocations which have not been deallocated explicitly previously.
|
int |
compiledWordSize()
Returns the word size (the size of a pointer) for the native code as compiled.
|
void |
copy(long source,
long dest,
long offset,
long len)
Copy the specified range of contents from the source memory block to the destination memory
block.
|
boolean |
deallocate(long addr)
De-allocate the memory block at the given 64-bit address.
|
private void |
deallocateImpl(long addr,
boolean cleanup)
De-allocate the memory block at the given 64-bit address.
|
long |
findNextNull(long addr,
long idx,
long size)
Returns the next index position into the array which is the
null byte, starting
at the given index position. |
void |
incrementReference(long addr)
Increments the number of references to this memory.
|
boolean |
isLittleEndian()
Reports on the native byte-ordering of the platform on which this library is
running.
|
byte[] |
read(long addr,
long offset,
long len)
Read the specified range of contents from the memory block at the given 64-bit address.
|
void |
write(long addr,
long offset,
byte[] data)
Write the specified contents to the memory block at the given 64-bit address plus offset.
|
private static java.lang.Object modToken
private java.util.Map<java.lang.Long,java.lang.Integer> allocatedAddresses
public MemoryDaemon(boolean single)
single
- true
to startup within the server process which must bypass the shared
infrastructure initialization. Use false
for the normal client JVM startup.public boolean isLittleEndian()
isLittleEndian
in interface LowLevelBuffer
true
if the platform uses little-endian byte-ordering, false
if
the platform is big-endian.public int compiledWordSize()
compiledWordSize
in interface LowLevelBuffer
public long allocate(long len)
To duplicate the permissiveness of the 4GL memptr allocation (where certain operations often can read or write past the end of the allocated buffer without an access violation), the buffer allocated by this function will always be 16 bytes larger than the requested size.
allocate
in interface LowLevelBuffer
len
- The size in bytes of the memory region to allocate.public boolean deallocate(long addr)
true
, the memory in this region MUST NOT be accessed after this call.deallocate
in interface LowLevelBuffer
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer. DO NOT deallocate the same pointer after this
method returned true
.true
if the memory was deallocated and false
if the deallocation
failed because this memory zone was shared with other variable. If so then the
counter was decremented and the memory will be released when it reaches zero.public void incrementReference(long addr)
deallocate(long)
. It will automatically decrement the counter and truly
deallocate the memory when it reaches 0.incrementReference
in interface LowLevelBuffer
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.public long findNextNull(long addr, long idx, long size)
null
byte, starting
at the given index position.findNextNull
in interface LowLevelBuffer
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.idx
- The index position at which to start the search.size
- The length of the buffer. If the search reaches this byte without finding a
null byte, then this value will be returned. If this is given as -1, then the
caller has no size information and we operate in unsafe mode. In this mode
the code will read until it finds a null
or gets an access violation.
This is how the 4GL does it too.null
byte or the
length of the array if no null
byte is contained.public byte[] read(long addr, long offset, long len)
read
in interface LowLevelBuffer
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.offset
- The 0-based index position from the beginning of the buffer, from which to start
reading.len
- The number of bytes to read. Must be greater than 0 and must NOT extend past the
end of the buffer.null
if there is any problem allocating
the array.public void write(long addr, long offset, byte[] data)
write
in interface LowLevelBuffer
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.offset
- The 0-based index position from the beginning of the buffer, at which to start
writing.data
- The bytes to write. The entire contents of the array will be written. The length
of the array must not be bigger than the number of bytes between the offset and
the end of the buffer.public void copy(long source, long dest, long offset, long len)
copy
in interface LowLevelBuffer
source
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.dest
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer.offset
- The 0-based index position from the beginning of the buffer, at which to start
writing.len
- The number of bytes to write. Must be greater than 0 and must NOT extend past the
end of the destination buffer.public void cleanup()
cleanup
in interface LowLevelBuffer
private void deallocateImpl(long addr, boolean cleanup)
addr
- The 64-bit pointer to a memory region previously allocated using this module.
DO NOT pass in an invalid pointer. DO NOT deallocate the same pointer more than
once.cleanup
- true
if called during cleanup processing; false
if called for an
explicit deallocation.