js/src/vm/MallocProvider.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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

mercurial