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 */
171 case REL_FILECONFLICT:
172 return " FILECONFLICT ";
183 id2evr(Pool *pool, Id id)
188 rd = GETRELDEP(pool, id);
189 if (ISRELDEP(rd->evr))
191 return pool->ss.stringspace + pool->ss.strings[rd->evr];
195 dep2strlen(Pool *pool, Id id)
201 Reldep *rd = GETRELDEP(pool, id);
202 /* add 2 for parens */
203 l += 2 + dep2strlen(pool, rd->name) + strlen(id2rel(pool, id));
206 return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
210 dep2strcpy(Pool *pool, char *p, Id id, int oldrel)
214 Reldep *rd = GETRELDEP(pool, id);
215 if (oldrel == REL_AND || oldrel == REL_OR || oldrel == REL_WITH)
216 if (rd->flags == REL_AND || rd->flags == REL_OR || rd->flags == REL_WITH)
217 if (oldrel != rd->flags)
220 dep2strcpy(pool, p, rd->name, rd->flags);
222 strcpy(p, id2rel(pool, id));
224 dep2strcpy(pool, p, rd->evr, rd->flags);
228 dep2strcpy(pool, p, rd->name, rd->flags);
230 if (rd->flags == REL_NAMESPACE)
233 dep2strcpy(pool, p, rd->evr, rd->flags);
237 if (rd->flags == REL_FILECONFLICT)
242 strcpy(p, id2rel(pool, id));
247 strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
251 dep2str(Pool *pool, Id id)
255 return pool->ss.stringspace + pool->ss.strings[id];
256 p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
257 dep2strcpy(pool, p, id, 0);
262 pool_shrink_strings(Pool *pool)
264 stringpool_shrink(&pool->ss);
268 pool_shrink_rels(Pool *pool)
270 pool->rels = sat_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
273 // reset all hash tables
276 pool_freeidhashes(Pool *pool)
278 pool->ss.stringhashtbl = sat_free(pool->ss.stringhashtbl);
279 pool->ss.stringhashmask = 0;
280 pool->relhashtbl = sat_free(pool->relhashtbl);
281 pool->relhashmask = 0;