jitcraft/PoolAllocator.h
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 20:58:13 2012 -0700 (2012-07-07)
changeset 56 abb4ae5bc915
parent 55 e073a9fe8e57
child 59 b7d61ddb3c8d
permissions -rw-r--r--
Convert StatementList to a PoolList; convert PoolList to Vector.
     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 #ifndef _include_jitcraft_pool_allocator_h_
    19 #define _include_jitcraft_pool_allocator_h_
    20 
    21 #include <new>
    22 #include <stddef.h>
    23 #include <limits.h>
    24 #include "Utility.h"
    25 #include "Vector.h"
    26 
    27 namespace jc {
    28 
    29 // Allocates memory in chunks that are not freed until the entire allocator
    30 // is freed. This is intended for use with large, temporary data structures.
    31 class PoolAllocator
    32 {
    33     friend class PoolAllocationScope;
    34 
    35     struct Pool {
    36         char *base;
    37         char *ptr;
    38         char *end;
    39         Pool *prev;
    40 
    41         size_t size() const {
    42             return size_t(end - base);
    43         }
    44     };
    45 
    46     static const size_t kDefaultPoolSize = 8 * 1024;
    47     static const size_t kMaxReserveSize = 64 * 1024;
    48 
    49   private:
    50     Pool *reserved_;
    51     Pool *last_;
    52 
    53   private:
    54     void unwind(char *pos);
    55     void *slowAllocate(size_t actualBytes);
    56 
    57   public:
    58     PoolAllocator();
    59     ~PoolAllocator();
    60 
    61     void *allocate(size_t bytes) {
    62         // Guarantee malloc alignment.
    63         size_t actualBytes = Align(bytes, kMallocAlignment);
    64         if (!last_ || (size_t(last_->end - last_->ptr) < actualBytes))
    65             return slowAllocate(actualBytes);
    66         char *ptr = last_->ptr;
    67         last_->ptr += actualBytes;
    68         return ptr;
    69     }
    70 
    71     char *enter();
    72     void leave(char *position);
    73 };
    74 
    75 class PoolScope
    76 {
    77     PoolAllocator &pool_;
    78     char *position_;
    79 
    80   public:
    81     PoolScope(PoolAllocator &allocator)
    82       : pool_(allocator),
    83         position_(allocator.enter())
    84     {
    85     }
    86     ~PoolScope()
    87     {
    88         pool_.leave(position_);
    89     }
    90 };
    91 
    92 class PoolObject
    93 {
    94   public:
    95     void *operator new(size_t size, PoolAllocator &pool) {
    96         return pool.allocate(size);
    97     }
    98     void *operator new [](size_t size, PoolAllocator &pool) {
    99         return pool.allocate(size);
   100     }
   101     
   102     // Using delete on pool-allocated objects is illegal.
   103     void operator delete(void *ptr, PoolAllocator &pool) {
   104         assert(false);
   105     }
   106     void operator delete [](void *ptr, PoolAllocator &pool) {
   107         assert(false);
   108     }
   109 };
   110 
   111 class PoolAllocationPolicy
   112 {
   113   protected:
   114     void reportAllocationOverflow();
   115     void reportOutOfMemory();
   116 
   117   public:
   118     void *malloc(size_t bytes);
   119     void free(void *ptr);
   120 };
   121 
   122 template <typename T>
   123 class PoolList
   124   : public Vector<T, PoolAllocationPolicy>,
   125     public PoolObject
   126 {
   127   public:
   128     PoolList()
   129       : Vector(PoolAllocationPolicy())
   130     {
   131     }
   132 };
   133 
   134 }
   135 
   136 #endif // _include_jitcraft_pool_allocator_h_