1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsSegmentedBuffer_h__
39 #define nsSegmentedBuffer_h__
44 class nsSegmentedBuffer
48 : mSegmentSize(0), mMaxSize(0),
49 mSegAllocator(nsnull
), mSegmentArray(nsnull
),
50 mSegmentArrayCount(0),
51 mFirstSegmentIndex(0), mLastSegmentIndex(0) {}
53 ~nsSegmentedBuffer() {
55 NS_IF_RELEASE(mSegAllocator
);
59 NS_COM nsresult
Init(PRUint32 segmentSize
, PRUint32 maxSize
,
60 nsIMemory
* allocator
= nsnull
);
62 NS_COM
char* AppendNewSegment(); // pushes at end
64 // returns true if no more segments remain:
65 PRBool
DeleteFirstSegment(); // pops from beginning
67 // returns true if no more segments remain:
68 PRBool
DeleteLastSegment(); // pops from beginning
70 // Call Realloc() on last segment. This is used to reduce memory
71 // consumption when data is not an exact multiple of segment size.
72 PRBool
ReallocLastSegment(size_t newSize
);
74 NS_COM
void Empty(); // frees all segments
76 inline PRUint32
GetSegmentCount() {
77 if (mFirstSegmentIndex
<= mLastSegmentIndex
)
78 return mLastSegmentIndex
- mFirstSegmentIndex
;
80 return mSegmentArrayCount
+ mLastSegmentIndex
- mFirstSegmentIndex
;
83 inline PRUint32
GetSegmentSize() { return mSegmentSize
; }
84 inline PRUint32
GetMaxSize() { return mMaxSize
; }
85 inline PRUint32
GetSize() { return GetSegmentCount() * mSegmentSize
; }
87 inline char* GetSegment(PRUint32 indx
) {
88 NS_ASSERTION(indx
< GetSegmentCount(), "index out of bounds");
89 PRInt32 i
= ModSegArraySize(mFirstSegmentIndex
+ (PRInt32
)indx
);
90 return mSegmentArray
[i
];
94 inline PRInt32
ModSegArraySize(PRInt32 n
) {
95 PRUint32 result
= n
& (mSegmentArrayCount
- 1);
96 NS_ASSERTION(result
== n
% mSegmentArrayCount
,
97 "non-power-of-2 mSegmentArrayCount");
101 inline PRBool
IsFull() {
102 return ModSegArraySize(mLastSegmentIndex
+ 1) == mFirstSegmentIndex
;
106 PRUint32 mSegmentSize
;
108 nsIMemory
* mSegAllocator
;
109 char** mSegmentArray
;
110 PRUint32 mSegmentArrayCount
;
111 PRInt32 mFirstSegmentIndex
;
112 PRInt32 mLastSegmentIndex
;
115 // NS_SEGMENTARRAY_INITIAL_SIZE: This number needs to start out as a
116 // power of 2 given how it gets used. We double the segment array
117 // when we overflow it, and use that fact that it's a power of 2
118 // to compute a fast modulus operation in IsFull.
120 // 32 segment array entries can accommodate 128k of data if segments
121 // are 4k in size. That seems like a reasonable amount that will avoid
122 // needing to grow the segment array.
123 #define NS_SEGMENTARRAY_INITIAL_COUNT 32
125 #endif // nsSegmentedBuffer_h__