Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
memory_region_test.cc
Go to the documentation of this file.
1// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/memory_region.h"
6#include "platform/assert.h"
7#include "vm/unit_test.h"
8
9namespace dart {
10
11static void* NewRegion(uword size) {
12 void* pointer = new uint8_t[size];
13 return pointer;
14}
15
16static void DeleteRegion(const MemoryRegion& region) {
17 delete[] reinterpret_cast<uint8_t*>(region.pointer());
18}
19
20VM_UNIT_TEST_CASE(NullRegion) {
21 const uword kSize = 512;
22 MemoryRegion region(nullptr, kSize);
23 EXPECT(region.pointer() == nullptr);
24 EXPECT_EQ(kSize, region.size());
25}
26
28 const uword kSize = 1024;
30 EXPECT_EQ(kSize, region.size());
31 EXPECT(region.pointer() != nullptr);
32
33 region.Store<int32_t>(0, 42);
34 EXPECT_EQ(42, region.Load<int32_t>(0));
35
36 DeleteRegion(region);
37}
38
40 const uword kSize = 1024;
41 const uword kSubOffset = 128;
42 const uword kSubSize = 512;
44 MemoryRegion sub_region;
45 sub_region.Subregion(region, kSubOffset, kSubSize);
46 EXPECT_EQ(kSubSize, sub_region.size());
47 EXPECT(sub_region.pointer() != nullptr);
48 EXPECT(sub_region.start() == region.start() + kSubOffset);
49
50 region.Store<int32_t>(0, 42);
51 sub_region.Store<int32_t>(0, 100);
52 EXPECT_EQ(42, region.Load<int32_t>(0));
53 EXPECT_EQ(100, region.Load<int32_t>(kSubOffset));
54
55 DeleteRegion(region);
56}
57
58VM_UNIT_TEST_CASE(ExtendedRegion) {
59 const uword kSize = 1024;
60 const uword kSubSize = 512;
61 const uword kExtendSize = 512;
63 MemoryRegion sub_region;
64 sub_region.Subregion(region, 0, kSubSize);
65 MemoryRegion extended_region;
66 extended_region.Extend(sub_region, kExtendSize);
67 EXPECT_EQ(kSize, extended_region.size());
68 EXPECT(extended_region.pointer() == region.pointer());
69 EXPECT(extended_region.pointer() == sub_region.pointer());
70
71 extended_region.Store<int32_t>(0, 42);
72 EXPECT_EQ(42, extended_region.Load<int32_t>(0));
73
74 DeleteRegion(region);
75}
76
77} // namespace dart
#define EXPECT(type, expectedAlignment, expectedSize)
void Subregion(const MemoryRegion &from, uword offset, uword size)
void * pointer() const
void Store(uword offset, T value) const
uword start() const
uword size() const
T Load(uword offset) const
void Extend(const MemoryRegion &region, uword extra)
static constexpr int kSize
uintptr_t uword
Definition globals.h:501
static void * NewRegion(uword size)
static void DeleteRegion(const MemoryRegion &region)
#define VM_UNIT_TEST_CASE(name)
Definition unit_test.h:33