2 (c) 2003-2008 The Music Player Daemon Project
3 This project's homepage is: http://www.musicpd.org
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
9 - Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 - Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
20 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #define NUM_SLOTS 4096
42 } __attribute__((packed
));
44 static struct slot
*slots
[NUM_SLOTS
];
46 static inline unsigned
47 calc_hash(const char *p
)
54 hash
= (hash
<< 5) + hash
+ *p
++;
59 static inline struct slot
*
60 value_to_slot(char *value
)
62 return (struct slot
*)(value
- offsetof(struct slot
, value
));
65 static struct slot
*slot_alloc(struct slot
*next
, const char *value
)
67 size_t length
= strlen(value
);
68 struct slot
*slot
= malloc(sizeof(*slot
) + length
);
74 memcpy(slot
->value
, value
, length
+ 1);
78 char *str_pool_get(const char *value
)
80 struct slot
**slot_p
, *slot
;
82 slot_p
= &slots
[calc_hash(value
) % NUM_SLOTS
];
83 for (slot
= *slot_p
; slot
!= NULL
; slot
= slot
->next
) {
84 if (strcmp(value
, slot
->value
) == 0 && slot
->ref
< 0xff) {
85 assert(slot
->ref
> 0);
91 slot
= slot_alloc(*slot_p
, value
);
96 char *str_pool_dup(char *value
)
98 struct slot
*slot
= value_to_slot(value
);
100 assert(slot
->ref
> 0);
102 if (slot
->ref
< 0xff) {
106 /* the reference counter overflows above 0xff;
107 duplicate the value, and start with 1 */
108 struct slot
**slot_p
=
109 &slots
[calc_hash(slot
->value
) % NUM_SLOTS
];
110 slot
= slot_alloc(*slot_p
, slot
->value
);
116 void str_pool_put(char *value
)
118 struct slot
**slot_p
, *slot
;
120 slot
= value_to_slot(value
);
121 assert(slot
->ref
> 0);
127 for (slot_p
= &slots
[calc_hash(value
) % NUM_SLOTS
];
129 slot_p
= &(*slot_p
)->next
) {
130 assert(*slot_p
!= NULL
);
133 *slot_p
= slot
->next
;