source: titan/titan/channelcache.h @ 41390

Last change on this file since 41390 was 36332, checked in by obi, 8 years ago

test old channel cache

File size: 2.1 KB
Line 
1#ifndef CHANNELCACHE_H
2#define CHANNELCACHE_H
3
4void debugchannelcache()
5{
6        int i, count = 0, maxcount = 0;;
7        struct channelcache* node = NULL;
8       
9        for(i = 0; i < CHANNELCACHEMAX; i++)
10        {
11                node = channelcache[i];
12                count = 0;
13                while(node != NULL)
14                {
15                        maxcount++;
16                        count++;
17                        node = node->next;
18                }
19                printf("hash=%d, count=%d\n", i, count);
20        }
21        printf("maxcount=%d\n", maxcount);
22}
23
24struct channel* getchannel(int serviceid, uint64_t transponderid)
25{
26        unsigned int hash;
27        struct channelcache* node = NULL;
28
29        hash = (transponderid + serviceid) % CHANNELCACHEMAX;
30        if(hash < 0 || hash >= CHANNELCACHEMAX) hash = 0;
31
32        m_lock(&status.channelmutex, 5);
33        node = channelcache[hash];
34
35        while(node != NULL)
36        {
37                if(node->serviceid == serviceid && node->transponderid == transponderid)
38                {
39                        m_unlock(&status.channelmutex, 5);
40                        return node->chnode;
41                }
42
43                node = node->next;
44        }
45
46        m_unlock(&status.channelmutex, 5);
47        return NULL;
48}
49
50struct channelcache* modifychannelcache(int serviceid, uint64_t transponderid, struct channel* chnode)
51{
52        unsigned int hash;
53        struct channelcache* node = NULL, *newnode = NULL;
54
55        hash = (transponderid + serviceid) % CHANNELCACHEMAX;
56        if(hash < 0 || hash >= CHANNELCACHEMAX) hash = 0;
57
58        newnode = (struct channelcache*)calloc(1, sizeof(struct channelcache));
59        if(newnode == NULL)
60        {
61                err("no memory");
62                return NULL;
63        }
64
65        newnode->serviceid = serviceid;
66        newnode->transponderid = transponderid;
67        newnode->chnode = chnode;
68
69        node = channelcache[hash];
70        channelcache[hash] = newnode;
71        newnode->next = node;
72
73        return newnode;
74}
75
76void delchannelcache(int serviceid, uint64_t transponderid)
77{
78        unsigned int hash;
79        struct channelcache *node = NULL, *prev = NULL;
80
81        hash = (transponderid + serviceid) % CHANNELCACHEMAX;
82        if(hash < 0 || hash >= CHANNELCACHEMAX) hash = 0;
83        node = channelcache[hash];
84        prev = channelcache[hash];
85
86        while(node != NULL)
87        {
88                if(serviceid == node->serviceid && transponderid == node->transponderid)
89                {
90                        if(node == channelcache[hash])
91                                channelcache[hash] = node->next;
92                        else
93                                prev->next = node->next;
94
95                        free(node);
96                        node = NULL;
97                        break;
98                }
99
100                prev = node;
101                node = node->next;
102        }
103}
104
105#endif
Note: See TracBrowser for help on using the repository browser.