1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #define NUM_SLOTS 4096
33 } __attribute__((packed
));
35 static struct slot
*slots
[NUM_SLOTS
];
37 static inline unsigned
38 calc_hash(const char *p
)
45 hash
= (hash
<< 5) + hash
+ *p
++;
50 static inline struct slot
*
51 value_to_slot(char *value
)
53 return (struct slot
*)(value
- offsetof(struct slot
, value
));
56 static struct slot
*slot_alloc(struct slot
*next
, const char *value
)
58 size_t length
= strlen(value
);
59 struct slot
*slot
= malloc(sizeof(*slot
) + length
);
65 memcpy(slot
->value
, value
, length
+ 1);
69 char *str_pool_get(const char *value
)
71 struct slot
**slot_p
, *slot
;
73 slot_p
= &slots
[calc_hash(value
) % NUM_SLOTS
];
74 for (slot
= *slot_p
; slot
!= NULL
; slot
= slot
->next
) {
75 if (strcmp(value
, slot
->value
) == 0 && slot
->ref
< 0xff) {
76 assert(slot
->ref
> 0);
82 slot
= slot_alloc(*slot_p
, value
);
87 char *str_pool_dup(char *value
)
89 struct slot
*slot
= value_to_slot(value
);
91 assert(slot
->ref
> 0);
93 if (slot
->ref
< 0xff) {
97 /* the reference counter overflows above 0xff;
98 duplicate the value, and start with 1 */
99 struct slot
**slot_p
=
100 &slots
[calc_hash(slot
->value
) % NUM_SLOTS
];
101 slot
= slot_alloc(*slot_p
, slot
->value
);
107 void str_pool_put(char *value
)
109 struct slot
**slot_p
, *slot
;
111 slot
= value_to_slot(value
);
112 assert(slot
->ref
> 0);
118 for (slot_p
= &slots
[calc_hash(value
) % NUM_SLOTS
];
120 slot_p
= &(*slot_p
)->next
) {
121 assert(*slot_p
!= NULL
);
124 *slot_p
= slot
->next
;