Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(167)

Side by Side Diff: base/memory/shared_memory_allocator_unittest.cc

Issue 1410213004: Create "persistent memory allocator" for persisting and sharing objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/memory/shared_memory_allocator.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "testing/gmock/include/gmock/gmock.h"
9
10 namespace {
11
12 const int32 TEST_MEMORY_SIZE = 1 << 20; // 1 MiB
13 const int32 TEST_MEMORY_PAGE = 64 << 10; // 64 KiB
14
15 } // namespace
16
17 namespace base {
18
19 class SharedMemoryAllocatorTest : public testing::Test {
20 public:
21 struct TestObject1 {
22 int32 onething;
23 char oranother;
24 };
25
26 struct TestObject2 {
27 int thiis;
28 long that;
29 float andthe;
30 char other;
31 double thing;
32 };
33
34 void SetUp() override {
35 allocator_.reset();
36 mem_segment_.reset(new char[TEST_MEMORY_SIZE]);
37 memset(mem_segment_.get(), 0, TEST_MEMORY_SIZE);
38 allocator_.reset(new SharedMemoryAllocator(mem_segment_.get(),
39 TEST_MEMORY_SIZE,
40 TEST_MEMORY_PAGE));
41 }
42
43 void TearDown() override {
44 allocator_.reset();
45 mem_segment_.reset();
46 }
47
48 scoped_ptr<char[]> mem_segment_;
49 scoped_ptr<SharedMemoryAllocator> allocator_;
50 };
51
52 TEST_F(SharedMemoryAllocatorTest, AllocateAndIterate) {
53 SharedMemoryAllocator::MemoryInfo meminfo0;
54 allocator_->GetMemoryInfo(&meminfo0);
55 EXPECT_EQ(TEST_MEMORY_SIZE, meminfo0.total);
56 EXPECT_GT(meminfo0.total, meminfo0.free);
57
58 int32 block1 = allocator_->Allocate(sizeof(TestObject1), 1);
59 EXPECT_NE(0, block1);
60 EXPECT_NE(nullptr, allocator_->GetObject(block1, 1, (TestObject1*)0));
61 EXPECT_EQ(nullptr, allocator_->GetObject(block1, 1, (TestObject2*)0));
62 SharedMemoryAllocator::MemoryInfo meminfo1;
63 allocator_->GetMemoryInfo(&meminfo1);
64 EXPECT_EQ(meminfo0.total, meminfo1.total);
65 EXPECT_GT(meminfo0.free, meminfo1.free);
66
67 SharedMemoryAllocator::Iterator iter;
68 int32 type;
69 EXPECT_EQ(0, allocator_->GetFirstIterable(&iter, &type));
70 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
71 allocator_->MakeIterable(block1);
72 EXPECT_EQ(block1, allocator_->GetNextIterable(&iter, &type));
73 EXPECT_EQ(1, type);
74 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
75
76 int32 block2 = allocator_->Allocate(sizeof(TestObject2), 2);
77 EXPECT_NE(0, block2);
78 EXPECT_NE(nullptr, allocator_->GetObject(block2, 2, (TestObject2*)0));
79 EXPECT_EQ(nullptr, allocator_->GetObject(block2, 1, (TestObject2*)0));
80 SharedMemoryAllocator::MemoryInfo meminfo2;
81 allocator_->GetMemoryInfo(&meminfo2);
82 EXPECT_EQ(meminfo1.total, meminfo2.total);
83 EXPECT_GT(meminfo1.free, meminfo2.free);
84
85 allocator_->MakeIterable(block2);
86 EXPECT_EQ(block2, allocator_->GetNextIterable(&iter, &type));
87 EXPECT_EQ(2, type);
88 EXPECT_EQ(0, allocator_->GetNextIterable(&iter, &type));
89
90 EXPECT_FALSE(allocator_->IsFull());
91 EXPECT_FALSE(allocator_->IsCorrupted());
92 }
93
94 } // namespace base
OLDNEW
« base/memory/shared_memory_allocator.cc ('K') | « base/memory/shared_memory_allocator.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698