1 /*
2 This file is part of Deadbeef Player source code
3 http://deadbeef.sourceforge.net
4
5 metadata string cache/database
6
7 Copyright (C) 2009-2013 Alexey Yakovenko
8
9 This software is provided 'as-is', without any express or implied
10 warranty. In no event will the authors be held liable for any damages
11 arising from the use of this software.
12
13 Permission is granted to anyone to use this software for any purpose,
14 including commercial applications, and to alter it and redistribute it
15 freely, subject to the following restrictions:
16
17 1. The origin of this software must not be misrepresented; you must not
18 claim that you wrote the original software. If you use this software
19 in a product, an acknowledgment in the product documentation would be
20 appreciated but is not required.
21 2. Altered source versions must be plainly marked as such, and must not be
22 misrepresented as being the original software.
23 3. This notice may not be removed or altered from any source distribution.
24
25 Alexey Yakovenko waker@users.sourceforge.net
26 */
27 #include <string.h>
28 #include <stdint.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 typedef struct metacache_str_s {
33 struct metacache_str_s *next;
34 uint32_t refcount;
35 char cmpidx; // positive means "equals", negative means "notequals"
36 char str[1];
37 } metacache_str_t;
38
39 typedef struct {
40 // uint32_t hash;
41 metacache_str_t *chain;
42 } metacache_hash_t;
43
44 #define HASH_SIZE 4096
45
46 static metacache_hash_t hash[HASH_SIZE];
47
48 uint32_t
metacache_get_hash_sdbm(const char * str)49 metacache_get_hash_sdbm (const char *str) {
50 uint32_t hash = 0;
51 int c;
52
53 while (c = *str++)
54 hash = c + (hash << 6) + (hash << 16) - hash;
55
56 return hash;
57 }
58
59 metacache_str_t *
metacache_find_in_bucket(uint32_t h,const char * str)60 metacache_find_in_bucket (uint32_t h, const char *str) {
61 metacache_hash_t *bucket = &hash[h];
62 metacache_str_t *chain = bucket->chain;
63 while (chain) {
64 if (!strcmp (chain->str, str)) {
65 return chain;
66 }
67 chain = chain->next;
68 }
69 return NULL;
70 }
71
72 static int n_strings = 0;
73 static int n_inserts = 0;
74 static int n_buckets = 0;
75
76 const char *
metacache_add_string(const char * str)77 metacache_add_string (const char *str) {
78 // printf ("n_strings=%d, n_inserts=%d, n_buckets=%d\n", n_strings, n_inserts, n_buckets);
79 uint32_t h = metacache_get_hash_sdbm (str);
80 metacache_str_t *data = metacache_find_in_bucket (h % HASH_SIZE, str);
81 n_inserts++;
82 if (data) {
83 data->refcount++;
84 return data->str;
85 }
86 metacache_hash_t *bucket = &hash[h % HASH_SIZE];
87 if (!bucket->chain) {
88 n_buckets++;
89 }
90 size_t len = strlen (str);
91 data = malloc (sizeof (metacache_str_t) + len);
92 memset (data, 0, sizeof (metacache_str_t) + len);
93 data->refcount = 1;
94 memcpy (data->str, str, len+1);
95 data->next = bucket->chain;
96 bucket->chain = data;
97 n_strings++;
98 return data->str;
99 }
100
101 void
metacache_remove_string(const char * str)102 metacache_remove_string (const char *str) {
103 uint32_t h = metacache_get_hash_sdbm (str);
104 metacache_hash_t *bucket = &hash[h % HASH_SIZE];
105 metacache_str_t *chain = bucket->chain;
106 metacache_str_t *prev = NULL;
107 while (chain) {
108 if (!strcmp (chain->str, str)) {
109 chain->refcount--;
110 if (chain->refcount == 0) {
111 if (prev) {
112 prev->next = chain->next;
113 }
114 else {
115 bucket->chain = chain->next;
116 }
117 free (chain);
118 }
119 break;
120 }
121 prev = chain;
122 chain = chain->next;
123 }
124 }
125
126 void
metacache_ref(const char * str)127 metacache_ref (const char *str) {
128 uint32_t *refc = (uint32_t *)(str-5);
129 *refc++;
130 }
131
132 void
metacache_unref(const char * str)133 metacache_unref (const char *str) {
134 uint32_t *refc = (uint32_t *)(str-5);
135 *refc--;
136 }
137