OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/discardable_shared_memory.h" | 5 #include "base/memory/discardable_shared_memory.h" |
6 | 6 |
7 #if defined(OS_POSIX) && !defined(OS_NACL) | 7 #include <stdint.h> |
8 // For madvise() which is available on all POSIX compatible systems. | |
9 #include <sys/mman.h> | |
10 #endif | |
11 | 8 |
12 #include <algorithm> | 9 #include <algorithm> |
13 | 10 |
14 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
15 #include "base/bits.h" | 12 #include "base/bits.h" |
16 #include "base/logging.h" | 13 #include "base/logging.h" |
17 #include "base/numerics/safe_math.h" | 14 #include "base/numerics/safe_math.h" |
18 #include "base/process/process_metrics.h" | 15 #include "base/process/process_metrics.h" |
| 16 #include "build/build_config.h" |
| 17 |
| 18 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 19 // For madvise() which is available on all POSIX compatible systems. |
| 20 #include <sys/mman.h> |
| 21 #endif |
19 | 22 |
20 #if defined(OS_ANDROID) | 23 #if defined(OS_ANDROID) |
21 #include "third_party/ashmem/ashmem.h" | 24 #include "third_party/ashmem/ashmem.h" |
22 #endif | 25 #endif |
23 | 26 |
24 #if defined(OS_WIN) | 27 #if defined(OS_WIN) |
25 #include "base/win/windows_version.h" | 28 #include "base/win/windows_version.h" |
26 #endif | 29 #endif |
27 | 30 |
28 namespace base { | 31 namespace base { |
29 namespace { | 32 namespace { |
30 | 33 |
31 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or | 34 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or |
32 // Atomic64 routines, depending on the architecture. | 35 // Atomic64 routines, depending on the architecture. |
33 typedef intptr_t AtomicType; | 36 typedef intptr_t AtomicType; |
34 typedef uintptr_t UAtomicType; | 37 typedef uintptr_t UAtomicType; |
35 | 38 |
36 // Template specialization for timestamp serialization/deserialization. This | 39 // Template specialization for timestamp serialization/deserialization. This |
37 // is used to serialize timestamps using Unix time on systems where AtomicType | 40 // is used to serialize timestamps using Unix time on systems where AtomicType |
38 // does not have enough precision to contain a timestamp in the standard | 41 // does not have enough precision to contain a timestamp in the standard |
39 // serialized format. | 42 // serialized format. |
40 template <int> | 43 template <int> |
41 Time TimeFromWireFormat(int64 value); | 44 Time TimeFromWireFormat(int64_t value); |
42 template <int> | 45 template <int> |
43 int64 TimeToWireFormat(Time time); | 46 int64_t TimeToWireFormat(Time time); |
44 | 47 |
45 // Serialize to Unix time when using 4-byte wire format. | 48 // Serialize to Unix time when using 4-byte wire format. |
46 // Note: 19 January 2038, this will cease to work. | 49 // Note: 19 January 2038, this will cease to work. |
47 template <> | 50 template <> |
48 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<4>(int64 value) { | 51 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<4>(int64_t value) { |
49 return value ? Time::UnixEpoch() + TimeDelta::FromSeconds(value) : Time(); | 52 return value ? Time::UnixEpoch() + TimeDelta::FromSeconds(value) : Time(); |
50 } | 53 } |
51 template <> | 54 template <> |
52 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<4>(Time time) { | 55 int64_t ALLOW_UNUSED_TYPE TimeToWireFormat<4>(Time time) { |
53 return time > Time::UnixEpoch() ? (time - Time::UnixEpoch()).InSeconds() : 0; | 56 return time > Time::UnixEpoch() ? (time - Time::UnixEpoch()).InSeconds() : 0; |
54 } | 57 } |
55 | 58 |
56 // Standard serialization format when using 8-byte wire format. | 59 // Standard serialization format when using 8-byte wire format. |
57 template <> | 60 template <> |
58 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<8>(int64 value) { | 61 Time ALLOW_UNUSED_TYPE TimeFromWireFormat<8>(int64_t value) { |
59 return Time::FromInternalValue(value); | 62 return Time::FromInternalValue(value); |
60 } | 63 } |
61 template <> | 64 template <> |
62 int64 ALLOW_UNUSED_TYPE TimeToWireFormat<8>(Time time) { | 65 int64_t ALLOW_UNUSED_TYPE TimeToWireFormat<8>(Time time) { |
63 return time.ToInternalValue(); | 66 return time.ToInternalValue(); |
64 } | 67 } |
65 | 68 |
66 struct SharedState { | 69 struct SharedState { |
67 enum LockState { UNLOCKED = 0, LOCKED = 1 }; | 70 enum LockState { UNLOCKED = 0, LOCKED = 1 }; |
68 | 71 |
69 explicit SharedState(AtomicType ivalue) { value.i = ivalue; } | 72 explicit SharedState(AtomicType ivalue) { value.i = ivalue; } |
70 SharedState(LockState lock_state, Time timestamp) { | 73 SharedState(LockState lock_state, Time timestamp) { |
71 int64 wire_timestamp = TimeToWireFormat<sizeof(AtomicType)>(timestamp); | 74 int64_t wire_timestamp = TimeToWireFormat<sizeof(AtomicType)>(timestamp); |
72 DCHECK_GE(wire_timestamp, 0); | 75 DCHECK_GE(wire_timestamp, 0); |
73 DCHECK_EQ(lock_state & ~1, 0); | 76 DCHECK_EQ(lock_state & ~1, 0); |
74 value.u = (static_cast<UAtomicType>(wire_timestamp) << 1) | lock_state; | 77 value.u = (static_cast<UAtomicType>(wire_timestamp) << 1) | lock_state; |
75 } | 78 } |
76 | 79 |
77 LockState GetLockState() const { return static_cast<LockState>(value.u & 1); } | 80 LockState GetLockState() const { return static_cast<LockState>(value.u & 1); } |
78 | 81 |
79 Time GetTimestamp() const { | 82 Time GetTimestamp() const { |
80 return TimeFromWireFormat<sizeof(AtomicType)>(value.u >> 1); | 83 return TimeFromWireFormat<sizeof(AtomicType)>(value.u >> 1); |
81 } | 84 } |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 &SharedStateFromSharedMemory(shared_memory_)->value.i, | 317 &SharedStateFromSharedMemory(shared_memory_)->value.i, |
315 old_state.value.i, | 318 old_state.value.i, |
316 new_state.value.i)); | 319 new_state.value.i)); |
317 | 320 |
318 DCHECK_EQ(old_state.value.u, result.value.u); | 321 DCHECK_EQ(old_state.value.u, result.value.u); |
319 | 322 |
320 last_known_usage_ = current_time; | 323 last_known_usage_ = current_time; |
321 } | 324 } |
322 | 325 |
323 void* DiscardableSharedMemory::memory() const { | 326 void* DiscardableSharedMemory::memory() const { |
324 return reinterpret_cast<uint8*>(shared_memory_.memory()) + | 327 return reinterpret_cast<uint8_t*>(shared_memory_.memory()) + |
325 AlignToPageSize(sizeof(SharedState)); | 328 AlignToPageSize(sizeof(SharedState)); |
326 } | 329 } |
327 | 330 |
328 bool DiscardableSharedMemory::Purge(Time current_time) { | 331 bool DiscardableSharedMemory::Purge(Time current_time) { |
329 // Calls to this function must be synchronized properly. | 332 // Calls to this function must be synchronized properly. |
330 DFAKE_SCOPED_LOCK(thread_collision_warner_); | 333 DFAKE_SCOPED_LOCK(thread_collision_warner_); |
331 DCHECK(shared_memory_.memory()); | 334 DCHECK(shared_memory_.memory()); |
332 | 335 |
333 SharedState old_state(SharedState::UNLOCKED, last_known_usage_); | 336 SharedState old_state(SharedState::UNLOCKED, last_known_usage_); |
334 SharedState new_state(SharedState::UNLOCKED, Time()); | 337 SharedState new_state(SharedState::UNLOCKED, Time()); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 | 410 |
408 void DiscardableSharedMemory::Close() { | 411 void DiscardableSharedMemory::Close() { |
409 shared_memory_.Close(); | 412 shared_memory_.Close(); |
410 } | 413 } |
411 | 414 |
412 Time DiscardableSharedMemory::Now() const { | 415 Time DiscardableSharedMemory::Now() const { |
413 return Time::Now(); | 416 return Time::Now(); |
414 } | 417 } |
415 | 418 |
416 } // namespace base | 419 } // namespace base |
OLD | NEW |