Java - 用于 native /非 native 例程的自定义分配器接口(interface)

标签 java memory-management interface memory-pool

我正在尝试创建一个接口(interface)来分配和释放数据并提供此类内存池。我决定创建一个分配器接口(interface),并在其内部提供各种内存分配方法和各种分配类型(例如 java new 或 FFTW native 绑定(bind))

这是界面:

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

}

以及实现该接口(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;
}

}

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)类 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 变量的声明必须使用正确的类型参数。你没有说 T 是什么;不管它是什么,它与 double[][] 不兼容。

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

关于Java - 用于 native /非 native 例程的自定义分配器接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18620677/

相关文章:

java在String.split中使用正则表达式来准备json参数

大块/内存管理中的python read()和write()

c - 从不同的指针 C 获取指向内存地址的指针

c++ - 如何在运行时确定 c++ 对象的内存

c# - 查找对象是否具有特定方法

java - 为什么我的 Spring Data JPA 查询比 Node.JS + oracledb 慢 8 倍?

Mac 上 SQLite 的 java.lang.UnsatisfiedLinkError

c# - 使用接口(interface)在 C# 中进行转换,以便能够使用所有对象功能

java : interface as input parameter in itself

java - 如何使用 ajax 设置 wicket listview 面板更新的 anchor