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.
5 #include "media/audio/shared_memory_util.h"
9 #include "base/atomicops.h"
10 #include "base/logging.h"
12 using base::subtle::Atomic32
;
14 static const uint32 kUnknownDataSize
= static_cast<uint32
>(-1);
18 uint32
TotalSharedMemorySizeInBytes(uint32 packet_size
) {
19 // Need to reserve extra 4 bytes for size of data.
20 return packet_size
+ sizeof(Atomic32
);
23 uint32
PacketSizeInBytes(uint32 shared_memory_created_size
) {
24 return shared_memory_created_size
- sizeof(Atomic32
);
27 uint32
GetActualDataSizeInBytes(base::SharedMemory
* shared_memory
,
29 char* ptr
= static_cast<char*>(shared_memory
->memory()) + packet_size
;
30 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr
) & 3);
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
);
38 void SetActualDataSizeInBytes(void* shared_memory_ptr
,
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);
44 // Set actual data size at the end of the buffer.
45 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32
*>(ptr
),
49 void SetActualDataSizeInBytes(base::SharedMemory
* shared_memory
,
51 uint32 actual_data_size
) {
52 SetActualDataSizeInBytes(shared_memory
->memory(),
53 packet_size
, actual_data_size
);
56 void SetUnknownDataSize(base::SharedMemory
* shared_memory
,
58 SetActualDataSizeInBytes(shared_memory
, packet_size
, kUnknownDataSize
);
61 bool IsUnknownDataSize(base::SharedMemory
* shared_memory
,
63 char* ptr
= static_cast<char*>(shared_memory
->memory()) + packet_size
;
64 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr
) & 3);
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
;