1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla SpiderMonkey JavaScript code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2011
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Chris Leary <cdleary@mozilla.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "LifoAlloc.h"
42 :
43 : #include <new>
44 :
45 : using namespace js;
46 :
47 : namespace js {
48 : namespace detail {
49 :
50 : BumpChunk *
51 93652 : BumpChunk::new_(size_t chunkSize)
52 : {
53 93652 : JS_ASSERT(RoundUpPow2(chunkSize) == chunkSize);
54 93652 : void *mem = js_malloc(chunkSize);
55 93652 : if (!mem)
56 0 : return NULL;
57 93652 : BumpChunk *result = new (mem) BumpChunk(chunkSize - sizeof(BumpChunk));
58 :
59 : /*
60 : * We assume that the alignment of sAlign is less than that of
61 : * the underlying memory allocator -- creating a new BumpChunk should
62 : * always satisfy the sAlign alignment constraint.
63 : */
64 93652 : JS_ASSERT(AlignPtr(result->bump) == result->bump);
65 93652 : return result;
66 : }
67 :
68 : void
69 93652 : BumpChunk::delete_(BumpChunk *chunk)
70 : {
71 : #ifdef DEBUG
72 93652 : memset(chunk, 0xcd, sizeof(*chunk) + chunk->bumpSpaceSize);
73 : #endif
74 93652 : js_free(chunk);
75 93652 : }
76 :
77 : bool
78 32198691 : BumpChunk::canAlloc(size_t n)
79 : {
80 32198691 : char *aligned = AlignPtr(bump);
81 32198691 : char *bumped = aligned + n;
82 32198691 : return bumped <= limit && bumped > headerBase();
83 : }
84 :
85 : } /* namespace detail */
86 : } /* namespace js */
87 :
88 : void
89 176541 : LifoAlloc::freeAll()
90 : {
91 446734 : while (first) {
92 93652 : BumpChunk *victim = first;
93 93652 : first = first->next();
94 93652 : BumpChunk::delete_(victim);
95 : }
96 176541 : first = latest = NULL;
97 176541 : }
98 :
99 : void
100 39964 : LifoAlloc::freeUnused()
101 : {
102 : /* Don't free anything if we have outstanding marks. */
103 39964 : if (markCount || !first)
104 20293 : return;
105 :
106 19671 : JS_ASSERT(first && latest);
107 :
108 : /* Rewind through any unused chunks. */
109 19671 : if (!latest->used()) {
110 19662 : BumpChunk *lastUsed = NULL;
111 19662 : for (BumpChunk *it = first; it != latest; it = it->next()) {
112 0 : if (it->used())
113 0 : lastUsed = it;
114 : }
115 19662 : if (!lastUsed) {
116 19662 : freeAll();
117 19662 : return;
118 : }
119 0 : latest = lastUsed;
120 : }
121 :
122 : /* Free all chunks after |latest|. */
123 9 : BumpChunk *it = latest->next();
124 18 : while (it) {
125 0 : BumpChunk *victim = it;
126 0 : it = it->next();
127 0 : BumpChunk::delete_(victim);
128 : }
129 :
130 9 : latest->setNext(NULL);
131 : }
132 :
133 : LifoAlloc::BumpChunk *
134 116962 : LifoAlloc::getOrCreateChunk(size_t n)
135 : {
136 116962 : if (first) {
137 : /* Look for existing, unused BumpChunks to satisfy the request. */
138 113962 : while (latest->next()) {
139 23310 : latest = latest->next();
140 23310 : latest->resetBump(); /* This was an unused BumpChunk on the chain. */
141 23310 : if (latest->canAlloc(n))
142 23310 : return latest;
143 : }
144 : }
145 :
146 93652 : size_t defaultChunkFreeSpace = defaultChunkSize_ - sizeof(BumpChunk);
147 : size_t chunkSize;
148 93652 : if (n > defaultChunkFreeSpace) {
149 135 : size_t allocSizeWithHeader = n + sizeof(BumpChunk);
150 :
151 : /* Guard for overflow. */
152 135 : if (allocSizeWithHeader < n ||
153 : (allocSizeWithHeader & (size_t(1) << (tl::BitSize<size_t>::result - 1)))) {
154 0 : return NULL;
155 : }
156 :
157 135 : chunkSize = RoundUpPow2(allocSizeWithHeader);
158 : } else {
159 93517 : chunkSize = defaultChunkSize_;
160 : }
161 :
162 : /* If we get here, we couldn't find an existing BumpChunk to fill the request. */
163 93652 : BumpChunk *newChunk = BumpChunk::new_(chunkSize);
164 93652 : if (!newChunk)
165 0 : return NULL;
166 93652 : if (!first) {
167 59981 : latest = first = newChunk;
168 : } else {
169 33671 : JS_ASSERT(latest && !latest->next());
170 33671 : latest->setNext(newChunk);
171 33671 : latest = newChunk;
172 : }
173 93652 : return newChunk;
174 : }
|