Debian Codegraph @

js/src/jsapi-tests/testMemoryAssociation.cpp

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 */
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7
8 #include "jsapi.h"
9 #include "jspubtd.h"
10
11 #include "js/CompilationAndEvaluation.h"
12 #include "jsapi-tests/tests.h"
13
14 static const JS::MemoryUse TestUse1 = JS::MemoryUse::XPCWrappedNative;
15 static const JS::MemoryUse TestUse2 = JS::MemoryUse::DOMBinding;
16
17 BEGIN_TEST(testMemoryAssociation) {
18 JS::RootedObject obj(cx, JS_NewPlainObject(cx));
19 CHECK(obj);
20
21 // No association is allowed for nursery objects.
22 JS_GC(cx);
23
24 // Test object/memory association.
25 JS::AddAssociatedMemory(obj, 100, TestUse1);
26 JS::RemoveAssociatedMemory(obj, 100, TestUse1);
27
28 // Test association when object moves due to compacting GC.
29 void* initial = obj;
30 JS::AddAssociatedMemory(obj, 300, TestUse1);
31 JS::PrepareForFullGC(cx);
32 JS::NonIncrementalGC(cx, JS::GCOptions::Shrink, JS::GCReason::DEBUG_GC);
33 CHECK(obj != initial);
34 JS::RemoveAssociatedMemory(obj, 300, TestUse1);
35
36 // Test multiple associations.
37 JS::AddAssociatedMemory(obj, 400, TestUse1);
38 JS::AddAssociatedMemory(obj, 500, TestUse2);
39 JS::RemoveAssociatedMemory(obj, 400, TestUse1);
40 JS::RemoveAssociatedMemory(obj, 500, TestUse2);
41
42 return true;
43 }
44 END_TEST(testMemoryAssociation)