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