OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/discardable_memory.h" | |
6 | |
7 #include "base/atomicops.h" | |
8 #include "base/logging.h" | |
9 | |
10 namespace base { | |
11 namespace { | |
12 | |
13 const struct TypeNamePair { | |
14 DiscardableMemoryType type; | |
15 const char* name; | |
16 } kTypeNamePairs[] = { | |
17 { DISCARDABLE_MEMORY_TYPE_ANDROID, "android" }, | |
18 { DISCARDABLE_MEMORY_TYPE_MAC, "mac" }, | |
19 { DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" } | |
20 }; | |
21 | |
22 subtle::AtomicWord g_default_type = DISCARDABLE_MEMORY_TYPE_NONE; | |
23 | |
24 } // namespace | |
25 | |
26 // static | |
27 DiscardableMemoryType DiscardableMemory::GetNamedType( | |
28 const std::string& name) { | |
29 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) { | |
30 if (name == kTypeNamePairs[i].name) | |
31 return kTypeNamePairs[i].type; | |
32 } | |
33 | |
34 return DISCARDABLE_MEMORY_TYPE_NONE; | |
35 } | |
36 | |
37 // static | |
38 const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) { | |
39 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) { | |
40 if (type == kTypeNamePairs[i].type) | |
41 return kTypeNamePairs[i].name; | |
42 } | |
43 | |
44 return "unknown"; | |
45 } | |
46 | |
47 // static | |
48 void DiscardableMemory::SetDefaultType(DiscardableMemoryType type) { | |
49 // NONE is a reserved value and not a valid default type. | |
50 DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type); | |
51 | |
52 subtle::AtomicWord value = subtle::NoBarrier_CompareAndSwap( | |
53 &g_default_type, | |
54 DISCARDABLE_MEMORY_TYPE_NONE, | |
55 type); | |
56 | |
57 // Make sure this function is only called once. | |
58 DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, value); | |
59 } | |
60 | |
61 // static | |
62 DiscardableMemoryType DiscardableMemory::GetDefaultType() { | |
63 subtle::AtomicWord value = subtle::Acquire_Load(&g_default_type); | |
willchan no longer on Chromium
2013/12/26 18:25:54
OK, this is too subtle for me :) What's the acquir
Jeffrey Yasskin
2013/12/26 18:36:07
Yep, I don't see any release operation in this fil
reveman
2013/12/26 21:53:35
Done. The caller is supposed to guarantee correct
| |
64 | |
65 // Ensure that SetDefaultType() was properly called prior to this. | |
66 DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, value); | |
67 | |
68 return static_cast<DiscardableMemoryType>(value); | |
69 } | |
70 | |
71 } // namespace base | |
OLD | NEW |