From dd94ddba57371b7ce36b48cdd1d783a55714e006 Mon Sep 17 00:00:00 2001 From: Pierangelo Masarati Date: Sun, 17 Dec 2006 22:04:25 +0000 Subject: [PATCH] don't leave dependencies on liblutil in libldap :) --- include/lutil.h | 7 -- libraries/libldap/abandon.c | 156 ++++++++++++++++++++++++++++++++++- libraries/libldap/ldap-int.h | 11 +++ libraries/libldap/result.c | 6 +- libraries/liblutil/utils.c | 151 --------------------------------- 5 files changed, 168 insertions(+), 163 deletions(-) diff --git a/include/lutil.h b/include/lutil.h index 7162c4bf9d..b4d0108e6b 100644 --- a/include/lutil.h +++ b/include/lutil.h @@ -322,13 +322,6 @@ lutil_unparse_time( char *buf, size_t buflen, unsigned long t ); } while ( 0 ); #endif /* ! timermul */ -LDAP_LUTIL_F (int) -lutil_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ); -LDAP_LUTIL_F (int) -lutil_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ); -LDAP_LUTIL_F (int) -lutil_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ); - LDAP_END_DECL #endif /* _LUTIL_H */ diff --git a/libraries/libldap/abandon.c b/libraries/libldap/abandon.c index 9785b21b16..568c073868 100644 --- a/libraries/libldap/abandon.c +++ b/libraries/libldap/abandon.c @@ -311,9 +311,9 @@ start_again:; /* use bisection */ i = 0; if ( ld->ld_nabandoned == 0 || - lutil_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, &i ) == 0 ) + ldap_int_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, &i ) == 0 ) { - lutil_bisect_insert( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, i ); + ldap_int_bisect_insert( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, i ); } if ( err != -1 ) { @@ -326,3 +326,155 @@ start_again:; #endif return( ld->ld_errno ); } + +/* + * ldap_int_bisect_find + * + * args: + * v: array of length n (in) + * n: length of array v (in) + * id: value to look for (in) + * idxp: pointer to location of value/insert point + * + * return: + * 0: not found + * 1: found + * -1: error + */ +int +ldap_int_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ) +{ + int begin, + end, + i, + rc = 0; + + assert( n >= 0 ); + assert( id >= 0 ); + + begin = 0; + end = n - 1; + + if ( n > 0 ) { + if ( id < v[ begin ] ) { + *idxp = 0; + + } else if ( id > v[ end ] ) { + *idxp = n; + + } else { + int pos; + ber_int_t curid; + + while ( end >= begin ) { + pos = (begin + end)/2; + curid = v[ pos ]; + + if ( id < curid ) { + end = pos - 1; + + } else if ( id > curid ) { + begin = pos + 1; + + } else { + /* already abandoned? */ + *idxp = pos; + rc = 1; + break; + } + } + + if ( rc == 0 ) { + *idxp = pos + ( id > curid ? 1 : 0 ); + } + } + + } else { + *idxp = 0; + } + + return rc; +} + +/* + * ldap_int_bisect_insert + * + * args: + * vp: pointer to array of length *np (in/out) + * np: pointer to length of array *vp (in/out) + * id: value to insert (in) + * idx: location of insert point (as computed by ldap_int_bisect_find()) + * + * return: + * 0: inserted + * -1: error + */ +int +ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ) +{ + ber_int_t *v; + ber_len_t n; + int i; + + assert( vp != NULL ); + assert( np != NULL ); + assert( *np >= 0 ); + assert( idx >= 0 ); + assert( idx <= *np ); + + n = *np; + + v = ber_memrealloc( *vp, sizeof( ber_int_t ) * ( n + 1 ) ); + if ( v == NULL ) { + return -1; + } + *vp = v; + + for ( i = n; i > idx; i-- ) { + v[ i ] = v[ i - 1 ]; + } + v[ idx ] = id; + ++(*np); + + return 0; +} + +/* + * ldap_int_bisect_delete + * + * args: + * vp: pointer to array of length *np (in/out) + * np: pointer to length of array *vp (in/out) + * id: value to delete (in) + * idx: location of value to delete (as computed by ldap_int_bisect_find()) + * + * return: + * 0: deleted + */ +int +ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ) +{ + ber_int_t *v; + ber_len_t n; + int i; + + assert( vp != NULL ); + assert( np != NULL ); + assert( *np >= 0 ); + assert( idx >= 0 ); + assert( idx < *np ); + + v = *vp; + + assert( v[ idx ] == id ); + + --(*np); + n = *np; + + for ( i = idx; i < n; i++ ) { + v[ i ] = v[ i + 1 ]; + } + + return 0; +} + diff --git a/libraries/libldap/ldap-int.h b/libraries/libldap/ldap-int.h index 9a19f29aa5..69473e0f85 100644 --- a/libraries/libldap/ldap-int.h +++ b/libraries/libldap/ldap-int.h @@ -398,6 +398,17 @@ LDAP_V( ldap_pvt_thread_mutex_t ) ldap_int_sasl_mutex; #define LDAP_NEXT_MSGID(ld, id) id = ++(ld)->ld_msgid #endif +/* + * in abandon.c + */ + +LDAP_F (int) +ldap_int_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ); +LDAP_F (int) +ldap_int_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ); +LDAP_F (int) +ldap_int_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ); + /* * in init.c */ diff --git a/libraries/libldap/result.c b/libraries/libldap/result.c index 9e594c017a..34cb90e11a 100644 --- a/libraries/libldap/result.c +++ b/libraries/libldap/result.c @@ -1237,7 +1237,7 @@ ldap_abandoned( LDAP *ld, ber_int_t msgid, int *idxp ) assert( msgid >= 0 ); assert( ld->ld_nabandoned >= 0 ); - return lutil_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, idxp ); + return ldap_int_bisect_find( ld->ld_abandoned, ld->ld_nabandoned, msgid, idxp ); } /* @@ -1252,11 +1252,11 @@ ldap_mark_abandoned( LDAP *ld, ber_int_t msgid, int idx ) LDAP_PVT_THREAD_ASSERT_MUTEX_OWNER( &ld->ld_res_mutex ); #endif - /* NOTE: those assertions are repeated in lutil_bisect_delete() */ + /* NOTE: those assertions are repeated in ldap_int_bisect_delete() */ assert( idx >= 0 ); assert( idx < ld->ld_nabandoned ); assert( ld->ld_abandoned[ idx ] == msgid ); - return lutil_bisect_delete( &ld->ld_abandoned, &ld->ld_nabandoned, + return ldap_int_bisect_delete( &ld->ld_abandoned, &ld->ld_nabandoned, msgid, idx ); } diff --git a/libraries/liblutil/utils.c b/libraries/liblutil/utils.c index bffeef84a4..94badf831e 100644 --- a/libraries/liblutil/utils.c +++ b/libraries/liblutil/utils.c @@ -572,154 +572,3 @@ lutil_unparse_time( return 0; } -/* - * lutil_bisect_find - * - * args: - * v: array of length n (in) - * n: length of array v (in) - * id: value to look for (in) - * idxp: pointer to location of value/insert point - * - * return: - * 0: not found - * 1: found - * -1: error - */ -int -lutil_bisect_find( ber_int_t *v, ber_len_t n, ber_int_t id, int *idxp ) -{ - int begin, - end, - i, - rc = 0; - - assert( n >= 0 ); - assert( id >= 0 ); - - begin = 0; - end = n - 1; - - if ( n > 0 ) { - if ( id < v[ begin ] ) { - *idxp = 0; - - } else if ( id > v[ end ] ) { - *idxp = n; - - } else { - int pos; - ber_int_t curid; - - while ( end >= begin ) { - pos = (begin + end)/2; - curid = v[ pos ]; - - if ( id < curid ) { - end = pos - 1; - - } else if ( id > curid ) { - begin = pos + 1; - - } else { - /* already abandoned? */ - *idxp = pos; - rc = 1; - break; - } - } - - if ( rc == 0 ) { - *idxp = pos + ( id > curid ? 1 : 0 ); - } - } - - } else { - *idxp = 0; - } - - return rc; -} - -/* - * lutil_bisect_insert - * - * args: - * vp: pointer to array of length *np (in/out) - * np: pointer to length of array *vp (in/out) - * id: value to insert (in) - * idx: location of insert point (as computed by lutil_bosect_find()) - * - * return: - * 0: inserted - * -1: error - */ -int -lutil_bisect_insert( ber_int_t **vp, ber_len_t *np, int id, int idx ) -{ - ber_int_t *v; - ber_len_t n; - int i; - - assert( vp != NULL ); - assert( np != NULL ); - assert( *np >= 0 ); - assert( idx >= 0 ); - assert( idx <= *np ); - - n = *np; - - v = ber_memrealloc( *vp, sizeof( ber_int_t ) * ( n + 1 ) ); - if ( v == NULL ) { - return -1; - } - *vp = v; - - for ( i = n; i > idx; i-- ) { - v[ i ] = v[ i - 1 ]; - } - v[ idx ] = id; - ++(*np); - - return 0; -} - -/* - * lutil_bisect_delete - * - * args: - * vp: pointer to array of length *np (in/out) - * np: pointer to length of array *vp (in/out) - * id: value to delete (in) - * idx: location of value to delete (as computed by lutil_bosect_find()) - * - * return: - * 0: deleted - */ -int -lutil_bisect_delete( ber_int_t **vp, ber_len_t *np, int id, int idx ) -{ - ber_int_t *v; - ber_len_t n; - int i; - - assert( vp != NULL ); - assert( np != NULL ); - assert( *np >= 0 ); - assert( idx >= 0 ); - assert( idx < *np ); - - v = *vp; - - assert( v[ idx ] == id ); - - --(*np); - n = *np; - - for ( i = idx; i < n; i++ ) { - v[ i ] = v[ i + 1 ]; - } - - return 0; -} - -- 2.39.5