4 * The pool contains information about solvables
5 * stored optimized for memory consumption and fast retrieval.
15 #include "poolid_private.h"
21 // reset all whatprovides
24 pool_freewhatprovides(Pool *pool)
26 pool->whatprovides = xfree(pool->whatprovides);
27 pool->whatprovidesdata = xfree(pool->whatprovidesdata);
28 pool->whatprovidesdataoff = 0;
29 pool->whatprovidesdataleft = 0;
33 // list of string constants, so we can do pointer/Id instead of string comparison
34 // index into array matches ID_xxx constants in pool.h
36 static char *initpool_data[] = {
47 "solvable:recommends",
49 "solvable:supplements",
52 "rpm:dbid", /* direct key into rpmdb */
53 "solvable:prereqmarker",
54 "solvable:filemarker",
55 "namespace:installed",
68 int count, totalsize = 0;
72 pool = (Pool *)xcalloc(1, sizeof(*pool));
74 // count number and total size of predefined strings
75 for (count = 0; count < sizeof(initpool_data)/sizeof(*initpool_data); count++)
76 totalsize += strlen(initpool_data[count]) + 1;
78 // alloc appropriate space
79 pool->stringspace = (char *)xmalloc((totalsize + STRINGSPACE_BLOCK) & ~STRINGSPACE_BLOCK);
80 pool->strings = (Offset *)xmalloc(((count + STRING_BLOCK) & ~STRING_BLOCK) * sizeof(Offset));
82 // now copy predefined strings into allocated space
84 for (count = 0; count < sizeof(initpool_data)/sizeof(*initpool_data); count++)
86 strcpy(pool->stringspace + pool->sstrings, initpool_data[count]);
87 pool->strings[count] = pool->sstrings;
88 pool->sstrings += strlen(initpool_data[count]) + 1;
90 pool->nstrings = count;
92 // pre-alloc space for a RelDep
93 pool->rels = (Reldep *)xcalloc(1 + REL_BLOCK, sizeof(Reldep));
96 // pre-alloc space for a Solvable
97 pool->solvables = (Solvable *)xcalloc(2, sizeof(Solvable));
99 s = pool->solvables + SYSTEMSOLVABLE;
100 s->name = SYSTEM_SYSTEM;
101 s->arch = ARCH_NOARCH;
110 pool_free(Pool *pool)
115 pool_freewhatprovides(pool);
116 pool_freeidhashes(pool);
117 for (i = 0; i < pool->nrepos; i++)
119 repo = pool->repos[i];
120 xfree(repo->idarraydata);
121 xfree(repo->rpmdbid);
124 xfree(pool->id2arch);
125 xfree(pool->solvables);
127 xfree(pool->stringspace);
128 xfree(pool->strings);
130 for (i = 0; i < DEP2STRBUF; i++)
131 xfree(pool->dep2strbuf[i]);
136 static Pool *pool_shrink_whatprovides_sortcmp_data;
139 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp)
142 Pool *pool = pool_shrink_whatprovides_sortcmp_data;
144 oa = pool->whatprovides[*(Id *)ap];
145 ob = pool->whatprovides[*(Id *)bp];
147 return *(Id *)ap - *(Id *)bp;
152 da = pool->whatprovidesdata + oa;
153 db = pool->whatprovidesdata + ob;
155 if ((r = (*da++ - *db++)) != 0)
159 return *(Id *)ap - *(Id *)bp;
163 pool_shrink_whatprovides(Pool *pool)
167 Id lastid, *last, *dp, *lp;
171 if (pool->nstrings < 3)
173 sorted = xmalloc2(pool->nstrings, sizeof(Id));
174 for (id = 0; id < pool->nstrings; id++)
176 pool_shrink_whatprovides_sortcmp_data = pool;
177 qsort(sorted + 1, pool->nstrings - 1, sizeof(Id), pool_shrink_whatprovides_sortcmp);
180 for (i = 1; i < pool->nstrings; i++)
183 o = pool->whatprovides[id];
184 if (o == 0 || o == 1)
186 dp = pool->whatprovidesdata + o;
200 pool->whatprovides[id] = -lastid;
204 last = pool->whatprovidesdata + o;
208 dp = pool->whatprovidesdata + 2;
209 for (id = 1; id < pool->nstrings; id++)
211 o = pool->whatprovides[id];
212 if (o == 0 || o == 1)
219 pool->whatprovides[id] = pool->whatprovides[i];
222 lp = pool->whatprovidesdata + o;
225 pool->whatprovides[id] = dp - pool->whatprovidesdata;
226 while ((*dp++ = *lp++) != 0)
229 o = dp - pool->whatprovidesdata;
231 printf("shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
232 if (pool->whatprovidesdataoff == o)
234 r = pool->whatprovidesdataoff - o;
235 pool->whatprovidesdataoff = o;
236 pool->whatprovidesdata = xrealloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
237 if (r > pool->whatprovidesdataleft)
238 r = pool->whatprovidesdataleft;
239 memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
246 * create hashes over complete pool to ease lookups
251 pool_prepare(Pool *pool)
253 int i, num, np, extra;
258 Offset *whatprovides;
259 Id *whatprovidesdata, *d;
262 printf("number of solvables: %d\n", pool->nsolvables);
264 printf("number of ids: %d + %d\n", pool->nstrings, pool->nrels);
266 pool_freeidhashes(pool);
267 pool_freewhatprovides(pool);
268 num = pool->nstrings + pool->nrels;
269 whatprovides = (Offset *)xcalloc(num, sizeof(Offset));
271 /* count providers for each name */
272 for (i = 1; i < pool->nsolvables; i++)
275 s = pool->solvables + i;
278 if (!pool_installable(pool, s))
280 pp = s->repo->idarraydata + s->provides;
281 while ((id = *pp++) != ID_NULL)
285 Reldep *rd = GETRELDEP(pool, id);
288 whatprovides[id]++; /* inc count of providers */
292 off = 2; /* first entry is undef, second is empty list */
294 np = 0; /* number of names provided */
295 for (i = 0; i < num; i++, idp++)
298 if (!n) /* no providers */
300 *idp = off; /* move from counts to offsets into whatprovidesdata */
301 off += n + 1; /* make space for all providers + terminating ID_NULL */
302 np++; /* inc # of provider 'slots' */
306 printf("provide ids: %d\n", np);
307 extra = 2 * pool->nrels;
313 printf("provide space needed: %d + %d\n", off, extra);
315 /* alloc space for all providers + extra */
316 whatprovidesdata = (Id *)xcalloc(off + extra, sizeof(Id));
318 /* now fill data for all provides */
319 for (i = 1; i < pool->nsolvables; i++)
322 s = pool->solvables + i;
325 if (!pool_installable(pool, s))
328 /* for all provides of this solvable */
329 pp = s->repo->idarraydata + s->provides;
330 while ((id = *pp++) != 0)
334 Reldep *rd = GETRELDEP(pool, id);
337 d = whatprovidesdata + whatprovides[id]; /* offset into whatprovidesdata */
341 while (*d) /* find free slot */
346 if (pool->verbose) printf("duplicate entry for %s in package %s.%s\n", id2str(pool, id), id2str(pool, s->name), id2str(pool, s->arch));
351 *d = i; /* put solvable Id into data */
354 pool->whatprovides = whatprovides;
355 pool->whatprovidesdata = whatprovidesdata;
356 pool->whatprovidesdataoff = off;
357 pool->whatprovidesdataleft = extra;
358 pool_shrink_whatprovides(pool);
362 /******************************************************************************/
365 * pool_queuetowhatprovides
367 * on-demand filling of provider information
368 * move queue data into whatprovidesdata
370 * returns: Offset into whatprovides
374 pool_queuetowhatprovides(Pool *pool, Queue *q)
377 int count = q->count;
379 if (count == 0) /* queue empty -> ID_EMPTY */
382 /* extend whatprovidesdata if needed, +1 for ID_NULL-termination */
383 if (pool->whatprovidesdataleft < count + 1)
386 printf("growing provides hash data...\n");
387 pool->whatprovidesdata = (Id *)xrealloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
388 pool->whatprovidesdataleft = count + 4096;
391 /* copy queue to next free slot */
392 off = pool->whatprovidesdataoff;
393 memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
395 /* adapt count and ID_NULL-terminate */
396 pool->whatprovidesdataoff += count;
397 pool->whatprovidesdata[pool->whatprovidesdataoff++] = ID_NULL;
398 pool->whatprovidesdataleft -= count + 1;
404 /******************************************************************************/
409 * add packages fulfilling the relation to whatprovides array
410 * no exact providers, do range match
415 pool_addrelproviders(Pool *pool, Id d)
417 Reldep *rd = GETRELDEP(pool, d);
423 int flags = rd->flags;
425 Id p, *pp, *pp2, *pp3;
427 d = GETRELID(pool, d);
428 queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
433 pp = GET_PROVIDESP(name, p);
434 pp2 = GET_PROVIDESP(evr, p);
435 while ((p = *pp++) != 0)
437 for (pp3 = pp2; *pp3;)
440 queue_push(&plist, p);
446 pp = GET_PROVIDESP(name, p);
447 while ((p = *pp++) != 0)
448 queue_push(&plist, p);
449 pp = GET_PROVIDESP(evr, p);
450 while ((p = *pp++) != 0)
451 queue_pushunique(&plist, p);
454 if (pool->nscallback)
456 p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
460 pool->whatprovides[d] = p;
461 return pool->whatprovidesdata + p;
464 queue_push(&plist, SYSTEMSOLVABLE);
471 /* convert to whatprovides id */
474 printf("addrelproviders: what provides %s?\n", id2str(pool, name));
476 if (flags && flags < 8)
478 FOR_PROVIDES(p, pp, name)
482 printf("addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
484 /* solvable p provides name in some rels */
485 pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
486 while ((pid = *pidp++) != 0)
492 break; /* yes, provides all versions */
494 continue; /* wrong provides name */
495 prd = GETRELDEP(pool, pid);
496 if (prd->name != name)
497 continue; /* wrong provides name */
498 /* right package, both deps are rels */
502 if (flags == 7 || pflags == 7)
503 break; /* included */
504 if ((pflags & flags & 5) != 0)
505 break; /* same direction, match */
509 if ((pflags & flags & 2) != 0)
510 break; /* both have =, match */
514 int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
515 if ((f & (1 << (1 + evrcmp(pool, pevr, evr)))) != 0)
520 continue; /* no rel match */
521 queue_push(&plist, p);
523 /* make our system solvable provide all unknown rpmlib() stuff */
524 if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
525 queue_push(&plist, SYSTEMSOLVABLE);
527 /* add providers to whatprovides */
529 if (pool->verbose) printf("addrelproviders: adding %d packages to %d\n", plist.count, d);
531 pool->whatprovides[d] = pool_queuetowhatprovides(pool, &plist);
534 return pool->whatprovidesdata + pool->whatprovides[d];