File: cache8.c

package info (click to toggle)
ruby-ox 2.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 464 kB
  • sloc: ansic: 7,167; ruby: 415; makefile: 2
file content (103 lines) | stat: -rw-r--r-- 2,048 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

#include "ruby.h"
#include "cache8.h"

#define BITS		4
#define MASK		0x000000000000000FULL
#define SLOT_CNT	16
#define DEPTH		16

typedef union {
    struct _Cache8	*child;
    slot_t		value;
} Bucket;

struct _Cache8 {
    Bucket	buckets[SLOT_CNT];
};

static void	cache8_delete(Cache8 cache, int depth);
static void	slot_print(Cache8 cache, sid_t key, unsigned int depth);

void
ox_cache8_new(Cache8 *cache) {
    Bucket	*b;
    int		i;
    
    *cache = ALLOC(struct _Cache8);
    for (i = SLOT_CNT, b = (*cache)->buckets; 0 < i; i--, b++) {
	b->value = 0;
    }
}

void
ox_cache8_delete(Cache8 cache) {
    cache8_delete(cache, 0);
}

static void
cache8_delete(Cache8 cache, int depth) {
    Bucket		*b;
    unsigned int	i;

    for (i = 0, b = cache->buckets; i < SLOT_CNT; i++, b++) {
	if (0 != b->child) {
	    if (DEPTH - 1 != depth) {
		cache8_delete(b->child, depth + 1);
	    }
	}
    }
    xfree(cache);
}

slot_t
ox_cache8_get(Cache8 cache, sid_t key, slot_t **slot) {
    Bucket	*b;
    int		i;
    sid_t	k8 = (sid_t)key;
    sid_t	k;
    
    for (i = 64 - BITS; 0 < i; i -= BITS) {
	k = (k8 >> i) & MASK;
	b = cache->buckets + k;
	if (0 == b->child) {
	    ox_cache8_new(&b->child);
	}
	cache = b->child;
    }
    *slot = &(cache->buckets + (k8 & MASK))->value;

    return **slot;
}

void
ox_cache8_print(Cache8 cache) {
    /*printf("-------------------------------------------\n"); */
    slot_print(cache, 0, 0);
}

static void
slot_print(Cache8 c, sid_t key, unsigned int depth) {
    Bucket		*b;
    unsigned int	i;
    sid_t		k8 = (sid_t)key;
    sid_t		k;

    for (i = 0, b = c->buckets; i < SLOT_CNT; i++, b++) {
	if (0 != b->child) {
	    k = (k8 << BITS) | i;
	    /*printf("*** key: 0x%016llx  depth: %u  i: %u\n", k, depth, i); */
	    if (DEPTH - 1 == depth) {
		printf("0x%016llx: %4llu\n", (unsigned long long)k, (unsigned long long)b->value);
	    } else {
		slot_print(b->child, k, depth + 1);
	    }
	}
    }
}