1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=99:
4 * Any copyright is dedicated to the Public Domain.
5 * http://creativecommons.org/licenses/publicdomain/
6 * Contributor: Igor Bukanov
10 #include "jsgcchunk.h"
13 /* We allow to allocate only single chunk. */
15 class CustomGCChunkAllocator: public js::GCChunkAllocator {
17 CustomGCChunkAllocator() : pool(NULL) {}
22 virtual void *doAlloc() {
30 virtual void doFree(void *chunk) {
36 static CustomGCChunkAllocator customGCChunkAllocator;
38 static unsigned errorCount = 0;
41 ErrorCounter(JSContext *cx, const char *message, JSErrorReport *report)
46 BEGIN_TEST(testGCChunkAlloc)
48 JS_SetErrorReporter(cx, ErrorCounter);
53 * We loop until out-of-memory happens during the chunk allocation. But
54 * we have to disable the jit since it cannot tolerate OOM during the
57 JS_ToggleOptions(cx, JSOPTION_JIT);
59 static const char source[] =
60 "var max = 0; (function() {"
65 JSBool ok = JS_EvaluateScript(cx, global, source, strlen(source), "", 1,
68 /* Check that we get OOM. */
70 CHECK(!JS_IsExceptionPending(cx));
71 CHECK(errorCount == 1);
72 CHECK(!customGCChunkAllocator.pool);
74 JS_ToggleOptions(cx, JSOPTION_JIT);
77 " for (var i = max >> 1; i != 0;) {"
81 "})();", root.addr());
82 CHECK(errorCount == 1);
86 virtual JSRuntime * createRuntime() {
88 * To test failure of chunk allocation allow to use GC twice the memory
89 * the single chunk contains.
91 JSRuntime *rt = JS_NewRuntime(2 * js::GC_CHUNK_SIZE);
95 customGCChunkAllocator.pool = js::AllocGCChunk();
96 JS_ASSERT(customGCChunkAllocator.pool);
98 rt->setCustomGCChunkAllocator(&customGCChunkAllocator);
102 virtual void destroyRuntime() {
103 JS_DestroyRuntime(rt);
105 /* We should get the initial chunk back at this point. */
106 JS_ASSERT(customGCChunkAllocator.pool);
107 js::FreeGCChunk(customGCChunkAllocator.pool);
108 customGCChunkAllocator.pool = NULL;
111 END_TEST(testGCChunkAlloc)