jitcraft/PoolAllocator.cpp
changeset 0 c455ebb9a5dc
child 55 e073a9fe8e57
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/jitcraft/PoolAllocator.cpp	Mon Jun 25 02:57:28 2012 -0700
     1.3 @@ -0,0 +1,103 @@
     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 +
    1.26 +using namespace jc;
    1.27 +
    1.28 +PoolAllocator::PoolAllocator()
    1.29 +  : reserved_(NULL),
    1.30 +    last_(NULL)
    1.31 +{
    1.32 +}
    1.33 +
    1.34 +PoolAllocator::~PoolAllocator()
    1.35 +{
    1.36 +    unwind(NULL);
    1.37 +    if (reserved_)
    1.38 +        free(reserved_);
    1.39 +}
    1.40 +
    1.41 +char *
    1.42 +PoolAllocator::enter()
    1.43 +{
    1.44 +    if (!last_)
    1.45 +        return NULL;
    1.46 +    return last_->ptr;
    1.47 +}
    1.48 +
    1.49 +void
    1.50 +PoolAllocator::leave(char *pos)
    1.51 +{
    1.52 +    unwind(pos);
    1.53 +}
    1.54 +
    1.55 +void
    1.56 +PoolAllocator::unwind(char *pos)
    1.57 +{
    1.58 +    while (last_) {
    1.59 +        if (pos && pos >= last_->base && pos < last_->end)
    1.60 +            break;
    1.61 +        Pool *prev = last_->prev;
    1.62 +        {
    1.63 +            if (last_->size() <= kMaxReserveSize &&
    1.64 +                (!reserved_ || reserved_->size() < last_->size()))
    1.65 +            {
    1.66 +                if (reserved_)
    1.67 +                    free(reserved_);
    1.68 +                reserved_ = last_;
    1.69 +            } else {
    1.70 +                free(last_);
    1.71 +            }
    1.72 +        }
    1.73 +        last_ = prev;
    1.74 +    }
    1.75 +
    1.76 +    if (!last_) {
    1.77 +        assert(!pos);
    1.78 +        return;
    1.79 +    }
    1.80 +
    1.81 +    last_->ptr = pos;
    1.82 +}
    1.83 +
    1.84 +void *
    1.85 +PoolAllocator::slowAllocate(size_t actualBytes)
    1.86 +{
    1.87 +    size_t bytesNeeded = actualBytes + sizeof(Pool);
    1.88 +    if (bytesNeeded < kDefaultPoolSize)
    1.89 +        bytesNeeded = kDefaultPoolSize;
    1.90 +
    1.91 +    Pool *pool;
    1.92 +    if (reserved_ && reserved_->size() >= bytesNeeded) {
    1.93 +        pool = reserved_;
    1.94 +        reserved_ = NULL;
    1.95 +    } else {
    1.96 +        pool = (Pool *)malloc(bytesNeeded);
    1.97 +        if (!pool)
    1.98 +            throw std::bad_alloc();
    1.99 +        pool->base = (char *)(pool + 1);
   1.100 +        pool->end = (char *)pool + bytesNeeded;
   1.101 +    }
   1.102 +    pool->ptr = pool->base + actualBytes;
   1.103 +    pool->prev = last_;
   1.104 +    last_ = pool;
   1.105 +    return pool->base;
   1.106 +}