~freenx-team/nx-x11/nxcomp-upstream

« back to all changes in this revision

Viewing changes to BlockCacheSet.cpp

  • Committer: Marcelo Boveto Shima
  • Date: 2009-03-28 22:24:56 UTC
  • Revision ID: mshima@ufserv-20090328222456-rdtaq3oedfyq890c
Import nxcomp 3.3.0-3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**************************************************************************/
 
2
/*                                                                        */
 
3
/* Copyright (c) 2001, 2007 NoMachine, http://www.nomachine.com/.         */
 
4
/*                                                                        */
 
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.                                */
 
9
/*                                                                        */
 
10
/* Check http://www.nomachine.com/licensing.html for applicability.       */
 
11
/*                                                                        */
 
12
/* NX and NoMachine are trademarks of NoMachine S.r.l.                    */
 
13
/*                                                                        */
 
14
/* All rights reserved.                                                   */
 
15
/*                                                                        */
 
16
/**************************************************************************/
 
17
 
 
18
#include "Misc.h"
 
19
#include "BlockCacheSet.h"
 
20
 
 
21
 
 
22
BlockCacheSet::BlockCacheSet(unsigned int numCaches):
 
23
  caches_(new BlockCache *[numCaches]), size_(numCaches), 
 
24
  length_(0)
 
25
{
 
26
  for (unsigned int i = 0; i < numCaches; i++)
 
27
    caches_[i] = new BlockCache();
 
28
}
 
29
 
 
30
 
 
31
BlockCacheSet::~BlockCacheSet()
 
32
{
 
33
  //
 
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.
 
40
  //
 
41
 
 
42
  #ifndef __CYGWIN32__
 
43
 
 
44
  for (unsigned int i = 0; i < size_; i++)
 
45
    delete caches_[i];
 
46
  delete[]caches_;
 
47
 
 
48
  #endif /* ifdef __CYGWIN32__ */
 
49
}
 
50
 
 
51
 
 
52
int
 
53
BlockCacheSet::lookup(unsigned int dataLength, const unsigned char *data,
 
54
                      unsigned int &index)
 
55
{
 
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)))
 
60
    {
 
61
      // match
 
62
      index = i;
 
63
      if (i)
 
64
      {
 
65
        BlockCache *save = caches_[i];
 
66
        unsigned int target = (i >> 1);
 
67
        do
 
68
        {
 
69
          caches_[i] = caches_[i - 1];
 
70
          i--;
 
71
        }
 
72
        while (i > target);
 
73
        caches_[target] = save;
 
74
      }
 
75
      return 1;
 
76
    }
 
77
  // no match
 
78
  unsigned int insertionPoint = (length_ >> 1);
 
79
  unsigned int start;
 
80
  if (length_ >= size_)
 
81
    start = size_ - 1;
 
82
  else
 
83
  {
 
84
    start = length_;
 
85
    length_++;
 
86
  }
 
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);
 
92
  return 0;
 
93
}
 
94
 
 
95
 
 
96
void
 
97
BlockCacheSet::get(unsigned index, unsigned int &size,
 
98
                   const unsigned char *&data)
 
99
{
 
100
  size = caches_[index]->getLength();
 
101
  data = caches_[index]->getData();
 
102
  if (index)
 
103
  {
 
104
    BlockCache *save = caches_[index];
 
105
    unsigned int target = (index >> 1);
 
106
    do
 
107
    {
 
108
      caches_[index] = caches_[index - 1];
 
109
      index--;
 
110
    }
 
111
    while (index > target);
 
112
    caches_[target] = save;
 
113
  }
 
114
}
 
115
 
 
116
 
 
117
 
 
118
void
 
119
BlockCacheSet::set(unsigned int dataLength, const unsigned char *data)
 
120
{
 
121
  unsigned int insertionPoint = (length_ >> 1);
 
122
  unsigned int start;
 
123
  if (length_ >= size_)
 
124
    start = size_ - 1;
 
125
  else
 
126
  {
 
127
    start = length_;
 
128
    length_++;
 
129
  }
 
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);
 
135
}