feature(routes): Add rtable update
[ric-plt/lib/rmr.git] / src / common / src / symtab.c
index b780d9e..e96af03 100644 (file)
@@ -1,14 +1,14 @@
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
 // : vi ts=4 sw=4 noet :
 /*
 ==================================================================================
-       Copyright (c) 2019 Nokia 
+       Copyright (c) 2019 Nokia
        Copyright (c) 2018-2019 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
        Copyright (c) 2018-2019 AT&T Intellectual Property.
 
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
 
-       http://www.apache.org/licenses/LICENSE-2.0
+          http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
 ------------------------------------------------------------------------------
 Mnemonic:      symtab.c
 Abstract:      Symbol table -- slightly streamlined from it's original 2000 version
 ------------------------------------------------------------------------------
 Mnemonic:      symtab.c
 Abstract:      Symbol table -- slightly streamlined from it's original 2000 version
-                       (a part of the {X}fm opensource code), though we must retain the 
+                       (a part of the {X}fm opensource code), though we must retain the
                        original copyright.
 
                        Things changed for the Ric Msg implemention (Nov 2018):
                                - no concept of copy/free of the user data (functions removed)
                                - add ability to support an integer key (class 0)
                        original copyright.
 
                        Things changed for the Ric Msg implemention (Nov 2018):
                                - no concept of copy/free of the user data (functions removed)
                                - add ability to support an integer key (class 0)
+                                 Numeric key is an unsigned, 64bit key.
                                - externally visible names given a rmr_ extension as it's being
                                - externally visible names given a rmr_ extension as it's being
-                                 incorporated into the RIC msg routing library and will be 
+                                 incorporated into the RIC msg routing library and will be
                                  available to user applications.
 
 Date:          11 Feb 2000
                                  available to user applications.
 
 Date:          11 Feb 2000
@@ -46,6 +47,7 @@ Mod:          2016 23 Feb - converted Symtab refs so that caller need only a
 #include <string.h>
 #include <stdlib.h>
 #include <memory.h>
 #include <string.h>
 #include <stdlib.h>
 #include <memory.h>
+#include <netdb.h>
 
 #include "rmr_symtab.h"
 
 
 #include "rmr_symtab.h"
 
@@ -56,11 +58,11 @@ typedef struct Sym_ele
        struct Sym_ele *next;   /* pointer at next element in list */
        struct Sym_ele *prev;   /* larger table, easier deletes */
        const char *name;               /* symbol name */
        struct Sym_ele *next;   /* pointer at next element in list */
        struct Sym_ele *prev;   /* larger table, easier deletes */
        const char *name;               /* symbol name */
-       unsigned int    nkey;   // the numeric key
+       uint64_t nkey;                  // the numeric key
        void *val;              /* user data associated with name */
        unsigned long mcount;   /* modificaitons to value */
        unsigned long rcount;   /* references to symbol */
        void *val;              /* user data associated with name */
        unsigned long mcount;   /* modificaitons to value */
        unsigned long rcount;   /* references to symbol */
-       //unsigned int flags; 
+       //unsigned int flags;
        unsigned int class;             /* helps divide things up and allows for duplicate names */
 } Sym_ele;
 
        unsigned int class;             /* helps divide things up and allows for duplicate names */
 } Sym_ele;
 
@@ -68,7 +70,7 @@ typedef struct Sym_tab {
        Sym_ele **symlist;                      /* pointer to list of element pointerss */
        long    inhabitants;            /* number of active residents */
        long    deaths;                         /* number of deletes */
        Sym_ele **symlist;                      /* pointer to list of element pointerss */
        long    inhabitants;            /* number of active residents */
        long    deaths;                         /* number of deletes */
-       long    size;   
+       long    size;
 } Sym_tab;
 
 // -------------------- internal ------------------------------------------------------------------
 } Sym_tab;
 
 // -------------------- internal ------------------------------------------------------------------
@@ -107,7 +109,7 @@ static void del_ele( Sym_tab *table, int hv, Sym_ele *eptr )
                        eptr->next->prev = eptr->prev;
 
                if( eptr->class && eptr->name ) {                               // class 0 entries are numeric, so name is NOT a pointer
                        eptr->next->prev = eptr->prev;
 
                if( eptr->class && eptr->name ) {                               // class 0 entries are numeric, so name is NOT a pointer
-                       free( (void *) eptr->name );                    // and if free fails, what?  panic? 
+                       free( (void *) eptr->name );                    // and if free fails, what?  panic?
                }
 
                free( eptr );
                }
 
                free( eptr );
@@ -130,17 +132,17 @@ static inline int same( unsigned int c1, unsigned int c2, const char *s1, const
        return 0;
 }
 
        return 0;
 }
 
-/*     
+/*
        Generic routine to put something into the table
        called by sym_map or sym_put since the logic for each is pretty
        much the same.
 */
 static int putin( Sym_tab *table, const char *name, unsigned int class, void *val ) {
        Generic routine to put something into the table
        called by sym_map or sym_put since the logic for each is pretty
        much the same.
 */
 static int putin( Sym_tab *table, const char *name, unsigned int class, void *val ) {
-       Sym_ele *eptr;          /* pointer into hash table */ 
-       Sym_ele **sym_tab;      /* pointer into hash table */ 
-       int hv;                  /* hash value */
-       int rc = 0;              /* assume it existed */
-       unsigned int    nkey = 0;       // numeric key if class == 0
+       Sym_ele *eptr;                  /* pointer into hash table */
+       Sym_ele **sym_tab;      /* pointer into hash table */
+       int hv;                 /* hash value */
+       int rc = 0;             /* assume it existed */
+       uint64_t nkey = 0;              // numeric key if class == 0
 
        sym_tab = table->symlist;
 
 
        sym_tab = table->symlist;
 
@@ -148,7 +150,7 @@ static int putin( Sym_tab *table, const char *name, unsigned int class, void *va
                hv = sym_hash( name, table->size );             // hash it
                for( eptr=sym_tab[hv]; eptr && ! same( class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
                hv = sym_hash( name, table->size );             // hash it
                for( eptr=sym_tab[hv]; eptr && ! same( class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
-               nkey = *((int *) name);
+               nkey = *((uint64_t *) name);
                hv = nkey % table->size;                                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
                hv = nkey % table->size;                                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
@@ -156,12 +158,12 @@ static int putin( Sym_tab *table, const char *name, unsigned int class, void *va
        if( ! eptr ) {                  // not found above, so add
                rc++;
                table->inhabitants++;
        if( ! eptr ) {                  // not found above, so add
                rc++;
                table->inhabitants++;
-       
+
                eptr = (Sym_ele *) malloc( sizeof( Sym_ele) );
                if( ! eptr ) {
                        fprintf( stderr, "[FAIL] symtab/putin: out of memory\n" );
                        return -1;
                eptr = (Sym_ele *) malloc( sizeof( Sym_ele) );
                if( ! eptr ) {
                        fprintf( stderr, "[FAIL] symtab/putin: out of memory\n" );
                        return -1;
-               } 
+               }
 
                eptr->prev = NULL;
                eptr->class = class;
 
                eptr->prev = NULL;
                eptr->class = class;
@@ -193,13 +195,13 @@ extern void rmr_sym_clear( void *vtable )
 {
        Sym_tab *table;
        Sym_ele **sym_tab;
 {
        Sym_tab *table;
        Sym_ele **sym_tab;
-       int i; 
+       int i;
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
 
        for( i = 0; i < table->size; i++ )
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
 
        for( i = 0; i < table->size; i++ )
-               while( sym_tab[i] ) 
+               while( sym_tab[i] )
                        del_ele( table, i, sym_tab[i] );
 }
 
                        del_ele( table, i, sym_tab[i] );
 }
 
@@ -222,7 +224,7 @@ extern void rmr_sym_free( void *vtable ) {
 extern void rmr_sym_dump( void *vtable )
 {
        Sym_tab *table;
 extern void rmr_sym_dump( void *vtable )
 {
        Sym_tab *table;
-       int i; 
+       int i;
        Sym_ele *eptr;
        Sym_ele **sym_tab;
 
        Sym_ele *eptr;
        Sym_ele **sym_tab;
 
@@ -232,12 +234,12 @@ extern void rmr_sym_dump( void *vtable )
        for( i = 0; i < table->size; i++ )
        {
                if( sym_tab[i] )
        for( i = 0; i < table->size; i++ )
        {
                if( sym_tab[i] )
-               for( eptr = sym_tab[i]; eptr; eptr = eptr->next )  
+               for( eptr = sym_tab[i]; eptr; eptr = eptr->next )
                {
                        if( eptr->val && eptr->class ) {
                                fprintf( stderr, "key=%s val@=%p\n", eptr->name, eptr->val );
                        } else {
                {
                        if( eptr->val && eptr->class ) {
                                fprintf( stderr, "key=%s val@=%p\n", eptr->name, eptr->val );
                        } else {
-                               fprintf( stderr, "nkey=%d val@=%p\n", eptr->nkey, eptr->val );
+                               fprintf( stderr, "nkey=%lu val@=%p\n", (unsigned long) eptr->nkey, eptr->val );
                        }
                }
        }
                        }
                }
        }
@@ -263,7 +265,7 @@ extern void *rmr_sym_alloc( int size )
 
        memset( table, 0, sizeof( *table ) );
 
 
        memset( table, 0, sizeof( *table ) );
 
-       if((table->symlist = (Sym_ele **) malloc( sizeof( Sym_ele *) * size ))) 
+       if((table->symlist = (Sym_ele **) malloc( sizeof( Sym_ele *) * size )))
        {
                memset( table->symlist, 0, sizeof( Sym_ele *) * size );
                table->size = size;
        {
                memset( table->symlist, 0, sizeof( Sym_ele *) * size );
                table->size = size;
@@ -277,16 +279,16 @@ extern void *rmr_sym_alloc( int size )
        return (void *) table;    /* user might want to know what the size is */
 }
 
        return (void *) table;    /* user might want to know what the size is */
 }
 
-/* 
+/*
        Delete an element given name/class or numeric key (class 0).
 */
 extern void rmr_sym_del( void *vtable, const char *name, unsigned int class )
 {
        Sym_tab *table;
        Sym_ele **sym_tab;
        Delete an element given name/class or numeric key (class 0).
 */
 extern void rmr_sym_del( void *vtable, const char *name, unsigned int class )
 {
        Sym_tab *table;
        Sym_ele **sym_tab;
-       Sym_ele *eptr;    /* pointer into hash table */ 
-       int hv;                  /* hash value */
-       unsigned int nkey;              // class 0, name points to integer not string
+       Sym_ele *eptr;                  /* pointer into hash table */
+       int hv;                 /* hash value */
+       uint64_t        nkey;           // class 0, name points to integer not string
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
@@ -295,7 +297,7 @@ extern void rmr_sym_del( void *vtable, const char *name, unsigned int class )
                hv = sym_hash( name, table->size );
                for(eptr=sym_tab[hv]; eptr &&  ! same(class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
                hv = sym_hash( name, table->size );
                for(eptr=sym_tab[hv]; eptr &&  ! same(class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
-               nkey = *((int *) name);
+               nkey = *((uint64_t *) name);
                hv = nkey % table->size;                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
                hv = nkey % table->size;                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
@@ -306,7 +308,7 @@ extern void rmr_sym_del( void *vtable, const char *name, unsigned int class )
 /*
        Delete element by numberic key.
 */
 /*
        Delete element by numberic key.
 */
-extern void *rmr_sym_ndel(  void *vtable, int key ) {
+extern void *rmr_sym_ndel(  void *vtable, uint64_t key ) {
        rmr_sym_del( vtable, (const char *) &key, 0 );
 }
 
        rmr_sym_del( vtable, (const char *) &key, 0 );
 }
 
@@ -317,7 +319,7 @@ extern void *rmr_sym_get( void *vtable, const char *name, unsigned int class )
        Sym_ele **sym_tab;
        Sym_ele *eptr;                  // element from table
        int hv;                 // hash value of key
        Sym_ele **sym_tab;
        Sym_ele *eptr;                  // element from table
        int hv;                 // hash value of key
-       unsigned int nkey;              // numeric key if class 0
+       uint64_t nkey;                  // numeric key if class 0
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
 
        table = (Sym_tab *) vtable;
        sym_tab = table->symlist;
@@ -326,7 +328,7 @@ extern void *rmr_sym_get( void *vtable, const char *name, unsigned int class )
                hv = sym_hash( name, table->size );
                for(eptr=sym_tab[hv]; eptr &&  ! same(class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
                hv = sym_hash( name, table->size );
                for(eptr=sym_tab[hv]; eptr &&  ! same(class, eptr->class, eptr->name, name); eptr=eptr->next );
        } else {
-               nkey = *((int *) name);
+               nkey = *((uint64_t *) name);
                hv = nkey % table->size;                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
                hv = nkey % table->size;                        // just hash the number
                for( eptr=sym_tab[hv]; eptr && eptr->nkey != nkey; eptr=eptr->next );
        }
@@ -343,11 +345,11 @@ extern void *rmr_sym_get( void *vtable, const char *name, unsigned int class )
 /*
        Retrieve the data referenced by a numerical key.
 */
 /*
        Retrieve the data referenced by a numerical key.
 */
-extern void *rmr_sym_pull(  void *vtable, int key ) {
+extern void *rmr_sym_pull(  void *vtable, uint64_t key ) {
        return rmr_sym_get( vtable, (const char *) &key, 0 );
 }
 
        return rmr_sym_get( vtable, (const char *) &key, 0 );
 }
 
-/* 
+/*
        Put an element with a string key into the table. Replaces the element
        if it was already there.  Class must be >0 and if not 1 will be forced.
        (class 0 keys are numeric).
        Put an element with a string key into the table. Replaces the element
        if it was already there.  Class must be >0 and if not 1 will be forced.
        (class 0 keys are numeric).
@@ -365,20 +367,20 @@ extern int rmr_sym_put( void *vtable, const char *name, unsigned int class, void
        return putin( table, name, class, val );
 }
 
        return putin( table, name, class, val );
 }
 
-/* 
-       Add a new entry assuming that the key is an unsigned integer.
+/*
+       Add a new entry assuming that the key is an unsigned, 64 bit, integer.
 
 
-       Returns 1 if new, 0 if existed 
+       Returns 1 if new, 0 if existed
 */
 */
-extern int rmr_sym_map( void *vtable, unsigned int key, void *val ) {
+extern int rmr_sym_map( void *vtable, uint64_t key, void *val ) {
        Sym_tab *table;
 
        table = (Sym_tab *) vtable;
        return putin( table, (const char *) &key, 0, val );
 }
 
        Sym_tab *table;
 
        table = (Sym_tab *) vtable;
        return putin( table, (const char *) &key, 0, val );
 }
 
-/* 
-       Dump some statistics to stderr dev. Higher level is the more info dumpped 
+/*
+       Dump some statistics to stderr dev. Higher level is the more info dumpped
 */
 extern void rmr_sym_stats( void *vtable, int level )
 {
 */
 extern void rmr_sym_stats( void *vtable, int level )
 {
@@ -400,14 +402,14 @@ extern void rmr_sym_stats( void *vtable, int level )
                ch_count = 0;
                if( sym_tab[i] )
                {
                ch_count = 0;
                if( sym_tab[i] )
                {
-                       for( eptr = sym_tab[i]; eptr; eptr = eptr->next )  
+                       for( eptr = sym_tab[i]; eptr; eptr = eptr->next )
                        {
                                ch_count++;
                                if( level > 3 ) {
                                        if( eptr->class  ) {                                    // a string key
                                                fprintf( stderr, "sym: (%d) key=%s val@=%p ref=%ld mod=%lu\n", i, eptr->name, eptr->val, eptr->rcount, eptr->mcount );
                                        } else {
                        {
                                ch_count++;
                                if( level > 3 ) {
                                        if( eptr->class  ) {                                    // a string key
                                                fprintf( stderr, "sym: (%d) key=%s val@=%p ref=%ld mod=%lu\n", i, eptr->name, eptr->val, eptr->rcount, eptr->mcount );
                                        } else {
-                                               fprintf( stderr, "sym: (%d) key=%d val@=%p ref=%ld mod=%lu\n", i, eptr->nkey, eptr->val, eptr->rcount, eptr->mcount );
+                                               fprintf( stderr, "sym: (%d) key=%lu val@=%p ref=%ld mod=%lu\n", i, (unsigned long) eptr->nkey, eptr->val, eptr->rcount, eptr->mcount );
                                        }
                                }
                        }
                                        }
                                }
                        }
@@ -415,7 +417,7 @@ extern void rmr_sym_stats( void *vtable, int level )
                else
                        empty++;
 
                else
                        empty++;
 
-               if( ch_count > max_chain ) 
+               if( ch_count > max_chain )
                {
                        max_chain = ch_count;
                        maxi = i;
                {
                        max_chain = ch_count;
                        maxi = i;
@@ -434,12 +436,12 @@ extern void rmr_sym_stats( void *vtable, int level )
                        if( eptr->class ) {
                                fprintf( stderr, "\t%s\n", eptr->name );
                        } else {
                        if( eptr->class ) {
                                fprintf( stderr, "\t%s\n", eptr->name );
                        } else {
-                               fprintf( stderr, "\t%d (numeric key)\n", eptr->nkey );
+                               fprintf( stderr, "\t%lu (numeric key)\n", (unsigned long) eptr->nkey );
                        }
                }
        }
 
                        }
                }
        }
 
-       fprintf( stderr, "sym:%ld(size)  %ld(inhab) %ld(occupied) %ld(dead) %ld(maxch) %d(>2per)\n", 
+       fprintf( stderr, "sym:%ld(size)  %ld(inhab) %ld(occupied) %ld(dead) %ld(maxch) %d(>2per)\n",
                        table->size, table->inhabitants, table->size - empty, table->deaths, max_chain, twoper );
 }
 
                        table->size, table->inhabitants, table->size - empty, table->deaths, max_chain, twoper );
 }