Commit 59a288ab authored by Kurt Zeilenga's avatar Kurt Zeilenga
Browse files

Rewrite IDL block code to use simple array and accessor macros

instead of array with overlayed struct fields.  (which is inherently
non-portable).
Change maxid check in insert to be greater than or equal instead
of equal.
Use SAFEMEMCPY.
parent 300a2424
......@@ -47,16 +47,26 @@ LDAP_BEGIN_DECL
* the list is terminated by an id of NOID.
* b_ids a list of the actual ids themselves
*/
typedef struct block {
ID b_nmax; /* max number of ids in this list */
#define ALLIDSBLOCK 0 /* == 0 => this is an allid block */
ID b_nids; /* current number of ids used */
#define INDBLOCK 0 /* == 0 => this is an indirect blk */
ID b_ids[1]; /* the ids - actually bigger */
} Block, IDList;
#define ALLIDS( idl ) ((idl)->b_nmax == ALLIDSBLOCK)
#define INDIRECT_BLOCK( idl ) ((idl)->b_nids == INDBLOCK)
typedef ID ID_BLOCK;
#define ID_BLOCK_NMAX_OFFSET 0
#define ID_BLOCK_NIDS_OFFSET 1
#define ID_BLOCK_IDS_OFFSET 2
/* all ID_BLOCK macros operate on a pointer to a ID_BLOCK */
#define ID_BLOCK_NMAX(b) ((b)[ID_BLOCK_NMAX_OFFSET])
#define ID_BLOCK_NIDS(b) ((b)[ID_BLOCK_NIDS_OFFSET])
#define ID_BLOCK_ID(b, n) ((b)[ID_BLOCK_IDS_OFFSET+(n)])
#define ID_BLOCK_NOID(b, n) (ID_BLOCK_ID((b),(n)) == NOID)
#define ID_BLOCK_ALLIDS_VALUE 0
#define ID_BLOCK_ALLIDS(b) (ID_BLOCK_NMAX(b) == ID_BLOCK_ALLIDS_VALUE)
#define ID_BLOCK_INDIRECT_VALUE 0
#define ID_BLOCK_INDIRECT(b) (ID_BLOCK_NIDS(b) == ID_BLOCK_INDIRECT_VALUE)
/* for the in-core cache of entries */
struct cache {
......
......@@ -109,7 +109,7 @@ ldbm_cache_open(
li->li_dbcache[i].dbc_blksize = DEFAULT_BLOCKSIZE;
}
li->li_dbcache[i].dbc_maxids = (li->li_dbcache[i].dbc_blksize /
sizeof(ID)) - 2;
sizeof(ID)) - ID_BLOCK_IDS_OFFSET;
li->li_dbcache[i].dbc_maxindirect = (SLAPD_LDBM_MIN_MAXIDS /
li->li_dbcache[i].dbc_maxids) + 1;
......
......@@ -10,12 +10,12 @@
#include "slap.h"
#include "back-ldbm.h"
static IDList *ava_candidates( Backend *be, Ava *ava, int type );
static IDList *presence_candidates( Backend *be, char *type );
static IDList *approx_candidates( Backend *be, Ava *ava );
static IDList *list_candidates( Backend *be, Filter *flist, int ftype );
static IDList *substring_candidates( Backend *be, Filter *f );
static IDList *substring_comp_candidates( Backend *be, char *type, char *val, int prepost );
static ID_BLOCK *ava_candidates( Backend *be, Ava *ava, int type );
static ID_BLOCK *presence_candidates( Backend *be, char *type );
static ID_BLOCK *approx_candidates( Backend *be, Ava *ava );
static ID_BLOCK *list_candidates( Backend *be, Filter *flist, int ftype );
static ID_BLOCK *substring_candidates( Backend *be, Filter *f );
static ID_BLOCK *substring_comp_candidates( Backend *be, char *type, char *val, int prepost );
/*
* test_filter - test a filter against a single entry.
......@@ -24,13 +24,13 @@ static IDList *substring_comp_candidates( Backend *be, char *type, char *val, in
* >0 an ldap error code
*/
IDList *
ID_BLOCK *
filter_candidates(
Backend *be,
Filter *f
)
{
IDList *result, *tmp1, *tmp2;
ID_BLOCK *result, *tmp1, *tmp2;
Debug( LDAP_DEBUG_TRACE, "=> filter_candidates\n", 0, 0, 0 );
......@@ -87,18 +87,18 @@ filter_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= filter_candidates %lu\n",
result ? result->b_nids : 0, 0, 0 );
result ? ID_BLOCK_NIDS(result) : 0, 0, 0 );
return( result );
}
static IDList *
static ID_BLOCK *
ava_candidates(
Backend *be,
Ava *ava,
int type
)
{
IDList *idl;
ID_BLOCK *idl;
Debug( LDAP_DEBUG_TRACE, "=> ava_candidates 0x%x\n", type, 0, 0 );
......@@ -118,35 +118,35 @@ ava_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= ava_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
static IDList *
static ID_BLOCK *
presence_candidates(
Backend *be,
char *type
)
{
IDList *idl;
ID_BLOCK *idl;
Debug( LDAP_DEBUG_TRACE, "=> presence_candidates\n", 0, 0, 0 );
idl = index_read( be, type, 0, "*" );
Debug( LDAP_DEBUG_TRACE, "<= presence_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
static IDList *
static ID_BLOCK *
approx_candidates(
Backend *be,
Ava *ava
)
{
char *w, *c;
IDList *idl, *tmp;
ID_BLOCK *idl, *tmp;
Debug( LDAP_DEBUG_TRACE, "=> approx_candidates\n", 0, 0, 0 );
......@@ -172,18 +172,18 @@ approx_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= approx_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
static IDList *
static ID_BLOCK *
list_candidates(
Backend *be,
Filter *flist,
int ftype
)
{
IDList *idl, *tmp, *tmp2;
ID_BLOCK *idl, *tmp, *tmp2;
Filter *f;
Debug( LDAP_DEBUG_TRACE, "=> list_candidates 0x%x\n", ftype, 0, 0 );
......@@ -213,18 +213,18 @@ list_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= list_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
static IDList *
static ID_BLOCK *
substring_candidates(
Backend *be,
Filter *f
)
{
int i;
IDList *idl, *tmp, *tmp2;
ID_BLOCK *idl, *tmp, *tmp2;
Debug( LDAP_DEBUG_TRACE, "=> substring_candidates\n", 0, 0, 0 );
......@@ -280,11 +280,11 @@ substring_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= substring_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
static IDList *
static ID_BLOCK *
substring_comp_candidates(
Backend *be,
char *type,
......@@ -293,7 +293,7 @@ substring_comp_candidates(
)
{
int i, len;
IDList *idl, *tmp, *tmp2;
ID_BLOCK *idl, *tmp, *tmp2;
char *p;
char buf[SUBLEN + 1];
......@@ -348,6 +348,6 @@ substring_comp_candidates(
}
Debug( LDAP_DEBUG_TRACE, "<= substring_comp_candidates %lu\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
......@@ -20,7 +20,7 @@ id2children_add(
struct dbcache *db;
Datum key;
int len, rc;
IDList *idl;
ID_BLOCK *idl;
char buf[20];
ldbm_datum_init( key );
......@@ -64,7 +64,7 @@ id2children_remove(
struct dbcache *db;
Datum key;
int len, rc;
IDList *idl;
ID_BLOCK *idl;
char buf[20];
Debug( LDAP_DEBUG_TRACE, "=> id2children_remove( %lu, %lu )\n", p ? p->e_id
......@@ -105,7 +105,7 @@ has_children(
struct dbcache *db;
Datum key;
int rc;
IDList *idl;
ID_BLOCK *idl;
char buf[20];
ldbm_datum_init( key );
......
This diff is collapsed.
......@@ -85,7 +85,7 @@ index_add_mods(
return( 0 );
}
IDList *
ID_BLOCK *
index_read(
Backend *be,
char *type,
......@@ -95,7 +95,7 @@ index_read(
{
struct dbcache *db;
Datum key;
IDList *idl;
ID_BLOCK *idl;
int indexmask, syntax;
char prefix;
char *realval, *tmpval;
......@@ -112,7 +112,7 @@ index_read(
idl = idl_allids( be );
Debug( LDAP_DEBUG_TRACE,
"<= index_read %lu candidates (allids - not indexed)\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
......@@ -152,7 +152,7 @@ index_read(
ldbm_cache_close( be, db );
Debug( LDAP_DEBUG_TRACE, "<= index_read %lu candidates\n",
idl ? idl->b_nids : 0, 0, 0 );
idl ? ID_BLOCK_NIDS(idl) : 0, 0, 0 );
return( idl );
}
......@@ -168,7 +168,7 @@ add_value(
{
int rc;
Datum key;
IDList *idl;
ID_BLOCK *idl;
char prefix;
char *realval, *tmpval, *s;
char buf[BUFSIZ];
......
......@@ -67,7 +67,7 @@ Entry * dn2entry_w LDAP_P(( Backend *be, char *dn, char **matched ));
* filterindex.c
*/
IDList * filter_candidates LDAP_P(( Backend *be, Filter *f ));
ID_BLOCK * filter_candidates LDAP_P(( Backend *be, Filter *f ));
/*
* id2children.c
......@@ -91,18 +91,18 @@ Entry * id2entry_w LDAP_P(( Backend *be, ID id ));
* idl.c
*/
IDList * idl_alloc LDAP_P(( int nids ));
IDList * idl_allids LDAP_P(( Backend *be ));
void idl_free LDAP_P(( IDList *idl ));
IDList * idl_fetch LDAP_P(( Backend *be, struct dbcache *db, Datum key ));
ID_BLOCK * idl_alloc LDAP_P(( int nids ));
ID_BLOCK * idl_allids LDAP_P(( Backend *be ));
void idl_free LDAP_P(( ID_BLOCK *idl ));
ID_BLOCK * idl_fetch LDAP_P(( Backend *be, struct dbcache *db, Datum key ));
int idl_insert_key LDAP_P(( Backend *be, struct dbcache *db, Datum key, ID id ));
int idl_insert LDAP_P(( IDList **idl, ID id, int maxids ));
int idl_insert LDAP_P(( ID_BLOCK **idl, ID id, int maxids ));
int idl_delete_key LDAP_P(( Backend *be, struct dbcache *db, Datum key, ID id ));
IDList * idl_intersection LDAP_P(( Backend *be, IDList *a, IDList *b ));
IDList * idl_union LDAP_P(( Backend *be, IDList *a, IDList *b ));
IDList * idl_notin LDAP_P(( Backend *be, IDList *a, IDList *b ));
ID idl_firstid LDAP_P(( IDList *idl ));
ID idl_nextid LDAP_P(( IDList *idl, ID id ));
ID_BLOCK * idl_intersection LDAP_P(( Backend *be, ID_BLOCK *a, ID_BLOCK *b ));
ID_BLOCK * idl_union LDAP_P(( Backend *be, ID_BLOCK *a, ID_BLOCK *b ));
ID_BLOCK * idl_notin LDAP_P(( Backend *be, ID_BLOCK *a, ID_BLOCK *b ));
ID idl_firstid LDAP_P(( ID_BLOCK *idl ));
ID idl_nextid LDAP_P(( ID_BLOCK *idl, ID id ));
/*
* index.c
......@@ -110,7 +110,7 @@ ID idl_nextid LDAP_P(( IDList *idl, ID id ));
int index_add_entry LDAP_P(( Backend *be, Entry *e ));
int index_add_mods LDAP_P(( Backend *be, LDAPModList *ml, ID id ));
IDList * index_read LDAP_P(( Backend *be, char *type, int indextype, char *val ));
ID_BLOCK * index_read LDAP_P(( Backend *be, char *type, int indextype, char *val ));
int index_add_values LDAP_P(( Backend *be, char *type, struct berval **vals, ID id ));
/*
......
......@@ -11,9 +11,9 @@
#include "back-ldbm.h"
#include "proto-back-ldbm.h"
static IDList *base_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
static IDList *onelevel_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
static IDList *subtree_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, Entry *e, int *err, int lookupbase);
static ID_BLOCK *base_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
static ID_BLOCK *onelevel_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, int *err);
static ID_BLOCK *subtree_candidates(Backend *be, Connection *conn, Operation *op, char *base, Filter *filter, char **attrs, int attrsonly, char **matched, Entry *e, int *err, int lookupbase);
#define GRABSIZE BUFSIZ
......@@ -45,7 +45,7 @@ ldbm_back_search(
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
int err;
time_t stoptime;
IDList *candidates;
ID_BLOCK *candidates;
ID id;
Entry *e;
Attribute *ref;
......@@ -305,7 +305,7 @@ ldbm_back_search(
return( 0 );
}
static IDList *
static ID_BLOCK *
base_candidates(
Backend *be,
Connection *conn,
......@@ -321,7 +321,7 @@ base_candidates(
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
int rc;
ID id;
IDList *idl;
ID_BLOCK *idl;
Entry *e;
Debug(LDAP_DEBUG_TRACE, "base_candidates: base: \"%s\"\n", base, 0, 0);
......@@ -346,7 +346,7 @@ base_candidates(
return( idl );
}
static IDList *
static ID_BLOCK *
onelevel_candidates(
Backend *be,
Connection *conn,
......@@ -363,7 +363,7 @@ onelevel_candidates(
Entry *e = NULL;
Filter *f;
char buf[20];
IDList *candidates;
ID_BLOCK *candidates;
Debug(LDAP_DEBUG_TRACE, "onelevel_candidates: base: \"%s\"\n", base, 0, 0);
......@@ -409,7 +409,7 @@ onelevel_candidates(
return( candidates );
}
static IDList *
static ID_BLOCK *
subtree_candidates(
Backend *be,
Connection *conn,
......@@ -426,7 +426,7 @@ subtree_candidates(
{
struct ldbminfo *li = (struct ldbminfo *) be->be_private;
Filter *f, **filterarg_ptr;
IDList *candidates;
ID_BLOCK *candidates;
Debug(LDAP_DEBUG_TRACE, "subtree_candidates: base: \"%s\" %s\n",
base ? base : "NULL", lookupbase ? "lookupbase" : "", 0);
......
......@@ -42,7 +42,7 @@ main( int argc, char **argv )
Datum savekey, key, data, last;
char *fname;
ID id;
IDList *idl;
ID_BLOCK *idl;
Backend *tbe;
int i;
char *tailorfile;
......@@ -127,7 +127,7 @@ main( int argc, char **argv )
get_keydata( stdin, buf[1], &key, NULL );
if ( (idl = idl_fetch( be, dbc, key )) != NULL ) {
data.dptr = (char *) idl;
data.dsize = (idl->b_nmax + 1) * sizeof(ID);
data.dsize = (ID_BLOCK_NMAX(idl) + 1) * sizeof(ID);
print_entry( stdout, buf[1], &key, "key: ",
&data, "data:\n" );
}
......@@ -266,7 +266,7 @@ main( int argc, char **argv )
get_keydata( stdin, buf[1], &key, &data );
idl = (IDList *) data.dptr;
idl = (ID_BLOCK *) data.dptr;
for ( id = idl_firstid( idl ); id != NOID;
id = idl_nextid( idl, id ) ) {
if ( idl_insert_key( be, dbc, key, id )
......@@ -390,7 +390,7 @@ get_idlist( FILE *fp, Datum *data )
{
char buf[20];
int i, j, fd, tty;
IDList *p;
ID_BLOCK *p;
int psize, pmax;
int nmax, nids;
......@@ -415,7 +415,7 @@ get_idlist( FILE *fp, Datum *data )
if ( psize + sizeof(ID) > pmax ) {
pmax += BUFSIZ;
p = (IDList *) myrealloc( (char *) p, pmax );
p = (ID_BLOCK *) myrealloc( (char *) p, pmax );
}
if ( strncmp( buf, "nids=0", 6 ) == 0 ) {
......@@ -423,7 +423,7 @@ get_idlist( FILE *fp, Datum *data )
continue;
}
p->b_ids[i++] = atol( buf );
ID_BLOCK_ID(p,i++) = atol( buf );
psize += sizeof(ID);
}
if ( nmax == 0 ) {
......@@ -440,19 +440,19 @@ get_idlist( FILE *fp, Datum *data )
}
}
if ( i > 0 ) {
p->b_nmax = nmax;
ID_BLOCK_NMAX(p) = nmax;
if ( nids != 0 ) {
p->b_nids = 0;
p->b_ids[i] = NOID;
ID_BLOCK_NIDS(p) = 0;
ID_BLOCK_ID(p,i) = NOID;
} else {
p->b_nids = i;
ID_BLOCK_NIDS(p) = i;
}
qsort( (void *) p->b_ids, i, sizeof(ID), dnid_cmp );
qsort( (void *) &ID_BLOCK_ID(p, 0), i, sizeof(ID), dnid_cmp );
}
data->dptr = (char *) p;
data->dsize = (nmax + 2) * sizeof(ID);
data->dsize = (nmax + ID_BLOCK_IDS_OFFSET) * sizeof(ID);
}
static void
......@@ -630,7 +630,7 @@ print_entry(
)
{
ID id;
IDList *idl;
ID_BLOCK *idl;
int i;
char msg[2];
......@@ -677,22 +677,22 @@ print_entry(
fprintf( fp, "%s%s (len %d)\n", klabel, key->dptr,
key->dsize );
if ( data != NULL ) {
idl = (IDList *) data->dptr;
idl = (ID_BLOCK *) data->dptr;
if ( dlabel )
fprintf( fp, "%s\tnmax=%ld\n\tncur=%ld\n", dlabel,
idl->b_nmax, idl->b_nids );
ID_BLOCK_NMAX(idl), ID_BLOCK_NIDS(idl) );
if ( INDIRECT_BLOCK( idl ) ) {
for ( i = 0; idl->b_ids[i] != NOID; i++ ) {
fprintf( fp, "\t%ld\n", idl->b_ids[i] );
if ( ID_BLOCK_INDIRECT( idl ) ) {
for ( i = 0; !ID_BLOCK_NOID(idl, i); i++ ) {
fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl, i) );
}
} else if ( ALLIDS( idl ) ) {
} else if ( ID_BLOCK_ALLIDS( idl ) ) {
fprintf( fp, "\tALLIDS (1..%ld)\n",
idl->b_nids - 1 );
ID_BLOCK_NIDS(idl) - 1 );
} else {
for ( i = 0; i < idl->b_nids; i++ ) {
fprintf( fp, "\t%ld\n", idl->b_ids[i] );
for ( i = 0; i < ID_BLOCK_NIDS(idl); i++ ) {
fprintf( fp, "\t%ld\n", ID_BLOCK_ID(idl,i) );
}
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment