/src/mozilla-central/gfx/layers/ipc/RefCountedShmem.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "RefCountedShmem.h" |
8 | | |
9 | | #include "mozilla/Atomics.h" |
10 | | #include "mozilla/ipc/ProtocolUtils.h" |
11 | | #include "mozilla/layers/WebRenderMessages.h" |
12 | | |
13 | 0 | #define SHM_REFCOUNT_HEADER_SIZE 16 |
14 | | |
15 | | namespace mozilla { |
16 | | namespace layers { |
17 | | |
18 | | uint8_t* |
19 | | RefCountedShm::GetBytes(const RefCountedShmem& aShm) |
20 | 0 | { |
21 | 0 | uint8_t* data = aShm.buffer().get<uint8_t>(); |
22 | 0 | if (!data) { |
23 | 0 | return nullptr; |
24 | 0 | } |
25 | 0 | return data + SHM_REFCOUNT_HEADER_SIZE; |
26 | 0 | } |
27 | | |
28 | | size_t |
29 | | RefCountedShm::GetSize(const RefCountedShmem& aShm) |
30 | 0 | { |
31 | 0 | if (!IsValid(aShm)) { |
32 | 0 | return 0; |
33 | 0 | } |
34 | 0 | size_t totalSize = aShm.buffer().Size<uint8_t>(); |
35 | 0 | if (totalSize < SHM_REFCOUNT_HEADER_SIZE) { |
36 | 0 | return 0; |
37 | 0 | } |
38 | 0 | |
39 | 0 | return totalSize - SHM_REFCOUNT_HEADER_SIZE; |
40 | 0 | } |
41 | | |
42 | | bool |
43 | | RefCountedShm::IsValid(const RefCountedShmem& aShm) |
44 | 0 | { |
45 | 0 | return aShm.buffer().IsWritable() && aShm.buffer().Size<uint8_t>() > SHM_REFCOUNT_HEADER_SIZE; |
46 | 0 | } |
47 | | |
48 | | int32_t |
49 | | RefCountedShm::GetReferenceCount(const RefCountedShmem& aShm) |
50 | 0 | { |
51 | 0 | if (!IsValid(aShm)) { |
52 | 0 | return 0; |
53 | 0 | } |
54 | 0 | |
55 | 0 | return *aShm.buffer().get<Atomic<int32_t>>(); |
56 | 0 | } |
57 | | |
58 | | int32_t |
59 | | RefCountedShm::AddRef(const RefCountedShmem& aShm) |
60 | 0 | { |
61 | 0 | if (!IsValid(aShm)) { |
62 | 0 | return 0; |
63 | 0 | } |
64 | 0 | |
65 | 0 | auto* counter = aShm.buffer().get<Atomic<int32_t>>(); |
66 | 0 | if (counter) { |
67 | 0 | return (*counter)++; |
68 | 0 | } |
69 | 0 | return 0; |
70 | 0 | } |
71 | | |
72 | | int32_t |
73 | | RefCountedShm::Release(const RefCountedShmem& aShm) |
74 | 0 | { |
75 | 0 | if (!IsValid(aShm)) { |
76 | 0 | return 0; |
77 | 0 | } |
78 | 0 | |
79 | 0 | auto* counter = aShm.buffer().get<Atomic<int32_t>>(); |
80 | 0 | if (counter) { |
81 | 0 | return --(*counter); |
82 | 0 | } |
83 | 0 | |
84 | 0 | return 0; |
85 | 0 | } |
86 | | |
87 | | bool |
88 | | RefCountedShm::Alloc(mozilla::ipc::IProtocol* aAllocator, size_t aSize, |
89 | | RefCountedShmem& aShm) |
90 | 0 | { |
91 | 0 | MOZ_ASSERT(!IsValid(aShm)); |
92 | 0 | auto shmType = ipc::SharedMemory::SharedMemoryType::TYPE_BASIC; |
93 | 0 | auto size = aSize + SHM_REFCOUNT_HEADER_SIZE; |
94 | 0 | if (!aAllocator->AllocUnsafeShmem(size, shmType, &aShm.buffer())) { |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | return true; |
98 | 0 | } |
99 | | |
100 | | void |
101 | | RefCountedShm::Dealloc(mozilla::ipc::IProtocol* aAllocator, RefCountedShmem& aShm) |
102 | 0 | { |
103 | 0 | aAllocator->DeallocShmem(aShm.buffer()); |
104 | 0 | aShm.buffer() = ipc::Shmem(); |
105 | 0 | } |
106 | | |
107 | | } //namespace |
108 | | } //namespace |