src/PoolAllocator.cpp
author David Anderson <danderson@mozilla.com>
Wed Jul 25 01:32:49 2012 -0700 (2012-07-25)
changeset 93 23189d937087
parent 92 e3974e7360e9
child 195 c1ba166f3fc4
permissions -rw-r--r--
Fix opt clang build.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include <assert.h>
    19 #include <stdlib.h>
    20 #include <new>
    21 #include "PoolAllocator.h"
    22 #include "Zone.h"
    23 #include "Spaces-inl.h"
    24 #include "Heap-inl.h"
    25 
    26 using namespace ke;
    27 
    28 PoolAllocator::PoolAllocator()
    29   : reserved_(NULL),
    30     last_(NULL)
    31 {
    32 }
    33 
    34 PoolAllocator::~PoolAllocator()
    35 {
    36     unwind(NULL);
    37     if (reserved_)
    38         free(reserved_);
    39 }
    40 
    41 char *
    42 PoolAllocator::enter()
    43 {
    44     if (!last_)
    45         return NULL;
    46     return last_->ptr;
    47 }
    48 
    49 void
    50 PoolAllocator::leave(char *pos)
    51 {
    52     unwind(pos);
    53 }
    54 
    55 void
    56 PoolAllocator::unwind(char *pos)
    57 {
    58     while (last_) {
    59         if (pos && pos >= last_->base && pos < last_->end)
    60             break;
    61         Pool *prev = last_->prev;
    62         {
    63             if (last_->size() <= kMaxReserveSize &&
    64                 (!reserved_ || reserved_->size() < last_->size()))
    65             {
    66                 if (reserved_)
    67                     free(reserved_);
    68                 reserved_ = last_;
    69             } else {
    70                 free(last_);
    71             }
    72         }
    73         last_ = prev;
    74     }
    75 
    76     if (!last_) {
    77         assert(!pos);
    78         return;
    79     }
    80 
    81     last_->ptr = pos;
    82 }
    83 
    84 void *
    85 PoolAllocator::slowAllocate(size_t actualBytes)
    86 {
    87     size_t bytesNeeded = actualBytes + sizeof(Pool);
    88     if (bytesNeeded < kDefaultPoolSize)
    89         bytesNeeded = kDefaultPoolSize;
    90 
    91     Pool *pool;
    92     if (reserved_ && reserved_->size() >= bytesNeeded) {
    93         pool = reserved_;
    94         reserved_ = NULL;
    95     } else {
    96         pool = (Pool *)malloc(bytesNeeded);
    97         if (!pool) {
    98             ZONE()->reportOutOfMemory();
    99             return NULL;
   100         }
   101         pool->base = (char *)(pool + 1);
   102         pool->end = (char *)pool + bytesNeeded;
   103     }
   104     pool->ptr = pool->base + actualBytes;
   105     pool->prev = last_;
   106     last_ = pool;
   107     return pool->base;
   108 }
   109 
   110 void
   111 PoolAllocationPolicy::reportOutOfMemory()
   112 {
   113     ZONE()->reportOutOfMemory();
   114 }
   115 
   116 void
   117 PoolAllocationPolicy::reportAllocationOverflow()
   118 {
   119     ZONE()->reportError(Message_AllocationOverflow);
   120 }
   121 
   122 void *
   123 PoolAllocationPolicy::malloc(size_t bytes)
   124 {
   125     return ZONE()->pool().allocate(bytes);
   126 }
   127 
   128 void
   129 PoolAllocationPolicy::free(void *ptr)
   130 {
   131 }