]> git.sur5r.net Git - openldap/blob - libraries/liblmdb/midl.c
ITS#7775 Drop <assert.h>, drop/replace assert()s.
[openldap] / libraries / liblmdb / midl.c
1 /**     @file midl.c
2  *      @brief ldap bdb back-end ID List functions */
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 2000-2013 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17
18 #include <limits.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include "midl.h"
24
25 /** @defgroup internal  MDB Internals
26  *      @{
27  */
28 /** @defgroup idls      ID List Management
29  *      @{
30  */
31 #define CMP(x,y)         ( (x) < (y) ? -1 : (x) > (y) )
32
33 unsigned mdb_midl_search( MDB_IDL ids, MDB_ID id )
34 {
35         /*
36          * binary search of id in ids
37          * if found, returns position of id
38          * if not found, returns first position greater than id
39          */
40         unsigned base = 0;
41         unsigned cursor = 1;
42         int val = 0;
43         unsigned n = ids[0];
44
45         while( 0 < n ) {
46                 unsigned pivot = n >> 1;
47                 cursor = base + pivot + 1;
48                 val = CMP( ids[cursor], id );
49
50                 if( val < 0 ) {
51                         n = pivot;
52
53                 } else if ( val > 0 ) {
54                         base = cursor;
55                         n -= pivot + 1;
56
57                 } else {
58                         return cursor;
59                 }
60         }
61
62         if( val > 0 ) {
63                 ++cursor;
64         }
65         return cursor;
66 }
67
68 #if 0   /* superseded by append/sort */
69 int mdb_midl_insert( MDB_IDL ids, MDB_ID id )
70 {
71         unsigned x, i;
72
73         x = mdb_midl_search( ids, id );
74         assert( x > 0 );
75
76         if( x < 1 ) {
77                 /* internal error */
78                 return -2;
79         }
80
81         if ( x <= ids[0] && ids[x] == id ) {
82                 /* duplicate */
83                 assert(0);
84                 return -1;
85         }
86
87         if ( ++ids[0] >= MDB_IDL_DB_MAX ) {
88                 /* no room */
89                 --ids[0];
90                 return -2;
91
92         } else {
93                 /* insert id */
94                 for (i=ids[0]; i>x; i--)
95                         ids[i] = ids[i-1];
96                 ids[x] = id;
97         }
98
99         return 0;
100 }
101 #endif
102
103 MDB_IDL mdb_midl_alloc(int num)
104 {
105         MDB_IDL ids = malloc((num+2) * sizeof(MDB_ID));
106         if (ids) {
107                 *ids++ = num;
108                 *ids = 0;
109         }
110         return ids;
111 }
112
113 void mdb_midl_free(MDB_IDL ids)
114 {
115         if (ids)
116                 free(ids-1);
117 }
118
119 int mdb_midl_shrink( MDB_IDL *idp )
120 {
121         MDB_IDL ids = *idp;
122         if (*(--ids) > MDB_IDL_UM_MAX &&
123                 (ids = realloc(ids, (MDB_IDL_UM_MAX+1) * sizeof(MDB_ID))))
124         {
125                 *ids++ = MDB_IDL_UM_MAX;
126                 *idp = ids;
127                 return 1;
128         }
129         return 0;
130 }
131
132 static int mdb_midl_grow( MDB_IDL *idp, int num )
133 {
134         MDB_IDL idn = *idp-1;
135         /* grow it */
136         idn = realloc(idn, (*idn + num + 2) * sizeof(MDB_ID));
137         if (!idn)
138                 return ENOMEM;
139         *idn++ += num;
140         *idp = idn;
141         return 0;
142 }
143
144 int mdb_midl_need( MDB_IDL *idp, unsigned num )
145 {
146         MDB_IDL ids = *idp;
147         num += ids[0];
148         if (num > ids[-1]) {
149                 num = (num + num/4 + (256 + 2)) & -256;
150                 if (!(ids = realloc(ids-1, num * sizeof(MDB_ID))))
151                         return ENOMEM;
152                 *ids++ = num - 2;
153                 *idp = ids;
154         }
155         return 0;
156 }
157
158 int mdb_midl_append( MDB_IDL *idp, MDB_ID id )
159 {
160         MDB_IDL ids = *idp;
161         /* Too big? */
162         if (ids[0] >= ids[-1]) {
163                 if (mdb_midl_grow(idp, MDB_IDL_UM_MAX))
164                         return ENOMEM;
165                 ids = *idp;
166         }
167         ids[0]++;
168         ids[ids[0]] = id;
169         return 0;
170 }
171
172 int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app )
173 {
174         MDB_IDL ids = *idp;
175         /* Too big? */
176         if (ids[0] + app[0] >= ids[-1]) {
177                 if (mdb_midl_grow(idp, app[0]))
178                         return ENOMEM;
179                 ids = *idp;
180         }
181         memcpy(&ids[ids[0]+1], &app[1], app[0] * sizeof(MDB_ID));
182         ids[0] += app[0];
183         return 0;
184 }
185
186 int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n )
187 {
188         MDB_ID *ids = *idp, len = ids[0];
189         /* Too big? */
190         if (len + n > ids[-1]) {
191                 if (mdb_midl_grow(idp, n | MDB_IDL_UM_MAX))
192                         return ENOMEM;
193                 ids = *idp;
194         }
195         ids[0] = len + n;
196         ids += len;
197         while (n)
198                 ids[n--] = id++;
199         return 0;
200 }
201
202 /* Quicksort + Insertion sort for small arrays */
203
204 #define SMALL   8
205 #define MIDL_SWAP(a,b)  { itmp=(a); (a)=(b); (b)=itmp; }
206
207 void
208 mdb_midl_sort( MDB_IDL ids )
209 {
210         /* Max possible depth of int-indexed tree * 2 items/level */
211         int istack[sizeof(int)*CHAR_BIT * 2];
212         int i,j,k,l,ir,jstack;
213         MDB_ID a, itmp;
214
215         ir = (int)ids[0];
216         l = 1;
217         jstack = 0;
218         for(;;) {
219                 if (ir - l < SMALL) {   /* Insertion sort */
220                         for (j=l+1;j<=ir;j++) {
221                                 a = ids[j];
222                                 for (i=j-1;i>=1;i--) {
223                                         if (ids[i] >= a) break;
224                                         ids[i+1] = ids[i];
225                                 }
226                                 ids[i+1] = a;
227                         }
228                         if (jstack == 0) break;
229                         ir = istack[jstack--];
230                         l = istack[jstack--];
231                 } else {
232                         k = (l + ir) >> 1;      /* Choose median of left, center, right */
233                         MIDL_SWAP(ids[k], ids[l+1]);
234                         if (ids[l] < ids[ir]) {
235                                 MIDL_SWAP(ids[l], ids[ir]);
236                         }
237                         if (ids[l+1] < ids[ir]) {
238                                 MIDL_SWAP(ids[l+1], ids[ir]);
239                         }
240                         if (ids[l] < ids[l+1]) {
241                                 MIDL_SWAP(ids[l], ids[l+1]);
242                         }
243                         i = l+1;
244                         j = ir;
245                         a = ids[l+1];
246                         for(;;) {
247                                 do i++; while(ids[i] > a);
248                                 do j--; while(ids[j] < a);
249                                 if (j < i) break;
250                                 MIDL_SWAP(ids[i],ids[j]);
251                         }
252                         ids[l+1] = ids[j];
253                         ids[j] = a;
254                         jstack += 2;
255                         if (ir-i+1 >= j-l) {
256                                 istack[jstack] = ir;
257                                 istack[jstack-1] = i;
258                                 ir = j-1;
259                         } else {
260                                 istack[jstack] = j-1;
261                                 istack[jstack-1] = l;
262                                 l = i;
263                         }
264                 }
265         }
266 }
267
268 unsigned mdb_mid2l_search( MDB_ID2L ids, MDB_ID id )
269 {
270         /*
271          * binary search of id in ids
272          * if found, returns position of id
273          * if not found, returns first position greater than id
274          */
275         unsigned base = 0;
276         unsigned cursor = 1;
277         int val = 0;
278         unsigned n = (unsigned)ids[0].mid;
279
280         while( 0 < n ) {
281                 unsigned pivot = n >> 1;
282                 cursor = base + pivot + 1;
283                 val = CMP( id, ids[cursor].mid );
284
285                 if( val < 0 ) {
286                         n = pivot;
287
288                 } else if ( val > 0 ) {
289                         base = cursor;
290                         n -= pivot + 1;
291
292                 } else {
293                         return cursor;
294                 }
295         }
296
297         if( val > 0 ) {
298                 ++cursor;
299         }
300         return cursor;
301 }
302
303 int mdb_mid2l_insert( MDB_ID2L ids, MDB_ID2 *id )
304 {
305         unsigned x, i;
306
307         x = mdb_mid2l_search( ids, id->mid );
308
309         if( x < 1 ) {
310                 /* internal error */
311                 return -2;
312         }
313
314         if ( x <= ids[0].mid && ids[x].mid == id->mid ) {
315                 /* duplicate */
316                 return -1;
317         }
318
319         if ( ids[0].mid >= MDB_IDL_UM_MAX ) {
320                 /* too big */
321                 return -2;
322
323         } else {
324                 /* insert id */
325                 ids[0].mid++;
326                 for (i=(unsigned)ids[0].mid; i>x; i--)
327                         ids[i] = ids[i-1];
328                 ids[x] = *id;
329         }
330
331         return 0;
332 }
333
334 int mdb_mid2l_append( MDB_ID2L ids, MDB_ID2 *id )
335 {
336         /* Too big? */
337         if (ids[0].mid >= MDB_IDL_UM_MAX) {
338                 return -2;
339         }
340         ids[0].mid++;
341         ids[ids[0].mid] = *id;
342         return 0;
343 }
344
345 /** @} */
346 /** @} */