Line data Source code
1 : #include "fd_trusted_slots.h" 2 : 3 : ulong 4 0 : fd_trusted_slots_align( void ) { 5 0 : return alignof(fd_trusted_slots_t); 6 0 : } 7 : 8 : ulong 9 0 : fd_trusted_slots_footprint( ulong slots_max ) { 10 0 : ulong l = FD_LAYOUT_INIT; 11 0 : l = FD_LAYOUT_APPEND( l, alignof(fd_trusted_slots_t), sizeof(fd_trusted_slots_t) ); 12 0 : l = FD_LAYOUT_APPEND( l, fd_slot_pool_align(), fd_slot_pool_footprint( slots_max ) ); 13 0 : l = FD_LAYOUT_APPEND( l, fd_slot_treap_align(), fd_slot_treap_footprint( slots_max ) ); 14 0 : l = FD_LAYOUT_FINI( l, fd_trusted_slots_align() ); 15 0 : return l; 16 0 : } 17 : 18 : void * 19 0 : fd_trusted_slots_new( void * shmem, ulong slot_max ) { 20 0 : if( FD_UNLIKELY( !shmem ) ) { 21 0 : FD_LOG_WARNING(( "NULL trusted_slots" )); 22 0 : return NULL; 23 0 : } 24 : 25 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_trusted_slots_align() ) ) ) { 26 0 : FD_LOG_WARNING(( "misaligned trusted_slots" )); 27 0 : return NULL; 28 0 : } 29 : 30 0 : FD_SCRATCH_ALLOC_INIT(l, shmem); 31 0 : fd_trusted_slots_t * trusted_slots = (fd_trusted_slots_t *)FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_trusted_slots_t), sizeof(fd_trusted_slots_t) ); 32 : 33 0 : void * slot_pool_mem = fd_slot_pool_new( FD_SCRATCH_ALLOC_APPEND( l, fd_slot_pool_align(), fd_slot_pool_footprint( slot_max ) ), slot_max ); 34 0 : if( !slot_pool_mem ) { 35 0 : FD_LOG_WARNING(( "fd_slot_pool_new failed" )); 36 0 : return NULL; 37 0 : } 38 : 39 0 : void * slot_treap_mem = fd_slot_treap_new( FD_SCRATCH_ALLOC_APPEND( l, fd_slot_treap_align(), fd_slot_treap_footprint( slot_max ) ), slot_max ); 40 0 : if( !slot_treap_mem ) { 41 0 : FD_LOG_WARNING(( "fd_slot_heap_new failed" )); 42 0 : return NULL; 43 0 : } 44 : 45 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL ); 46 0 : if ( scratch_top > (ulong)shmem + fd_trusted_slots_footprint( slot_max ) ) { 47 0 : FD_LOG_WARNING(( "not enough space allocated for trusted_slots" )); 48 0 : return NULL; 49 0 : } 50 : 51 0 : trusted_slots->slot_pool = (fd_slot_ele_t *)slot_pool_mem; 52 0 : trusted_slots->slot_treap = (fd_slot_treap_t *)slot_treap_mem; 53 : 54 0 : return shmem; 55 0 : } 56 : 57 : fd_trusted_slots_t * 58 0 : fd_trusted_slots_join( void * shmem ) { 59 0 : if( FD_UNLIKELY( !shmem ) ) { 60 0 : FD_LOG_WARNING(( "NULL trusted_slots" )); 61 0 : return NULL; 62 0 : } 63 : 64 0 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)shmem, fd_trusted_slots_align() ) ) ) { 65 0 : FD_LOG_WARNING(( "misaligned trusted_slots" )); 66 0 : return NULL; 67 0 : } 68 : 69 0 : fd_trusted_slots_t * trusted_slots = (fd_trusted_slots_t *)shmem; 70 0 : trusted_slots->slot_pool = fd_slot_pool_join( trusted_slots->slot_pool ); 71 0 : if( !trusted_slots->slot_pool ) { 72 0 : FD_LOG_WARNING(( "fd_slot_pool_join failed" )); 73 0 : return NULL; 74 0 : } 75 : 76 0 : trusted_slots->slot_treap = fd_slot_treap_join( trusted_slots->slot_treap ); 77 0 : if( !trusted_slots->slot_treap ) { 78 0 : FD_LOG_WARNING(( "fd_slot_heap_join failed" )); 79 0 : return NULL; 80 0 : } 81 : 82 0 : return trusted_slots; 83 0 : } 84 : 85 : int 86 : fd_trusted_slots_find( fd_trusted_slots_t * trusted_slots, 87 0 : ulong slot ) { 88 0 : fd_slot_ele_t * ele = fd_slot_treap_ele_query( trusted_slots->slot_treap, slot, trusted_slots->slot_pool ); 89 0 : return (ele!=NULL); 90 0 : } 91 : 92 : void 93 : fd_trusted_slots_add( fd_trusted_slots_t * trusted_slots, 94 0 : ulong slot ) { 95 0 : if( fd_trusted_slots_find( trusted_slots, slot ) ) { 96 0 : return; 97 0 : } 98 : 99 0 : fd_slot_ele_t * ele = fd_slot_pool_ele_acquire( trusted_slots->slot_pool ); 100 0 : ele->key = slot; 101 : 102 0 : fd_slot_treap_ele_insert( trusted_slots->slot_treap, ele, trusted_slots->slot_pool ); 103 0 : } 104 : 105 : void 106 : fd_trusted_slots_publish( fd_trusted_slots_t * trusted_slots, 107 0 : ulong root ) { 108 0 : fd_slot_ele_t * remove_ele = NULL; 109 0 : for( fd_slot_treap_fwd_iter_t iter = fd_slot_treap_fwd_iter_init( trusted_slots->slot_treap, trusted_slots->slot_pool ); 110 0 : !fd_slot_treap_fwd_iter_done( iter ); 111 0 : iter = fd_slot_treap_fwd_iter_next( iter, trusted_slots->slot_pool ) ) { 112 0 : fd_slot_ele_t * ele = fd_slot_treap_fwd_iter_ele( iter, trusted_slots->slot_pool ); 113 0 : if( root>ele->key ) { 114 : /* this next slot is behind the root, delete it */ 115 0 : remove_ele = ele; 116 0 : } 117 : 118 0 : if( remove_ele!=NULL ) { 119 0 : fd_slot_treap_ele_remove( trusted_slots->slot_treap, remove_ele, trusted_slots->slot_pool ); 120 0 : fd_slot_pool_ele_release( trusted_slots->slot_pool, remove_ele ); 121 0 : remove_ele = NULL; 122 0 : } 123 0 : } 124 : 125 0 : if( remove_ele!=NULL ) { 126 0 : fd_slot_treap_ele_remove( trusted_slots->slot_treap, remove_ele, trusted_slots->slot_pool ); 127 0 : fd_slot_pool_ele_release( trusted_slots->slot_pool, remove_ele ); 128 0 : } 129 0 : }