1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002-2005 Roman Zippel <zippel@linux-m68k.org>
4 * Copyright (C) 2002-2005 Sam Ravnborg <sam@ravnborg.org>
11 #include "hashtable.h"
14 unsigned int strhash(const char *s)
17 unsigned int hash = 2166136261U;
20 hash = (hash ^ *s) * 0x01000193;
24 /* hash table of all parsed Kconfig files */
25 static HASHTABLE_DEFINE(file_hashtable, 1U << 11);
28 struct hlist_node node;
32 /* file already present in list? If not add it */
33 const char *file_lookup(const char *name)
37 int hash = strhash(name);
39 hash_for_each_possible(file_hashtable, file, node, hash)
40 if (!strcmp(name, file->name))
44 file = xmalloc(sizeof(*file) + len + 1);
45 memset(file, 0, sizeof(*file));
46 memcpy(file->name, name, len);
47 file->name[len] = '\0';
49 hash_add(file_hashtable, &file->node, hash);
51 str_printf(&autoconf_cmd, "\t%s \\\n", name);
56 /* Allocate initial growable string */
57 struct gstr str_new(void)
60 gs.s = xmalloc(sizeof(char) * 64);
67 /* Free storage for growable string */
68 void str_free(struct gstr *gs)
75 /* Append to growable string */
76 void str_append(struct gstr *gs, const char *s)
80 l = strlen(gs->s) + strlen(s) + 1;
82 gs->s = xrealloc(gs->s, l);
89 /* Append printf formatted string to growable string */
90 void str_printf(struct gstr *gs, const char *fmt, ...)
93 char s[10000]; /* big enough... */
95 vsnprintf(s, sizeof(s), fmt, ap);
100 /* Retrieve value of growable string */
101 char *str_get(struct gstr *gs)
106 void *xmalloc(size_t size)
108 void *p = malloc(size);
111 fprintf(stderr, "Out of memory.\n");
115 void *xcalloc(size_t nmemb, size_t size)
117 void *p = calloc(nmemb, size);
120 fprintf(stderr, "Out of memory.\n");
124 void *xrealloc(void *p, size_t size)
126 p = realloc(p, size);
129 fprintf(stderr, "Out of memory.\n");
133 char *xstrdup(const char *s)
140 fprintf(stderr, "Out of memory.\n");
144 char *xstrndup(const char *s, size_t n)
151 fprintf(stderr, "Out of memory.\n");