Line data Source code
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 : * JS allocation policies.
9 : *
10 : * The allocators here are for system memory with lifetimes which are not
11 : * managed by the GC. See the comment at the top of vm/MallocProvider.h.
12 : */
13 :
14 : #ifndef jsalloc_h
15 : #define jsalloc_h
16 :
17 : #include "js/TypeDecls.h"
18 : #include "js/Utility.h"
19 :
20 : extern JS_PUBLIC_API(void) JS_ReportOutOfMemory(JSContext* cx);
21 :
22 : namespace js {
23 :
24 : enum class AllocFunction {
25 : Malloc,
26 : Calloc,
27 : Realloc
28 : };
29 : /* Policy for using system memory functions and doing no error reporting. */
30 8517 : class SystemAllocPolicy
31 : {
32 : public:
33 13912 : template <typename T> T* maybe_pod_malloc(size_t numElems) { return js_pod_malloc<T>(numElems); }
34 11235 : template <typename T> T* maybe_pod_calloc(size_t numElems) { return js_pod_calloc<T>(numElems); }
35 15432 : template <typename T> T* maybe_pod_realloc(T* p, size_t oldSize, size_t newSize) {
36 15432 : return js_pod_realloc<T>(p, oldSize, newSize);
37 : }
38 13912 : template <typename T> T* pod_malloc(size_t numElems) { return maybe_pod_malloc<T>(numElems); }
39 10322 : template <typename T> T* pod_calloc(size_t numElems) { return maybe_pod_calloc<T>(numElems); }
40 1197 : template <typename T> T* pod_realloc(T* p, size_t oldSize, size_t newSize) {
41 1197 : return maybe_pod_realloc<T>(p, oldSize, newSize);
42 : }
43 29190 : void free_(void* p) { js_free(p); }
44 0 : void reportAllocOverflow() const {}
45 482553 : bool checkSimulatedOOM() const {
46 482553 : return !js::oom::ShouldFailWithOOM();
47 : }
48 : };
49 :
50 : void ReportOutOfMemory(JSContext* cx);
51 :
52 : /*
53 : * Allocation policy that calls the system memory functions and reports errors
54 : * to the context. Since the JSContext given on construction is stored for
55 : * the lifetime of the container, this policy may only be used for containers
56 : * whose lifetime is a shorter than the given JSContext.
57 : *
58 : * FIXME bug 647103 - rewrite this in terms of temporary allocation functions,
59 : * not the system ones.
60 : */
61 : class TempAllocPolicy
62 : {
63 : JSContext* const cx_;
64 :
65 : /*
66 : * Non-inline helper to call JSRuntime::onOutOfMemory with minimal
67 : * code bloat.
68 : */
69 : JS_FRIEND_API(void*) onOutOfMemory(AllocFunction allocFunc, size_t nbytes,
70 : void* reallocPtr = nullptr);
71 :
72 : template <typename T>
73 0 : T* onOutOfMemoryTyped(AllocFunction allocFunc, size_t numElems, void* reallocPtr = nullptr) {
74 : size_t bytes;
75 0 : if (MOZ_UNLIKELY(!CalculateAllocSize<T>(numElems, &bytes)))
76 0 : return nullptr;
77 0 : return static_cast<T*>(onOutOfMemory(allocFunc, bytes, reallocPtr));
78 : }
79 :
80 : public:
81 242148 : MOZ_IMPLICIT TempAllocPolicy(JSContext* cx) : cx_(cx) {}
82 :
83 : template <typename T>
84 67204 : T* maybe_pod_malloc(size_t numElems) {
85 67204 : return js_pod_malloc<T>(numElems);
86 : }
87 :
88 : template <typename T>
89 2121 : T* maybe_pod_calloc(size_t numElems) {
90 2121 : return js_pod_calloc<T>(numElems);
91 : }
92 :
93 : template <typename T>
94 9708 : T* maybe_pod_realloc(T* prior, size_t oldSize, size_t newSize) {
95 9708 : return js_pod_realloc<T>(prior, oldSize, newSize);
96 : }
97 :
98 : template <typename T>
99 67204 : T* pod_malloc(size_t numElems) {
100 67204 : T* p = maybe_pod_malloc<T>(numElems);
101 67204 : if (MOZ_UNLIKELY(!p))
102 0 : p = onOutOfMemoryTyped<T>(AllocFunction::Malloc, numElems);
103 67204 : return p;
104 : }
105 :
106 : template <typename T>
107 2121 : T* pod_calloc(size_t numElems) {
108 2121 : T* p = maybe_pod_calloc<T>(numElems);
109 2121 : if (MOZ_UNLIKELY(!p))
110 0 : p = onOutOfMemoryTyped<T>(AllocFunction::Calloc, numElems);
111 2121 : return p;
112 : }
113 :
114 : template <typename T>
115 9708 : T* pod_realloc(T* prior, size_t oldSize, size_t newSize) {
116 9708 : T* p2 = maybe_pod_realloc<T>(prior, oldSize, newSize);
117 9708 : if (MOZ_UNLIKELY(!p2))
118 0 : p2 = onOutOfMemoryTyped<T>(AllocFunction::Realloc, newSize, prior);
119 9708 : return p2;
120 : }
121 :
122 69473 : void free_(void* p) {
123 69473 : js_free(p);
124 69473 : }
125 :
126 : JS_FRIEND_API(void) reportAllocOverflow() const;
127 :
128 253428 : bool checkSimulatedOOM() const {
129 253428 : if (js::oom::ShouldFailWithOOM()) {
130 0 : ReportOutOfMemory(cx_);
131 0 : return false;
132 : }
133 :
134 253428 : return true;
135 : }
136 : };
137 :
138 : } /* namespace js */
139 :
140 : #endif /* jsalloc_h */
|