src/PoolAllocator.cpp
changeset 263 ba85a47ee414
parent 195 c1ba166f3fc4
     1.1 --- a/src/PoolAllocator.cpp	Mon Jun 10 00:46:29 2013 +0100
     1.2 +++ b/src/PoolAllocator.cpp	Mon Jun 10 01:04:43 2013 +0100
     1.3 @@ -1,131 +1,131 @@
     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 -#include "Spaces-inl.h"
    1.27 -#include "Heap-inl.h"
    1.28 -
    1.29 -using namespace ke;
    1.30 -
    1.31 -PoolAllocator::PoolAllocator()
    1.32 -  : reserved_(NULL),
    1.33 -    last_(NULL)
    1.34 -{
    1.35 -}
    1.36 -
    1.37 -PoolAllocator::~PoolAllocator()
    1.38 -{
    1.39 -    unwind(NULL);
    1.40 -    if (reserved_)
    1.41 -        free(reserved_);
    1.42 -}
    1.43 -
    1.44 -char *
    1.45 -PoolAllocator::enter()
    1.46 -{
    1.47 -    if (!last_)
    1.48 -        return NULL;
    1.49 -    return last_->ptr;
    1.50 -}
    1.51 -
    1.52 -void
    1.53 -PoolAllocator::leave(char *pos)
    1.54 -{
    1.55 -    unwind(pos);
    1.56 -}
    1.57 -
    1.58 -void
    1.59 -PoolAllocator::unwind(char *pos)
    1.60 -{
    1.61 -    while (last_) {
    1.62 -        if (pos && pos >= last_->base && pos < last_->end)
    1.63 -            break;
    1.64 -        Pool *prev = last_->prev;
    1.65 -        {
    1.66 -            if (last_->size() <= kMaxReserveSize &&
    1.67 -                (!reserved_ || reserved_->size() < last_->size()))
    1.68 -            {
    1.69 -                if (reserved_)
    1.70 -                    free(reserved_);
    1.71 -                reserved_ = last_;
    1.72 -            } else {
    1.73 -                free(last_);
    1.74 -            }
    1.75 -        }
    1.76 -        last_ = prev;
    1.77 -    }
    1.78 -
    1.79 -    if (!last_) {
    1.80 -        assert(!pos);
    1.81 -        return;
    1.82 -    }
    1.83 -
    1.84 -    last_->ptr = pos;
    1.85 -}
    1.86 -
    1.87 -void *
    1.88 -PoolAllocator::slowAllocate(size_t actualBytes)
    1.89 -{
    1.90 -    size_t bytesNeeded = actualBytes + sizeof(Pool);
    1.91 -    if (bytesNeeded < kDefaultPoolSize)
    1.92 -        bytesNeeded = kDefaultPoolSize;
    1.93 -
    1.94 -    Pool *pool;
    1.95 -    if (reserved_ && reserved_->size() >= bytesNeeded) {
    1.96 -        pool = reserved_;
    1.97 -        reserved_ = NULL;
    1.98 -    } else {
    1.99 -        pool = (Pool *)malloc(bytesNeeded);
   1.100 -        if (!pool) {
   1.101 -            ZONE()->reportOutOfMemory();
   1.102 -            return NULL;
   1.103 -        }
   1.104 -        pool->base = (char *)(pool + 1);
   1.105 -        pool->end = (char *)pool + bytesNeeded;
   1.106 -    }
   1.107 -    pool->ptr = pool->base + actualBytes;
   1.108 -    pool->prev = last_;
   1.109 -    last_ = pool;
   1.110 -    return pool->base;
   1.111 -}
   1.112 -
   1.113 -void
   1.114 -PoolAllocationPolicy::reportOutOfMemory()
   1.115 -{
   1.116 -    ZONE()->reportOutOfMemory();
   1.117 -}
   1.118 -
   1.119 -void
   1.120 -PoolAllocationPolicy::reportAllocationOverflow()
   1.121 -{
   1.122 -    ZONE()->reportError(Message_AllocationOverflow);
   1.123 -}
   1.124 -
   1.125 -void *
   1.126 -PoolAllocationPolicy::malloc(size_t bytes)
   1.127 -{
   1.128 -    return ZONE()->pool().rawAllocate(bytes);
   1.129 -}
   1.130 -
   1.131 -void
   1.132 -PoolAllocationPolicy::free(void *ptr)
   1.133 -{
   1.134 -}
   1.135 +/*
   1.136 + * Copyright (C) 2012 David Anderson
   1.137 + *
   1.138 + * This file is part of JITCraft.
   1.139 + *
   1.140 + * JITCraft is free software: you can redistribute it and/or modify it under
   1.141 + * the terms of the GNU General Public License as published by the Free
   1.142 + * Software Foundation, either version 3 of the License, or (at your option)
   1.143 + * any later version.
   1.144 + * 
   1.145 + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
   1.146 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.147 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   1.148 + *
   1.149 + * You should have received a copy of the GNU General Public License along with
   1.150 + * JITCraft. If not, see http://www.gnu.org/licenses/.
   1.151 + */
   1.152 +#include <assert.h>
   1.153 +#include <stdlib.h>
   1.154 +#include <new>
   1.155 +#include "PoolAllocator.h"
   1.156 +#include "Zone.h"
   1.157 +#include "Spaces-inl.h"
   1.158 +#include "Heap-inl.h"
   1.159 +
   1.160 +using namespace ke;
   1.161 +
   1.162 +PoolAllocator::PoolAllocator()
   1.163 +  : reserved_(NULL),
   1.164 +    last_(NULL)
   1.165 +{
   1.166 +}
   1.167 +
   1.168 +PoolAllocator::~PoolAllocator()
   1.169 +{
   1.170 +    unwind(NULL);
   1.171 +    if (reserved_)
   1.172 +        free(reserved_);
   1.173 +}
   1.174 +
   1.175 +char *
   1.176 +PoolAllocator::enter()
   1.177 +{
   1.178 +    if (!last_)
   1.179 +        return NULL;
   1.180 +    return last_->ptr;
   1.181 +}
   1.182 +
   1.183 +void
   1.184 +PoolAllocator::leave(char *pos)
   1.185 +{
   1.186 +    unwind(pos);
   1.187 +}
   1.188 +
   1.189 +void
   1.190 +PoolAllocator::unwind(char *pos)
   1.191 +{
   1.192 +    while (last_) {
   1.193 +        if (pos && pos >= last_->base && pos < last_->end)
   1.194 +            break;
   1.195 +        Pool *prev = last_->prev;
   1.196 +        {
   1.197 +            if (last_->size() <= kMaxReserveSize &&
   1.198 +                (!reserved_ || reserved_->size() < last_->size()))
   1.199 +            {
   1.200 +                if (reserved_)
   1.201 +                    free(reserved_);
   1.202 +                reserved_ = last_;
   1.203 +            } else {
   1.204 +                free(last_);
   1.205 +            }
   1.206 +        }
   1.207 +        last_ = prev;
   1.208 +    }
   1.209 +
   1.210 +    if (!last_) {
   1.211 +        assert(!pos);
   1.212 +        return;
   1.213 +    }
   1.214 +
   1.215 +    last_->ptr = pos;
   1.216 +}
   1.217 +
   1.218 +void *
   1.219 +PoolAllocator::slowAllocate(size_t actualBytes)
   1.220 +{
   1.221 +    size_t bytesNeeded = actualBytes + sizeof(Pool);
   1.222 +    if (bytesNeeded < kDefaultPoolSize)
   1.223 +        bytesNeeded = kDefaultPoolSize;
   1.224 +
   1.225 +    Pool *pool;
   1.226 +    if (reserved_ && reserved_->size() >= bytesNeeded) {
   1.227 +        pool = reserved_;
   1.228 +        reserved_ = NULL;
   1.229 +    } else {
   1.230 +        pool = (Pool *)malloc(bytesNeeded);
   1.231 +        if (!pool) {
   1.232 +            ZONE()->reportOutOfMemory();
   1.233 +            return NULL;
   1.234 +        }
   1.235 +        pool->base = (char *)(pool + 1);
   1.236 +        pool->end = (char *)pool + bytesNeeded;
   1.237 +    }
   1.238 +    pool->ptr = pool->base + actualBytes;
   1.239 +    pool->prev = last_;
   1.240 +    last_ = pool;
   1.241 +    return pool->base;
   1.242 +}
   1.243 +
   1.244 +void
   1.245 +PoolAllocationPolicy::reportOutOfMemory()
   1.246 +{
   1.247 +    ZONE()->reportOutOfMemory();
   1.248 +}
   1.249 +
   1.250 +void
   1.251 +PoolAllocationPolicy::reportAllocationOverflow()
   1.252 +{
   1.253 +    ZONE()->reportError(Message_AllocationOverflow);
   1.254 +}
   1.255 +
   1.256 +void *
   1.257 +PoolAllocationPolicy::malloc(size_t bytes)
   1.258 +{
   1.259 +    return ZONE()->pool().rawAllocate(bytes);
   1.260 +}
   1.261 +
   1.262 +void
   1.263 +PoolAllocationPolicy::free(void *ptr)
   1.264 +{
   1.265 +}