Chromium Code Reviews| Index: base/memory/discardable_memory.cc |
| diff --git a/base/memory/discardable_memory.cc b/base/memory/discardable_memory.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fdf52b72a70706cf888faa5878000428642fc99f |
| --- /dev/null |
| +++ b/base/memory/discardable_memory.cc |
| @@ -0,0 +1,71 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/discardable_memory.h" |
| + |
| +#include "base/atomicops.h" |
| +#include "base/logging.h" |
| + |
| +namespace base { |
| +namespace { |
| + |
| +const struct TypeNamePair { |
| + DiscardableMemoryType type; |
| + const char* name; |
| +} kTypeNamePairs[] = { |
| + { DISCARDABLE_MEMORY_TYPE_ANDROID, "android" }, |
| + { DISCARDABLE_MEMORY_TYPE_MAC, "mac" }, |
| + { DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" } |
| +}; |
| + |
| +subtle::AtomicWord g_default_type = DISCARDABLE_MEMORY_TYPE_NONE; |
| + |
| +} // namespace |
| + |
| +// static |
| +DiscardableMemoryType DiscardableMemory::GetNamedType( |
| + const std::string& name) { |
| + for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) { |
| + if (name == kTypeNamePairs[i].name) |
| + return kTypeNamePairs[i].type; |
| + } |
| + |
| + return DISCARDABLE_MEMORY_TYPE_NONE; |
| +} |
| + |
| +// static |
| +const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) { |
| + for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) { |
| + if (type == kTypeNamePairs[i].type) |
| + return kTypeNamePairs[i].name; |
| + } |
| + |
| + return "unknown"; |
| +} |
| + |
| +// static |
| +void DiscardableMemory::SetDefaultType(DiscardableMemoryType type) { |
| + // NONE is a reserved value and not a valid default type. |
| + DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type); |
| + |
| + subtle::AtomicWord value = subtle::NoBarrier_CompareAndSwap( |
| + &g_default_type, |
| + DISCARDABLE_MEMORY_TYPE_NONE, |
| + type); |
| + |
| + // Make sure this function is only called once. |
| + DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, value); |
| +} |
| + |
| +// static |
| +DiscardableMemoryType DiscardableMemory::GetDefaultType() { |
| + 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
|
| + |
| + // Ensure that SetDefaultType() was properly called prior to this. |
| + DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, value); |
| + |
| + return static_cast<DiscardableMemoryType>(value); |
| +} |
| + |
| +} // namespace base |