2 * Copyright (C) 2013 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
33 #ifndef INITIAL_SCRATCH_CAPACITY
34 #define INITIAL_SCRATCH_CAPACITY 240
37 #if INITIAL_SCRATCH_CAPACITY <= 0
38 #error "bad value for INITIAL_SCRATCH_CAPACITY"
43 #define SET_CAPACITY 97
44 #define HASHCODE_INIT 5381
45 #define HASHCODE_NEXT(H,C) (((H) << 5) + (H) + (C))
46 #ifndef NOT_MULTI_THREAD_SAFE
47 #define NOT_MULTI_THREAD_SAFE
51 #ifndef NOT_MULTI_THREAD_SAFE
53 static pthread_key_t tlskey;
54 static int key_initialized = 0;
56 static void *global_scratch = NULL;
60 /* structure for recording items in the hash map */
62 struct set_item *next; /* chain to next item */
63 size_t hashcode; /* hash of the string */
64 size_t length; /* length of the string including null */
67 /* the array of recorded strings */
68 static struct set_item *global_path_set[SET_CAPACITY]; /* initialized to zeros */
70 /* instanciate (or retrieve an instance) of the 'string' that
71 is granted to have the 'length' including terminating null and a
72 hash code 'hashcode' */
73 static const char *instantiate(const char *string, size_t length, size_t hashcode)
75 struct set_item **pp, *item;
78 /* get first item in the table */
79 pp = &global_path_set[hashcode % SET_CAPACITY];
82 /* inspect the item */
85 /* no item: create it */
86 item = malloc(length + sizeof * item);
91 item->hashcode = hashcode;
92 item->length = length;
93 result = (char *)(item + 1);
94 memcpy(result, string, length);
97 } else if (item->hashcode == hashcode
98 && item->length == length
99 && 0 == strcmp(string, (const char *)(item + 1))) {
101 result = (char *)(item + 1);
112 /* CAUTION: in a multitheaded context, it is expected that
113 =========== the function scratchcat is call under a mutex.
114 If it is not the case please check for initializing 'tlskey'
115 only one time before use of it. */
117 const char *scratchcat( int ispath, const char **strings)
121 size_t length, capacity;
125 size_t hashcode = HASHCODE_INIT;
128 /* get the recorded pointer on scrtch area */
129 #ifndef NOT_MULTI_THREAD_SAFE
130 if (!key_initialized) {
132 pthread_key_create( &tlskey, (void(*)(void*))free);
134 scratch = pthread_getspecific( tlskey);
136 scratch = global_scratch;
139 /* create the scratch area if needed */
140 if (scratch == NULL) {
141 capacity = INITIAL_SCRATCH_CAPACITY;
142 p = malloc( capacity + sizeof(size_t));
145 *((size_t*)p) = capacity;
147 #ifndef NOT_MULTI_THREAD_SAFE
148 pthread_setspecific( tlskey, p);
154 /* set local data for scratch area */
155 capacity = *((size_t*)scratch);
156 result = (char*)(1+((size_t*)scratch));
159 /* copy the strings */
172 else if(c != '/' && pc != '/')
174 else if(c == '/' && pc == '/') {
189 /* extend the scratch area if needed */
190 if (length == capacity) {
191 capacity = 2 * capacity;
192 p = realloc( scratch, capacity + sizeof(size_t));
195 *((size_t*)p) = capacity;
198 #ifndef NOT_MULTI_THREAD_SAFE
199 pthread_setspecific( tlskey, p);
203 result = (char*)(1+((size_t*)p));
207 /* append the char */
208 pc = result[length++] = c;
210 hashcode = HASHCODE_NEXT(hashcode, (size_t)c);
215 return instantiate(result, length, hashcode);
224 int main(int argc, const char**argv) {
227 ispath = argv[0] && argv[0][0] == '-' && argv[0][1] == 'p';
228 for (i = 0 ; i < 2 ; i++) {
230 while (iter < argc) {
231 const char *p = scratchcat(ispath,argv+iter++);
232 printf("%p: %s\n",p,p);