| 1 | /* |
| 2 | This file is part of Konsole, an X terminal. |
| 3 | Copyright (C) 2000 by Stephan Kulow <[email protected]> |
| 4 | |
| 5 | Rewritten for QT4 by e_k <e_k at users.sourceforge.net>, Copyright (C)2008 |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 20 | 02110-1301 USA. |
| 21 | |
| 22 | */ |
| 23 | |
| 24 | #include <QtDebug> |
| 25 | |
| 26 | // Own |
| 27 | #include "BlockArray.h"cxx . . $ `<file>/lib/BlockArray.h`/ |
| 28 | |
| 29 | // System |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/param.h> |
| 32 | #include <unistd.h> |
| 33 | #include <cstdio> |
| 34 | |
| 35 | |
| 36 | using namespace Konsole; |
| 37 | |
| 38 | static int blocksizecxx . . $ blocksize. = 0; |
| 39 | |
| 40 | BlockArraycxx . . $ Konsole/BlockArray#::BlockArraycxx . . $ Konsole/BlockArray#BlockArray(49f6e7a06ebc5aa8).() |
| 41 | : sizecxx . . $ Konsole/BlockArray#size.(0), |
| 42 | currentcxx . . $ Konsole/BlockArray#current.(size_t(-1)), |
| 43 | indexcxx . . $ Konsole/BlockArray#index.(size_t(-1)), |
| 44 | lastmapcxx . . $ Konsole/BlockArray#lastmap.(nullptr), |
| 45 | lastmap_indexcxx . . $ Konsole/BlockArray#lastmap_index.(size_t(-1)), |
| 46 | lastblockcxx . . $ Konsole/BlockArray#lastblock.(nullptr), ioncxx . . $ Konsole/BlockArray#ion.(-1), |
| 47 | lengthcxx . . $ Konsole/BlockArray#length.(0) |
| 48 | { |
| 49 | // lastmap_index = index = current = size_t(-1); |
| 50 | if (blocksizecxx . . $ blocksize. == 0) { |
| 51 | blocksizecxx . . $ blocksize. = ((sizeof(Blockcxx . . $ Konsole/Block#) / getpagesize()) + 1) * getpagesize(); |
| 52 | } |
| 53 | |
| 54 | } |
| 55 | |
| 56 | BlockArraycxx . . $ Konsole/BlockArray#::~cxx . . $ Konsole/BlockArray#`~BlockArray`(ced63f7c635d850d).BlockArraycxx . . $ Konsole/BlockArray#() |
| 57 | { |
| 58 | setHistorySizecxx . . $ Konsole/BlockArray#setHistorySize(31596a199e1d8a50).(0); |
| 59 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(!lastblock); |
| 60 | } |
| 61 | |
| 62 | size_t BlockArraycxx . . $ Konsole/BlockArray#::appendcxx . . $ Konsole/BlockArray#append(6fe1facc1dab5d78).(Blockcxx . . $ Konsole/Block# * block) |
| 63 | { |
| 64 | if (!sizecxx . . $ Konsole/BlockArray#size.) { |
| 65 | return size_t(-1); |
| 66 | } |
| 67 | |
| 68 | ++currentcxx . . $ Konsole/BlockArray#current.; |
| 69 | if (currentcxx . . $ Konsole/BlockArray#current. >= sizecxx . . $ Konsole/BlockArray#size.) { |
| 70 | currentcxx . . $ Konsole/BlockArray#current. = 0; |
| 71 | } |
| 72 | |
| 73 | int rc; |
| 74 | rc = lseek(ioncxx . . $ Konsole/BlockArray#ion., currentcxx . . $ Konsole/BlockArray#current. * blocksizecxx . . $ blocksize., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 75 | if (rc < 0) { |
| 76 | perror("HistoryBuffer::add.seek"); |
| 77 | setHistorySizecxx . . $ Konsole/BlockArray#setHistorySize(31596a199e1d8a50).(0); |
| 78 | return size_t(-1); |
| 79 | } |
| 80 | rc = write(ioncxx . . $ Konsole/BlockArray#ion., block, blocksizecxx . . $ blocksize.); |
| 81 | if (rc < 0) { |
| 82 | perror("HistoryBuffer::add.write"); |
| 83 | setHistorySizecxx . . $ Konsole/BlockArray#setHistorySize(31596a199e1d8a50).(0); |
| 84 | return size_t(-1); |
| 85 | } |
| 86 | |
| 87 | lengthcxx . . $ Konsole/BlockArray#length.++; |
| 88 | if (lengthcxx . . $ Konsole/BlockArray#length. > sizecxx . . $ Konsole/BlockArray#size.) { |
| 89 | lengthcxx . . $ Konsole/BlockArray#length. = sizecxx . . $ Konsole/BlockArray#size.; |
| 90 | } |
| 91 | |
| 92 | ++indexcxx . . $ Konsole/BlockArray#index.; |
| 93 | |
| 94 | delete block; |
| 95 | return currentcxx . . $ Konsole/BlockArray#current.; |
| 96 | } |
| 97 | |
| 98 | size_t BlockArraycxx . . $ Konsole/BlockArray#::newBlockcxx . . $ Konsole/BlockArray#newBlock(f708dce9a8856c70).() |
| 99 | { |
| 100 | if (!sizecxx . . $ Konsole/BlockArray#size.) { |
| 101 | return size_t(-1); |
| 102 | } |
| 103 | appendcxx . . $ Konsole/BlockArray#append(6fe1facc1dab5d78).(lastblockcxx . . $ Konsole/BlockArray#lastblock.); |
| 104 | |
| 105 | lastblockcxx . . $ Konsole/BlockArray#lastblock. = new Blockcxx . . $ Konsole/Block#(); |
| 106 | return indexcxx . . $ Konsole/BlockArray#index. + 1; |
| 107 | } |
| 108 | |
| 109 | Blockcxx . . $ Konsole/Block# * BlockArraycxx . . $ Konsole/BlockArray#::lastBlockcxx . . $ Konsole/BlockArray#lastBlock(ed43a5b5d99d0b0e).() const |
| 110 | { |
| 111 | return lastblockcxx . . $ Konsole/BlockArray#lastblock.; |
| 112 | } |
| 113 | |
| 114 | bool BlockArraycxx . . $ Konsole/BlockArray#::hascxx . . $ Konsole/BlockArray#has(2ff7c7de1baa6d0a).(size_t i) const |
| 115 | { |
| 116 | if (i == indexcxx . . $ Konsole/BlockArray#index. + 1) { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | if (i > indexcxx . . $ Konsole/BlockArray#index.) { |
| 121 | return false; |
| 122 | } |
| 123 | if (indexcxx . . $ Konsole/BlockArray#index. - i >= lengthcxx . . $ Konsole/BlockArray#length.) { |
| 124 | return false; |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | const Blockcxx . . $ Konsole/Block# * BlockArraycxx . . $ Konsole/BlockArray#::atcxx . . $ Konsole/BlockArray#at(7d5b4b52cd5680c7).(size_t i) |
| 130 | { |
| 131 | if (i == indexcxx . . $ Konsole/BlockArray#index. + 1) { |
| 132 | return lastblockcxx . . $ Konsole/BlockArray#lastblock.; |
| 133 | } |
| 134 | |
| 135 | if (i == lastmap_indexcxx . . $ Konsole/BlockArray#lastmap_index.) { |
| 136 | return lastmapcxx . . $ Konsole/BlockArray#lastmap.; |
| 137 | } |
| 138 | |
| 139 | if (i > indexcxx . . $ Konsole/BlockArray#index.) { |
| 140 | qDebugcxx . . $ `<external>/10450329605257866924/qlogging.h:164:9`!() <<cxx . . $ QDebug#`operator<<`(d8a917b173851aad). "BlockArray::at() i > index\n"; |
| 141 | return nullptr; |
| 142 | } |
| 143 | |
| 144 | // if (index - i >= length) { |
| 145 | // kDebug(1211) << "BlockArray::at() index - i >= length\n"; |
| 146 | // return 0; |
| 147 | // } |
| 148 | |
| 149 | size_t j = i; // (current - (index - i) + (index/size+1)*size) % size ; |
| 150 | |
| 151 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(j < size); |
| 152 | unmapcxx . . $ Konsole/BlockArray#unmap(49f6e7a06ebc5aa8).(); |
| 153 | |
| 154 | Blockcxx . . $ Konsole/Block# * block = (Blockcxx . . $ Konsole/Block# *)mmap(nullptr, blocksizecxx . . $ blocksize., PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!, MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`!, ioncxx . . $ Konsole/BlockArray#ion., j * blocksizecxx . . $ blocksize.); |
| 155 | |
| 156 | if (block == (Blockcxx . . $ Konsole/Block# *)-1) { |
| 157 | perror("mmap"); |
| 158 | return nullptr; |
| 159 | } |
| 160 | |
| 161 | lastmapcxx . . $ Konsole/BlockArray#lastmap. = block; |
| 162 | lastmap_indexcxx . . $ Konsole/BlockArray#lastmap_index. = i; |
| 163 | |
| 164 | return block; |
| 165 | } |
| 166 | |
| 167 | void BlockArraycxx . . $ Konsole/BlockArray#::unmapcxx . . $ Konsole/BlockArray#unmap(49f6e7a06ebc5aa8).() |
| 168 | { |
| 169 | if (lastmapcxx . . $ Konsole/BlockArray#lastmap.) { |
| 170 | int res = munmap((char *)lastmapcxx . . $ Konsole/BlockArray#lastmap., blocksizecxx . . $ blocksize.); |
| 171 | if (res < 0) { |
| 172 | perror("munmap"); |
| 173 | } |
| 174 | } |
| 175 | lastmapcxx . . $ Konsole/BlockArray#lastmap. = nullptr; |
| 176 | lastmap_indexcxx . . $ Konsole/BlockArray#lastmap_index. = size_t(-1); |
| 177 | } |
| 178 | |
| 179 | bool BlockArraycxx . . $ Konsole/BlockArray#::setSizecxx . . $ Konsole/BlockArray#setSize(31596a199e1d8a50).(size_t newsize) |
| 180 | { |
| 181 | return setHistorySizecxx . . $ Konsole/BlockArray#setHistorySize(31596a199e1d8a50).(newsize * 1024 / blocksizecxx . . $ blocksize.); |
| 182 | } |
| 183 | |
| 184 | bool BlockArraycxx . . $ Konsole/BlockArray#::setHistorySizecxx . . $ Konsole/BlockArray#setHistorySize(31596a199e1d8a50).(size_t newsize) |
| 185 | { |
| 186 | // kDebug(1211) << "setHistorySize " << size << " " << newsize; |
| 187 | |
| 188 | if (sizecxx . . $ Konsole/BlockArray#size. == newsize) { |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | unmapcxx . . $ Konsole/BlockArray#unmap(49f6e7a06ebc5aa8).(); |
| 193 | |
| 194 | if (!newsize) { |
| 195 | delete lastblockcxx . . $ Konsole/BlockArray#lastblock.; |
| 196 | lastblockcxx . . $ Konsole/BlockArray#lastblock. = nullptr; |
| 197 | if (ioncxx . . $ Konsole/BlockArray#ion. >= 0) { |
| 198 | close(ioncxx . . $ Konsole/BlockArray#ion.); |
| 199 | } |
| 200 | ioncxx . . $ Konsole/BlockArray#ion. = -1; |
| 201 | currentcxx . . $ Konsole/BlockArray#current. = size_t(-1); |
| 202 | return true; |
| 203 | } |
| 204 | |
| 205 | if (!sizecxx . . $ Konsole/BlockArray#size.) { |
| 206 | FILEcxx . . $ FILE# * tmp = tmpfile(); |
| 207 | if (!tmp) { |
| 208 | perror("konsole: cannot open temp file.\n"); |
| 209 | } else { |
| 210 | ioncxx . . $ Konsole/BlockArray#ion. = dup(fileno(tmp)); |
| 211 | if (ioncxx . . $ Konsole/BlockArray#ion.<0) { |
| 212 | perror("konsole: cannot dup temp file.\n"); |
| 213 | fclose(tmp); |
| 214 | } |
| 215 | } |
| 216 | if (ioncxx . . $ Konsole/BlockArray#ion. < 0) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | Q_ASSERTcxx . . $ `<external>/15045578679651060896/qglobal.h:859:13`!(!lastblock); |
| 221 | |
| 222 | lastblockcxx . . $ Konsole/BlockArray#lastblock. = new Blockcxx . . $ Konsole/Block#(); |
| 223 | sizecxx . . $ Konsole/BlockArray#size. = newsize; |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | if (newsize > sizecxx . . $ Konsole/BlockArray#size.) { |
| 228 | increaseBuffercxx . . $ Konsole/BlockArray#increaseBuffer(49f6e7a06ebc5aa8).(); |
| 229 | sizecxx . . $ Konsole/BlockArray#size. = newsize; |
| 230 | return false; |
| 231 | } else { |
| 232 | decreaseBuffercxx . . $ Konsole/BlockArray#decreaseBuffer(2c44cd906ae4e829).(newsize); |
| 233 | ftruncate(ioncxx . . $ Konsole/BlockArray#ion., lengthcxx . . $ Konsole/BlockArray#length.*blocksizecxx . . $ blocksize.); |
| 234 | sizecxx . . $ Konsole/BlockArray#size. = newsize; |
| 235 | |
| 236 | return true; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | void moveBlockcxx . . $ moveBlock(9dcba3fa01181d3b).(FILEcxx . . $ FILE# * fion, int cursor, int newpos, char * buffer2) |
| 241 | { |
| 242 | int res = fseek(fion, cursor * blocksizecxx . . $ blocksize., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 243 | if (res) { |
| 244 | perror("fseek"); |
| 245 | } |
| 246 | res = fread(buffer2, blocksizecxx . . $ blocksize., 1, fion); |
| 247 | if (res != 1) { |
| 248 | perror("fread"); |
| 249 | } |
| 250 | |
| 251 | res = fseek(fion, newpos * blocksizecxx . . $ blocksize., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 252 | if (res) { |
| 253 | perror("fseek"); |
| 254 | } |
| 255 | res = fwrite(buffer2, blocksizecxx . . $ blocksize., 1, fion); |
| 256 | if (res != 1) { |
| 257 | perror("fwrite"); |
| 258 | } |
| 259 | // printf("moving block %d to %d\n", cursor, newpos); |
| 260 | } |
| 261 | |
| 262 | void BlockArraycxx . . $ Konsole/BlockArray#::decreaseBuffercxx . . $ Konsole/BlockArray#decreaseBuffer(2c44cd906ae4e829).(size_t newsize) |
| 263 | { |
| 264 | if (indexcxx . . $ Konsole/BlockArray#index. < newsize) { // still fits in whole |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | int offset = (currentcxx . . $ Konsole/BlockArray#current. - (newsize - 1) + sizecxx . . $ Konsole/BlockArray#size.) % sizecxx . . $ Konsole/BlockArray#size.; |
| 269 | |
| 270 | if (!offset) { |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // The Block constructor could do somthing in future... |
| 275 | char * buffer1 = new char[blocksizecxx . . $ blocksize.]; |
| 276 | |
| 277 | FILEcxx . . $ FILE# * fion = fdopen(dup(ioncxx . . $ Konsole/BlockArray#ion.), "w+b"); |
| 278 | if (!fion) { |
| 279 | delete [] buffer1; |
| 280 | perror("fdopen/dup"); |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | int firstblock; |
| 285 | if (currentcxx . . $ Konsole/BlockArray#current. <= newsize) { |
| 286 | firstblock = currentcxx . . $ Konsole/BlockArray#current. + 1; |
| 287 | } else { |
| 288 | firstblock = 0; |
| 289 | } |
| 290 | |
| 291 | size_t oldpos; |
| 292 | for (size_t i = 0, cursor=firstblock; i < newsize; i++) { |
| 293 | oldpos = (sizecxx . . $ Konsole/BlockArray#size. + cursor + offset) % sizecxx . . $ Konsole/BlockArray#size.; |
| 294 | moveBlockcxx . . $ moveBlock(9dcba3fa01181d3b).(fion, oldpos, cursor, buffer1); |
| 295 | if (oldpos < newsize) { |
| 296 | cursor = oldpos; |
| 297 | } else { |
| 298 | cursor++; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | currentcxx . . $ Konsole/BlockArray#current. = newsize - 1; |
| 303 | lengthcxx . . $ Konsole/BlockArray#length. = newsize; |
| 304 | |
| 305 | delete [] buffer1; |
| 306 | |
| 307 | fclose(fion); |
| 308 | |
| 309 | } |
| 310 | |
| 311 | void BlockArraycxx . . $ Konsole/BlockArray#::increaseBuffercxx . . $ Konsole/BlockArray#increaseBuffer(49f6e7a06ebc5aa8).() |
| 312 | { |
| 313 | if (indexcxx . . $ Konsole/BlockArray#index. < sizecxx . . $ Konsole/BlockArray#size.) { // not even wrapped once |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | int offset = (currentcxx . . $ Konsole/BlockArray#current. + sizecxx . . $ Konsole/BlockArray#size. + 1) % sizecxx . . $ Konsole/BlockArray#size.; |
| 318 | if (!offset) { // no moving needed |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // The Block constructor could do somthing in future... |
| 323 | char * buffer1 = new char[blocksizecxx . . $ blocksize.]; |
| 324 | char * buffer2 = new char[blocksizecxx . . $ blocksize.]; |
| 325 | |
| 326 | int runs = 1; |
| 327 | int bpr = sizecxx . . $ Konsole/BlockArray#size.; // blocks per run |
| 328 | |
| 329 | if (sizecxx . . $ Konsole/BlockArray#size. % offset == 0) { |
| 330 | bpr = sizecxx . . $ Konsole/BlockArray#size. / offset; |
| 331 | runs = offset; |
| 332 | } |
| 333 | |
| 334 | FILEcxx . . $ FILE# * fion = fdopen(dup(ioncxx . . $ Konsole/BlockArray#ion.), "w+b"); |
| 335 | if (!fion) { |
| 336 | perror("fdopen/dup"); |
| 337 | delete [] buffer1; |
| 338 | delete [] buffer2; |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | int res; |
| 343 | for (int i = 0; i < runs; i++) { |
| 344 | // free one block in chain |
| 345 | int firstblock = (offset + i) % sizecxx . . $ Konsole/BlockArray#size.; |
| 346 | res = fseek(fion, firstblock * blocksizecxx . . $ blocksize., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 347 | if (res) { |
| 348 | perror("fseek"); |
| 349 | } |
| 350 | res = fread(buffer1, blocksizecxx . . $ blocksize., 1, fion); |
| 351 | if (res != 1) { |
| 352 | perror("fread"); |
| 353 | } |
| 354 | int newpos = 0; |
| 355 | for (int j = 1, cursor=firstblock; j < bpr; j++) { |
| 356 | cursor = (cursor + offset) % sizecxx . . $ Konsole/BlockArray#size.; |
| 357 | newpos = (cursor - offset + sizecxx . . $ Konsole/BlockArray#size.) % sizecxx . . $ Konsole/BlockArray#size.; |
| 358 | moveBlockcxx . . $ moveBlock(9dcba3fa01181d3b).(fion, cursor, newpos, buffer2); |
| 359 | } |
| 360 | res = fseek(fion, i * blocksizecxx . . $ blocksize., SEEK_SETcxx . . $ `<external>/232824719582829767/stdio.h:110:9`!); |
| 361 | if (res) { |
| 362 | perror("fseek"); |
| 363 | } |
| 364 | res = fwrite(buffer1, blocksizecxx . . $ blocksize., 1, fion); |
| 365 | if (res != 1) { |
| 366 | perror("fwrite"); |
| 367 | } |
| 368 | } |
| 369 | currentcxx . . $ Konsole/BlockArray#current. = sizecxx . . $ Konsole/BlockArray#size. - 1; |
| 370 | lengthcxx . . $ Konsole/BlockArray#length. = sizecxx . . $ Konsole/BlockArray#size.; |
| 371 | |
| 372 | delete [] buffer1; |
| 373 | delete [] buffer2; |
| 374 | |
| 375 | fclose(fion); |
| 376 | |
| 377 | } |
| 378 |