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 ";
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 dep2strlen(Pool *pool, Id id)
197 Reldep *rd = GETRELDEP(pool, id);
198 l += dep2strlen(pool, rd->name) + strlen(id2rel(pool, id));
201 return l + strlen(pool->ss.stringspace + pool->ss.strings[id]);
205 dep2strcpy(Pool *pool, char *p, Id id)
209 Reldep *rd = GETRELDEP(pool, id);
210 dep2strcpy(pool, p, rd->name);
212 if (rd->flags == REL_NAMESPACE)
215 dep2strcpy(pool, p, rd->evr);
219 strcpy(p, id2rel(pool, id));
223 strcpy(p, pool->ss.stringspace + pool->ss.strings[id]);
227 dep2str(Pool *pool, Id id)
231 return pool->ss.stringspace + pool->ss.strings[id];
232 p = pool_alloctmpspace(pool, dep2strlen(pool, id) + 1);
233 dep2strcpy(pool, p, id);
238 pool_shrink_strings(Pool *pool)
240 stringpool_shrink(&pool->ss);
244 pool_shrink_rels(Pool *pool)
246 pool->rels = sat_extend_resize(pool->rels, pool->nrels, sizeof(Reldep), REL_BLOCK);
249 // reset all hash tables
252 pool_freeidhashes(Pool *pool)
254 pool->ss.stringhashtbl = sat_free(pool->ss.stringhashtbl);
255 pool->ss.stringhashmask = 0;
256 pool->relhashtbl = sat_free(pool->relhashtbl);
257 pool->relhashmask = 0;