Java - Custom allocator interface for native/non-native routines

1.9k views Asked by At

I am trying to create an interface to allocate and deallocate data and provide a pool of this type of memory. I decided to make an Allocator interface and have various methods for memory allocation inside of it with various allocation types (for example java new or FFTW native bindings)

Here is the interface:

public interface Allocator<T> {
public T allocate(int ... n);
public T deallocate(T memory);

}

And two examples of classes that implement the interface:

public class JavaAllocator implements Allocator<double[][]> {

@Override
public double[][] allocate(int... n) {

    // 0 is width
    // 1 is height
    int n1 = 0;
    int n2 = 0;
    if (n.length == 2) {
        n1 = n[0];
        n2 = n[1];

    }

    return new double[n1][n2];
}

@Override
public double[][] deallocate(double[][] memory) {
    memory = null;
    return null;
}

}

and

public class PointerAllocator implements Allocator<Pointer<Double>> {

@Override
public Pointer<Double> allocate(int... n) {
    int size = 1;
    for (int val : n)
    {
        size *= val;
    }

    return FFTW3Library.fftw_alloc_complex(size);
}

@Override
public Pointer<Double> deallocate(Pointer<Double> memory) {
    FFTW3Library.fftw_free(memory);
    return memory;
}

}

I am trying to use these within my DynamicMemoryPool: public class DynamicMemoryPool {

private BlockingQueue<T> memoryQueue;
private boolean dynamic;
private Allocator<T> allocator;
private int [] sz;

/**
 * Allocates a dynamic memory pool given a size, a type of tile and whether
 * the pool is dynamically growing or not
 * @param queueSize the size of the pool
 * @param initTile the initial tile to determine what type of pool it is
 * @param dynamic whether the pool is dynamic or not
 */
public DynamicMemoryPool(int queueSize, boolean dynamic, Allocator<T> allocator, int ... sz)
{       
    this.allocator = allocator;
    this.sz = sz;
    this.dynamic = dynamic;
    Collection<T> values = new ArrayList<T>(queueSize);

    for (int i = 0; i < queueSize; i++)
    {
        values.add(allocator.allocate(sz));
    }

    if (dynamic)
        queueSize*=2;

    memoryQueue = new ArrayBlockingQueue<T>(queueSize, false, values);      
}

/**
 * Releases all memory from this pool
 */
public void releaseAll()
{
    for (T p : memoryQueue)
    {
        p = allocator.deallocate(p);
    }

}   

/**
 * Gets pointer memory from the pool
 * @return
 */
public T getMemory()
{
    try {
        if (memoryQueue.peek() == null && dynamic)
        {
            // Add a piece of memory
            memoryQueue.offer(allocator.allocate(sz));              
        }

        return memoryQueue.take();
    } catch (InterruptedException e) {
        e.printStackTrace();
        return null;
    }
}

/**
 * Adds java memory to the pool
 * @param o the java memory
 */
public void addMemory(T o)
{
    try {
        memoryQueue.put(o);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

}

So my issue is when trying to create an instance of a DynamicMemoryPool and declaring the type of allocator. For example:

DynamicMemoryPool<T> memoryPool new DynamicMemoryPool<T>(200, false, new JavaAllocator(), size);

The line above is giving me an error with the JavaAllocator, which requires Allocator. Any ideas on how to get this type of structure working would be awesome. This is a recode of some previous code that I wrote when doing initial testing in which I essentially spelled out about 8 different BlockingQueues of different types. Now I want to have 8 different DynamicMemoryPools of different types. Thanks for any help.

Edit: I seemed to have fixed the problem with:

DynamicMemoryPool<T> memoryPool = (DynamicMemoryPool<T>) new DynamicMemoryPool<double[][]>(200, false, new JavaAllocator(), size);

Unfortunately this forces me to add @SuppressWarnings("unchecked")

1

There are 1 answers

0
Joni On BEST ANSWER

The declaration of the memoryPool variable must use the correct type parameter. You don't say what T is; whatever it is it's incompatible with double[][].

DynamicMemoryPool<double[][]> memoryPool = new DynamicMemoryPool<double[][]>(200, false, new JavaAllocator(), size);