OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "media/audio/shared_memory_util.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/atomicops.h" | |
10 #include "base/logging.h" | |
11 | |
12 using base::subtle::Atomic32; | |
13 | |
14 static const uint32 kUnknownDataSize = static_cast<uint32>(-1); | |
15 | |
16 namespace media { | |
17 | |
18 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { | |
19 // Need to reserve extra 4 bytes for size of data. | |
20 return packet_size + sizeof(Atomic32); | |
21 } | |
22 | |
23 uint32 PacketSizeInBytes(uint32 shared_memory_created_size) { | |
24 return shared_memory_created_size - sizeof(Atomic32); | |
25 } | |
26 | |
27 uint32 GetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
28 uint32 packet_size) { | |
29 char* ptr = static_cast<char*>(shared_memory->memory()) + packet_size; | |
30 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
31 | |
32 // Actual data size stored at the end of the buffer. | |
33 uint32 actual_data_size = | |
34 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
35 return std::min(actual_data_size, packet_size); | |
36 } | |
37 | |
38 void SetActualDataSizeInBytes(void* shared_memory_ptr, | |
39 uint32 packet_size, | |
40 uint32 actual_data_size) { | |
41 char* ptr = static_cast<char*>(shared_memory_ptr) + packet_size; | |
42 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
43 | |
44 // Set actual data size at the end of the buffer. | |
45 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), | |
46 actual_data_size); | |
47 } | |
48 | |
49 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
50 uint32 packet_size, | |
51 uint32 actual_data_size) { | |
52 SetActualDataSizeInBytes(shared_memory->memory(), | |
53 packet_size, actual_data_size); | |
54 } | |
55 | |
56 void SetUnknownDataSize(base::SharedMemory* shared_memory, | |
57 uint32 packet_size) { | |
58 SetActualDataSizeInBytes(shared_memory, packet_size, kUnknownDataSize); | |
59 } | |
60 | |
61 bool IsUnknownDataSize(base::SharedMemory* shared_memory, | |
62 uint32 packet_size) { | |
63 char* ptr = static_cast<char*>(shared_memory->memory()) + packet_size; | |
64 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
65 | |
66 // Actual data size stored at the end of the buffer. | |
67 uint32 actual_data_size = | |
68 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
69 return actual_data_size == kUnknownDataSize; | |
70 } | |
71 | |
72 } // namespace media | |
OLD | NEW |