src/PoolAllocator.cpp
changeset 86 32ad0523b309
parent 69 dfce0539a77d
child 92 e3974e7360e9
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/PoolAllocator.cpp	Tue Jul 24 23:53:34 2012 -0700
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft is free software: you can redistribute it and/or modify it under
    1.10 + * the terms of the GNU General Public License as published by the Free
    1.11 + * Software Foundation, either version 3 of the License, or (at your option)
    1.12 + * any later version.
    1.13 + * 
    1.14 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License along with
    1.19 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#include <assert.h>
    1.22 +#include <stdlib.h>
    1.23 +#include <new>
    1.24 +#include "PoolAllocator.h"
    1.25 +#include "Zone.h"
    1.26 +
    1.27 +using namespace ke;
    1.28 +
    1.29 +PoolAllocator::PoolAllocator()
    1.30 +  : reserved_(NULL),
    1.31 +    last_(NULL)
    1.32 +{
    1.33 +}
    1.34 +
    1.35 +PoolAllocator::~PoolAllocator()
    1.36 +{
    1.37 +    unwind(NULL);
    1.38 +    if (reserved_)
    1.39 +        free(reserved_);
    1.40 +}
    1.41 +
    1.42 +char *
    1.43 +PoolAllocator::enter()
    1.44 +{
    1.45 +    if (!last_)
    1.46 +        return NULL;
    1.47 +    return last_->ptr;
    1.48 +}
    1.49 +
    1.50 +void
    1.51 +PoolAllocator::leave(char *pos)
    1.52 +{
    1.53 +    unwind(pos);
    1.54 +}
    1.55 +
    1.56 +void
    1.57 +PoolAllocator::unwind(char *pos)
    1.58 +{
    1.59 +    while (last_) {
    1.60 +        if (pos && pos >= last_->base && pos < last_->end)
    1.61 +            break;
    1.62 +        Pool *prev = last_->prev;
    1.63 +        {
    1.64 +            if (last_->size() <= kMaxReserveSize &&
    1.65 +                (!reserved_ || reserved_->size() < last_->size()))
    1.66 +            {
    1.67 +                if (reserved_)
    1.68 +                    free(reserved_);
    1.69 +                reserved_ = last_;
    1.70 +            } else {
    1.71 +                free(last_);
    1.72 +            }
    1.73 +        }
    1.74 +        last_ = prev;
    1.75 +    }
    1.76 +
    1.77 +    if (!last_) {
    1.78 +        assert(!pos);
    1.79 +        return;
    1.80 +    }
    1.81 +
    1.82 +    last_->ptr = pos;
    1.83 +}
    1.84 +
    1.85 +void *
    1.86 +PoolAllocator::slowAllocate(size_t actualBytes)
    1.87 +{
    1.88 +    size_t bytesNeeded = actualBytes + sizeof(Pool);
    1.89 +    if (bytesNeeded < kDefaultPoolSize)
    1.90 +        bytesNeeded = kDefaultPoolSize;
    1.91 +
    1.92 +    Pool *pool;
    1.93 +    if (reserved_ && reserved_->size() >= bytesNeeded) {
    1.94 +        pool = reserved_;
    1.95 +        reserved_ = NULL;
    1.96 +    } else {
    1.97 +        pool = (Pool *)malloc(bytesNeeded);
    1.98 +        if (!pool) {
    1.99 +            ZONE()->reportOutOfMemory();
   1.100 +            return NULL;
   1.101 +        }
   1.102 +        pool->base = (char *)(pool + 1);
   1.103 +        pool->end = (char *)pool + bytesNeeded;
   1.104 +    }
   1.105 +    pool->ptr = pool->base + actualBytes;
   1.106 +    pool->prev = last_;
   1.107 +    last_ = pool;
   1.108 +    return pool->base;
   1.109 +}
   1.110 +
   1.111 +void
   1.112 +PoolAllocationPolicy::reportOutOfMemory()
   1.113 +{
   1.114 +    ZONE()->reportOutOfMemory();
   1.115 +}
   1.116 +
   1.117 +void
   1.118 +PoolAllocationPolicy::reportAllocationOverflow()
   1.119 +{
   1.120 +    ZONE()->reportError(Message_AllocationOverflow);
   1.121 +}
   1.122 +
   1.123 +void *
   1.124 +PoolAllocationPolicy::malloc(size_t bytes)
   1.125 +{
   1.126 +    return ZONE()->pool().allocate(bytes);
   1.127 +}
   1.128 +
   1.129 +void
   1.130 +PoolAllocationPolicy::free(void *ptr)
   1.131 +{
   1.132 +}