mfbt/AllocPolicy.h

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     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  * An allocation policy concept, usable for structures and algorithms to
     9  * control how memory is allocated and how failures are handled.
    10  */
    12 #ifndef mozilla_AllocPolicy_h
    13 #define mozilla_AllocPolicy_h
    15 #include <stddef.h>
    16 #include <stdlib.h>
    18 namespace mozilla {
    20 /*
    21  * Allocation policies are used to implement the standard allocation behaviors
    22  * in a customizable way.  Additionally, custom behaviors may be added to these
    23  * behaviors, such as additionally reporting an error through an out-of-band
    24  * mechanism when OOM occurs.  The concept modeled here is as follows:
    25  *
    26  *  - public copy constructor, assignment, destructor
    27  *  - void* malloc_(size_t)
    28  *      Responsible for OOM reporting when null is returned.
    29  *  - void* calloc_(size_t)
    30  *      Responsible for OOM reporting when null is returned.
    31  *  - void* realloc_(void*, size_t, size_t)
    32  *      Responsible for OOM reporting when null is returned.  The *used* bytes
    33  *      of the previous buffer is passed in (rather than the old allocation
    34  *      size), in addition to the *new* allocation size requested.
    35  *  - void free_(void*)
    36  *  - void reportAllocOverflow() const
    37  *      Called on allocation overflow (that is, an allocation implicitly tried
    38  *      to allocate more than the available memory space -- think allocating an
    39  *      array of large-size objects, where N * size overflows) before null is
    40  *      returned.
    41  *
    42  * mfbt provides (and typically uses by default) only MallocAllocPolicy, which
    43  * does nothing more than delegate to the malloc/alloc/free functions.
    44  */
    46 /*
    47  * A policy that straightforwardly uses malloc/calloc/realloc/free and adds no
    48  * extra behaviors.
    49  */
    50 class MallocAllocPolicy
    51 {
    52   public:
    53     void* malloc_(size_t bytes) { return malloc(bytes); }
    54     void* calloc_(size_t bytes) { return calloc(bytes, 1); }
    55     void* realloc_(void* p, size_t oldBytes, size_t bytes) { return realloc(p, bytes); }
    56     void free_(void* p) { free(p); }
    57     void reportAllocOverflow() const {}
    58 };
    61 } // namespace mozilla
    63 #endif /* mozilla_AllocPolicy_h */

mercurial