+#include <stdlib.h>
+#include "pvector.h"
+
+/**
+ * Representing a vector of void* accessible via an indexing structure
+ * as levels of same-size pages. A "pvector_page" is a contiguous
+ * array of 256 void*.
+ */
+
+/**
+ * Advances a pvector index to the next used slot at or below the
+ * given level, starting from the indexed entry (inclusive) and up.
+ * The function will free any empty pages it discovers, and then
+ * update the index slots accordingly. The given index is advanced
+ * cyclically to match the found slot. The function returns a slot
+ * pointer to the used slot, if any, and 0 otherwise.
+ */
+static void **pvector_level_next_used(
+ pvector_page *page,unsigned int *index,int level,int end) {
+ void **p = (void**)&(*page)[ ((pvector_index*)index)->level[ level ] ];
+ for( ; *index < end; p++ ) {
+ if ( *p ) {
+ if ( level == 0 ) {
+ return p; // This is a used entry
+ }
+ // *p is an index that needs to be inspected recursively
+ int whole = ((pvector_index*)index)->level[ level - 1 ] == 0;
+ void **x = pvector_level_next_used( *p, index, level - 1, end );
+ if ( x ) {
+ return x; // Used slot was found; return it.
+ }
+ // The page *p is all empty, so can/should be reclaimed.
+ if ( whole ) {
+ free( *p );
+ *p = 0;
+ }
+ }
+ if ( ++(((pvector_index*)index)->level[ level ]) == 0 ) {
+ break; // cycling this level => nothing found
+ }
+ }
+ return 0;
+}
+
+// Find the next used slot at given index or later
+void **pvector_next_used(pvector *pv,unsigned int *index,
+ int (*reclaim)(pvector *,int,void*) )
+{
+ int levels = PV_LEVELS( pv->size );
+ while ( *index < pv->size ) {
+ void **slot = pvector_level_next_used(
+ pv->entries, index, levels - 1, pv->size ) ;
+ if ( slot && *slot ) {
+ if ( reclaim && reclaim( pv, *index, *slot ) == 0 ) {
+ *slot = 0;
+ } else {
+ return *slot;
+ }
+ }
+ (*index)++;
+ }
+ return 0;
+}
+
+// Reclaim tree of unused pages
+static void pvector_reclaim(pvector_page *page) {
+ int i = 0;
+ for ( ; i < 256; i++ ) {
+ if ( (*page)[i] ) {
+ pvector_reclaim( (pvector_page *) (*page)[i] );
+ }
+ }
+ free( page );
+}
+
+// Resize vector, using the reclaim function as needed, to handle any
+// excess items or to veto the resize. Returns the index of the veto, if
+// any, or <0 otherwise, with -1 indicating success and -2 indicating
+// OOM while growing.
+//
+// Nothe that resizing may result in the introduction/removal of
+// indexing levels and pages, so as to keep the leveling accurate for
+// the size.
+int pvector_resize(
+ pvector *pv,unsigned int new_size,
+ int (*reclaim)(pvector *,int,void*) )
+{
+ // Table of number of slots for a level above that of the number
+ // at the prior lower level.
+ // The first level (i.e., level 0) adds 255 slots to the one slot
+ // of no index page. Level 1 adds 255*256 slots, level 2 adds
+ // 255*(256^2), and generically level i adds 255*(256^i) slots.
+ static int level_delta[8];
+ if ( level_delta[ 0 ] == 0 ) {
+ int d = 1;
+ int i;
+ for ( i = 0; i < 8; i++ ) {
+ level_delta[ i ] = 255 * d;
+ d = 256 * d;
+ }
+ }
+ struct {
+ int old;
+ int new;
+ } level = {
+ PV_LEVELS( pv->size ),
+ PV_LEVELS( new_size )
+ };
+ if ( pv->entries == 0 ) {
+ pv->size = new_size;
+ return 0;
+ }
+ // A shrinking pvector might be veto-ed
+ if ( new_size < pv->size ) {
+ unsigned int index = new_size;
+ void **slot = pvector_next_used( pv, &index, reclaim );
+ if ( slot ) {
+ return index;
+ }
+ // At this point we know that there are no slots used after
+ // the new_size size, so now it's time to remove and reclaim
+ // any superflouous top level pages.
+ pvector_page *entries;
+ pvector_page **pp = &pv->entries;
+ while ( level.old-- > level.new ) {
+ pp = (pvector_page **)(*pp)[0];
+ }
+ if ( pp != &pv->entries ) {
+ entries = pv->entries;
+ pv->entries = *pp;
+ *pp = 0;
+ pvector_reclaim( entries );
+ }
+ } else {
+ // pvector is growing. Maybe insert levels.
+ while ( level.old < level.new ) {
+ pvector_page *p = (pvector_page *)
+ calloc( 1, sizeof( pvector_page ) );
+ if ( p == 0 ) {
+ return -2; // OOM
+ }
+ (*p)[0] = pv->entries;
+ pv->entries = p;
+ pv->size += level_delta[ level.old++ ];
+ // Note that the last level addition might make the size
+ // larger than requested, which gets corrected below.
+ }
+ }
+ pv->size = new_size;
+ return -1;
+}
+
+// Return a pointer to the indexed item the given page level, adding
+// intermediate pages if requested. Returns 0 if addition fails (OOM),
+// or if not requested and page is missing.
+// Level 0 = pointer to the item entry itself.
+// Level PVECTORLEVELS( pv->size ) - 1 =
+static void **pvector_access(
+ pvector *pv,unsigned int index,int level,int add)
+{
+ if ( index >= pv->size ) {
+ return 0;
+ }
+ void **page = (void**) &pv->entries;
+ int i = PV_LEVELS( pv->size );
+ while ( i-- > level ) {
+ if ( add && (*page) == 0 ) {
+ (*page) = calloc( 256, sizeof( void* ) );
+ }
+ page = (*page);
+ if ( page == 0 ) {
+ return 0;
+ }
+ page += ((pvector_index)index).level[ i ];
+ }
+ return page;
+}
+
+// Map index into a value slot
+void **pvector_entry(pvector *pv,unsigned int index) {
+ return pvector_access( pv, index, 0, 1 );
+}
+
+inline void pvector_set(pvector *pv,unsigned int index,void *value) {
+ void **p = pvector_entry( pv, index );
+ *p = value;
+}
+
+inline void *pvector_get(pvector *pv,unsigned int index) {
+ return *(pvector_entry( pv, index ));
+}
+