Moved out most ob mavl_ into libmavl.

Current libmavl is here:
https://svn.planix.org/rsrch/trunk/libmavl
shell
7u83 9 months ago
parent 1734000f41
commit 8d09b1d596
  1. 41
      src/cw/Makefile
  2. 4
      src/cw/conn_process_packet.c
  3. 4
      src/cw/connlist.c
  4. 5
      src/cw/cw_ktv_add.c
  5. 2
      src/cw/cw_ktv_add_from_str.c
  6. 8
      src/cw/cw_ktv_idx_get.c
  7. 2
      src/cw/cw_process_element.c
  8. 25
      src/cw/dtls_openssl.c
  9. 2
      src/cw/dtls_openssl_bio.c
  10. 4
      src/cw/dtls_openssl_connect.c
  11. 2
      src/cw/intavltree.c
  12. 2
      src/cw/intavltree.h
  13. 4
      src/cw/ktv.h
  14. 298
      src/cw/mavl.h
  15. 233
      src/cw/mavl_add.c
  16. 9
      src/cw/mavl_add_ptr.c
  17. 1
      src/cw/mavl_cmpstr.c
  18. 50
      src/cw/mavl_create.c
  19. 248
      src/cw/mavl_del.c
  20. 44
      src/cw/mavl_del_all.c
  21. 9
      src/cw/mavl_destroy.c
  22. 39
      src/cw/mavl_foreach.c
  23. 17
      src/cw/mavl_foreach_lr.c
  24. 5
      src/cw/mavl_freeptr.c
  25. 47
      src/cw/mavl_get.c
  26. 49
      src/cw/mavl_get_ext.c
  27. 36
      src/cw/mavl_get_node.c
  28. 18
      src/cw/mavl_get_node_cmp..c
  29. 3
      src/cw/mavl_get_ptr.c
  30. 10
      src/cw/mavl_merge.c
  31. 4
      src/cw/mavl_replace.c
  32. 12
      src/cw/mavl_replace_data.c
  33. 14
      src/cw/mavliter_get.c
  34. 1
      src/cw/mavliter_get_ptr.c
  35. 18
      src/cw/mavliter_init.c
  36. 68
      src/cw/mavliter_next.c
  37. 69
      src/cw/mavliter_seek.c
  38. 8
      src/cw/mavliter_seek_set.c
  39. 6
      src/cw/mod.c
  40. 19
      src/cw/msgset.c
  41. 2
      src/cw/strheap.c
  42. 9
      src/mod/capwap/capwap_actions_ac.c
  43. 2
      src/mod/capwap/capwap_out_ac_ip_list.c
  44. 6
      src/wtp/Makefile
  45. 2
      src/wtp/join.c
  46. 3
      src/wtp/wtp_main.c

@ -121,32 +121,6 @@ LWSRC=\
lw_put_sockaddr.c\
lw_readelem_wtp_name.c\
MAVLSRC=\
mavl_add.c\
mavl_add_ptr.c\
mavl_cmpstr.c\
mavl_create.c\
mavl_del_all.c\
mavl_del.c\
mavl_destroy.c\
mavl_foreach.c\
mavl_foreach_lr.c\
mavl_freeptr.c\
mavl_get.c\
mavl_get_ext.c\
mavl_get_node.c\
mavl_get_node_cmp..c\
mavl_get_ptr.c\
mavliter_get.c\
mavliter_get_ptr.c\
mavliter_init.c\
mavliter_next.c\
mavliter_seek.c\
mavliter_seek_set.c\
mavl_merge.c\
mavl_replace.c\
mavl_replace_data.c\
MLISTSRC=\
mlist_append.c\
@ -240,6 +214,15 @@ RADIOSRC=\
cw_out_radio_generic_struct.c\
cw_read_radio_generic.c\
MAVLSRC=\
mavl_get_ptr.c\
mavl_insert_ptr.c\
mavl_replace.c\
mavl_merge.c\
mavliter_get_ptr.c\
mavl_cmpstr.c\
DTLSSRC += dtls_openssl.c \
dtls_openssl_accept.c \
@ -258,7 +241,8 @@ LIBS+=-lssl
#SRC=$(wildcard *.c)
SOURCES = $(CWSRC) $(LWSRC) $(MAVLSRC) $(MLISTSRC) $(SOCKSRC) $(LOGSRC) $(DTLSSRC) $(RADIOSRC) $(KTVSRC) $(MISCSRC)
SOURCES = $(CWSRC) $(LWSRC) $(MAVLSRC) $(MLISTSRC) $(SOCKSRC) \
$(LOGSRC) $(DTLSSRC) $(RADIOSRC) $(KTVSRC) $(MISCSRC)
@ -270,7 +254,8 @@ OBJDIR := ../../obj/cw/$(KERNEL)/$(ARCH)
SNAME := $(LIBARCHDIR)/libcw.a
DNAME := $(LIBARCHDIR)/libcw.so
CFLAGS+=-fPIC -DUSE_OPENSSL
CFLAGS+=-fPIC -DUSE_OPENSSL -I../contrib/openssl-1.1.1f/include/ -O0
CFLAGS+=-g -I/home/tube/v/rsrch/libmavl
all: $(SNAME) $(DNAME)
static: $(SNAME)

