VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/io/nsSegmentedBuffer.h@ 44770

最後變更 在這個檔案從44770是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 4.6 KB
 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#ifndef nsSegmentedBuffer_h__
39#define nsSegmentedBuffer_h__
40
41#include "nsMemory.h"
42#include "prclist.h"
43
44class nsSegmentedBuffer
45{
46public:
47 nsSegmentedBuffer()
48 : mSegmentSize(0), mMaxSize(0),
49 mSegAllocator(nsnull), mSegmentArray(nsnull),
50 mSegmentArrayCount(0),
51 mFirstSegmentIndex(0), mLastSegmentIndex(0) {}
52
53 ~nsSegmentedBuffer() {
54 Empty();
55 NS_IF_RELEASE(mSegAllocator);
56 }
57
58
59 NS_COM nsresult Init(PRUint32 segmentSize, PRUint32 maxSize,
60 nsIMemory* allocator = nsnull);
61
62 NS_COM char* AppendNewSegment(); // pushes at end
63
64 // returns true if no more segments remain:
65 PRBool DeleteFirstSegment(); // pops from beginning
66
67 // returns true if no more segments remain:
68 PRBool DeleteLastSegment(); // pops from beginning
69
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);
73
74 NS_COM void Empty(); // frees all segments
75
76 inline PRUint32 GetSegmentCount() {
77 if (mFirstSegmentIndex <= mLastSegmentIndex)
78 return mLastSegmentIndex - mFirstSegmentIndex;
79 else
80 return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex;
81 }
82
83 inline PRUint32 GetSegmentSize() { return mSegmentSize; }
84 inline PRUint32 GetMaxSize() { return mMaxSize; }
85 inline PRUint32 GetSize() { return GetSegmentCount() * mSegmentSize; }
86
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];
91 }
92
93protected:
94 inline PRInt32 ModSegArraySize(PRInt32 n) {
95 PRUint32 result = n & (mSegmentArrayCount - 1);
96 NS_ASSERTION(result == n % mSegmentArrayCount,
97 "non-power-of-2 mSegmentArrayCount");
98 return result;
99 }
100
101 inline PRBool IsFull() {
102 return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex;
103 }
104
105protected:
106 PRUint32 mSegmentSize;
107 PRUint32 mMaxSize;
108 nsIMemory* mSegAllocator;
109 char** mSegmentArray;
110 PRUint32 mSegmentArrayCount;
111 PRInt32 mFirstSegmentIndex;
112 PRInt32 mLastSegmentIndex;
113};
114
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.
119//
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
124
125#endif // nsSegmentedBuffer_h__
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette