]> 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 93886e8ae51475861befa4ac605ad1599b144f9c..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.
 
 #include "portable.h"
 
-#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 <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) fputs("RR error\n", stderr); exit(1); \
+               (void) fputs("RR error\n", stderr); exit( EXIT_FAILURE ); \
        }\
        tmp = (*(x))->avl_left;\
        (*(x))->avl_left = tmp->avl_right;\
@@ -36,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) fputs("RL error\n", stderr); 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;\
 }
 
@@ -63,7 +71,7 @@ ravl_insert(
        Avlnode *l, *r;
 
        if ( *iroot == 0 ) {
-               if ( (*iroot = (Avlnode *) malloc( sizeof( Avlnode ) ))
+               if ( (*iroot = (Avlnode *) ber_memalloc( sizeof( Avlnode ) ))
                    == NULL ) {
                        return( -1 );
                }
@@ -608,6 +616,7 @@ avl_free( Avlnode *root, AVL_FREE dfree )
 
        if ( dfree )
                (*dfree)( root->avl_data );
+       free( root );
 
        return( nleft + nright + 1 );
 }
@@ -620,7 +629,7 @@ avl_free( Avlnode *root, AVL_FREE dfree )
  */
 
 void*
-avl_find( Avlnode *root, void* data, AVL_CMP fcmp )
+avl_find( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
        int     cmp;
 
@@ -642,7 +651,7 @@ avl_find( Avlnode *root, void* data, AVL_CMP fcmp )
  */
 
 void*
-avl_find_lin( Avlnode *root, void* data, AVL_CMP fcmp )
+avl_find_lin( Avlnode *root, const void* data, AVL_CMP fcmp )
 {
        void*   res;
 
@@ -663,6 +672,8 @@ avl_find_lin( Avlnode *root, void* data, AVL_CMP fcmp )
                return( avl_find_lin( root->avl_right, data, fcmp ) );
 }
 
+/* NON-REENTRANT INTERFACE */
+
 static void*   *avl_list;
 static int     avl_maxlist;
 static int     avl_nextlist;
@@ -676,12 +687,12 @@ avl_buildlist( void* data, void* arg )
        static int      slots;
 
        if ( avl_list == (void* *) 0 ) {
-               avl_list = (void* *) malloc(AVL_GRABSIZE * sizeof(void*));
+               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 = (void* *) realloc( (char *) avl_list,
+               avl_list = (void* *) ber_memrealloc( (char *) avl_list,
                    (unsigned) slots * sizeof(void*));
        }
 
@@ -735,6 +746,9 @@ avl_getnext( void )
        return( avl_list[ avl_nextlist++ ] );
 }
 
+/* end non-reentrant code */
+
+
 int
 avl_dup_error( void* left, void* right )
 {