0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 package org.apache.spark.unsafe.memory;
0019
0020 import org.apache.spark.unsafe.Platform;
0021
0022
0023
0024
0025 public class UnsafeMemoryAllocator implements MemoryAllocator {
0026
0027 @Override
0028 public MemoryBlock allocate(long size) throws OutOfMemoryError {
0029 long address = Platform.allocateMemory(size);
0030 MemoryBlock memory = new MemoryBlock(null, address, size);
0031 if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
0032 memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_CLEAN_VALUE);
0033 }
0034 return memory;
0035 }
0036
0037 @Override
0038 public void free(MemoryBlock memory) {
0039 assert (memory.obj == null) :
0040 "baseObject not null; are you trying to use the off-heap allocator to free on-heap memory?";
0041 assert (memory.pageNumber != MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER) :
0042 "page has already been freed";
0043 assert ((memory.pageNumber == MemoryBlock.NO_PAGE_NUMBER)
0044 || (memory.pageNumber == MemoryBlock.FREED_IN_TMM_PAGE_NUMBER)) :
0045 "TMM-allocated pages must be freed via TMM.freePage(), not directly in allocator free()";
0046
0047 if (MemoryAllocator.MEMORY_DEBUG_FILL_ENABLED) {
0048 memory.fill(MemoryAllocator.MEMORY_DEBUG_FILL_FREED_VALUE);
0049 }
0050 Platform.freeMemory(memory.offset);
0051
0052
0053 memory.offset = 0;
0054
0055 memory.pageNumber = MemoryBlock.FREED_IN_ALLOCATOR_PAGE_NUMBER;
0056 }
0057 }