PageRenderTime 25ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/js/src/nanojit/Allocator.h

http://github.com/zpao/v8monkey
C++ Header | 143 lines | 64 code | 19 blank | 60 comment | 4 complexity | 404a43062200e1dec86cb0cf5271e8f1 MD5 | raw file
  1/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2/* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
  3/* ***** BEGIN LICENSE BLOCK *****
  4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5 *
  6 * The contents of this file are subject to the Mozilla Public License Version
  7 * 1.1 (the "License"); you may not use this file except in compliance with
  8 * the License. You may obtain a copy of the License at
  9 * http://www.mozilla.org/MPL/
 10 *
 11 * Software distributed under the License is distributed on an "AS IS" basis,
 12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 13 * for the specific language governing rights and limitations under the
 14 * License.
 15 *
 16 * The Original Code is [Open Source Virtual Machine].
 17 *
 18 * The Initial Developer of the Original Code is
 19 * Adobe System Incorporated.
 20 * Portions created by the Initial Developer are Copyright (C) 2009
 21 * the Initial Developer. All Rights Reserved.
 22 *
 23 * Contributor(s):
 24 *   Adobe AS3 Team
 25 *
 26 * Alternatively, the contents of this file may be used under the terms of
 27 * either the GNU General Public License Version 2 or later (the "GPL"), or
 28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29 * in which case the provisions of the GPL or the LGPL are applicable instead
 30 * of those above. If you wish to allow use of your version of this file only
 31 * under the terms of either the GPL or the LGPL, and not to allow others to
 32 * use your version of this file under the terms of the MPL, indicate your
 33 * decision by deleting the provisions above and replace them with the notice
 34 * and other provisions required by the GPL or the LGPL. If you do not delete
 35 * the provisions above, a recipient may use your version of this file under
 36 * the terms of any one of the MPL, the GPL or the LGPL.
 37 *
 38 * ***** END LICENSE BLOCK ***** */
 39
 40#ifndef __nanojit_Allocator__
 41#define __nanojit_Allocator__
 42
 43namespace nanojit
 44{
 45    /**
 46     * Allocator is a bump-pointer allocator with an SPI for getting more
 47     * memory from embedder-implemented allocator, such as malloc()/free().
 48     *
 49     * alloc() never returns NULL.  The implementation of allocChunk()
 50     * is expected to perform a longjmp or exception when an allocation can't
 51     * proceed.  fallibleAlloc() (and fallibleAllocChunk()) may return NULL.
 52     * They should be used for large allocations whose failure can be handled
 53     * without too much difficulty.
 54     */
 55    class Allocator {
 56    public:
 57        Allocator();
 58        ~Allocator();
 59
 60        // Usable space in the minimum chunk size;  there are also a few bytes
 61        // used for administration.
 62        static const size_t MIN_CHUNK_SZB = 2000;
 63
 64        void reset();
 65
 66        /** alloc memory, never return null. */
 67        void* alloc(size_t nbytes) {
 68            void* p;
 69            nbytes = (nbytes + 7) & ~7; // round up
 70            if (current_top + nbytes <= current_limit) {
 71                p = current_top;
 72                current_top += nbytes;
 73            } else {
 74                p = allocSlow(nbytes, /* fallible = */false);
 75                NanoAssert(p);
 76            }
 77            return p;
 78        }
 79
 80        /** alloc memory, maybe return null. */
 81        void* fallibleAlloc(size_t nbytes) {
 82            void* p;
 83            nbytes = (nbytes + 7) & ~7; // round up
 84            if (current_top + nbytes <= current_limit) {
 85                p = current_top;
 86                current_top += nbytes;
 87            } else {
 88                p = allocSlow(nbytes, /* fallible = */true);
 89            }
 90            return p;
 91        }
 92
 93        size_t getBytesAllocated(size_t(*my_malloc_usable_size)(void *));
 94
 95    protected:
 96        void* allocSlow(size_t nbytes, bool fallible = false);
 97        bool fill(size_t minbytes, bool fallible);
 98
 99        class Chunk {
100        public:
101            Chunk* prev;
102            size_t size;
103            int64_t data[1]; // int64_t forces 8-byte alignment.
104        };
105
106        Chunk* current_chunk;
107        char* current_top;
108        char* current_limit;
109
110        // allocator SPI
111
112        /** allocate another block from a host provided allocator */
113        void* allocChunk(size_t nbytes, bool fallible);
114
115        /** free back to the same allocator */
116        void freeChunk(void*);
117
118        /** hook for post-reset action. */
119        void postReset();
120    };
121}
122
123/** global new overload enabling this pattern:  new (allocator) T(...) */
124inline void* operator new(size_t size, nanojit::Allocator &a) {
125    return a.alloc(size);
126}
127
128/** global new overload enabling this pattern:  new (allocator) T(...) */
129inline void* operator new(size_t size, nanojit::Allocator *a) {
130    return a->alloc(size);
131}
132
133/** global new[] overload enabling this pattern: new (allocator) T[] */
134inline void* operator new[](size_t size, nanojit::Allocator& a) {
135    return a.alloc(size);
136}
137
138/** global new[] overload enabling this pattern: new (allocator) T[] */
139inline void* operator new[](size_t size, nanojit::Allocator* a) {
140    return a->alloc(size);
141}
142
143#endif // __nanojit_Allocator__