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 && pool->whatprovides && 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 && pool->whatprovides && 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->nrels + 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_realloc2(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 "; /* actually not used in dep2str */
181 id2evr(Pool *pool, Id id)
186 rd = GETRELDEP(pool, id);
187 if (ISRELDEP(rd->evr))
189 return pool->ss.stringspace + pool->ss.strings[rd->evr];
193 dep2strlen(Pool *pool, Id id)
199 Reldep *rd = GETRELDEP(pool, id);
200 /* add 2 for parens */
201 l += 2 + dep2strlen(pool, rd->name) + strlen(id2rel(pool, id));
204 return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
208 dep2strcpy(Pool *pool, char *p, Id id, int oldrel)
212 Reldep *rd = GETRELDEP(pool, id);
213 if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH)
214 if (rd->flags == REL_AND || rd->flags == REL_OR || rd->flags == REL_WITH)
215 if (oldrel != rd->flags)
218 dep2strcpy(pool, p, rd->name, rd->flags);
220 strcpy(p, id2rel(pool, id));
222 dep2strcpy(pool, p, rd->evr, rd->flags);
226 dep2strcpy(pool, p, rd->name, rd->flags);
228 if (rd->flags == REL_NAMESPACE)
231 dep2strcpy(pool, p, rd->evr, rd->flags);
235 strcpy(p, id2rel(pool, id));
240 strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
244 dep2str(Pool *pool, Id id)
248 return pool->ss.stringspace + pool->ss.strings[id];
249 p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
250 dep2strcpy(pool, p, id, 0);
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;