js/src/vm/MallocProvider.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:c13cf11a0683
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /*
8 * Hierarchy of SpiderMonkey system memory allocators:
9 *
10 * - System {m,c,re}alloc/new/free: Overridden by jemalloc in most
11 * environments. Do not use these functions directly.
12 *
13 * - js_{m,c,re}alloc/new/free: Wraps the system allocators and adds a
14 * failure injection framework for use by the fuzzers as well as templated,
15 * typesafe variants. See js/public/Utility.h.
16 *
17 * - AllocPolicy: An interface for the js allocators, for use with templates.
18 * These allocators are for system memory whose lifetime is not associated
19 * with a GC thing. See js/src/jsalloc.h.
20 *
21 * - SystemAllocPolicy: No extra functionality over bare allocators.
22 *
23 * - TempAllocPolicy: Adds automatic error reporting to the provided
24 * Context when allocations fail.
25 *
26 * - MallocProvider. A mixin base class that handles automatically updating
27 * the GC's state in response to allocations that are tied to a GC lifetime
28 * or are for a particular GC purpose. These allocators must only be used
29 * for memory that will be freed when a GC thing is swept.
30 *
31 * - gc::Zone: Automatically triggers zone GC.
32 * - JSRuntime: Automatically triggers full GC.
33 * - ThreadsafeContext > ExclusiveContext > JSContext:
34 * Dispatches directly to the runtime.
35 */
36
37 #ifndef vm_MallocProvider_h
38 #define vm_MallocProvider_h
39
40 #include "mozilla/Attributes.h"
41 #include "mozilla/Likely.h"
42
43 #include "js/Utility.h"
44
45 namespace js {
46
47 template<class Client>
48 struct MallocProvider
49 {
50 void *malloc_(size_t bytes) {
51 Client *client = static_cast<Client *>(this);
52 client->updateMallocCounter(bytes);
53 void *p = js_malloc(bytes);
54 return MOZ_LIKELY(!!p) ? p : client->onOutOfMemory(nullptr, bytes);
55 }
56
57 void *calloc_(size_t bytes) {
58 Client *client = static_cast<Client *>(this);
59 client->updateMallocCounter(bytes);
60 void *p = js_calloc(bytes);
61 return MOZ_LIKELY(!!p) ? p : client->onOutOfMemory(reinterpret_cast<void *>(1), bytes);
62 }
63
64 void *realloc_(void *p, size_t oldBytes, size_t newBytes) {
65 Client *client = static_cast<Client *>(this);
66 /*
67 * For compatibility we do not account for realloc that decreases
68 * previously allocated memory.
69 */
70 if (newBytes > oldBytes)
71 client->updateMallocCounter(newBytes - oldBytes);
72 void *p2 = js_realloc(p, newBytes);
73 return MOZ_LIKELY(!!p2) ? p2 : client->onOutOfMemory(p, newBytes);
74 }
75
76 void *realloc_(void *p, size_t bytes) {
77 Client *client = static_cast<Client *>(this);
78 /*
79 * For compatibility we do not account for realloc that increases
80 * previously allocated memory.
81 */
82 if (!p)
83 client->updateMallocCounter(bytes);
84 void *p2 = js_realloc(p, bytes);
85 return MOZ_LIKELY(!!p2) ? p2 : client->onOutOfMemory(p, bytes);
86 }
87
88 template <class T>
89 T *pod_malloc() {
90 return (T *)malloc_(sizeof(T));
91 }
92
93 template <class T>
94 T *pod_calloc() {
95 return (T *)calloc_(sizeof(T));
96 }
97
98 template <class T>
99 T *pod_malloc(size_t numElems) {
100 if (numElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
101 Client *client = static_cast<Client *>(this);
102 client->reportAllocationOverflow();
103 return nullptr;
104 }
105 return (T *)malloc_(numElems * sizeof(T));
106 }
107
108 template <class T>
109 T *pod_calloc(size_t numElems, JSCompartment *comp = nullptr, JSContext *cx = nullptr) {
110 if (numElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
111 Client *client = static_cast<Client *>(this);
112 client->reportAllocationOverflow();
113 return nullptr;
114 }
115 return (T *)calloc_(numElems * sizeof(T));
116 }
117
118 JS_DECLARE_NEW_METHODS(new_, malloc_, MOZ_ALWAYS_INLINE)
119 };
120
121 } /* namespace js */
122
123 #endif /* vm_MallocProvider_h */

mercurial