Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / io / nsSegmentedBuffer.h
blobc654956df9d47c2e4b4ce29200368cf0c0885c2b
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/. */
7 #ifndef nsSegmentedBuffer_h__
8 #define nsSegmentedBuffer_h__
10 #include <stddef.h>
11 #include <functional>
13 #include "nsCOMPtr.h"
14 #include "nsDebug.h"
15 #include "nsError.h"
16 #include "nsTArray.h"
17 #include "mozilla/DataMutex.h"
19 class nsIEventTarget;
21 class nsSegmentedBuffer {
22 public:
23 nsSegmentedBuffer()
24 : mSegmentSize(0),
25 mSegmentArray(nullptr),
26 mSegmentArrayCount(0),
27 mFirstSegmentIndex(0),
28 mLastSegmentIndex(0) {}
30 ~nsSegmentedBuffer() { Empty(); }
32 nsresult Init(uint32_t aSegmentSize);
34 char* AppendNewSegment(); // pushes at end
36 // returns true if no more segments remain:
37 bool DeleteFirstSegment(); // pops from beginning
39 // returns true if no more segments remain:
40 bool DeleteLastSegment(); // pops from beginning
42 // Call Realloc() on last segment. This is used to reduce memory
43 // consumption when data is not an exact multiple of segment size.
44 bool ReallocLastSegment(size_t aNewSize);
46 void Empty(); // frees all segments
48 inline uint32_t GetSegmentCount() {
49 if (mFirstSegmentIndex <= mLastSegmentIndex) {
50 return mLastSegmentIndex - mFirstSegmentIndex;
51 } else {
52 return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex;
56 inline uint32_t GetSegmentSize() { return mSegmentSize; }
58 inline char* GetSegment(uint32_t aIndex) {
59 NS_ASSERTION(aIndex < GetSegmentCount(), "index out of bounds");
60 int32_t i = ModSegArraySize(mFirstSegmentIndex + (int32_t)aIndex);
61 return mSegmentArray[i];
64 protected:
65 inline int32_t ModSegArraySize(int32_t aIndex) {
66 uint32_t result = aIndex & (mSegmentArrayCount - 1);
67 NS_ASSERTION(result == aIndex % mSegmentArrayCount,
68 "non-power-of-2 mSegmentArrayCount");
69 return result;
72 inline bool IsFull() {
73 return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex;
76 protected:
77 uint32_t mSegmentSize;
78 char** mSegmentArray;
79 uint32_t mSegmentArrayCount;
80 int32_t mFirstSegmentIndex;
81 int32_t mLastSegmentIndex;
83 private:
84 class FreeOMTPointers {
85 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(FreeOMTPointers)
87 public:
88 FreeOMTPointers() : mTasks("nsSegmentedBuffer::FreeOMTPointers") {}
90 void FreeAll();
92 // Adds a task to the array. Returns the size of the array.
93 size_t AddTask(std::function<void()>&& aTask) {
94 auto tasks = mTasks.Lock();
95 tasks->AppendElement(std::move(aTask));
96 return tasks->Length();
99 private:
100 ~FreeOMTPointers() = default;
102 mozilla::DataMutex<nsTArray<std::function<void()>>> mTasks;
105 void FreeOMT(void* aPtr);
106 void FreeOMT(std::function<void()>&& aTask);
108 nsCOMPtr<nsIEventTarget> mIOThread;
110 // This object is created the first time we need to dispatch to another thread
111 // to free segments. It is only freed when the nsSegmentedBufer is destroyed
112 // or when the runnable is finally handled and its refcount goes to 0.
113 RefPtr<FreeOMTPointers> mFreeOMT;
116 // NS_SEGMENTARRAY_INITIAL_SIZE: This number needs to start out as a
117 // power of 2 given how it gets used. We double the segment array
118 // when we overflow it, and use that fact that it's a power of 2
119 // to compute a fast modulus operation in IsFull.
121 // 32 segment array entries can accommodate 128k of data if segments
122 // are 4k in size. That seems like a reasonable amount that will avoid
123 // needing to grow the segment array.
124 #define NS_SEGMENTARRAY_INITIAL_COUNT 32
126 #endif // nsSegmentedBuffer_h__