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.

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

mercurial