2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
20 #include "poolid_private.h"
24 /* intern string into pool, return id */
27 str2id(Pool *pool, const char *str, int create)
29 int oldnstrings = pool->ss.nstrings;
30 Id id = stringpool_str2id (&pool->ss, str, create);
31 if (create && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
33 /* grow whatprovides array */
34 pool->whatprovides = sat_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
35 memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
41 strn2id(Pool *pool, const char *str, unsigned int len, int create)
43 int oldnstrings = pool->ss.nstrings;
44 Id id = stringpool_strn2id (&pool->ss, str, len, create);
45 if (create && oldnstrings != pool->ss.nstrings && (id & WHATPROVIDES_BLOCK) == 0)
47 /* grow whatprovides array */
48 pool->whatprovides = sat_realloc(pool->whatprovides, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
49 memset(pool->whatprovides + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
55 rel2id(Pool *pool, Id name, Id evr, int flags, int create)
65 hashmask = pool->relhashmask;
66 hashtbl = pool->relhashtbl;
69 /* extend hashtable if needed */
70 if (pool->nrels * 2 > hashmask)
72 sat_free(pool->relhashtbl);
73 pool->relhashmask = hashmask = mkmask(pool->ss.nstrings + REL_BLOCK);
74 pool->relhashtbl = hashtbl = sat_calloc(hashmask + 1, sizeof(Id));
75 // rehash all rels into new hashtable
76 for (i = 1; i < pool->nrels; i++)
78 h = relhash(ran[i].name, ran[i].evr, ran[i].flags) & hashmask;
81 h = HASHCHAIN_NEXT(h, hh, hashmask);
86 /* compute hash and check for match */
87 h = relhash(name, evr, flags) & hashmask;
89 while ((id = hashtbl[h]) != 0)
91 if (ran[id].name == name && ran[id].evr == evr && ran[id].flags == flags)
93 h = HASHCHAIN_NEXT(h, hh, hashmask);
96 return MAKERELDEP(id);
102 /* extend rel space if needed */
103 pool->rels = sat_extend(pool->rels, id, 1, sizeof(Reldep), REL_BLOCK);
105 ran = pool->rels + id;
110 /* extend whatprovides_rel if needed */
111 if (pool->whatprovides_rel && (id & WHATPROVIDES_BLOCK) == 0)
113 pool->whatprovides_rel = sat_realloc(pool->whatprovides_rel, (id + (WHATPROVIDES_BLOCK + 1)) * sizeof(Offset));
114 memset(pool->whatprovides_rel + id, 0, (WHATPROVIDES_BLOCK + 1) * sizeof(Offset));
116 return MAKERELDEP(id);
121 // for rels (returns name only) and strings
124 id2str(Pool *pool, Id id)
128 Reldep *rd = GETRELDEP(pool, id);
129 if (ISRELDEP(rd->name))
131 return pool->ss.stringspace + pool->ss.strings[rd->name];
133 return pool->ss.stringspace + pool->ss.strings[id];
136 static const char *rels[] = {
148 // get operator for RelId
150 id2rel(Pool *pool, Id id)
155 rd = GETRELDEP(pool, id);
158 case 0: case 1: case 2: case 3:
159 case 4: case 5: case 6: case 7:
160 return rels[rd->flags & 7];
168 return " NAMESPACE ";
179 id2evr(Pool *pool, Id id)
184 rd = GETRELDEP(pool, id);
185 if (ISRELDEP(rd->evr))
187 return pool->ss.stringspace + pool->ss.strings[rd->evr];
191 dep2str(Pool *pool, Id id)
196 int n, l, ls1, ls2, lsr;
199 return pool->ss.stringspace + pool->ss.strings[id];
200 rd = GETRELDEP(pool, id);
203 sr = id2rel(pool, id);
206 s2 = (char *)dep2str(pool, rd->evr);
210 s1 = (char *)dep2str(pool, rd->name);
214 if (rd->flags == REL_NAMESPACE)
222 if (l + 1 > pool->dep2strlen[n])
224 if (s1 != pool->dep2strbuf[n])
225 pool->dep2strbuf[n] = sat_realloc(pool->dep2strbuf[n], l + 32);
228 pool->dep2strbuf[n] = sat_realloc(pool->dep2strbuf[n], l + 32);
229 s1 = pool->dep2strbuf[n];
231 pool->dep2strlen[n] = l + 32;
233 if (s1 != pool->dep2strbuf[n])
235 strcpy(pool->dep2strbuf[n], s1);
236 s1 = pool->dep2strbuf[n];
238 strcpy(s1 + ls1, sr);
239 pool->dep2strbuf[n] = s1 + ls1 + lsr;
240 s2 = (char *)dep2str(pool, rd->evr);
241 if (s2 != pool->dep2strbuf[n])
242 strcpy(pool->dep2strbuf[n], s2);
243 pool->dep2strbuf[n] = s1;
244 if (rd->flags == REL_NAMESPACE)
246 s1[ls1 + ls2 + lsr - 1] = ')';
247 s1[ls1 + ls2 + lsr] = 0;
249 pool->dep2strn = (n + 1) % DEP2STRBUF;
255 pool_shrink_strings(Pool *pool)
257 stringpool_shrink(&pool->ss);
261 pool_shrink_rels(Pool *pool)
263 pool->rels = sat_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
266 // reset all hash tables
269 pool_freeidhashes(Pool *pool)
271 pool->ss.stringhashtbl = sat_free(pool->ss.stringhashtbl);
272 pool->ss.stringhashmask = 0;
273 pool->relhashtbl = sat_free(pool->relhashtbl);
274 pool->relhashmask = 0;