Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

buflib: optimize getting start of block from end of block

The block header has a variable length due to the embedded name.
The name length is stored at the back of the header after the
name, in order to allow finding the start of the header if only
the user data pointer is known (eg. from the handle table).

The name length is actually not interesting in itself; storing
the total length of the block header instead is marginally more
efficient, saving one addition in handle_to_block().

Instead the extra arithmetic must be done by buflib_get_name(),
which is a much less common operation than handle_to_block().

Change-Id: Ia339a1d2f556a11a49deae0871203e70548bd234

+12 -10
+12 -10
firmware/buflib.c
··· 65 65 * H - handle table entry pointer 66 66 * C - pointer to struct buflib_callbacks 67 67 * c - variable sized string identifier 68 - * L2 - second length marker for string identifier 68 + * L2 - length of the metadata 69 69 * crc - crc32 protecting buflib metadata integrity 70 70 * X - actual payload 71 71 * Y - unallocated space ··· 240 240 * has already been allocated but not the data */ 241 241 if (!data) 242 242 return NULL; 243 - volatile size_t len = data[-2].val; 244 - return data - (len + 4); 243 + 244 + return data - data[-2].val; 245 245 } 246 246 247 247 /* Shrink the handle table, returning true if its size was reduced, false if ··· 627 627 /* Set up the allocated block, by marking the size allocated, and storing 628 628 * a pointer to the handle. 629 629 */ 630 - union buflib_data *name_len_slot, *crc_slot; 630 + union buflib_data *header_size_slot, *crc_slot; 631 631 block->val = size; 632 632 block[1].handle = handle; 633 633 block[2].ops = ops; 634 634 if (name_len > 0) 635 635 strcpy(block[3].name, name); 636 - name_len_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len); 637 - name_len_slot->val = 1 + name_len/sizeof(union buflib_data); 638 - crc_slot = (union buflib_data*)(name_len_slot + 1); 636 + header_size_slot = (union buflib_data*)B_ALIGN_UP(block[3].name + name_len); 637 + header_size_slot->val = 5 + name_len/sizeof(union buflib_data); 638 + crc_slot = (union buflib_data*)(header_size_slot + 1); 639 639 crc_slot->crc = crc_32((void *)block, 640 640 (crc_slot - block)*sizeof(union buflib_data), 641 641 0xffffffff); ··· 742 742 free_space_at_end(struct buflib_context* ctx) 743 743 { 744 744 /* subtract 5 elements for 745 - * val, handle, name_len, ops and the handle table entry*/ 745 + * val, handle, meta_len, ops and the handle table entry*/ 746 746 ptrdiff_t diff = (ctx->last_handle - ctx->alloc_end - 5); 747 747 diff -= 16; /* space for future handles */ 748 748 diff *= sizeof(union buflib_data); /* make it bytes */ ··· 933 933 { 934 934 union buflib_data *data = ALIGN_DOWN(buflib_get_data(ctx, handle), sizeof (*data)); 935 935 size_t len = data[-2].val; 936 - if (len <= 1) 936 + if (len <= 5) 937 937 return NULL; 938 - return data[-len-1].name; 938 + 939 + data -= len; 940 + return data[3].name; 939 941 } 940 942 941 943 #ifdef DEBUG