LCOV - app.info - xpcom/io/nsSegmentedBuffer.h

LCOV - code coverage report
| Current view: | directory - xpcom/io - nsSegmentedBuffer.h (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 23 | 23 | 100.0 %
| Date: | 2012-04-21 | Functions: | 9 | 9 | 100.0 %

           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                 :
          44                 : class nsSegmentedBuffer
          45                 : {
          46                 : public:
          47           26490 :     nsSegmentedBuffer()
          48                 :         : mSegmentSize(0), mMaxSize(0),
          49                 :           mSegAllocator(nsnull), mSegmentArray(nsnull),
          50                 :           mSegmentArrayCount(0),
          51           26490 :           mFirstSegmentIndex(0), mLastSegmentIndex(0) {}
          52                 :
          53           26475 :     ~nsSegmentedBuffer() {
          54           26475 :         Empty();
          55           26475 :         NS_IF_RELEASE(mSegAllocator);
          56           26475 :     }
          57                 :
          58                 :
          59                 :     nsresult Init(PRUint32 segmentSize, PRUint32 maxSize,
          60                 :                   nsIMemory* allocator = nsnull);
          61                 :
          62                 :     char* AppendNewSegment();   // pushes at end
          63                 :
          64                 :     // returns true if no more segments remain:
          65                 :     bool DeleteFirstSegment();  // pops from beginning
          66                 :
          67                 :     // returns true if no more segments remain:
          68                 :     bool 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                 :     bool ReallocLastSegment(size_t newSize);
          73                 :
          74                 :     void Empty();               // frees all segments
          75                 :
          76         7583512 :     inline PRUint32 GetSegmentCount() {
          77         7583512 :         if (mFirstSegmentIndex <= mLastSegmentIndex)
          78         7583006 :             return mLastSegmentIndex - mFirstSegmentIndex;
          79                 :         else
          80             506 :             return mSegmentArrayCount + mLastSegmentIndex - mFirstSegmentIndex;
          81                 :     }
          82                 :
          83           40149 :     inline PRUint32 GetSegmentSize() { return mSegmentSize; }
          84            1139 :     inline PRUint32 GetMaxSize() { return mMaxSize; }
          85         3680269 :     inline PRUint32 GetSize() { return GetSegmentCount() * mSegmentSize; }
          86                 :
          87         3690704 :     inline char* GetSegment(PRUint32 indx) {
          88         3690704 :         NS_ASSERTION(indx < GetSegmentCount(), "index out of bounds");
          89         3690704 :         PRInt32 i = ModSegArraySize(mFirstSegmentIndex + (PRInt32)indx);
          90         3690704 :         return mSegmentArray[i];
          91                 :     }
          92                 :
          93                 : protected:
          94        11059173 :     inline PRInt32 ModSegArraySize(PRInt32 n) {
          95        11059173 :         PRUint32 result = n & (mSegmentArrayCount - 1);
          96        11059173 :         NS_ASSERTION(result == n % mSegmentArrayCount,
          97                 :                      "non-power-of-2 mSegmentArrayCount");
          98        11059173 :         return result;
          99                 :     }
         100                 :
         101         3679014 :    inline bool IsFull() {
         102         3679014 :         return ModSegArraySize(mLastSegmentIndex + 1) == mFirstSegmentIndex;
         103                 :     }
         104                 :
         105                 : protected:
         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__

Generated by: LCOV version 1.7