Reduce C&P code by factoring out the uniquifying string pool.
[platform/upstream/libsolv.git] / src / pool.h
1 /*
2  * Copyright (c) 2007, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * pool.h
10  * 
11  */
12
13 #ifndef POOL_H
14 #define POOL_H
15
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19
20 #include "pooltypes.h"
21 #include "poolid.h"
22 #include "repo.h"
23 #include "solvable.h"
24 #include "queue.h"
25 #include "strpool.h"
26
27 // see initpool_data[] in pool.c
28
29 /* well known ids */
30 #define ID_NULL                 STRID_NULL
31 #define ID_EMPTY                STRID_EMPTY
32 #define SOLVABLE_NAME           2
33 #define SOLVABLE_ARCH           3
34 #define SOLVABLE_EVR            4
35 #define SOLVABLE_VENDOR         5
36 #define SOLVABLE_PROVIDES       6
37 #define SOLVABLE_OBSOLETES      7
38 #define SOLVABLE_CONFLICTS      8
39 #define SOLVABLE_REQUIRES       9
40 #define SOLVABLE_RECOMMENDS     10
41 #define SOLVABLE_SUGGESTS       11
42 #define SOLVABLE_SUPPLEMENTS    12
43 #define SOLVABLE_ENHANCES       13
44 #define SOLVABLE_FRESHENS       14
45 #define RPM_RPMDBID             15
46 #define SOLVABLE_PREREQMARKER   16              // normal requires before this, prereqs after this
47 #define SOLVABLE_FILEMARKER     17              // normal provides before this, generated file provides after this
48 #define NAMESPACE_INSTALLED     18
49 #define NAMESPACE_MODALIAS      19
50 #define SYSTEM_SYSTEM           20
51 #define ARCH_SRC                21
52 #define ARCH_NOSRC              22
53 #define ARCH_NOARCH             23
54
55 /* well known solvable */
56 #define SYSTEMSOLVABLE          1
57
58
59 /* how many strings to maintain (round robin) */
60 #define DEP2STRBUF 16
61
62 //-----------------------------------------------
63
64 struct _Pool {
65   int verbose;          // pool is used everywhere, so put the verbose flag here
66
67   struct _Stringpool ss;
68
69   Reldep *rels;               // table of rels: Id -> Reldep
70   int nrels;                  // number of unique rels
71   Hashtable relhashtbl;       // hash table: (name,evr,op ->) Hash -> Id
72   Hashmask relhashmask;
73
74   Repo **repos;
75   int nrepos;
76
77   Solvable *solvables;
78   int nsolvables;
79
80   int promoteepoch;             /* 0/1  */
81
82   Id *id2arch;                  /* map arch ids to scores */
83   Id lastarch;                  /* last valid entry in id2arch */
84   Queue vendormap;              /* map vendor to vendorclasses mask */
85
86   /* providers data, as two-step indirect list
87    * whatprovides[Id] -> Offset into whatprovidesdata for name
88    * whatprovidesdata[Offset] -> ID_NULL-terminated list of solvables providing Id
89    */
90   Offset *whatprovides;         /* Offset to providers of a specific name, Id -> Offset  */
91   Id *whatprovidesdata;         /* Ids of solvable providing Id */
92   Offset whatprovidesdataoff;   /* next free slot within whatprovidesdata */
93   int whatprovidesdataleft;     /* number of 'free slots' within whatprovidesdata */
94
95   Id (*nscallback)(struct _Pool *, void *data, Id name, Id evr);
96   void *nscallbackdata;
97
98   /* our dep2str string space */
99   char *dep2strbuf[DEP2STRBUF];
100   int   dep2strlen[DEP2STRBUF];
101   int   dep2strn;
102 };
103
104 #define TYPE_ID                 1
105 #define TYPE_IDARRAY            2
106 #define TYPE_STR                3
107 #define TYPE_U32                4
108 #define TYPE_BITMAP             128
109
110
111 //-----------------------------------------------
112
113 // whatprovides
114 //  "foo" -> Id -> lookup in table, returns offset into whatprovidesdata where array of Ids providing 'foo' are located, 0-terminated
115
116
117 #define GET_PROVIDESP(d, v) (ISRELDEP(d) ?                              \
118              (v = GETRELID(pool, d), pool->whatprovides[v] ?            \
119                pool->whatprovidesdata + pool->whatprovides[v] :         \
120                pool_addrelproviders(pool, d)                            \
121              ) :                                                        \
122              (pool->whatprovidesdata + pool->whatprovides[d]))
123
124 /* loop over all providers of d */
125 #define FOR_PROVIDES(v, vp, d)                                          \
126   for (vp = GET_PROVIDESP(d, v) ; (v = *vp++) != ID_NULL; )
127
128 /* mark dependencies with relation by setting bit31 */
129
130 #define MAKERELDEP(id) ((id) | 0x80000000)
131 #define ISRELDEP(id) (((id) & 0x80000000) != 0)
132 #define GETRELID(pool, id) ((pool)->ss.nstrings + ((id) ^ 0x80000000))     /* returns Id  */
133 #define GETRELDEP(pool, id) ((pool)->rels + ((id) ^ 0x80000000))        /* returns Reldep* */
134
135 #define REL_GT          1
136 #define REL_EQ          2
137 #define REL_LT          4
138
139 #define REL_AND         16
140 #define REL_OR          17
141 #define REL_WITH        18
142 #define REL_NAMESPACE   19
143
144 /**
145  * Creates a new pool
146  */
147 extern Pool *pool_create(void);
148 /**
149  * Delete a pool
150  */
151 extern void pool_free(Pool *pool);
152 /**
153  * Prepares a pool for solving
154  */
155 extern void pool_prepare(Pool *pool);
156 extern void pool_freewhatprovides(Pool *pool);
157 extern Id pool_queuetowhatprovides(Pool *pool, Queue *q);
158
159 extern Id *pool_addrelproviders(Pool *pool, Id d);
160
161 static inline int pool_installable(Pool *pool, Solvable *s)
162 {
163   if (!s->arch || s->arch == ARCH_SRC || s->arch == ARCH_NOSRC)
164     return 0;
165   if (pool->id2arch && (s->arch > pool->lastarch || !pool->id2arch[s->arch]))
166     return 0;
167   return 1;
168 }
169
170 #ifdef __cplusplus
171 }
172 #endif
173
174 #endif /* POOL_H */