我正在尝试创建一个接口来分配和取消分配数据,并提供这种类型的内存池。我决定制作一个Allocator接口,并在其中使用各种分配类型(例如java new或FFTW本机绑定)使用各种方法进行内存分配。

这是界面:

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


}

还有两个实现该接口的类的示例:

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;
}


}



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;
}


}

我试图在我的DynamicMemoryPool中使用这些:
    公共类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();
    }
}


}

所以我的问题是当尝试创建DynamicMemoryPool的实例并声明分配器的类型时。例如:

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


上面的代码行给我一个JavaAllocator错误,它需要Allocator。关于如何使这种类型的结构工作的任何想法都很棒。这是我在进行初始测试时编写的一些先前代码的重新编码,其中我实际上拼出了大约8种不同类型的BlockingQueue。现在,我想拥有8种不同类型的DynamicMemoryPool。谢谢你的帮助。

编辑:
我似乎已经解决了以下问题:

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

不幸的是,这迫使我添加@SuppressWarnings(“ unchecked”)

最佳答案

memoryPool变量的声明必须使用正确的type参数。您没有说T是什么;无论它与double[][]不兼容。

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

10-08 01:44