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