Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/media/SharedBuffer.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* This Source Code Form is subject to the terms of the Mozilla Public
3
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4
 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6
#ifndef MOZILLA_SHAREDBUFFER_H_
7
#define MOZILLA_SHAREDBUFFER_H_
8
9
#include "mozilla/CheckedInt.h"
10
#include "mozilla/mozalloc.h"
11
#include "mozilla/MemoryReporting.h"
12
#include "nsISupportsImpl.h"
13
14
namespace mozilla {
15
16
class AudioBlockBuffer;
17
class ThreadSharedFloatArrayBufferList;
18
19
/**
20
 * Base class for objects with a thread-safe refcount and a virtual
21
 * destructor.
22
 */
23
class ThreadSharedObject {
24
public:
25
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ThreadSharedObject)
26
27
0
  bool IsShared() { return mRefCnt.get() > 1; }
28
29
0
  virtual AudioBlockBuffer* AsAudioBlockBuffer() { return nullptr; };
30
  virtual ThreadSharedFloatArrayBufferList* AsThreadSharedFloatArrayBufferList()
31
0
  {
32
0
    return nullptr;
33
0
  };
34
35
  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
36
0
  {
37
0
    return 0;
38
0
  }
39
40
  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
41
0
  {
42
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
43
0
  }
44
protected:
45
  // Protected destructor, to discourage deletion outside of Release():
46
0
  virtual ~ThreadSharedObject() {}
47
};
48
49
/**
50
 * Heap-allocated chunk of arbitrary data with threadsafe refcounting.
51
 * Typically you would allocate one of these, fill it in, and then treat it as
52
 * immutable while it's shared.
53
 * This only guarantees 4-byte alignment of the data. For alignment we simply
54
 * assume that the memory from malloc is at least 4-byte aligned and the
55
 * refcount's size is large enough that SharedBuffer's size is divisible by 4.
56
 */
57
class SharedBuffer : public ThreadSharedObject {
58
public:
59
0
  void* Data() { return this + 1; }
60
61
  static already_AddRefed<SharedBuffer> Create(size_t aSize, const fallible_t&)
62
0
  {
63
0
    void* m = operator new(AllocSize(aSize), fallible);
64
0
    if (!m) {
65
0
      return nullptr;
66
0
    }
67
0
    RefPtr<SharedBuffer> p = new (m) SharedBuffer();
68
0
    return p.forget();
69
0
  }
70
71
  static already_AddRefed<SharedBuffer> Create(size_t aSize)
72
0
  {
73
0
    void* m = operator new(AllocSize(aSize));
74
0
    RefPtr<SharedBuffer> p = new (m) SharedBuffer();
75
0
    return p.forget();
76
0
  }
77
78
  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override
79
0
  {
80
0
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
81
0
  }
82
83
private:
84
  static size_t
85
  AllocSize(size_t aDataSize)
86
0
  {
87
0
    CheckedInt<size_t> size = sizeof(SharedBuffer);
88
0
    size += aDataSize;
89
0
    if (!size.isValid()) {
90
0
      MOZ_CRASH();
91
0
    }
92
0
    return size.value();
93
0
  }
94
95
  SharedBuffer()
96
0
  {
97
0
    NS_ASSERTION((reinterpret_cast<char*>(this + 1) - reinterpret_cast<char*>(this)) % 4 == 0,
98
0
                 "SharedBuffers should be at least 4-byte aligned");
99
0
  }
100
};
101
102
} // namespace mozilla
103
104
#endif /* MOZILLA_SHAREDBUFFER_H_ */