1
/**************************************************************************/
3
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/. */
5
/* NXCOMP, NX protocol compression and NX extensions to this software */
6
/* are copyright of NoMachine. Redistribution and use of the present */
7
/* software is allowed according to terms specified in the file LICENSE */
8
/* which comes in the source distribution. */
10
/* Check http://www.nomachine.com/licensing.html for applicability. */
12
/* NX and NoMachine are trademarks of NoMachine S.r.l. */
14
/* All rights reserved. */
16
/**************************************************************************/
19
#include "BlockCacheSet.h"
22
BlockCacheSet::BlockCacheSet(unsigned int numCaches):
23
caches_(new BlockCache *[numCaches]), size_(numCaches),
26
for (unsigned int i = 0; i < numCaches; i++)
27
caches_[i] = new BlockCache();
31
BlockCacheSet::~BlockCacheSet()
34
// TODO: There is still a strange segfault occurring
35
// at random time under Cygwin, when proxy is being
36
// shutdown. Problem appeared just after upgrading
37
// to the latest version of the Cygwin DLL. A stack
38
// trace, obtained at the last minute, reveals that
39
// failure happens in this destructor.
44
for (unsigned int i = 0; i < size_; i++)
48
#endif /* ifdef __CYGWIN32__ */
53
BlockCacheSet::lookup(unsigned int dataLength, const unsigned char *data,
56
unsigned int checksum = BlockCache::checksum(dataLength, data);
57
for (unsigned int i = 0; i < length_; i++)
58
if ((caches_[i]->getChecksum() == checksum) &&
59
(caches_[i]->compare(dataLength, data, 0)))
65
BlockCache *save = caches_[i];
66
unsigned int target = (i >> 1);
69
caches_[i] = caches_[i - 1];
73
caches_[target] = save;
78
unsigned int insertionPoint = (length_ >> 1);
87
BlockCache *save = caches_[start];
88
for (unsigned int k = start; k > insertionPoint; k--)
89
caches_[k] = caches_[k - 1];
90
caches_[insertionPoint] = save;
91
save->set(dataLength, data);
97
BlockCacheSet::get(unsigned index, unsigned int &size,
98
const unsigned char *&data)
100
size = caches_[index]->getLength();
101
data = caches_[index]->getData();
104
BlockCache *save = caches_[index];
105
unsigned int target = (index >> 1);
108
caches_[index] = caches_[index - 1];
111
while (index > target);
112
caches_[target] = save;
119
BlockCacheSet::set(unsigned int dataLength, const unsigned char *data)
121
unsigned int insertionPoint = (length_ >> 1);
123
if (length_ >= size_)
130
BlockCache *save = caches_[start];
131
for (unsigned int k = start; k > insertionPoint; k--)
132
caches_[k] = caches_[k - 1];
133
caches_[insertionPoint] = save;
134
save->set(dataLength, data);