File indexing completed on 2025-01-19 04:23:24

0001 /*
0002     This file is part of Konsole, an X terminal.
0003     Copyright (C) 2000 by Stephan Kulow <coolo@kde.org>
0004 
0005     Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008
0006 
0007     This program is free software; you can redistribute it and/or modify
0008     it under the terms of the GNU General Public License as published by
0009     the Free Software Foundation; either version 2 of the License, or
0010     (at your option) any later version.
0011 
0012     This program is distributed in the hope that it will be useful,
0013     but WITHOUT ANY WARRANTY; without even the implied warranty of
0014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015     GNU General Public License for more details.
0016 
0017     You should have received a copy of the GNU General Public License
0018     along with this program; if not, write to the Free Software
0019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0020     02110-1301  USA.
0021 */
0022 
0023 #ifndef BLOCKARRAY_H
0024 #define BLOCKARRAY_H
0025 
0026 #include <unistd.h>
0027 
0028 //#error Do not use in KDE 2.1
0029 
0030 #define BlockSize (1 << 12)
0031 #define ENTRIES   ((BlockSize - sizeof(size_t) ) / sizeof(unsigned char))
0032 
0033 namespace Konsole {
0034 
0035 struct Block {
0036     Block() {
0037         size = 0;
0038     }
0039     unsigned char data[ENTRIES];
0040     size_t size;
0041 };
0042 
0043 // ///////////////////////////////////////////////////////
0044 
0045 class BlockArray {
0046 public:
0047     /**
0048     * Creates a history file for holding
0049     * maximal size blocks. If more blocks
0050     * are requested, then it drops earlier
0051     * added ones.
0052     */
0053     BlockArray();
0054 
0055     /// destructor
0056     ~BlockArray();
0057 
0058     /**
0059     * adds the Block at the end of history.
0060     * This may drop other blocks.
0061     *
0062     * The ownership on the block is transfered.
0063     * An unique index number is returned for accessing
0064     * it later (if not yet dropped then)
0065     *
0066     * Note, that the block may be dropped completely
0067     * if history is turned off.
0068     */
0069     size_t append(Block * block);
0070 
0071     /**
0072     * gets the block at the index. Function may return
0073     * 0 if the block isn't available any more.
0074     *
0075     * The returned block is strictly readonly as only
0076     * maped in memory - and will be invalid on the next
0077     * operation on this class.
0078     */
0079     const Block * at(size_t index);
0080 
0081     /**
0082     * reorders blocks as needed. If newsize is null,
0083     * the history is emptied completely. The indices
0084     * returned on append won't change their semantic,
0085     * but they may not be valid after this call.
0086     */
0087     bool setHistorySize(size_t newsize);
0088 
0089     size_t newBlock();
0090 
0091     Block * lastBlock() const;
0092 
0093     /**
0094     * Convenient function to set the size in KBytes
0095     * instead of blocks
0096     */
0097     bool setSize(size_t newsize);
0098 
0099     size_t len() const {
0100         return length;
0101     }
0102 
0103     bool has(size_t index) const;
0104 
0105     size_t getCurrent() const {
0106         return current;
0107     }
0108 
0109 private:
0110     void unmap();
0111     void increaseBuffer();
0112     void decreaseBuffer(size_t newsize);
0113 
0114     size_t size;
0115     // current always shows to the last inserted block
0116     size_t current;
0117     size_t index;
0118 
0119     Block * lastmap;
0120     size_t lastmap_index;
0121     Block * lastblock;
0122 
0123     int ion;
0124     size_t length;
0125 
0126 };
0127 
0128 }
0129 
0130 #endif