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.

     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/. */
     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  */
    37 #ifndef vm_MallocProvider_h
    38 #define vm_MallocProvider_h
    40 #include "mozilla/Attributes.h"
    41 #include "mozilla/Likely.h"
    43 #include "js/Utility.h"
    45 namespace js {
    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     }
    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     }
    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     }
    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     }
    88     template <class T>
    89     T *pod_malloc() {
    90         return (T *)malloc_(sizeof(T));
    91     }
    93     template <class T>
    94     T *pod_calloc() {
    95         return (T *)calloc_(sizeof(T));
    96     }
    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     }
   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     }
   118     JS_DECLARE_NEW_METHODS(new_, malloc_, MOZ_ALWAYS_INLINE)
   119 };
   121 } /* namespace js */
   123 #endif /* vm_MallocProvider_h */

mercurial