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


i trying create interface allocate , deallocate data , provide pool of type of memory. decided make allocator interface , have various methods memory allocation inside of various allocation types (for example java new or fftw native bindings)

here interface:

public interface allocator<t> { public t allocate(int ... n); public t deallocate(t memory); 

}

and 2 examples of classes implement interface:

public class javaallocator implements allocator<double[][]> {  @override public double[][] allocate(int... n) {      // 0 width     // 1 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;     (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 trying use these within dynamicmemorypool: public class dynamicmemorypool {

private blockingqueue<t> memoryqueue; private boolean dynamic; private allocator<t> allocator; private int [] sz;  /**  * allocates dynamic memory pool given size, type of tile , whether  * pool dynamically growing or not  * @param queuesize size of pool  * @param inittile initial tile determine type of pool  * @param dynamic whether pool 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);      (int = 0; < queuesize; i++)     {         values.add(allocator.allocate(sz));     }      if (dynamic)         queuesize*=2;      memoryqueue = new arrayblockingqueue<t>(queuesize, false, values);       }  /**  * releases memory pool  */ public void releaseall() {     (t p : memoryqueue)     {         p = allocator.deallocate(p);     }  }     /**  * gets pointer memory pool  * @return  */ public t getmemory() {     try {         if (memoryqueue.peek() == null && dynamic)         {             // add piece of memory             memoryqueue.offer(allocator.allocate(sz));                       }          return memoryqueue.take();     } catch (interruptedexception e) {         e.printstacktrace();         return null;     } }  /**  * adds java memory pool  * @param o java memory  */ public void addmemory(t o) {     try {         memoryqueue.put(o);     } catch (interruptedexception e) {         e.printstacktrace();     } } 

}

so issue when trying create instance of dynamicmemorypool , declaring type of allocator. example:

dynamicmemorypool<t> memorypool new dynamicmemorypool<t>(200, false, new javaallocator(), size); 

the line above giving me error javaallocator, requires allocator. ideas on how type of structure working awesome. recode of previous code wrote when doing initial testing in spelled out 8 different blockingqueues of different types. want have 8 different dynamicmemorypools of different types. help.

edit: seemed have fixed problem with:

dynamicmemorypool<t> memorypool = (dynamicmemorypool<t>) new dynamicmemorypool<double[][]>(200, false, new javaallocator(), size);

unfortunately forces me add @suppresswarnings("unchecked")

the declaration of memorypool variable must use correct type parameter. don't t is; whatever it's incompatible double[][].

dynamicmemorypool<double[][]> memorypool = new dynamicmemorypool<double[][]>(200, false, new javaallocator(), size);  

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -