Bug 891177 - Add AllocPolicy.h to define an implementation policy concept for use in mfbt. r=terrence
authorJeff Walden <jwalden@mit.edu>
Tue, 02 Jul 2013 17:16:07 -0700
changeset 151215 272dce99bdd9943c8b1bf22c844e951c34d95935
parent 151214 365723243b79195006a803f70a7c1610477e8e2a
child 151216 2e8e8ebef9285fc9d18186b11666f19f546fc6df
push id382
push userakeybl@mozilla.com
push dateMon, 21 Oct 2013 21:47:13 +0000
treeherdermozilla-release@5f1868ee45cb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersterrence
bugs891177
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 891177 - Add AllocPolicy.h to define an implementation policy concept for use in mfbt. r=terrence
js/src/jsalloc.h
mfbt/AllocPolicy.h
mfbt/exported_headers.mk
--- a/js/src/jsalloc.h
+++ b/js/src/jsalloc.h
@@ -1,42 +1,29 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  * vim: set ts=8 sts=4 et sw=4 tw=99:
  * This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
+/* JS allocation policies. */
+
 #ifndef jsalloc_h
 #define jsalloc_h
 
+#include "mozilla/AllocPolicy.h"
+
 #include "js/Utility.h"
 
 struct JSContext;
 
 namespace js {
 
 class ContextFriendFields;
 
-/*
- * Allocation policies.  These model the concept:
- *  - public copy constructor, assignment, destructor
- *  - void *malloc_(size_t)
- *      Responsible for OOM reporting on NULL return value.
- *  - void *calloc_(size_t)
- *      Responsible for OOM reporting on NULL return value.
- *  - void *realloc_(size_t)
- *      Responsible for OOM reporting on NULL return value.
- *      The *used* bytes of the previous buffer is passed in
- *      (rather than the old allocation size), in addition to
- *      the *new* allocation size requested.
- *  - void free_(void *)
- *  - reportAllocOverflow()
- *      Called on overflow before the container returns NULL.
- */
-
 /* Policy for using system memory functions and doing no error reporting. */
 class SystemAllocPolicy
 {
   public:
     void *malloc_(size_t bytes) { return js_malloc(bytes); }
     void *calloc_(size_t bytes) { return js_calloc(bytes); }
     void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); }
     void free_(void *p) { js_free(p); }
new file mode 100644
--- /dev/null
+++ b/mfbt/AllocPolicy.h
@@ -0,0 +1,62 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+ * An allocation policy concept, usable for structures and algorithms to
+ * control how memory is allocated and how failures are handled.
+ */
+
+#ifndef mozilla_AllocPolicy_h_
+#define mozilla_AllocPolicy_h_
+
+#include <stddef.h>
+#include <stdlib.h>
+
+namespace mozilla {
+
+/*
+ * Allocation policies are used to implement the standard allocation behaviors
+ * in a customizable way.  Additionally, custom behaviors may be added to these
+ * behaviors, such as additionally reporting an error through an out-of-band
+ * mechanism when OOM occurs.  The concept modeled here is as follows:
+ *
+ *  - public copy constructor, assignment, destructor
+ *  - void* malloc_(size_t)
+ *      Responsible for OOM reporting when null is returned.
+ *  - void* calloc_(size_t)
+ *      Responsible for OOM reporting when null is returned.
+ *  - void* realloc_(void*, size_t, size_t)
+ *      Responsible for OOM reporting when null is returned.  The *used* bytes
+ *      of the previous buffer is passed in (rather than the old allocation
+ *      size), in addition to the *new* allocation size requested.
+ *  - void free_(void*)
+ *  - void reportAllocOverflow() const
+ *      Called on allocation overflow (that is, an allocation implicitly tried
+ *      to allocate more than the available memory space -- think allocating an
+ *      array of large-size objects, where N * size overflows) before null is
+ *      returned.
+ *
+ * mfbt provides (and typically uses by default) only MallocAllocPolicy, which
+ * does nothing more than delegate to the malloc/alloc/free functions.
+ */
+
+/*
+ * A policy that straightforwardly uses malloc/calloc/realloc/free and adds no
+ * extra behaviors.
+ */
+class MallocAllocPolicy
+{
+  public:
+    void* malloc_(size_t bytes) { return malloc(bytes); }
+    void* calloc_(size_t bytes) { return calloc(bytes, 1); }
+    void* realloc_(void* p, size_t oldBytes, size_t bytes) { return realloc(p, bytes); }
+    void free_(void* p) { free(p); }
+    void reportAllocOverflow() const {}
+};
+
+
+} // namespace mozilla
+
+#endif // mozilla_AllocPolicy_h_
--- a/mfbt/exported_headers.mk
+++ b/mfbt/exported_headers.mk
@@ -4,16 +4,17 @@
 
 # This file defines the headers exported by mfbt.  It is included by mfbt
 # itself and by the JS engine, which, when built standalone, must install
 # mfbt's exported headers itself.
 
 EXPORTS_NAMESPACES += mozilla
 
 EXPORTS_mozilla += \
+  AllocPolicy.h \
   Assertions.h \
   Atomics.h \
   Attributes.h \
   BloomFilter.h \
   Casting.h \
   Char16.h \
   CheckedInt.h \
   Compiler.h \