Line data Source code
1 : #include "fd_borrowed_account.h" 2 : #include "fd_acc_mgr.h" 3 : 4 : fd_borrowed_account_t * 5 6573885 : fd_borrowed_account_init( void * ptr ) { 6 6573885 : if( FD_UNLIKELY( !ptr ) ) { 7 0 : FD_LOG_WARNING(( "NULL ptr" )); 8 0 : return NULL; 9 0 : } 10 : 11 6573885 : if( FD_UNLIKELY( !fd_ulong_is_aligned( (ulong)ptr, alignof(fd_borrowed_account_t) ) ) ) { 12 0 : FD_LOG_WARNING(( "misaligned ptr" )); 13 0 : return NULL; 14 0 : } 15 : 16 6573885 : memset(ptr, 0, FD_BORROWED_ACCOUNT_FOOTPRINT); 17 : 18 6573885 : fd_borrowed_account_t * ret = (fd_borrowed_account_t *)ptr; 19 6573885 : ret->starting_dlen = ULONG_MAX; 20 6573885 : ret->starting_lamports = ULONG_MAX; 21 6573885 : ret->account_found = 1; 22 : 23 6573885 : FD_COMPILER_MFENCE(); 24 6573885 : ret->magic = FD_BORROWED_ACCOUNT_MAGIC; 25 6573885 : FD_COMPILER_MFENCE(); 26 : 27 6573885 : return ret; 28 6573885 : } 29 : 30 : void 31 : fd_borrowed_account_resize( fd_borrowed_account_t * borrowed_account, 32 2493 : ulong dlen ) { 33 : 34 : /* Because the memory for an account is preallocated for the transaction 35 : up to the max account size, we only need to zero out bytes (for the case 36 : where the account grew) and update the account dlen. */ 37 : 38 2493 : ulong old_sz = borrowed_account->meta->dlen; 39 2493 : ulong new_sz = dlen; 40 2493 : ulong memset_sz = fd_ulong_sat_sub( new_sz, old_sz ); 41 2493 : fd_memset( borrowed_account->data+old_sz, 0, memset_sz ); 42 : 43 2493 : borrowed_account->meta->dlen = dlen; 44 2493 : } 45 : 46 : fd_borrowed_account_t * 47 : fd_borrowed_account_make_modifiable( fd_borrowed_account_t * borrowed_account, 48 33207 : void * buf ) { 49 33207 : uchar * new_raw_data = (uchar *)buf; 50 33207 : if( borrowed_account->data != NULL ) { 51 0 : FD_LOG_ERR(( "borrowed account is already modifiable" )); 52 0 : } 53 : 54 33207 : ulong dlen = ( borrowed_account->const_meta != NULL ) ? borrowed_account->const_meta->dlen : 0; 55 : 56 33207 : if( borrowed_account->const_meta != NULL ) { 57 27264 : fd_memcpy( new_raw_data, (uchar *)borrowed_account->const_meta, sizeof(fd_account_meta_t)+dlen ); 58 27264 : } else { 59 : /* Account did not exist, set up metadata */ 60 5943 : fd_account_meta_init( (fd_account_meta_t *)new_raw_data ); 61 5943 : } 62 : 63 33207 : borrowed_account->const_meta = borrowed_account->meta = (fd_account_meta_t *)new_raw_data; 64 33207 : borrowed_account->const_data = borrowed_account->data = new_raw_data + sizeof(fd_account_meta_t); 65 33207 : borrowed_account->meta->dlen = dlen; 66 : 67 33207 : return borrowed_account; 68 33207 : } 69 : 70 : fd_borrowed_account_t * 71 : fd_borrowed_account_make_readonly_copy( fd_borrowed_account_t * borrowed_account, 72 135 : void * buf ) { 73 135 : uchar * new_raw_data = (uchar *)buf; 74 135 : if( borrowed_account->data != NULL ) { 75 0 : FD_LOG_ERR(( "borrowed account is already modifiable" )); 76 0 : } 77 : 78 135 : ulong dlen = ( borrowed_account->const_meta != NULL ) ? borrowed_account->const_meta->dlen : 0; 79 : 80 135 : if( borrowed_account->const_meta != NULL ) { 81 135 : fd_memcpy( new_raw_data, (uchar *)borrowed_account->const_meta, sizeof(fd_account_meta_t)+dlen ); 82 135 : } else { 83 : /* Account did not exist, set up metadata */ 84 0 : fd_account_meta_init( (fd_account_meta_t *)new_raw_data ); 85 0 : } 86 : 87 135 : borrowed_account->orig_meta = borrowed_account->const_meta = (fd_account_meta_t *)new_raw_data; 88 135 : borrowed_account->orig_data = borrowed_account->const_data = new_raw_data + sizeof(fd_account_meta_t); 89 135 : ((fd_account_meta_t *)new_raw_data)->dlen = dlen; 90 : 91 135 : return borrowed_account; 92 135 : } 93 : 94 : void * 95 0 : fd_borrowed_account_restore( fd_borrowed_account_t * borrowed_account ) { 96 0 : fd_account_meta_t * meta = borrowed_account->meta; 97 0 : uint is_changed = meta != borrowed_account->orig_meta; 98 : 99 0 : borrowed_account->const_meta = borrowed_account->orig_meta; 100 0 : borrowed_account->const_data = borrowed_account->orig_data; 101 0 : borrowed_account->const_rec = borrowed_account->orig_rec; 102 : 103 0 : if( is_changed ) { 104 0 : return meta; 105 0 : } 106 : 107 0 : return NULL; 108 0 : } 109 : 110 : void * 111 0 : fd_borrowed_account_destroy( fd_borrowed_account_t * borrowed_account ) { 112 0 : return borrowed_account->meta; 113 0 : }