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.

Buffering: Remove statically-sized path buffer from handle struct

Paths are stored after the structure at their actual length plus
any aligment padding. In principle, any type of auxilliary data
could go there.

Change-Id: Ic5487dc4089781b5cc52414d1691ba6d9dc1893c

+27 -18
+27 -18
apps/buffering.c
··· 82 82 83 83 struct memory_handle { 84 84 struct lld_node hnode; /* Handle list node (first!) */ 85 - struct lld_node mrunode;/* MRU list node */ 86 - int id; /* A unique ID for the handle (after list!) */ 85 + struct lld_node mrunode;/* MRU list node (second!) */ 86 + size_t size; /* Size of this structure + its auxilliary data */ 87 + int id; /* A unique ID for the handle */ 87 88 enum data_type type; /* Type of data buffered with this handle */ 88 89 uint8_t flags; /* Handle property flags */ 89 90 int8_t pinned; /* Count of pinnings */ 90 91 int8_t signaled; /* Stop any attempt at waiting to get the data */ 91 - char path[MAX_PATH]; /* Path if data originated in a file */ 92 92 int fd; /* File descriptor to path (-1 if closed) */ 93 93 size_t data; /* Start index of the handle's data buffer */ 94 94 size_t ridx; /* Read pointer, relative to the main buffer */ ··· 97 97 off_t start; /* Offset at which we started reading the file */ 98 98 off_t pos; /* Read position in file */ 99 99 off_t volatile end; /* Offset at which we stopped reading the file */ 100 + char path[]; /* Path if data originated in a file */ 100 101 }; 102 + 103 + /* Minimum allowed handle movement */ 104 + #define MIN_MOVE_DELTA sizeof(struct memory_handle) 101 105 102 106 struct buf_message_data 103 107 { ··· 344 348 NULL if there memory_handle itself cannot be allocated or if the 345 349 data_size cannot be allocated and alloc_all is set. */ 346 350 static struct memory_handle * 347 - add_handle(unsigned int flags, size_t data_size, size_t *data_out) 351 + add_handle(unsigned int flags, size_t data_size, const char *path, 352 + size_t *data_out) 348 353 { 349 354 /* Gives each handle a unique id */ 350 355 if (num_handles >= BUF_MAX_HANDLES) ··· 376 381 } 377 382 378 383 /* Align to align size up */ 384 + size_t pathsize = path ? strlen(path) + 1 : 0; 379 385 size_t adjust = ALIGN_UP(widx, alignof(struct memory_handle)) - widx; 380 386 size_t index = ringbuf_add(widx, adjust); 381 - size_t len = data_size + sizeof(struct memory_handle); 387 + size_t handlesize = ALIGN_UP(sizeof(struct memory_handle) + pathsize, 388 + alignof(struct memory_handle)); 389 + size_t len = handlesize + data_size; 382 390 383 391 /* First, will the handle wrap? */ 384 392 /* If the handle would wrap, move to the beginning of the buffer, 385 393 * or if the data must not but would wrap, move it to the beginning */ 386 - if (index + sizeof(struct memory_handle) > buffer_len || 394 + if (index + handlesize > buffer_len || 387 395 (!(flags & H_CANWRAP) && index + len > buffer_len)) { 388 396 index = 0; 389 397 } ··· 405 413 /* There is enough space for the required data, initialize the struct */ 406 414 struct memory_handle *h = ringbuf_ptr(index); 407 415 416 + h->size = handlesize; 408 417 h->id = next_handle_id(); 409 418 h->flags = flags; 410 419 h->pinned = 0; /* Can be moved */ 411 420 h->signaled = 0; /* Data can be waited for */ 412 421 422 + /* Save the provided path */ 423 + memcpy(h->path, path, pathsize); 424 + 413 425 /* Return the start of the data area */ 414 - *data_out = ringbuf_add(index, sizeof (struct memory_handle)); 426 + *data_out = ringbuf_add(index, handlesize); 415 427 416 428 return h; 417 429 } ··· 457 469 if (h == NULL || (src = *h) == NULL) 458 470 return false; 459 471 460 - size_t size_to_move = sizeof(struct memory_handle) + data_size; 472 + size_t size_to_move = src->size + data_size; 461 473 462 474 /* Align to align size down */ 463 475 size_t final_delta = *delta; 464 476 final_delta = ALIGN_DOWN(final_delta, alignof(struct memory_handle)); 465 - if (final_delta < sizeof(struct memory_handle)) { 466 - /* It's not legal to move less than the size of the struct */ 477 + if (final_delta < MIN_MOVE_DELTA) { 478 + /* It's not legal to move less than MIN_MOVE_DELTA */ 467 479 return false; 468 480 } 469 481 ··· 490 502 if (correction) { 491 503 /* Align correction to align size up */ 492 504 correction = ALIGN_UP(correction, alignof(struct memory_handle)); 493 - if (final_delta < correction + sizeof(struct memory_handle)) { 494 - /* Delta cannot end up less than the size of the struct */ 505 + if (final_delta < correction + MIN_MOVE_DELTA) { 506 + /* Delta cannot end up less than MIN_MOVE_DELTA */ 495 507 return false; 496 508 } 497 509 newpos -= correction; ··· 918 930 /* ID3 case: allocate space, init the handle and return. */ 919 931 mutex_lock(&llist_mutex); 920 932 921 - h = add_handle(H_ALLOCALL, sizeof(struct mp3entry), &data); 933 + h = add_handle(H_ALLOCALL, sizeof(struct mp3entry), file, &data); 922 934 923 935 if (h) { 924 936 handle_id = h->id; 925 937 926 938 h->type = type; 927 - strlcpy(h->path, file, MAX_PATH); 928 939 h->fd = -1; 929 940 h->data = data; 930 941 h->ridx = data; ··· 983 994 984 995 mutex_lock(&llist_mutex); 985 996 986 - h = add_handle(hflags, padded_size, &data); 997 + h = add_handle(hflags, padded_size, file, &data); 987 998 if (!h) { 988 999 DEBUGF("%s(): failed to add handle\n", __func__); 989 1000 mutex_unlock(&llist_mutex); ··· 994 1005 handle_id = h->id; 995 1006 996 1007 h->type = type; 997 - strlcpy(h->path, file, MAX_PATH); 998 1008 h->fd = -1; 999 1009 1000 1010 #ifdef STORAGE_WANTS_ALIGN ··· 1080 1090 mutex_lock(&llist_mutex); 1081 1091 1082 1092 size_t data; 1083 - struct memory_handle *h = add_handle(H_ALLOCALL, size, &data); 1093 + struct memory_handle *h = add_handle(H_ALLOCALL, size, NULL, &data); 1084 1094 1085 1095 if (h) { 1086 1096 handle_id = h->id; ··· 1095 1105 } 1096 1106 1097 1107 h->type = type; 1098 - h->path[0] = '\0'; 1099 1108 h->fd = -1; 1100 1109 h->data = data; 1101 1110 h->ridx = data;