ASC-4041: Skip two spidermonkey regression tests due to stack overflow when compiling...
[tamarin-stm.git] / MMgc / FixedMalloc.h
blob05b1346177b47ee93d657945f89070a72dc385f7
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
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/
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.
16 * The Original Code is [Open Source Virtual Machine.].
18 * The Initial Developer of the Original Code is
19 * Adobe System Incorporated.
20 * Portions created by the Initial Developer are Copyright (C) 2004-2006
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Adobe AS3 Team
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.
38 * ***** END LICENSE BLOCK ***** */
40 #ifndef __Malloc__
41 #define __Malloc__
43 namespace MMgc
45 /**
46 * A general purpose memory allocator.
48 * FixedMalloc is a singleton, obtainable by calling FixedMalloc::GetFixedMalloc.
49 * The "owner" of the FixedMalloc is GCHeap, the block manager.
51 * FixedMalloc uses size classes; each size class is handled by a FixedAllocSafe
52 * instance. Large objects are handled specially. All objects are headerless.
54 class FixedMalloc
56 friend class GCHeap;
58 public:
59 /**
60 * Obtain a FixedMalloc instance.
62 * @return the FixedMalloc singleton.
64 static FixedMalloc *GetFixedMalloc();
66 /**
67 * Obtain a FixedMalloc instance.
69 * @return the FixedMalloc singleton.
71 * @note Backward compatible name for GetFixedMalloc, not used by Tamarin.
73 static FixedMalloc *GetInstance();
75 /**
76 * Allocate one object from this allocator.
78 * @param size The size of the object.
79 * @param flags A bit vector of allocation options.
81 * @return A pointer to the object. The pointer may be NULL only if kCanFail is
82 * part of flags. The memory is zeroed only if kZero is part of flags.
84 void* Alloc(size_t size, FixedMallocOpts flags=kNone);
86 /**
87 * Allocate one object from this allocator, may return NULL.
89 * @param size The size of the object.
91 * @return A pointer to the object. The pointer may be NULL.
92 * The memory is /not/ necessarily zeroed; for zeroed memory, use Alloc().
94 * @note Exactly like Alloc with flags=kCanFail.
96 void *PleaseAlloc(size_t size);
98 /**
99 * Allocate one object from this allocator.
101 * @param size The size of the object.
102 * @param flags A bit vector of allocation options.
104 * @return A pointer to the object. The pointer may be NULL only if kCanFail is
105 * part of flags. The memory is zeroed only if kZero is part of flags.
107 * @note Exactly like Alloc, but guaranteed not to be inlined - used by ::new etc.
109 void* FASTCALL OutOfLineAlloc(size_t size, FixedMallocOpts flags=kNone);
112 * Allocate space for an array of objects from this allocator.
114 * @param count The number of objects
115 * @param size The size of each part object
116 * @param flags A bit vector of allocation options
118 * @return A pointer to the aggregate object. The pointer may be NULL only if
119 * kCanFail is part of flags. The memory is zeroed only if kZero is part
120 * of flags.
122 * @note Unlike 'calloc' in the C library, this does /not/ zero the memory
123 * unless kZero is passed in flags. The name 'Calloc' comes from the
124 * shape of the API.
126 void *Calloc(size_t count, size_t size, FixedMallocOpts flags=kNone);
129 * Free an object allocated through FixedMalloc.
131 * @param item The object to free.
133 void Free(void *item);
136 * Free an object allocated through FixedMalloc.
138 * @param item The object to free.
140 * @note Exactly like Free, but guaranteed not to be inlined - used by ::delete etc.
142 void FASTCALL OutOfLineFree(void* p);
145 * Obtain the size of an object allocated through FixedMalloc.
147 * @param item An object reference.
149 * @return the allocated size of 'item'
151 size_t Size(const void *item);
154 * Obtain FixedMalloc's heap usage.
156 * @return The total number of /blocks/ managed by FixedMalloc, where the
157 * block size is given by GCHeap::kBlockSize.
159 size_t GetTotalSize();
162 * Obtain current (not running total) allocation information for FixedMalloc.
164 * @param totalAskSize (out) The total number of bytes requested
165 * @param totalAllocated (out) The number of bytes actually allocated
167 void GetUsageInfo(size_t& totalAskSize, size_t& totalAllocated);
170 * Obtain current allocation information for FixedMalloc.
172 * @return the number bytes currently allocated by FixedMalloc.
174 * @note The returned value is the totalAllocated value returned from GetUsageInfo.
176 size_t GetBytesInUse();
178 #ifdef MMGC_MEMORY_PROFILER
180 * Print semi-structured human-readable data about FixedMalloc memory usage
181 * on the VMPI_log channel.
183 void DumpMemoryInfo();
184 #endif
186 private:
187 #ifdef DEBUG
188 // Data type used for tracking live large objects, used by EnsureFixedMallocMemory.
189 struct LargeObject;
190 #endif
192 // Return true if item is a large-object item allocated through FixedMalloc.
193 static bool IsLargeAlloc(const void *item);
195 // Initialize FixedMalloc. Must be called from GCHeap during GCHeap setup.
196 void InitInstance(GCHeap *heap);
198 // Destroy FixedMalloc and free all resources.
199 void DestroyInstance();
201 // Return the total number of blocks allocated by FixedMalloc for large-object allocations.
202 size_t GetNumLargeBlocks();
204 // Record that 'blocksAllocated' blocks are about to be or have been allocated
205 // in support of large-object allocation.
206 void UpdateLargeAllocStats(void* item, size_t blocksAllocated);
208 // Record that 'blocksFreed' blocks are about to be or have been freed
209 // in support of large-object freeing.
210 void UpdateLargeFreeStats(void* item, size_t blocksFreed);
212 #ifdef DEBUG
213 // Check that item was allocated by an allocator owned by this FixedMalloc,
214 // otherwise trigger an assertion failure.
215 void EnsureFixedMallocMemory(const void* item);
217 #ifndef AVMPLUS_SAMPLER
218 // Track large object 'item', which is newly allocated.
219 void AddToLargeObjectTracker(const void* item);
221 // Untrack large object 'item', which is about to be freed.
222 void RemoveFromLargeObjectTracker(const void* item);
223 #endif // !AVMPLUS_SAMPLER
224 #endif // DEBUG
226 // Return a thread-safe allocator for objects of the given size.
227 FixedAllocSafe* FindAllocatorForSize(size_t size);
229 // Return an object of at least the requested size, allocated with the given
230 // flags. The object's real size will be an integral number of blocks.
231 void *LargeAlloc(size_t size, FixedMallocOpts flags=kNone);
233 // Free the item returned from LargeAlloc.
234 void LargeFree(void *item);
236 // Return the allocated size (in bytes) of 'item', which must have been returned
237 // from LargeAlloc.
238 size_t LargeSize(const void *item);
240 private:
241 static FixedMalloc *instance; // The singleton FixedMalloc
243 #ifdef MMGC_64BIT
244 const static int kLargestAlloc = 2016; // The largest small-object allocation
245 #else
246 const static int kLargestAlloc = 2032; // The largest small-object allocation
247 #endif
248 const static int kNumSizeClasses = 41; // The number of small-object size classes
250 // A table whose nth entry is the maximum size accomodated by the
251 // allocator in the nth entry of the m_allocs table.
252 const static int16_t kSizeClasses[kNumSizeClasses];
254 // The number of entries in the table mapping a request not greater than
255 // kLargestAlloc to the appropriate index in m_allocs.
256 const static unsigned kMaxSizeClassIndex = (kLargestAlloc>>3)+1;
258 // A table mapping a request not greater than kLargestAlloc to the appropriate
259 // index in m_allocs. The mapping is complicated and explained in comments
260 // in FixedMalloc.cpp, also see the implementation of FindAllocatorForSize.
261 const static uint8_t kSizeClassIndex[kMaxSizeClassIndex];
263 private:
264 GCHeap *m_heap; // The heap from which we allocate, set in InitInstance
265 FixedAllocSafe m_allocs[kNumSizeClasses]; // The array of size-segregated allocators, set in InitInstance
267 vmpi_spin_lock_t m_largeAllocInfoLock; // Protects numLargeBlocks and totalAskSizeLargeAllocs
269 size_t numLargeBlocks; // Number of large-object blocks owned by this FixedMalloc
270 #ifdef MMGC_MEMORY_PROFILER
271 size_t totalAskSizeLargeAllocs; // The current number of bytes requested for large objects
272 #endif
273 #if defined DEBUG
274 vmpi_spin_lock_t m_largeObjectLock; // Protects largeObjects
275 LargeObject *largeObjects; // Data structure of live large objects, initially NULL
276 #endif
279 #endif /* __Malloc__ */