Browse code

splay replace policy

aCaB authored on 2010/01/11 04:24:09
Showing 1 changed files
... ...
@@ -202,7 +202,7 @@ struct node {
202 202
     int64_t digest[2];
203 203
     struct node *left;
204 204
     struct node *right;
205
-    uint32_t size; /* 0 is used to mark an empty hash slot! */
205
+    uint32_t size;
206 206
 };
207 207
 
208 208
 struct cache_set {
... ...
@@ -282,6 +282,24 @@ static int cacheset_lookup(struct cache_set *cs, unsigned char *md5, size_t size
282 282
     return 1337;
283 283
 }
284 284
 
285
+static int get_worst(struct node *n, struct node *parent, struct node **worst, struct node **wparent) {
286
+    unsigned int left, right;
287
+    struct node *wl = n, *wr = n, *pl = parent, *pr = parent;
288
+
289
+    if(!n) return 0;
290
+    left = get_worst(n->left, n, &wl, &pl);
291
+    right = get_worst(n->right, n, &wr, &pr);
292
+
293
+    if(left < right) {
294
+	*worst = wr;
295
+	*wparent = pr;
296
+	return right + 1;
297
+    }
298
+    *worst = wl;
299
+    *wparent = pl;
300
+    return left +1;
301
+}
302
+
285 303
 static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size) {
286 304
     struct node *newnode;
287 305
     int64_t hash[2];
... ...
@@ -296,9 +314,12 @@ static void cacheset_add(struct cache_set *cs, unsigned char *md5, size_t size)
296 296
     }
297 297
 
298 298
     if(cs->used == cs->total) {
299
-	cli_errmsg("TREE IS FULL, BYE!\n");
300
-	abort();
301
-	return;
299
+	struct node *parent;
300
+	get_worst(cs->root, NULL, &newnode, &parent);
301
+	if(parent->left == newnode)
302
+	    parent->left = NULL;
303
+	else
304
+	    parent->right = NULL;
302 305
     } else {
303 306
 	newnode = &cs->data[cs->used++];
304 307
     }