KHtml

tilecache.h
1 /*
2  Large image displaying library.
3 
4  Copyright (C) 2004,2005 Maks Orlovich ([email protected])
5 
6  Permission is hereby granted, free of charge, to any person obtaining a copy
7  of this software and associated documentation files (the "Software"), to deal
8  in the Software without restriction, including without limitation the rights
9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  copies of the Software, and to permit persons to whom the Software is
11  furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 
23 */
24 
25 #ifndef TILE_CACHE_H
26 #define TILE_CACHE_H
27 
28 #include <assert.h>
29 
30 #include "tile.h"
31 
32 namespace khtmlImLoad
33 {
34 
35 class TileCacheNode
36 {
37 public:
38  //Interface to the cache LRU chains
39  TileCacheNode *cacheNext;
40  TileCacheNode *cachePrev;
41 
42  void unlink()
43  {
44  cacheNext->cachePrev = cachePrev;
45  cachePrev->cacheNext = cacheNext;
46  cacheNext = nullptr;
47  cachePrev = nullptr;
48  }
49 
50  void linkBefore(TileCacheNode *node)
51  {
52  this->cacheNext = node;
53  this->cachePrev = node->cachePrev;
54 
55  node->cachePrev = this;
56  this->cachePrev->cacheNext = this;
57  }
58 
59  Tile *tile;
60 
61  TileCacheNode(): cacheNext(nullptr), cachePrev(nullptr), tile(nullptr)
62  {}
63 };
64 
65 /**
66  An LRU-replacement cache for tiles.
67 */
68 class TileCache
69 {
70 public:
71  typedef TileCacheNode Node;
72 
73  Node *poolHead;
74 
75  //### TODO: consider smarter allocation for these?.
76  Node *create()
77  {
78  if (poolHead) {
79  Node *toRet = poolHead;
80  poolHead = poolHead->cacheNext;
81  return toRet;
82  } else {
83  return new Node();
84  }
85  }
86 
87  void release(Node *entry)
88  {
89  //### TODO: Limit ??
90  entry->cacheNext = poolHead;
91  poolHead = entry;
92  }
93 
94 private:
95  int sizeLimit;
96  int size;
97 
98  /**
99  We keep tiles in a double-linked list, with the most recent one being at the rear
100  */
101  Node *front;
102  Node *rear;
103 
104  /**
105  Discard the node, and removes it from the list. Does not free the node.
106  */
107  void doDiscard(Node *node)
108  {
109  assert(node->tile->cacheNode == node);
110  node->tile->discard();
111  node->tile->cacheNode = nullptr;
112  node->unlink();
113  --size;
114  assert(size >= 0);
115  }
116 public:
117 
118  TileCache(int _sizeLimit): sizeLimit(_sizeLimit), size(0)
119  {
120  front = new Node;
121  rear = new Node;
122 
123  front->cacheNext = rear;
124  rear ->cachePrev = front;
125 
126  poolHead = nullptr;
127  }
128 
129  /**
130  Add an entry to the cache.
131  */
132  void addEntry(Tile *tile)
133  {
134  assert(tile->cacheNode == nullptr);
135 
136  Node *node;
137 
138  //Must have room
139  if (size >= sizeLimit) {
140  //Remove the front entry, reuse it
141  //for the new node
142  node = front->cacheNext;
143  doDiscard(node);
144  } else {
145  node = create();
146  }
147 
148  //Link in before the end sentinel, i.e. at the very last spot, increment usage count
149  node->tile = tile;
150  tile->cacheNode = node;
151  node->linkBefore(rear);
152  size++;
153  }
154 
155  /**
156  "Touches" the entry, making it the most recent
157  (i.e. moves the entry to the rear of the chain)
158  */
159  void touchEntry(Tile *tile)
160  {
161  Node *node = tile->cacheNode;
162  node->unlink();
163  node->linkBefore(rear);
164  }
165 
166  /**
167  Removes the entry from the cache, discards it.
168  */
169  void removeEntry(Tile *tile)
170  {
171  Node *node = tile->cacheNode;
172  doDiscard(node);
173 
174  release(node);
175  }
176 };
177 
178 }
179 
180 #endif
void addEntry(Tile *tile)
Add an entry to the cache.
Definition: tilecache.h:132
An LRU-replacement cache for tiles.
Definition: tilecache.h:68
void removeEntry(Tile *tile)
Removes the entry from the cache, discards it.
Definition: tilecache.h:169
void touchEntry(Tile *tile)
"Touches" the entry, making it the most recent (i.e.
Definition: tilecache.h:159
We hold pointers tiles in the cache.
Definition: tile.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue Oct 26 2021 22:48:10 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.