Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.unsafe.memory;
0019 
0020 import javax.annotation.Nullable;
0021 
0022 import org.apache.spark.unsafe.Platform;
0023 
0024 /**
0025  * A consecutive block of memory, starting at a {@link MemoryLocation} with a fixed size.
0026  */
0027 public class MemoryBlock extends MemoryLocation {
0028 
0029   /** Special `pageNumber` value for pages which were not allocated by TaskMemoryManagers */
0030   public static final int NO_PAGE_NUMBER = -1;
0031 
0032   /**
0033    * Special `pageNumber` value for marking pages that have been freed in the TaskMemoryManager.
0034    * We set `pageNumber` to this value in TaskMemoryManager.freePage() so that MemoryAllocator
0035    * can detect if pages which were allocated by TaskMemoryManager have been freed in the TMM
0036    * before being passed to MemoryAllocator.free() (it is an error to allocate a page in
0037    * TaskMemoryManager and then directly free it in a MemoryAllocator without going through
0038    * the TMM freePage() call).
0039    */
0040   public static final int FREED_IN_TMM_PAGE_NUMBER = -2;
0041 
0042   /**
0043    * Special `pageNumber` value for pages that have been freed by the MemoryAllocator. This allows
0044    * us to detect double-frees.
0045    */
0046   public static final int FREED_IN_ALLOCATOR_PAGE_NUMBER = -3;
0047 
0048   private final long length;
0049 
0050   /**
0051    * Optional page number; used when this MemoryBlock represents a page allocated by a
0052    * TaskMemoryManager. This field is public so that it can be modified by the TaskMemoryManager,
0053    * which lives in a different package.
0054    */
0055   public int pageNumber = NO_PAGE_NUMBER;
0056 
0057   public MemoryBlock(@Nullable Object obj, long offset, long length) {
0058     super(obj, offset);
0059     this.length = length;
0060   }
0061 
0062   /**
0063    * Returns the size of the memory block.
0064    */
0065   public long size() {
0066     return length;
0067   }
0068 
0069   /**
0070    * Creates a memory block pointing to the memory used by the long array.
0071    */
0072   public static MemoryBlock fromLongArray(final long[] array) {
0073     return new MemoryBlock(array, Platform.LONG_ARRAY_OFFSET, array.length * 8L);
0074   }
0075 
0076   /**
0077    * Fills the memory block with the specified byte value.
0078    */
0079   public void fill(byte value) {
0080     Platform.setMemory(obj, offset, length, value);
0081   }
0082 }