@ -206,7 +206,7 @@ void cw_read_elem(struct cw_ElemHandler * handler, struct conn * conn,
handler->type->to_str(result,str,30);
printf("Read %d-%s: %s %s\n", handler->id, handler->name, handler->key, str);
//mavl_add(conn->remote_cfg
//mavl_insert(conn->remote_cfg
}
*/
@ -290,7 +290,7 @@ static int process_elements(struct conn *conn, uint8_t * rawmsg, int len,
/* Search message */
message = mavl_find(conn->msgset->msgdata,&search);
message = mavl_get(conn->msgset->msgdata,&search);
result_code = 0;

@ -155,7 +155,7 @@ struct conn * connlist_add ( struct connlist * cl, struct conn * conn )
return NULL;
conn->connlist = cl;
return mavl_add_ptr ( cl->by_addr, conn );
return mavl_insert_ptr ( cl->by_addr, conn );
}
struct conn * connlist_get_by_session_id ( struct connlist *cl, struct conn * conn )
@ -165,7 +165,7 @@ struct conn * connlist_get_by_session_id ( struct connlist *cl, struct conn * co
struct conn * connlist_add_by_session_id ( struct connlist * cl, struct conn * conn )
{
return mavl_add_ptr ( cl->by_session_id, conn );
return mavl_insert_ptr ( cl->by_session_id, conn );
}
void connlist_remove ( struct connlist *cl, struct conn * conn )

@ -10,6 +10,7 @@ cw_KTV_t * cw_ktv_add(mavl_t kvtstore, const char *key, const struct cw_Type *ty
{
cw_KTV_t mdata, *mresult;
int exists;
printf("KTV ADD: %s\n",key);
mdata.key=cw_strdup(key);
mdata.valguard=valguard;
@ -29,8 +30,10 @@ cw_KTV_t * cw_ktv_add(mavl_t kvtstore, const char *key, const struct cw_Type *ty
return NULL;
}
printf("MAVLCOUNT: %d - %s\n",kvtstore->count, mdata.key);
mavl_del(kvtstore,&mdata);
mresult = mavl_add(kvtstore, &mdata, &exists);
mresult = mavl_insert(kvtstore, &mdata, &exists);
if (exists){
cw_log(LOG_ERR, "Element already exists %s", key);
/* element already exists */

@ -31,7 +31,7 @@ const char * cw_ktv_add_from_str(mavl_t kvtstore, const char *key,
return NULL;
}
mresult = mavl_add(kvtstore, &mdata, &exists);
mresult = mavl_insert(kvtstore, &mdata, &exists);
if (exists){
cw_log(LOG_ERR, "Element already exists %s", key);
/* element already exists */

@ -24,19 +24,19 @@ void * ktvn(struct mavl *t ,const void *search)
if (rc<0){
/*lastl = n;*/
if (n->left==NULL){
if (n->s[0]==NULL){
return mavlnode_dataptr(lastb);
}
n=n->left;
n=n->s[0];
}
else{
lastb=n;
if(n->right==NULL){
if(n->s[1]==NULL){
return mavlnode_dataptr(lastb);
}
n=n->right;
n=n->s[1];
}
}
return NULL;

@ -35,7 +35,7 @@ int cw_process_element(struct cw_ElemHandlerParams *params, int proto, int vendo
elem_data_search.id=elem_id;
elem_data_search.proto=proto;
elem_data_search.vendor=vendor;
elem_data = mavl_find(params->msgdata->elements_tree,&elem_data_search);
elem_data = mavl_get(params->msgdata->elements_tree,&elem_data_search);
if (!elem_data){
cw_dbg(DBG_ELEM_ERR, "Element %d - %s, not allowed here",
elem_id, handler->name);

@ -48,8 +48,8 @@ static const char * ssl_version2str(int version)
return "TLSv1";
case DTLS1_VERSION:
return "DTLSv1";
/* case DTLS1_2_VERSION:
return "DTLSv1.2"; */
case DTLS1_2_VERSION:
return "DTLSv1.2";
}
return "Version unknown";
}
@ -65,7 +65,7 @@ static void dtls_debug_cb(int write_p,int version,int type, const void * buf,siz
s += sprintf(s,"SSL msg in: ");
s+=sprintf(s,"type = %d (0x%02X), %s (%08x), len = %d",type,type,ssl_version2str(version),version,(int)len);
/* cw_dbg(DBG_DTLS_DETAIL,buffer); */
cw_dbg(DBG_DTLS_DETAIL,buffer);
}
@ -319,7 +319,17 @@ struct dtls_openssl_data * dtls_openssl_data_create(struct conn * conn, const SS
return NULL;
}
rc = SSL_CTX_get_security_level(d->ctx);
printf("Security Level is %d\n");
SSL_CTX_set_security_level(d->ctx,0);
printf("Security Level is %d\n");
/* SSL_CTX_set_min_proto_version (d->ctx, DTLS1_VERSION);*/
SSL_CTX_set_max_proto_version (d->ctx, DTLS1_VERSION);
rc = SSL_CTX_set_cipher_list(d->ctx, conn->dtls_cipher);
if (!rc){
dtls_openssl_log_error(0,rc,"DTLS setup cipher error:");
@ -372,7 +382,6 @@ struct dtls_openssl_data * dtls_openssl_data_create(struct conn * conn, const SS
rc = SSL_CTX_load_verify_locations(d->ctx,"../../ssl/root-ca.pem",NULL);
/*// SSL_CTX_set_session_cache_mode(d->ctx, SSL_SESS_CACHE_BOTH);*/
@ -381,10 +390,10 @@ struct dtls_openssl_data * dtls_openssl_data_create(struct conn * conn, const SS
SSL_CTX_set_timeout(d->ctx,30);
/*
// rc =SSL_CTX_set_max_proto_version (d->ctx,DTLS1_VERSION);
// printf("MAXMAMX = %d\n",rc);
*/
rc =SSL_CTX_set_max_proto_version (d->ctx,DTLS1_VERSION);
printf("MAXMAMX = %d\n",rc);
/*
// SSL_CTX_set_verify(d->ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, dtls_verify_callback);

@ -38,7 +38,7 @@
BIO_METHOD *dtls_openssl_bio_method()
{
int index = BIO_get_new_index() ;
index = BIO_TYPE_DGRAM;
/* index = BIO_TYPE_DGRAM;*/
cw_dbg(DBG_DTLS_BIO, "Creating new OpenSSL BIO Methods");
BIO_METHOD * bio_methods;

@ -46,7 +46,7 @@ dtls_openssl_connect(struct conn *conn)
if (!conn->dtls_data)
conn->dtls_data =
dtls_openssl_data_create(conn, DTLSv1_client_method(),
dtls_openssl_data_create(conn, DTLS_client_method(),
biomethod);
cw_dbg(DBG_DTLS_BIO, "DTLS Connect call 2");
@ -68,7 +68,7 @@ dtls_openssl_connect(struct conn *conn)
rc = SSL_connect(d->ssl);
}while(rc!=1 && errno==EAGAIN && !cw_timer_timeout(timer));
cw_dbg(DBG_DTLS_BIO, "DTLS Connect call 4");
cw_dbg(DBG_DTLS_BIO, "DTLS Connect call 4 %d ",rc);
if (rc == 1) {
cw_dbg(DBG_DTLS,"SSL connect successfull!");

@ -31,5 +31,5 @@ int * intavltree_add(struct mavl * t, int val)
if (!v)
return NULL;
*v=val;
return mavl_add(t,v,NULL);
return mavl_insert(t,v,NULL);
}

@ -1,7 +1,7 @@
#ifndef __INTAVLTREE_H
#define __INTAVLTREE_H
#include "mavl.h"
#include <mavl.h>
extern struct mavl * intavltree_create();
typedef struct mavl * intavltree_t;

@ -4,7 +4,9 @@
#include <stdint.h>
#include <stdio.h>
#include "mavl.h"
#include <mavl.h>
#include "mavltypes.h"
#include "bstr.h"

@ -1,298 +0,0 @@
/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file mavl.h
* @brief MAVL, Mini AVL Tree,
* Yet another AVL Tree implementation
*/
/**
* @addtogroup ALGOS
* @{
*
* @defgroup MAVL Mavl Tree
* @{
*/
#ifndef __MAVL_H
#define __MAVL_H
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
/** Maximum AVL Tree depth.
The number of nodes is calculated by 2^depth.
So a value of 32 should be enough for around 4
billion nodes. */
#define MAVL_MAX_DEPTH 32
/**
* Defines the structure of an AVL Node.
*/
struct mavlnode {
/** Pointer to data, that belongs to the node */
/* union mavldata data; */
/** Pointer to left son*/
struct mavlnode *left;
/** Pointer to right son*/
struct mavlnode *right;
/** AVL balance */
int bal;
};
/**
* AVL Tree
*/
struct mavl {
/** Pointer to root node */
struct mavlnode *root;
/** Compare function */
int ( *cmp ) ( const void *, const void * );
/** Delete element function */
void ( *del ) ( void * );
/** Number of elements currently stored in the tree */
int count;
/** size of data appended to each mavlnode element.
* used to allocate space in #mavl_add. */
size_t data_size;
};
/**
* MAVL AVL Tree type
*/
typedef struct mavl * mavl_t;
#define MAVL_FIND_FIRST 1
#define MAVL_FIND_LAST 2
/**
* @param node node
*/
#define mavlnode_dataptr(node) ((void*)(((uint8_t*)(node))+sizeof(struct mavlnode)))
struct mavl *mavl_create ( int ( *cmp ) ( const void *, const void * ),
void ( *del ) ( void * ), size_t data_size );
void *mavl_add ( struct mavl *t, const void *data, int *exists );
/*void *mavl_add ( struct mavl *t, const void *data );*/
void * mavl_get ( struct mavl *t , const void *data );
void * mavl_get_ext(struct mavl *t ,const void *search, int mode);
void *mavl_del ( struct mavl *t, const void *data );
void *mavl_replace ( struct mavl *t, const void *data, int * result );
void mavl_destroy ( struct mavl *t );
void mavl_del_all ( struct mavl *t );
void mavl_merge ( mavl_t target, mavl_t source );
void mavlnode_destroy ( struct mavl *t, struct mavlnode *n );
struct mavlnode *mavlnode_get ( struct mavl *t, void *data );
struct mavlnode * mavl_get_node_cmp(struct mavl *t ,void *data,
int ( *cmp ) ( const void *, const void * ));
#define mavl_get_first(tree,search) mavl_get_ext(tree,search,MAVL_FIND_FIRST)
#define mavl_get_last(tree,search) mavl_get_ext(tree,search,MAVL_FIND_LAST)
extern int mavl_foreach_lr ( struct mavlnode *n, int ( *callback ) ( void *, void * ),
void *cbpriv );
extern int mavl_foreach_rl ( struct mavlnode *n, int ( *callback ) ( void *, void * ),
void *cbpriv );
int mavl_foreach_from_lr ( struct mavl *t, struct mavlnode *n, void *data,
int ( *callback ) ( void *, void * ), void *cbpriv );
void *mavl_replace_data ( struct mavl *t, void *data, int len );
/**
* This macro is an alias for #mavl_get
*/
#define mavl_find(t,d) mavl_get(t,d)
#define mavl_find_ptr(tree,search_ptr) mavl_get_ptr(tree,search_ptr)
#define mavl_insert(t,d) mavl_add(t,d)
#define mavl_foreach_asc(t,cb,priv) mavl_foreach_lr((t)->root,cb,priv)
#define mavl_foreach_desc(t,cb,priv) mavl_foreach_rl((t)->root,cb,priv)
#define mavl_foreach_from_asc(t,d,cb,priv) mavl_foreach_from_lr(t,(t)->root,d,cb,priv);
struct mavliter {
struct mavlnode *stack[MAVL_MAX_DEPTH * 2];
struct mavlnode *cur;
int stack_ptr;
struct mavlnode * root;
int ( *cmp ) ( const void *, const void * );
};
typedef struct mavliter mavliter_t;
void * mavliter_next ( mavliter_t *i );
void * mavliter_seek_set ( struct mavliter *i );
void mavliter_init ( mavliter_t *i, mavl_t t );
void * mavliter_get ( mavliter_t *i );
void * mavliter_get_ptr(mavliter_t *i);
void * mavliter_seek ( mavliter_t *i, void *d );
/**
* Define a AVL Iterator varialble and accociate it with
* an AVL Tree.
* @param i Name of the variable to define
* @param t #mavl_t Tree to associate
* @see #mavliter_get #mavliter_foreach
*
* Example:
* \code
mavl_t datatree = mavl_create();
// fill up datatree with some values
MAVLITER_DEFINE (i,datatree);
mavliter_foreach(&i) {
// Get the current value
void * value = mavliter_get(&i);
}
\endcode
*/
#define MAVLITER_DEFINE(iterator,mavl_obj)\
mavliter_t iterator; mavliter_init(&iterator,mavl_obj)
/**
* Iterate through all elements of an MAVL Object using a MAVL Iterator.
* @param i pointer to MAVL Interator object
*/
#define mavliter_foreach(iterator)\
for (mavliter_seek_set(iterator); NULL != mavliter_get(iterator); mavliter_next(iterator))
#define mavliter_foreach_from(i,from)\
for (mavliter_seek(i,from); NULL != mavliter_get(i); mavliter_next(i))
#define mavliter_foreach_asc(iter,val) \
while(NULL != (val = mavliter_next(iter)))
/**
* Get the number of elements stored in a mavl object
* @param m mavl object
* @return Number of elemenst
*/
#define mavl_get_count(m) (m->count)
/*
#typedef mavl_t mavl_conststr_t;
#extern mavl_conststr_t mavl_create_conststr();
*/
/*
//int mavl_cmp_str ( const union mavldata *e1, const union mavldata *e2 );
*/
/*
static inline void *mavl_replace_data(struct mavl *t, void *data, int len)
{
void *df = mavl_get(t, data);
if (!df)
return NULL;
memcpy(df, data, len);
return df;
}
static inline void *mavl_replace(struct mavl *t,void *data){
struct mavlnode * node = mavl_get_node(t,data);
if (node){
t->del(node->data);
return node->data=data;
}
return mavl_add(t,data);
}
*/
void mavl_freeptr(void *ptr);
/**
* Create a mavl_t object which stores pointers.
* @param cmp a pointer to the compare function
* @param del a pointer to a delete function
* @return The mavl_t object or NULL if an error has occured.
*/
#define mavl_create_ptr(cmp,del)\
mavl_create(cmp,del,sizeof(void*))
int mavl_cmpstr(const void *p1, const void *p2);
#define mavl_create_str() mavl_create_ptr(mavl_cmpstr,mavl_freeptr)
#define mavl_create_conststr() mavl_create_ptr(mavl_cmpstr,NULL)
void * mavl_get_ptr ( mavl_t tree, void * search );
void * mavl_add_ptr ( mavl_t tree, const void *ptr );
#define mavl_add_str(tree,str) mavl_add_ptr(tree,str)
#define mavl_get_str(tree,search) ((char *)(mavl_get_ptr(tree,search)))
#define mavliter_get_str(iter) ((char*)(mavliter_get_ptr(iter)))
#define mavliter_get_conststr(iter) ((const char*)(mavliter_get_ptr(iter)))
/**
* @} MAVL
*
* @} ALGOS
*
*/
#endif

@ -1,233 +0,0 @@
/**
* @file
* @brief Implementation of mavl_add
*/
#include "mavl.h"
static struct mavlnode *mavlnode_create ( struct mavl *t, const void *data )
{
struct mavlnode *n = malloc ( sizeof ( struct mavlnode ) + t->data_size );
if ( !n )
return NULL;
n->left = n->right = NULL;
n->bal = 0;
/*n->data = *data;*/
memcpy ( mavlnode_dataptr ( n ), data, t->data_size );
return n;
}
static int mavl_add0 ( struct mavl *t, struct mavlnode **parent, const void ** data )
{
struct mavlnode *tmp;
struct mavlnode *n = *parent;
int rc = t->cmp ( *data, mavlnode_dataptr ( n ) );
int bal;
if ( rc == 0 ) {
/* *data = &n->data;*/
/* memcpy(data,mavlnode_dataptr(n),t->data_size);*/
*data = mavlnode_dataptr ( n );
return 2;
}
if ( rc < 0 ) {
if ( n->left ) {
bal = mavl_add0 ( t, &n->left, data );
if ( bal > 1 )
return bal;
n->bal -= bal;
if ( n->bal == 0 )
return 0;
if ( n->bal == -2 ) {
if ( n->left->bal == -1 ) {
n->bal = 0;
n->left->bal = 0;
*parent = n->left;
tmp = n->left->right;
n->left->right = n;
n->left = tmp;
return 0;
}
if ( n->left->bal == 1 ) {
*parent = n->left->right;
if ( ( *parent )->bal == 1 ) {
n->bal = 0;
n->left->bal = -1;
} else if ( ( *parent )->bal == -1 ) {
n->bal = 1;
n->left->bal = 0;
} else {
n->bal = 0;
n->left->bal = 0;
}
( *parent )->bal = 0;
n->left->right = ( *parent )->left;
( *parent )->left = n->left;
tmp = ( *parent )->right;
( *parent )->right = n;
n->left = tmp;
return 0;
}
}
return bal;
}
/* n->left is 0 */
n->left = mavlnode_create ( t, *data );
*data = mavlnode_dataptr(n->left);
if ( !n->left )
return 3;
t->count++;
if ( n->right == 0 ) {
n->bal = -1;
return 1;
}
n->bal = 0;
return 0;
} else {
if ( n->right ) {
bal = mavl_add0 ( t, &n->right, data );
if ( bal > 1 )
return bal;
n->bal += bal;
if ( n->bal == 0 )
return 0;
if ( n->bal == 2 ) {
if ( n->right->bal == 1 ) {
n->bal = 0;
n->right->bal = 0;
*parent = n->right;
tmp = n->right->left;
n->right->left = n;
n->right = tmp;
return 0;
} else if ( n->right->bal == -1 ) {
*parent = n->right->left;
if ( ( *parent )->bal == -1 ) {
n->bal = 0;
n->right->bal = 1;
} else if ( ( *parent )->bal == 1 ) {
n->bal = -1;
n->right->bal = 0;
} else {
n->bal = 0;
n->right->bal = 0;
}
( *parent )->bal = 0;
n->right->left = ( *parent )->right;
( *parent )->right = n->right;
tmp = ( *parent )->left;
( *parent )->left = n;
n->right = tmp;
return 0;
}
}
return bal;
}
/* n->right is 0 */
n->right = mavlnode_create ( t, *data );
*data = mavlnode_dataptr(n->right);
if ( !n->right )
return 3;
t->count++;
if ( n->left == 0 ) {
n->bal = 1;
return 1;
}
n->bal = 0;
return 0;
}
}
/**
* @brief mavl add function
* Add an element to an AVL tree
* @param t mavl
* @param data pointer to the element to add
* @param exists pointer to an interger vairable which wil be set to 1, if
* the element to add already exists in the tree. If the element did't
* exist, the variable ist to 0.
* @return A pointer to the element in the tree. If the element was added,
* it the pointer to newly created element. If the element had already existed,
* the pointer points to the existing element. mavl_add returns thero, if
* something else went wrong. Therefore consult errno.
*
*
*/
void *mavl_add ( struct mavl *t, const void *data, int *exists )
{
const void * d;
int rc;
if ( t->root == NULL ) {
t->root = mavlnode_create ( t, data );
if ( t->root )
t->count++;
/* return &t->root->data; */
if (exists != NULL)
*exists=0;
return mavlnode_dataptr(t->root);
}
d = data;
rc = mavl_add0 ( t, &t->root, &d );
if ( rc >= 3 )
return NULL;
if ( exists != NULL){
if (rc==2)
*exists=1;
else
*exists=0;
}
return (void*)d;
}

@ -1,9 +0,0 @@
#include "mavl.h"
void *mavl_add_ptr(mavl_t tree, const void *ptr){
void * result;
result = mavl_add(tree,&ptr,NULL);
if (result)
return *(void**)result;
return NULL;
}

@ -1,4 +1,5 @@
#include "mavl.h"
#include "mavltypes.h"
int mavl_cmpstr (const void *s1, const void *s2)
{

@ -1,50 +0,0 @@
/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
*@file
*@brief Implementation of mavl_create
*/
#include "mavl.h"
/**
* Create an AVL tree
* @param cmp pointer to compare function
* @param del pointer to delete function which is called when an element
* will be deletet
* @param dta_size size of a data element
* @return pointer to a #mavl struct. If the return value is NULL something
* went wrong, and you should consult errno to get details.
*/
struct mavl *mavl_create(int (*cmp) (const void *, const void *),
void (*del) (void *), size_t data_size)
{
struct mavl *t = malloc(sizeof(struct mavl));
if (!t)
return NULL;
t->root = NULL;
t->count = 0;
t->cmp = cmp;
t->del = del;
t->data_size=data_size;
return t;
}

@ -1,248 +0,0 @@
#include "mavl.h"
void mavlnode_destroy(struct mavl *t, struct mavlnode *n)
{
if (t->del) {
t->del(mavlnode_dataptr(n));
}
free(n);
t->count--;
}
static void rot_rl(struct mavlnode *n, struct mavlnode **parent)
{
struct mavlnode *tmp;
*parent = n->right->left;
n->right->left = (*parent)->right;
(*parent)->right = n->right;
tmp = (*parent)->left;
(*parent)->left = n;
n->right = tmp;
}
static void rot_lr(struct mavlnode *n, struct mavlnode **parent)
{
struct mavlnode *tmp;
*parent = n->left->right;
n->left->right = (*parent)->left;
(*parent)->left = n->left;
tmp = (*parent)->right;
(*parent)->right = n;
n->left = tmp;
}
static void rot_l(struct mavlnode *n, struct mavlnode **parent)
{
struct mavlnode *tmp;
*parent = n->right;
tmp = n->right->left;
n->right->left = n;
n->right = tmp;
}
static void rot_r(struct mavlnode *n, struct mavlnode **parent)
{
struct mavlnode *tmp;
*parent = n->left;
tmp = n->left->right;
n->left->right = n;
n->left = tmp;
}
static int adj_bal_l(struct mavlnode *n, struct mavlnode **parent)
{
if (n->right->bal == 1) {
n->bal = 0;
n->right->bal = 0;
rot_l(n, parent);
return 1;
} else if (n->right->bal == 0) {
n->bal = 1;
n->right->bal = -1;
rot_l(n, parent);
return 0;
} else if (n->right->bal == -1) {
n->bal = 0;
n->right->bal = 0;
n->right->left->bal = 0;
rot_rl(n, parent);
return 1;
}
return -11; /* that should never happen */
}
static int adj_bal_r(struct mavlnode *n, struct mavlnode **parent)
{
if (n->left->bal == -1) {
n->bal = 0;
n->left->bal = 0;
rot_r(n, parent);
return 1;
} else if (n->left->bal == 0) {
n->bal = -1;
n->left->bal = 1;
rot_r(n, parent);
return 0;
} else if (n->left->bal == 1) {
n->bal = 0;
n->left->bal = 0;
n->left->right->bal = 0;
rot_lr(n, parent);
return 1;
}
return -11; /* that should never happen */
}
static int mavl_del_lo(mavl_t t, struct mavlnode **parent, void *data)
{
struct mavlnode *n = *parent;
if (n->left != 0) {
int bal = mavl_del_lo(t, &n->left, data);
n->bal += bal;
if (n->bal == 1) {
return 0;
}
if (n->bal != 2)
return bal;
adj_bal_l(n, parent);
return 0;
}
/* found the lowest element */
*parent = n->right;
/* *data = n->data;*/
memcpy(data,mavlnode_dataptr(n),t->data_size);
free(n);
return 1;
if (n->right) {
free(n);
return 1;
}
free(n);
return 1;
}
int mavl_del0(struct mavl *t, struct mavlnode **parent, void *data)
{
struct mavlnode *n = *parent;
int rc;
int bal;
rc = t->cmp(data, mavlnode_dataptr(n));
if (rc == 0) {
if (n->right == 0 && n->left == 0) {
*parent = 0;
mavlnode_destroy(t, n);
return 1;
}
if (n->right && n->left == 0) {
*parent = n->right;
mavlnode_destroy(t, n);
return 1;
}
if (n->left && n->right == 0) {
mavlnode_destroy(t, n);
*parent = n->left;
return 1;
}
/* node has two childs */
if (t->del) {
t->del(mavlnode_dataptr(n));
}
t->count--;
bal = mavl_del_lo(t,&n->right, mavlnode_dataptr(n));
n->bal -= bal;
if (n->bal == -1)
return 0;
if (n->bal != -2)
return bal;
return adj_bal_r(n, parent);
}
if (rc < 0) {
if (n->left) {
bal = mavl_del0(t, &n->left, data);
if (bal == 2)
return 2;
n->bal += bal;
if (n->bal == 1)
return 0;
if (n->bal != 2)
return bal;
return adj_bal_l(n, parent);
}
return 2; /* not found */
} else { /* rc must be > 0 */
if (n->right) {
bal = mavl_del0(t, &n->right, data);
if (bal == 2)
return 2;
n->bal -= bal;
if (n->bal == -1)
return 0;
if (n->bal != -2)
return bal;
return adj_bal_r(n, parent);
}
return 2; /* not found */
}
}
void *mavl_del(struct mavl *t, const void *data)
{
void *d;
int rc;
if (!t->root)
return NULL;
d = (void*)data;
rc = mavl_del0(t, &t->root, d);
if (rc == 2)
return NULL;
return (void*)data;
}

@ -1,44 +0,0 @@
/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Implements mavl_del_all
*/
#include "mavl.h"
static void mavl_del_all0(struct mavl *t ,struct mavlnode * n)
{
if (!n)
return;
mavl_del_all0(t,n->left);
mavl_del_all0(t,n->right);
mavlnode_destroy(t,n);
}
/**
* Delete all elemets of a mavl object
* @parm t mavl object
*/
void mavl_del_all(struct mavl *t)
{
mavl_del_all0(t,t->root);
t->root=NULL;
}

@ -1,9 +0,0 @@
#include "mavl.h"
void mavl_destroy(struct mavl *t)
{
mavl_del_all(t);
free (t);
}

@ -1,39 +0,0 @@
/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mavl.h"
int mavl_foreach_rl(struct mavlnode *n, int (*callback)(void *,void *),void *cbpriv)
{
if (!n)
return 1;
if (!mavl_foreach_rl(n->right,callback,cbpriv))
return 0;
if (!callback(cbpriv,mavlnode_dataptr(n)))
return 0;
return mavl_foreach_rl(n->left,callback,cbpriv);
}
void mavl_foreach(struct mavl *t, int (*callback)(void *,void *),void * cbpriv,int dir)
{
if (dir)
mavl_foreach_lr(t->root,callback,cbpriv);
else
mavl_foreach_rl(t->root,callback,cbpriv);
}

@ -1,17 +0,0 @@
#include "mavl.h"
int mavl_foreach_lr(struct mavlnode *n, int (*callback)(void *,void *),void *cbpriv)
{
if (!n)
return 1;
if (!mavl_foreach_lr(n->left,callback,cbpriv))
return 0;
if (!callback(cbpriv,mavlnode_dataptr(n)))
return 0;
return mavl_foreach_lr(n->right,callback,cbpriv);
}

@ -1,5 +0,0 @@
#include "mavl.h"
void mavl_freeptr(void *ptr){
free( *((void**)ptr) );
}

@ -1,47 +0,0 @@
/*
This file is part of libcapwap.
libcapwap is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libcapwap is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file
* @brief Implementation of mavl_get
*/
#include "mavl.h"
/**
* Get an AVL tree element.
* @param t pointer to mavl_t
* @param data Element to get
* @return pointer to element or NULL if not found.
*/
void * mavl_get(struct mavl *t ,const void *data)
{
struct mavlnode *n = t->root;
while(n){
int rc=t->cmp(data,mavlnode_dataptr(n));
if (rc==0)
return mavlnode_dataptr(n);
if (rc<0)
n=n->left;
else
n=n->right;