Skip to content

Commit

Permalink
Bug 891177 - Add AllocPolicy.h to define an implementation policy con…
Browse files Browse the repository at this point in the history
…cept for use in mfbt. r=terrence

--HG--
extra : rebase_source : f6336b5ba3298bbf9c5418b4e9d993b9173f7926
  • Loading branch information
jswalden committed Jul 3, 2013
1 parent 325ce1a commit 791a4e2
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 17 deletions.
21 changes: 4 additions & 17 deletions js/src/jsalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
* 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;
Expand All @@ -15,23 +19,6 @@ 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
{
Expand Down
62 changes: 62 additions & 0 deletions mfbt/AllocPolicy.h
Original file line number Diff line number Diff line change
@@ -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_
1 change: 1 addition & 0 deletions mfbt/exported_headers.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
EXPORTS_NAMESPACES += mozilla

EXPORTS_mozilla += \
AllocPolicy.h \
Assertions.h \
Atomics.h \
Attributes.h \
Expand Down

0 comments on commit 791a4e2

Please sign in to comment.