Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.memory;
 
 
 
 import static com.facebook.presto.operator.Operator.NOT_BLOCKED;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkState;
 import static java.util.Objects.requireNonNull;
 
 public class MemoryPool
 {
     private final MemoryPoolId id;
     private final long maxBytes;
     private final boolean enableBlocking;
 
     @GuardedBy("this")
     private long freeBytes;
 
     @Nullable
     @GuardedBy("this")
     private SettableFuture<?> future;
 
     public MemoryPool(MemoryPoolId idDataSize sizeboolean enableBlocking)
     {
         this. = requireNonNull(id"name is null");
         requireNonNull(size"size is null");
          = size.toBytes();
         this. = enableBlocking;
          = size.toBytes();
     }
 
     public MemoryPoolId getId()
     {
         return ;
     }
 
     public synchronized MemoryPoolInfo getInfo()
     {
         return new MemoryPoolInfo();
     }

    
Reserves the given number of bytes. Caller should wait on the returned future, before allocating more memory.
 
     public synchronized ListenableFuture<?> reserve(long bytes)
     {
         checkArgument(bytes >= 0, "bytes is negative");
          -= bytes;
         if ( <= 0) {
             if ( == null) {
                  = SettableFuture.create();
             }
             checkState(!.isDone(), "future is already completed");
             if () {
                 return ;
             }
         }
         return ;
     }

    
Try to reserve the given number of bytes. Return value indicates whether the caller may use the requested memory.
 
     public synchronized boolean tryReserve(long bytes)
     {
         checkArgument(bytes >= 0, "bytes is negative");
         if ( - bytes < 0) {
             return false;
         }
          -= bytes;
         return true;
     }
 
     public synchronized void free(long bytes)
     {
         checkArgument(bytes >= 0, "bytes is negative");
         checkArgument( + bytes <= "tried to free more memory than is reserved");
          += bytes;
         if ( > 0 &&  != null) {
            .set(null);
             = null;
        }
    }

    
Returns the number of free bytes. This value may be negative, which indicates that the pool is over-committed.
    @Managed
    public synchronized long getFreeBytes()
    {
        return ;
    }
    @Managed
    public synchronized long getMaxBytes()
    {
        return ;
    }
    @Override
    public synchronized String toString()
    {
        return MoreObjects.toStringHelper(this)
                .add("id")
                .add("maxBytes")
                .add("enableBlocking")
                .add("freeBytes")
                .add("future")
                .toString();
    }
New to GrepCode? Check out our FAQ X