jitcraft/Zone.h
author David Anderson <dvander@alliedmods.net>
Mon Jul 02 01:24:57 2012 -0700 (2012-07-02)
changeset 25 23e8b3d8e4e6
parent 12 75e385f68b7e
child 27 28697d267222
permissions -rw-r--r--
Scanning, parsing, and static analysis now report errors properly.
     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_thread_h_
    19 #define _include_jitcraft_thread_h_
    20 
    21 #include "PoolAllocator.h"
    22 #include "Stack.h"
    23 #include "Heap.h"
    24 #include "Barriers.h"
    25 
    26 namespace jc {
    27 
    28 class TypeManager;
    29 
    30 class Zone
    31 {
    32     PoolAllocator pool_;
    33     Stack stack_;
    34     Heap heap_;
    35 
    36     // Map cache.
    37     Unbarriered<Map> maps_[MapKinds_Total];
    38 
    39     TypeManager *types_;
    40 
    41   public:
    42     Zone();
    43     bool initialize();
    44 
    45     PoolAllocator &pool() {
    46         return pool_;
    47     }
    48     Stack &stack() {
    49         return stack_;
    50     }
    51     Heap &heap() {
    52         return heap_;
    53     }
    54     Object *allocate(MapKind kind, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    55         assert(maps_[kind]);
    56         return allocate(maps_[kind], bytes, tenure);
    57     }
    58     // :TODO: handle
    59     Object *allocate(Map *map, size_t bytes, Heap::Tenure tenure = Heap::Tenure_Default) {
    60         return heap_.allocate(map, bytes, tenure);
    61     }
    62     Map *allocateMap(MapKind kind, size_t bytes, MapKind parent = MapKind_Meta);
    63     TypeManager *types() const {
    64         return types_;
    65     }
    66 };
    67 
    68 // In a multi-threaded system, this would be a TLS variable.
    69 // In the interest of simplicity, we are single-threaded for now.
    70 extern Zone SingleThreadedZone;
    71 
    72 static inline Zone *ZONE()
    73 {
    74     return &SingleThreadedZone;
    75 }
    76 
    77 class LocalTop
    78 {
    79     Top *prev_;
    80     Top &mine_;
    81 
    82   public:
    83     LocalTop(Top &mine)
    84       : mine_(mine),
    85         prev_(ZONE()->stack().top())
    86     {
    87         ZONE()->stack().setTop(&mine_);
    88     }
    89 
    90     ~LocalTop()
    91     {
    92         ZONE()->stack().setTop(prev_);
    93     }
    94 };
    95 
    96 }
    97 
    98 #endif // _include_jitcraft_thread_h_