debugging
authorRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 26 Jun 2022 05:01:08 +0000 (15:01 +1000)
committerRalph Ronnquist <ralph.ronnquist@gmail.com>
Sun, 26 Jun 2022 05:01:08 +0000 (15:01 +1000)
pvector/aaa [deleted file]
tests/aaa [new file with mode: 0644]
tests/example-vector.c [new file with mode: 0644]
vector/vector.c
vector/vector.h

diff --git a/pvector/aaa b/pvector/aaa
deleted file mode 100644 (file)
index 3d4dabe..0000000
+++ /dev/null
@@ -1,260 +0,0 @@
-127.0.0.0/8
-2001:19f0:5801:e4:5400:1ff:feed:77b6/64
-10.0.0.0/8
-196.168.0.0/16
-12.12.0.3/24
-12.12.1.3/24
-12.12.2.3/24
-12.12.3.3/24
-12.12.4.3/24
-12.12.5.3/24
-12.12.6.3/24
-12.12.7.3/24
-12.12.8.3/24
-12.12.9.3/24
-12.12.10.3/24
-12.12.11.3/24
-12.12.12.3/24
-12.12.13.3/24
-12.12.14.3/24
-12.12.15.3/24
-12.12.16.3/24
-12.12.17.3/24
-12.12.18.3/24
-12.12.19.3/24
-12.12.20.3/24
-12.12.21.3/24
-12.12.22.3/24
-12.12.23.3/24
-12.12.24.3/24
-12.12.25.3/24
-12.12.26.3/24
-12.12.27.3/24
-12.12.28.3/24
-12.12.29.3/24
-12.12.30.3/24
-12.12.31.3/24
-12.12.32.3/24
-12.12.33.3/24
-12.12.34.3/24
-12.12.35.3/24
-12.12.36.3/24
-12.12.37.3/24
-12.12.38.3/24
-12.12.39.3/24
-12.12.40.3/24
-12.12.41.3/24
-12.12.42.3/24
-12.12.43.3/24
-12.12.44.3/24
-12.12.45.3/24
-12.12.46.3/24
-12.12.47.3/24
-12.12.48.3/24
-12.12.49.3/24
-12.12.50.3/24
-12.12.51.3/24
-12.12.52.3/24
-12.12.53.3/24
-12.12.54.3/24
-12.12.55.3/24
-12.12.56.3/24
-12.12.57.3/24
-12.12.58.3/24
-12.12.59.3/24
-12.12.60.3/24
-12.12.61.3/24
-12.12.62.3/24
-12.12.63.3/24
-12.12.64.3/24
-12.12.65.3/24
-12.12.66.3/24
-12.12.67.3/24
-12.12.68.3/24
-12.12.69.3/24
-12.12.70.3/24
-12.12.71.3/24
-12.12.72.3/24
-12.12.73.3/24
-12.12.74.3/24
-12.12.75.3/24
-12.12.76.3/24
-12.12.77.3/24
-12.12.78.3/24
-12.12.79.3/24
-12.12.80.3/24
-12.12.81.3/24
-12.12.82.3/24
-12.12.83.3/24
-12.12.84.3/24
-12.12.85.3/24
-12.12.86.3/24
-12.12.87.3/24
-12.12.88.3/24
-12.12.89.3/24
-12.12.90.3/24
-12.12.91.3/24
-12.12.92.3/24
-12.12.93.3/24
-12.12.94.3/24
-12.12.95.3/24
-12.12.96.3/24
-12.12.97.3/24
-12.12.98.3/24
-12.12.99.3/24
-12.12.100.3/24
-12.12.101.3/24
-12.12.102.3/24
-12.12.103.3/24
-12.12.104.3/24
-12.12.105.3/24
-12.12.106.3/24
-12.12.107.3/24
-12.12.108.3/24
-12.12.109.3/24
-12.12.110.3/24
-12.12.111.3/24
-12.12.112.3/24
-12.12.113.3/24
-12.12.114.3/24
-12.12.115.3/24
-12.12.116.3/24
-12.12.117.3/24
-12.12.118.3/24
-12.12.119.3/24
-12.12.120.3/24
-12.12.121.3/24
-12.12.122.3/24
-12.12.123.3/24
-12.12.124.3/24
-12.12.125.3/24
-12.12.126.3/24
-12.12.127.3/24
-12.12.128.3/24
-12.12.129.3/24
-12.12.130.3/24
-12.12.131.3/24
-12.12.132.3/24
-12.12.133.3/24
-12.12.134.3/24
-12.12.135.3/24
-12.12.136.3/24
-12.12.137.3/24
-12.12.138.3/24
-12.12.139.3/24
-12.12.140.3/24
-12.12.141.3/24
-12.12.142.3/24
-12.12.143.3/24
-12.12.144.3/24
-12.12.145.3/24
-12.12.146.3/24
-12.12.147.3/24
-12.12.148.3/24
-12.12.149.3/24
-12.12.150.3/24
-12.12.151.3/24
-12.12.152.3/24
-12.12.153.3/24
-12.12.154.3/24
-12.12.155.3/24
-12.12.156.3/24
-12.12.157.3/24
-12.12.158.3/24
-12.12.159.3/24
-12.12.160.3/24
-12.12.161.3/24
-12.12.162.3/24
-12.12.163.3/24
-12.12.164.3/24
-12.12.165.3/24
-12.12.166.3/24
-12.12.167.3/24
-12.12.168.3/24
-12.12.169.3/24
-12.12.170.3/24
-12.12.171.3/24
-12.12.172.3/24
-12.12.173.3/24
-12.12.174.3/24
-12.12.175.3/24
-12.12.176.3/24
-12.12.177.3/24
-12.12.178.3/24
-12.12.179.3/24
-12.12.180.3/24
-12.12.181.3/24
-12.12.182.3/24
-12.12.183.3/24
-12.12.184.3/24
-12.12.185.3/24
-12.12.186.3/24
-12.12.187.3/24
-12.12.188.3/24
-12.12.189.3/24
-12.12.190.3/24
-12.12.191.3/24
-12.12.192.3/24
-12.12.193.3/24
-12.12.194.3/24
-12.12.195.3/24
-12.12.196.3/24
-12.12.197.3/24
-12.12.198.3/24
-12.12.199.3/24
-12.12.200.3/24
-12.12.201.3/24
-12.12.202.3/24
-12.12.203.3/24
-12.12.204.3/24
-12.12.205.3/24
-12.12.206.3/24
-12.12.207.3/24
-12.12.208.3/24
-12.12.209.3/24
-12.12.210.3/24
-12.12.211.3/24
-12.12.212.3/24
-12.12.213.3/24
-12.12.214.3/24
-12.12.215.3/24
-12.12.216.3/24
-12.12.217.3/24
-12.12.218.3/24
-12.12.219.3/24
-12.12.220.3/24
-12.12.221.3/24
-12.12.222.3/24
-12.12.223.3/24
-12.12.224.3/24
-12.12.225.3/24
-12.12.226.3/24
-12.12.227.3/24
-12.12.228.3/24
-12.12.229.3/24
-12.12.230.3/24
-12.12.231.3/24
-12.12.232.3/24
-12.12.233.3/24
-12.12.234.3/24
-12.12.235.3/24
-12.12.236.3/24
-12.12.237.3/24
-12.12.238.3/24
-12.12.239.3/24
-12.12.240.3/24
-12.12.241.3/24
-12.12.242.3/24
-12.12.243.3/24
-12.12.244.3/24
-12.12.245.3/24
-12.12.246.3/24
-12.12.247.3/24
-12.12.248.3/24
-12.12.249.3/24
-12.12.250.3/24
-12.12.251.3/24
-12.12.252.3/24
-12.12.253.3/24
-12.12.254.3/24
-12.12.255.3/24
diff --git a/tests/aaa b/tests/aaa
new file mode 100644 (file)
index 0000000..3d4dabe
--- /dev/null
+++ b/tests/aaa
@@ -0,0 +1,260 @@
+127.0.0.0/8
+2001:19f0:5801:e4:5400:1ff:feed:77b6/64
+10.0.0.0/8
+196.168.0.0/16
+12.12.0.3/24
+12.12.1.3/24
+12.12.2.3/24
+12.12.3.3/24
+12.12.4.3/24
+12.12.5.3/24
+12.12.6.3/24
+12.12.7.3/24
+12.12.8.3/24
+12.12.9.3/24
+12.12.10.3/24
+12.12.11.3/24
+12.12.12.3/24
+12.12.13.3/24
+12.12.14.3/24
+12.12.15.3/24
+12.12.16.3/24
+12.12.17.3/24
+12.12.18.3/24
+12.12.19.3/24
+12.12.20.3/24
+12.12.21.3/24
+12.12.22.3/24
+12.12.23.3/24
+12.12.24.3/24
+12.12.25.3/24
+12.12.26.3/24
+12.12.27.3/24
+12.12.28.3/24
+12.12.29.3/24
+12.12.30.3/24
+12.12.31.3/24
+12.12.32.3/24
+12.12.33.3/24
+12.12.34.3/24
+12.12.35.3/24
+12.12.36.3/24
+12.12.37.3/24
+12.12.38.3/24
+12.12.39.3/24
+12.12.40.3/24
+12.12.41.3/24
+12.12.42.3/24
+12.12.43.3/24
+12.12.44.3/24
+12.12.45.3/24
+12.12.46.3/24
+12.12.47.3/24
+12.12.48.3/24
+12.12.49.3/24
+12.12.50.3/24
+12.12.51.3/24
+12.12.52.3/24
+12.12.53.3/24
+12.12.54.3/24
+12.12.55.3/24
+12.12.56.3/24
+12.12.57.3/24
+12.12.58.3/24
+12.12.59.3/24
+12.12.60.3/24
+12.12.61.3/24
+12.12.62.3/24
+12.12.63.3/24
+12.12.64.3/24
+12.12.65.3/24
+12.12.66.3/24
+12.12.67.3/24
+12.12.68.3/24
+12.12.69.3/24
+12.12.70.3/24
+12.12.71.3/24
+12.12.72.3/24
+12.12.73.3/24
+12.12.74.3/24
+12.12.75.3/24
+12.12.76.3/24
+12.12.77.3/24
+12.12.78.3/24
+12.12.79.3/24
+12.12.80.3/24
+12.12.81.3/24
+12.12.82.3/24
+12.12.83.3/24
+12.12.84.3/24
+12.12.85.3/24
+12.12.86.3/24
+12.12.87.3/24
+12.12.88.3/24
+12.12.89.3/24
+12.12.90.3/24
+12.12.91.3/24
+12.12.92.3/24
+12.12.93.3/24
+12.12.94.3/24
+12.12.95.3/24
+12.12.96.3/24
+12.12.97.3/24
+12.12.98.3/24
+12.12.99.3/24
+12.12.100.3/24
+12.12.101.3/24
+12.12.102.3/24
+12.12.103.3/24
+12.12.104.3/24
+12.12.105.3/24
+12.12.106.3/24
+12.12.107.3/24
+12.12.108.3/24
+12.12.109.3/24
+12.12.110.3/24
+12.12.111.3/24
+12.12.112.3/24
+12.12.113.3/24
+12.12.114.3/24
+12.12.115.3/24
+12.12.116.3/24
+12.12.117.3/24
+12.12.118.3/24
+12.12.119.3/24
+12.12.120.3/24
+12.12.121.3/24
+12.12.122.3/24
+12.12.123.3/24
+12.12.124.3/24
+12.12.125.3/24
+12.12.126.3/24
+12.12.127.3/24
+12.12.128.3/24
+12.12.129.3/24
+12.12.130.3/24
+12.12.131.3/24
+12.12.132.3/24
+12.12.133.3/24
+12.12.134.3/24
+12.12.135.3/24
+12.12.136.3/24
+12.12.137.3/24
+12.12.138.3/24
+12.12.139.3/24
+12.12.140.3/24
+12.12.141.3/24
+12.12.142.3/24
+12.12.143.3/24
+12.12.144.3/24
+12.12.145.3/24
+12.12.146.3/24
+12.12.147.3/24
+12.12.148.3/24
+12.12.149.3/24
+12.12.150.3/24
+12.12.151.3/24
+12.12.152.3/24
+12.12.153.3/24
+12.12.154.3/24
+12.12.155.3/24
+12.12.156.3/24
+12.12.157.3/24
+12.12.158.3/24
+12.12.159.3/24
+12.12.160.3/24
+12.12.161.3/24
+12.12.162.3/24
+12.12.163.3/24
+12.12.164.3/24
+12.12.165.3/24
+12.12.166.3/24
+12.12.167.3/24
+12.12.168.3/24
+12.12.169.3/24
+12.12.170.3/24
+12.12.171.3/24
+12.12.172.3/24
+12.12.173.3/24
+12.12.174.3/24
+12.12.175.3/24
+12.12.176.3/24
+12.12.177.3/24
+12.12.178.3/24
+12.12.179.3/24
+12.12.180.3/24
+12.12.181.3/24
+12.12.182.3/24
+12.12.183.3/24
+12.12.184.3/24
+12.12.185.3/24
+12.12.186.3/24
+12.12.187.3/24
+12.12.188.3/24
+12.12.189.3/24
+12.12.190.3/24
+12.12.191.3/24
+12.12.192.3/24
+12.12.193.3/24
+12.12.194.3/24
+12.12.195.3/24
+12.12.196.3/24
+12.12.197.3/24
+12.12.198.3/24
+12.12.199.3/24
+12.12.200.3/24
+12.12.201.3/24
+12.12.202.3/24
+12.12.203.3/24
+12.12.204.3/24
+12.12.205.3/24
+12.12.206.3/24
+12.12.207.3/24
+12.12.208.3/24
+12.12.209.3/24
+12.12.210.3/24
+12.12.211.3/24
+12.12.212.3/24
+12.12.213.3/24
+12.12.214.3/24
+12.12.215.3/24
+12.12.216.3/24
+12.12.217.3/24
+12.12.218.3/24
+12.12.219.3/24
+12.12.220.3/24
+12.12.221.3/24
+12.12.222.3/24
+12.12.223.3/24
+12.12.224.3/24
+12.12.225.3/24
+12.12.226.3/24
+12.12.227.3/24
+12.12.228.3/24
+12.12.229.3/24
+12.12.230.3/24
+12.12.231.3/24
+12.12.232.3/24
+12.12.233.3/24
+12.12.234.3/24
+12.12.235.3/24
+12.12.236.3/24
+12.12.237.3/24
+12.12.238.3/24
+12.12.239.3/24
+12.12.240.3/24
+12.12.241.3/24
+12.12.242.3/24
+12.12.243.3/24
+12.12.244.3/24
+12.12.245.3/24
+12.12.246.3/24
+12.12.247.3/24
+12.12.248.3/24
+12.12.249.3/24
+12.12.250.3/24
+12.12.251.3/24
+12.12.252.3/24
+12.12.253.3/24
+12.12.254.3/24
+12.12.255.3/24
diff --git a/tests/example-vector.c b/tests/example-vector.c
new file mode 100644 (file)
index 0000000..48403fc
--- /dev/null
@@ -0,0 +1,163 @@
+#include <arpa/inet.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include "vector.h"
+
+typedef struct _ipslot {
+    int family;
+    unsigned int bits;
+    char data[16];
+} ipslot;
+
+static struct {
+    vector data;
+    int fill;
+} table;
+
+#define BUFSZ 10000
+static struct {
+    char data[ BUFSZ ];
+    int cur;
+    int end;
+} stream;
+
+static int readline(int fd,char **outp) {
+    for ( ;; ) {
+       char *curp = stream.data + stream.cur;
+       char *endp = stream.data + stream.end;
+       char *top = curp;
+       while  ( curp < endp ) {
+           if ( *(curp++) == '\n' ) {
+               stream.cur = curp - stream.data;
+               (*outp) = top;
+               return curp - top;
+           }
+       }
+       if ( top != stream.data ) {
+           curp = stream.data;
+           while ( top < endp ) {
+               *(curp++) = *(top++);
+           }
+           endp = curp;
+           stream.end = endp - stream.data;
+       }
+       stream.cur = 0;
+       ssize_t n = read( fd, endp, BUFSZ - stream.end );
+       if ( n <= 0 ) {
+           if ( stream.end == 0 ) {
+               return -1; // No more data
+           }
+           (*outp) = stream.data;
+           return stream.end;
+       }
+       stream.end += n;
+    }
+    //unreachable
+}
+
+// Scan to NUL, CR or c. Return pointer not including character.
+static char *scanto(char *p, char c) {
+    while ( *p && *p != '\n' && *p != c ) {
+       p++;
+    }
+    return p;
+}
+
+static int parse_addr(char *line,ipslot *addr) {
+    char *end = scanto( line, '\n' );
+    char *slash = scanto( line, '/' );
+    *slash = 0;
+    *end = 0;
+    if ( scanto( line, ':' ) != slash ) {
+       fprintf( stdout, "AF_INET6: %s\n", line );
+       if ( inet_pton( AF_INET6, line, addr->data ) == 1 ) {
+           addr->family = AF_INET6;
+           addr->bits = 128;
+       } else {
+           return 1;
+       }
+    } else if ( inet_pton( AF_INET, line, addr->data ) == 1 ) {
+       fprintf( stdout, "AF_INET: %s\n", line );
+       addr->family = AF_INET;
+       addr->bits = 32;
+    } else {
+       return 1;
+    }
+    if ( slash != end && sscanf( slash+1, "%u", &addr->bits ) != 1 ) {
+       return 1;
+    }
+    return 0;
+}
+
+static void add_entry(ipslot *tmp) {
+    ipslot *p = (ipslot *) malloc( sizeof( ipslot ) );
+    memmove( p, tmp, sizeof( ipslot ) );
+    if ( table.data.size == table.fill ) {
+       (void) vector_resize( &table.data, table.fill + 256, 0, 0 );
+    }
+    vector_set( &table.data, table.fill++, p );
+}
+
+static void load_file(const char *filename) {
+    int fd = open( filename, O_RDONLY );
+    if ( fd < 0 ) {
+       perror( filename );
+       exit( errno );
+    }
+    char *line;
+    int n;
+    while ( ( n = readline( fd, &line ) ) >= 0 ) {
+       ipslot addr;
+       if ( parse_addr( line, &addr ) ) {
+           fprintf( stderr, "Bad address: %s\n", line );
+           continue;
+       }
+       add_entry( &addr );
+    }
+}
+
+static int int_reclaim(vector *pv,unsigned long index,void *item,void *data) {
+    return 0;
+}
+
+static int dumpitem(const unsigned long index,const void *item) {
+    fprintf( stdout, "[%ld] %p\n", index, item );
+    return 0;
+}
+
+static int dump_ipslot(const unsigned long index,const void *item) {
+    static char buffer[100];
+    ipslot *ip = (ipslot*) item;
+    const char *p = inet_ntop( ip->family, ip->data, buffer, 100 );
+    fprintf( stdout, "[%ld] %s/%d\n", index, p, ip->bits );
+    return 0;
+}
+
+int main(int argc,char **argv) {
+    vector test = { 0 };
+    vector_resize( &test, 100, 0, 0 );
+    vector_set( &test, 5, (void*) 500 );
+    vector_set( &test, 55, (void*) 600 );
+    vector_resize( &test, 300, 0, 0 );
+    vector_set( &test, 55, (void*) 650 );
+    vector_resize( &test, 30000, 0, 0 );
+    vector_set( &test, 22255, (void*) 26 );
+    vector_dump( &test, dumpitem );
+    vector_resize( &test, 100, int_reclaim, 0 );
+    vector_set( &test, 5, (void*) 2 );
+    vector_dump( &test, dumpitem );
+
+    int i;
+    for ( i = 1; i < argc; i++ ) {
+       load_file( argv[ i ] );
+    }
+    vector_dump( &table.data, dump_ipslot );
+
+    return 0;
+}
index 8dadf00a440873ad7ee4e4962ee50c822b75db29..129ba02990e8e01d03ec6364b1092ac418e654d9 100644 (file)
@@ -7,6 +7,31 @@
  * void*, and an index is "unsigned long" (64 bits).
  */
 
