]> git.sur5r.net Git - openldap/blobdiff - libraries/libavl/avl.c
stdlib.h should be included as <ac/stdlib.h>
[openldap] / libraries / libavl / avl.c
index 8eb5275d71ee0678234a39c46c6057b59ba18fc3..bd0ede4d66b1cb2c6a52aea333e4024cd8a32f29 100644 (file)
@@ -1,4 +1,9 @@
 /* avl.c - routines to implement an avl tree */
+/* $OpenLDAP$ */
+/*
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
+ * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+ */
 /*
  * Copyright (c) 1993 Regents of the University of Michigan.
  * All rights reserved.
  * is provided ``as is'' without express or implied warranty.
  */
 
-#ifndef lint
-static char copyright[] = "@(#) Copyright (c) 1993 Regents of the University of Michigan.\nAll rights reserved.\n";
-static char avl_version[] = "AVL library version 1.0\n";
-#endif
+#include "portable.h"
 
-#include <sys/types.h>
 #include <stdio.h>
-#include <stdlib.h>
+#include <ac/stdlib.h>
+
+#ifdef CSRIMALLOC
+#define ber_memalloc malloc
+#define ber_memrealloc realloc
+#else
+#include "lber.h"
+#endif
+
+#define AVL_INTERNAL
 #include "avl.h"
 
 #define ROTATERIGHT(x) { \
        Avlnode *tmp;\
        if ( *(x) == NULL || (*(x))->avl_left == NULL ) {\
-               (void) printf("RR error\n"); exit(1); \
+               (void) fputs("RR error\n", stderr); exit( EXIT_FAILURE ); \
        }\
        tmp = (*(x))->avl_left;\
        (*(x))->avl_left = tmp->avl_right;\
@@ -34,11 +44,11 @@ static char avl_version[] = "AVL library version 1.0\n";
 #define ROTATELEFT(x)  { \
        Avlnode *tmp;\
        if ( *(x) == NULL || (*(x))->avl_right == NULL ) {\
-               (void) printf("RL error\n"); exit(1); \
+               (void) fputs("RL error\n", stderr); exit( EXIT_FAILURE ); \
        }\
        tmp = (*(x))->avl_right;\
        (*(x))->avl_right = tmp->avl_left;\
-       tmp->avl_left = *x;\
+       tmp->avl_left = *(x);\
        *(x) = tmp;\
 }
 
@@ -47,20 +57,21 @@ static char avl_version[] = "AVL library version 1.0\n";
  * and balance of an avl tree.
  */
 
