/** * Allocates native memory, aligned to a minimum boundary. * * @param runtime The Ruby runtime * @param size The number of bytes to allocate * @param align The minimum alignment of the memory * @param clear Whether the memory should be cleared (zeroed) * @return A new {@link org.jruby.ext.ffi.AllocatedDirectMemoryIO} */ static MemoryIO allocateAligned(Ruby runtime, int size, int align, boolean clear) { // Caching seems to work best for small allocations (<= 256 bytes). For everything else, use the default allocator if (size > 256 || align > 8) { return AllocatedNativeMemoryIO.allocateAligned(runtime, size, align, clear); } Reference<Allocator> allocatorReference = currentAllocator.get(); Allocator allocator = allocatorReference != null ? allocatorReference.get() : null; if (allocator == null) { allocator = new Allocator(); currentAllocator.set(new SoftReference<Allocator>(allocator)); } return allocator.allocate(runtime, size, clear); }
/** * Allocates native memory, aligned to a minimum boundary. * * @param runtime The Ruby runtime * @param size The number of bytes to allocate * @param align The minimum alignment of the memory * @param clear Whether the memory should be cleared (zeroed) * @return A new {@link org.jruby.ext.ffi.AllocatedDirectMemoryIO} */ static MemoryIO allocateAligned(Ruby runtime, int size, int align, boolean clear) { // Caching seems to work best for small allocations (<= 256 bytes). For everything else, use the default allocator if (size > 256 || align > 8) { return AllocatedNativeMemoryIO.allocateAligned(runtime, size, align, clear); } Reference<Allocator> allocatorReference = currentAllocator.get(); Allocator allocator = allocatorReference != null ? allocatorReference.get() : null; if (allocator == null) { allocator = new Allocator(); currentAllocator.set(new SoftReference<Allocator>(allocator)); } return allocator.allocate(runtime, size, clear); }
/** * Allocates native memory, aligned to a minimum boundary. * * @param runtime The Ruby runtime * @param size The number of bytes to allocate * @param align The minimum alignment of the memory * @param clear Whether the memory should be cleared (zeroed) * @return A new {@link org.jruby.ext.ffi.AllocatedDirectMemoryIO} */ static MemoryIO allocateAligned(Ruby runtime, int size, int align, boolean clear) { // Caching seems to work best for small allocations (<= 256 bytes). For everything else, use the default allocator if (size > 256 || align > 8) { return AllocatedNativeMemoryIO.allocateAligned(runtime, size, align, clear); } Reference<Allocator> allocatorReference = currentAllocator.get(); Allocator allocator = allocatorReference != null ? allocatorReference.get() : null; if (allocator == null) { allocator = new Allocator(); currentAllocator.set(new SoftReference<Allocator>(allocator)); } return allocator.allocate(runtime, size, clear); }
/** * Allocates native memory, aligned to a minimum boundary. * * @param runtime The Ruby runtime * @param size The number of bytes to allocate * @param align The minimum alignment of the memory * @param clear Whether the memory should be cleared (zeroed) * @return A new {@link org.jruby.ext.ffi.AllocatedDirectMemoryIO} */ static MemoryIO allocateAligned(Ruby runtime, int size, int align, boolean clear) { // Caching seems to work best for small allocations (<= 256 bytes). For everything else, use the default allocator if (size > 256 || align > 8) { return AllocatedNativeMemoryIO.allocateAligned(runtime, size, align, clear); } Reference<Allocator> allocatorReference = currentAllocator.get(); Allocator allocator = allocatorReference != null ? allocatorReference.get() : null; if (allocator == null) { allocator = new Allocator(); currentAllocator.set(new SoftReference<Allocator>(allocator)); } return allocator.allocate(runtime, size, clear); }