+#if VECTOR_LEVEL_BITS == 4
+typedef union {
+    vector_index as_whole;
+    struct {
+       unsigned int msb:4; unsigned int lsb:4;
+    } __attribute__ ((__packed__)) as_byte[8];
+} vector_indexing;
+
+#define VECTOR_PART_BYTE(i,p) ((vector_indexing*)(i))->as_byte[ (p)/2 ]
+
+static int VECTOR_INDEX_PART(vector_index *index,int part) {
+    if ( part & 1 ) {
+       return VECTOR_PART_BYTE(index,part).lsb;
+    }
+    return VECTOR_PART_BYTE(index,part).msb;
+}
+
+static int VECTOR_INDEX_PART_INC(vector_index *index,int part) {
+    if ( part & 1 ) {
+       return ++VECTOR_PART_BYTE(index,part).lsb;
+    }
+    return ++VECTOR_PART_BYTE(index,part).msb;
+}
+#endif
+
 /**
  * Advances a vector index to the next used slot at or below the
  * given level, starting from the indexed entry (inclusive) and up.
@@ -35,7 +60,7 @@ static void **vector_level_next_used(
                *p = 0;
            }
        }
-       if ( ++VECTOR_INDEX_PART( index, level ) == 0 ) {
+       if ( VECTOR_INDEX_PART_INC( index, level ) == 0 ) {
            break; // cycling this level => nothing found
        }
     }
@@ -149,15 +174,21 @@ int vector_resize(
        vector_page *entries;
        vector_page **pp = &pv->entries;
        while ( level.old-- > level.new ) {
-           pp = (vector_page **)(*pp)[0];
+           if ( pp ) {
+               pp = (vector_page **)(*pp)[0];
+           }
        }
        if ( pp != &pv->entries ) {
            entries = pv->entries;
-           pv->entries = *pp;
-           *pp = 0;
+           if ( pp ) {
+               pv->entries = *pp;
+               *pp = 0; // Detach subtree
+           } else {
+               pv->entries = 0;
+           }
            vector_reclaim( entries, level.old );
        }
-       if ( new_size == 0 ) {
+       if ( new_size == 0 && pv->entries ) {
            free( pv->entries );
            pv->entries = 0;
        }
index 1227860e9978007a87dd4dda0a00992521be0f8b..d84c08c1f8222603d8490015732b435f7bfa8ffc 100644 (file)
@@ -22,7 +22,7 @@ typedef unsigned long vector_index;
  * Macro: VECTOR_INDEX_BITS
  * This defines the number of bits of a vector index
  */
-#define VECTOR_INDEX_BITS sizeof( vector_index )
+#define VECTOR_INDEX_BITS ( sizeof( vector_index ) * 8 )
 
 /*!
  * Macro: VECTOR_INDEX_FIELDS
@@ -44,27 +44,6 @@ typedef unsigned long vector_index;
  */
 typedef void* vector_page[ VECTOR_SLOTS ];
 
-/*!
- * Type: vector_field
- * This is VECTOR_LEVEL_BITS size bit field
- */
-typedef struct { int bits:VECTOR_LEVEL_BITS; } vector_field;
-
-/*!
- * Type: vector_indexing
- *
- * A vector index is ether viewed in whole as an VECTOR_INDEX_BITS wide
- * unsigned, or in levels as a packed array of vector_field index
- * parts. This implementation assumes LE integer layout.
- */
-typedef union {
-    vector_index whole;                     // as a whole
-    vector_field level[ VECTOR_INDEX_FIELDS ];  // qua bits fields
-} vector_indexing;
-
-// The indexing part for level part p in index i
-#define VECTOR_INDEX_PART(i,p) (((vector_indexing*)(i))->level[ p ].bits)
-
 /*!
  * Type: vector
  *