1

我正在尝试创建一个接口来分配和取消分配数据并提供这种类型的内存池。我决定制作一个 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 中使用这些: 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();
    }
}

}

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

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

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

编辑:我似乎已经解决了这个问题:

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

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

4

1 回答 1

0

变量的声明memoryPool必须使用正确的类型参数。你不说是什么T;不管它是什么都不兼容double[][]

DynamicMemoryPool<double[][]> memoryPool = new DynamicMemoryPool<double[][]>(200, false, new JavaAllocator(), size); 
于 2013-09-05T12:40:47.907 回答