-static
-int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
-    Avlnode    **iroot;
-    caddr_t    data;
-    int                *taller;
-    IFP                fcmp;           /* comparison function */
-    IFP                fdup;           /* function to call for duplicates */
-    int                depth;
+static int
+ravl_insert(
+    Avlnode    **iroot,
+    void*      data,
+    int                *taller,
+    AVL_CMP            fcmp,                   /* comparison function */
+    AVL_DUP            fdup,                   /* function to call for duplicates */
+    int                depth
+)
 {
        int     rc, cmp, tallersub;
        Avlnode *l, *r;
 
        if ( *iroot == 0 ) {
-               if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
+               if ( (*iroot = (Avlnode *) ber_memalloc( sizeof( Avlnode ) ))
                    == NULL ) {
                        return( -1 );
                }
@@ -201,11 +212,8 @@ int ravl_insert( iroot, data, taller, fcmp, fdup, depth )
  * NOTE: this routine may malloc memory
  */
 
-int avl_insert( root, data, fcmp, fdup )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
-    IFP                fdup;
+int
+avl_insert( Avlnode **root, void* data, AVL_CMP fcmp, AVL_DUP fdup )
 {
        int     taller;
 
@@ -218,8 +226,7 @@ int avl_insert( root, data, fcmp, fdup )
  */
 
 static int
-right_balance( root )
-    Avlnode    **root;
+right_balance( Avlnode **root )
 {
        int     shorter = -1;
        Avlnode *r, *l;
@@ -282,9 +289,8 @@ right_balance( root )
  * been shortened because of a deletion.
  */
 
-static
-int left_balance( root )
-    Avlnode    **root;
+static int
+left_balance( Avlnode **root )
 {
        int     shorter = -1;
        Avlnode *r, *l;
@@ -349,16 +355,12 @@ int left_balance( root )
  * rebalancing.
  */
 
-static caddr_t
-ravl_delete( root, data, fcmp, shorter )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
-    int                *shorter;
+static void*
+ravl_delete( Avlnode **root, void* data, AVL_CMP fcmp, int *shorter )
 {
        int     shortersubtree = 0;
        int     cmp;
-       caddr_t savedata;
+       void*   savedata;
        Avlnode *minnode, *savenode;
 
        if ( *root == NULLAVL )
@@ -442,23 +444,16 @@ ravl_delete( root, data, fcmp, shorter )
  * the avl tree rooted at root.
  */
 
-caddr_t
-avl_delete( root, data, fcmp )
-    Avlnode    **root;
-    caddr_t    data;
-    IFP                fcmp;
+void*
+avl_delete( Avlnode **root, void* data, AVL_CMP fcmp )
 {
        int     shorter;
 
        return( ravl_delete( root, data, fcmp, &shorter ) );
 }
 
-static
-int avl_inapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_inapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -477,12 +472,8 @@ int avl_inapply( root, fn, arg, stopflag )
                return( avl_inapply( root->avl_right, fn, arg, stopflag ) );
 }
 
-static
-int avl_postapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_postapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -500,12 +491,8 @@ int avl_postapply( root, fn, arg, stopflag )
        return( (*fn)( root->avl_data, arg ) );
 }
 
-static
-int avl_preapply( root, fn, arg, stopflag )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
+static int
+avl_preapply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag )
 {
        if ( root == 0 )
                return( AVL_NOMORE );
@@ -532,12 +519,8 @@ int avl_preapply( root, fn, arg, stopflag )
  * of nodes.
  */
 
-int avl_apply( root, fn, arg, stopflag, type )
-    Avlnode    *root;
-    IFP                fn;
-    caddr_t    arg;
-    int                stopflag;
-    int                type;
+int
+avl_apply( Avlnode *root, AVL_APPLY fn, void* arg, int stopflag, int type )
 {
        switch ( type ) {
        case AVL_INORDER:
@@ -565,21 +548,23 @@ int avl_apply( root, fn, arg, stopflag, type )
  * AVL_NOMORE is returned.
  */
 
-int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP                fmatch;
-    caddr_t    marg;
-    IFP                fcmp;
-    caddr_t    carg;
-    int                stopflag;
+int
+avl_prefixapply(
+    Avlnode    *root,
+    void*      data,
+    AVL_CMP            fmatch,
+    void*      marg,
+    AVL_CMP            fcmp,
+    void*      carg,
+    int                stopflag
+)
 {
        int     cmp;
 
        if ( root == 0 )
                return( AVL_NOMORE );
 
-       cmp = (*fcmp)( data, root->avl_data, carg );
+       cmp = (*fcmp)( data, root->avl_data /* , carg */);
        if ( cmp == 0 ) {
                if ( (*fmatch)( root->avl_data, marg ) == stopflag )
                        return( stopflag );
@@ -614,9 +599,8 @@ int avl_prefixapply( root, data, fmatch, marg, fcmp, carg, stopflag )
  * number of items actually freed is returned.
  */
 
-int avl_free( root, dfree )
-    Avlnode    *root;
-    IFP                dfree;
+int
+avl_free( Avlnode *root, AVL_FREE dfree )
 {
        int     nleft, nright;
 
@@ -632,6 +616,7 @@ int avl_free( root, dfree )
 
        if ( dfree )
                (*dfree)( root->avl_data );
+       free( root );
 
        return( nleft + nright + 1 );
 }
@@ -643,11 +628,8 @@ int avl_free( root, dfree )
  * < 0 if arg1 is less than arg2 and > 0 if arg1 is greater than arg2.
  */
 
-caddr_t
-avl_find( root, data, fcmp )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP        fcmp;
+void*
+avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
        int     cmp;
 
@@ -668,13 +650,10 @@ avl_find( root, data, fcmp )
  * they match, non-zero otherwise.
  */
 
-caddr_t
-avl_find_lin( root, data, fcmp )
-    Avlnode    *root;
-    caddr_t    data;
-    IFP                fcmp;
+void*
+avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
-       caddr_t res;
+       void*   res;
 
        if ( root == 0 )
                return( NULL );
@@ -693,28 +672,28 @@ avl_find_lin( root, data, fcmp )
                return( avl_find_lin( root->avl_right, data, fcmp ) );
 }
 
-static caddr_t *avl_list;
+/* NON-REENTRANT INTERFACE */
+
+static void*   *avl_list;
 static int     avl_maxlist;
 static int     avl_nextlist;
 
 #define AVL_GRABSIZE   100
 
 /* ARGSUSED */
-static
-int avl_buildlist( data, arg )
-    caddr_t    data;
-    int        arg;
+static int
+avl_buildlist( void* data, void* arg )
 {
        static int      slots;
 
-       if ( avl_list == (caddr_t *) 0 ) {
-               avl_list = (caddr_t *) malloc(AVL_GRABSIZE * sizeof(caddr_t));
+       if ( avl_list == (void* *) 0 ) {
+               avl_list = (void* *) ber_memalloc(AVL_GRABSIZE * sizeof(void*));
                slots = AVL_GRABSIZE;
                avl_maxlist = 0;
        } else if ( avl_maxlist == slots ) {
                slots += AVL_GRABSIZE;
-               avl_list = (caddr_t *) realloc( (char *) avl_list,
-                   (unsigned) slots * sizeof(caddr_t));
+               avl_list = (void* *) ber_memrealloc( (char *) avl_list,
+                   (unsigned) slots * sizeof(void*));
        }
 
        avl_list[ avl_maxlist++ ] = data;
@@ -734,13 +713,12 @@ int avl_buildlist( data, arg )
  * different trees) cannot be active at once.
  */
 
-caddr_t
-avl_getfirst( root )
-    Avlnode    *root;
+void*
+avl_getfirst( Avlnode *root )
 {
        if ( avl_list ) {
                free( (char *) avl_list);
-               avl_list = (caddr_t *) 0;
+               avl_list = (void* *) 0;
        }
        avl_maxlist = 0;
        avl_nextlist = 0;
@@ -748,32 +726,37 @@ avl_getfirst( root )
        if ( root == 0 )
                return( 0 );
 
-       (void) avl_apply( root, avl_buildlist, (caddr_t) 0, -1, AVL_INORDER );
+       (void) avl_apply( root, avl_buildlist, (void*) 0, -1, AVL_INORDER );
 
        return( avl_list[ avl_nextlist++ ] );
 }
 
-caddr_t
-avl_getnext()
+void*
+avl_getnext( void )
 {
        if ( avl_list == 0 )
                return( 0 );
 
        if ( avl_nextlist == avl_maxlist ) {
-               free( (caddr_t) avl_list);
-               avl_list = (caddr_t *) 0;
+               free( (void*) avl_list);
+               avl_list = (void* *) 0;
                return( 0 );
        }
 
        return( avl_list[ avl_nextlist++ ] );
 }
 
-int avl_dup_error()
+/* end non-reentrant code */
+
+
+int
+avl_dup_error( void* left, void* right )
 {
        return( -1 );
 }
 
-int avl_dup_ok()
+int
+avl_dup_ok( void* left, void* right )
 {
        return( 0 );
 }