fix logic
[personal-kdelibs.git] / khtml / imload / tilecache.h
blobfde0fec28dd540dac3da9d71944e5d6e67f7ad88
1 /*
2 Large image displaying library.
4 Copyright (C) 2004,2005 Maks Orlovich (maksim@kde.org)
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:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
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.
25 #ifndef TILE_CACHE_H
26 #define TILE_CACHE_H
28 #include <assert.h>
30 #include "tile.h"
32 namespace khtmlImLoad {
35 class TileCacheNode
37 public:
38 //Interface to the cache LRU chains
39 TileCacheNode* cacheNext;
40 TileCacheNode* cachePrev;
42 void unlink()
44 cacheNext->cachePrev = cachePrev;
45 cachePrev->cacheNext = cacheNext;
46 cacheNext = 0;
47 cachePrev = 0;
50 void linkBefore(TileCacheNode* node)
52 this->cacheNext = node;
53 this->cachePrev = node->cachePrev;
55 node->cachePrev = this;
56 this->cachePrev->cacheNext = this;
59 Tile* tile;
61 TileCacheNode(): cacheNext(0), cachePrev(0), tile(0)
66 /**
67 An LRU-replacement cache for tiles.
69 class TileCache
71 public:
72 typedef TileCacheNode Node;
75 Node* poolHead;
77 //### TODO: consider smarter allocation for these?.
78 Node* create()
80 if (poolHead)
82 Node* toRet = poolHead;
83 poolHead = poolHead->cacheNext;
84 return toRet;
86 else
87 return new Node();
90 void release(Node* entry)
92 //### TODO: Limit ??
93 entry->cacheNext = poolHead;
94 poolHead = entry;
97 private:
98 int sizeLimit;
99 int size;
102 We keep tiles in a double-linked list, with the most recent one being at the rear
104 Node* front;
105 Node* rear;
108 Discard the node, and removes it from the list. Does not free the node.
110 void doDiscard(Node* node)
112 assert(node->tile->cacheNode == node);
113 node->tile->discard();
114 node->tile->cacheNode = 0;
115 node->unlink();
116 --size;
117 assert(size >= 0);
119 public:
121 TileCache(int _sizeLimit):sizeLimit(_sizeLimit), size(0)
123 front = new Node;
124 rear = new Node;
126 front->cacheNext = rear;
127 rear ->cachePrev = front;
129 poolHead = 0;
133 Add an entry to the cache.
135 void addEntry(Tile* tile)
137 assert (tile->cacheNode == 0);
139 Node* node;
141 //Must have room
142 if (size >= sizeLimit)
144 //Remove the front entry, reuse it
145 //for the new node
146 node = front->cacheNext;
147 doDiscard(node);
149 else
150 node = create();
152 //Link in before the end sentinel, i.e. at the very last spot, increment usage count
153 node->tile = tile;
154 tile->cacheNode = node;
155 node->linkBefore(rear);
156 size++;
161 "Touches" the entry, making it the most recent
162 (i.e. moves the entry to the rear of the chain)
164 void touchEntry(Tile* tile)
166 Node* node = tile->cacheNode;
167 node->unlink();
168 node->linkBefore(rear);
172 Removes the entry from the cache, discards it.
174 void removeEntry(Tile* tile)
176 Node* node = tile->cacheNode;
177 doDiscard(node);
179 release(node);
185 #endif
186 // kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;