2013-04-16 20:47:10 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: set ts=8 sts=4 et sw=4 tw=99:
|
2012-05-21 11:12:37 +00:00
|
|
|
* 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/. */
|
2011-03-30 10:10:12 +00:00
|
|
|
|
2014-01-24 21:22:31 +00:00
|
|
|
/*
|
|
|
|
* JS allocation policies.
|
|
|
|
*
|
|
|
|
* The allocators here are for system memory with lifetimes which are not
|
|
|
|
* managed by the GC. See the comment at the top of vm/MallocProvider.h.
|
|
|
|
*/
|
2013-07-03 00:16:07 +00:00
|
|
|
|
2013-06-20 00:59:46 +00:00
|
|
|
#ifndef jsalloc_h
|
|
|
|
#define jsalloc_h
|
2011-03-30 10:10:12 +00:00
|
|
|
|
2013-08-28 02:59:14 +00:00
|
|
|
#include "js/TypeDecls.h"
|
2013-04-24 06:44:36 +00:00
|
|
|
#include "js/Utility.h"
|
|
|
|
|
2011-03-30 10:10:12 +00:00
|
|
|
namespace js {
|
|
|
|
|
2013-07-10 15:29:52 +00:00
|
|
|
class ContextFriendFields;
|
|
|
|
|
2011-03-30 10:10:12 +00:00
|
|
|
/* Policy for using system memory functions and doing no error reporting. */
|
|
|
|
class SystemAllocPolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void *malloc_(size_t bytes) { return js_malloc(bytes); }
|
2012-11-12 23:30:39 +00:00
|
|
|
void *calloc_(size_t bytes) { return js_calloc(bytes); }
|
2011-06-16 01:11:59 +00:00
|
|
|
void *realloc_(void *p, size_t oldBytes, size_t bytes) { return js_realloc(p, bytes); }
|
2011-03-30 10:10:12 +00:00
|
|
|
void free_(void *p) { js_free(p); }
|
|
|
|
void reportAllocOverflow() const {}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocation policy that calls the system memory functions and reports errors
|
|
|
|
* to the context. Since the JSContext given on construction is stored for
|
|
|
|
* the lifetime of the container, this policy may only be used for containers
|
|
|
|
* whose lifetime is a shorter than the given JSContext.
|
|
|
|
*
|
|
|
|
* FIXME bug 647103 - rewrite this in terms of temporary allocation functions,
|
|
|
|
* not the system ones.
|
|
|
|
*/
|
2011-06-17 17:11:33 +00:00
|
|
|
class TempAllocPolicy
|
2011-03-30 10:10:12 +00:00
|
|
|
{
|
2013-07-10 15:29:52 +00:00
|
|
|
ContextFriendFields *const cx_;
|
2011-03-30 10:10:12 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Non-inline helper to call JSRuntime::onOutOfMemory with minimal
|
|
|
|
* code bloat.
|
|
|
|
*/
|
|
|
|
JS_FRIEND_API(void *) onOutOfMemory(void *p, size_t nbytes);
|
|
|
|
|
|
|
|
public:
|
2013-07-10 15:29:52 +00:00
|
|
|
TempAllocPolicy(JSContext *cx) : cx_((ContextFriendFields *) cx) {} // :(
|
|
|
|
TempAllocPolicy(ContextFriendFields *cx) : cx_(cx) {}
|
2011-03-30 10:10:12 +00:00
|
|
|
|
|
|
|
void *malloc_(size_t bytes) {
|
|
|
|
void *p = js_malloc(bytes);
|
2014-01-25 03:57:58 +00:00
|
|
|
if (MOZ_UNLIKELY(!p))
|
2013-10-07 16:42:55 +00:00
|
|
|
p = onOutOfMemory(nullptr, bytes);
|
2011-03-30 10:10:12 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2012-11-12 23:30:39 +00:00
|
|
|
void *calloc_(size_t bytes) {
|
|
|
|
void *p = js_calloc(bytes);
|
2014-01-25 03:57:58 +00:00
|
|
|
if (MOZ_UNLIKELY(!p))
|
2013-10-07 16:42:55 +00:00
|
|
|
p = onOutOfMemory(nullptr, bytes);
|
2012-11-12 23:30:39 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2011-06-16 01:11:59 +00:00
|
|
|
void *realloc_(void *p, size_t oldBytes, size_t bytes) {
|
2011-03-30 10:10:12 +00:00
|
|
|
void *p2 = js_realloc(p, bytes);
|
2014-01-25 03:57:58 +00:00
|
|
|
if (MOZ_UNLIKELY(!p2))
|
2011-03-30 10:10:12 +00:00
|
|
|
p2 = onOutOfMemory(p2, bytes);
|
|
|
|
return p2;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_(void *p) {
|
|
|
|
js_free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FRIEND_API(void) reportAllocOverflow() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace js */
|
|
|
|
|
2013-06-20 00:59:46 +00:00
|
|
|
#endif /* jsalloc_h */
|