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"
7 #include "base/atomicops.h"
8 #include "base/logging.h"
10 using base::subtle::Atomic32
;
12 static const uint32 kUnknownDataSize
= static_cast<uint32
>(-1);
16 uint32
TotalSharedMemorySizeInBytes(uint32 packet_size
) {
17 // Need to reserve extra 4 bytes for size of data.
18 return packet_size
+ sizeof(Atomic32
);
21 uint32
PacketSizeInBytes(uint32 shared_memory_created_size
) {
22 return shared_memory_created_size
- sizeof(Atomic32
);
25 uint32
GetActualDataSizeInBytes(base::SharedMemory
* shared_memory
,
27 char* ptr
= static_cast<char*>(shared_memory
->memory()) + packet_size
;
28 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr
) & 3);
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
);
36 void SetActualDataSizeInBytes(void* shared_memory_ptr
,
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);
42 // Set actual data size at the end of the buffer.
43 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32
*>(ptr
),
47 void SetActualDataSizeInBytes(base::SharedMemory
* shared_memory
,
49 uint32 actual_data_size
) {
50 SetActualDataSizeInBytes(shared_memory
->memory(),
51 packet_size
, actual_data_size
);
54 void SetUnknownDataSize(base::SharedMemory
* shared_memory
,
56 SetActualDataSizeInBytes(shared_memory
, packet_size
, kUnknownDataSize
);
59 bool IsUnknownDataSize(base::SharedMemory
* shared_memory
,
61 char* ptr
= static_cast<char*>(shared_memory
->memory()) + packet_size
;
62 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr
) & 3);
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
;