Skip to content

Commit

Permalink
srtp_stream_list_ctx_t_: standarize naming
Browse files Browse the repository at this point in the history
use 'capacity' and 'size' in order to represent the allocated storage
and the number of elements respectively.
  • Loading branch information
jmillan committed Jan 17, 2024
1 parent 5deeaea commit 1515b76
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions srtp/srtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4857,8 +4857,8 @@ typedef struct list_entry {

typedef struct srtp_stream_list_ctx_t_ {
list_entry *entries;
size_t capacity;
size_t size;
size_t available;
} srtp_stream_list_ctx_t_;

srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
Expand All @@ -4876,8 +4876,8 @@ srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
return srtp_err_status_alloc_fail;
}

list->size = INITIAL_STREAM_INDEX_SIZE;
list->available = INITIAL_STREAM_INDEX_SIZE;
list->capacity = INITIAL_STREAM_INDEX_SIZE;
list->size = 0;

*list_ptr = list;

Expand All @@ -4887,7 +4887,7 @@ srtp_err_status_t srtp_stream_list_alloc(srtp_stream_list_t *list_ptr)
srtp_err_status_t srtp_stream_list_dealloc(srtp_stream_list_t list)
{
/* list must be empty */
if (list->available != list->size) {
if (list->size != 0) {
return srtp_err_status_fail;
}

Expand All @@ -4908,32 +4908,31 @@ srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
* there is no space to hold the new entry in the entries buffer,
* double the size of the buffer.
*/
if (list->available == 0) {
size_t new_size = list->size * 2;
if (list->size == list->capacity) {
size_t new_capacity = list->capacity * 2;
list_entry *new_entries =
srtp_crypto_alloc(sizeof(list_entry) * new_size);
srtp_crypto_alloc(sizeof(list_entry) * new_capacity);
if (new_entries == NULL) {
return srtp_err_status_alloc_fail;
}

// copy previous entries into the new buffer
memcpy(new_entries, list->entries, sizeof(list_entry) * list->size);
memcpy(new_entries, list->entries, sizeof(list_entry) * list->capacity);
// release previous entries
srtp_crypto_free(list->entries);
// assign new entries to the list
list->entries = new_entries;
// update list info
list->size = new_size;
list->available = new_size / 2;
// update list capacity
list->capacity = new_capacity;
}

// fill the first available entry
size_t next_index = list->size - list->available;
size_t next_index = list->size;
list->entries[next_index].ssrc = stream->ssrc;
list->entries[next_index].stream = stream;

// update available value
list->available--;
// update size value
list->size++;

return srtp_err_status_ok;
}
Expand All @@ -4946,14 +4945,14 @@ srtp_err_status_t srtp_stream_list_insert(srtp_stream_list_t list,
void srtp_stream_list_remove(srtp_stream_list_t list,
srtp_stream_t stream_to_remove)
{
size_t end = list->size - list->available;
size_t end = list->size;

for (size_t i = 0; i < end; i++) {
if (list->entries[i].ssrc == stream_to_remove->ssrc) {
size_t entries_to_move = list->size - list->available - i - 1;
size_t entries_to_move = list->size - i - 1;
memmove(&list->entries[i], &list->entries[i + 1],
sizeof(list_entry) * entries_to_move);
list->available++;
list->size--;

break;
}
Expand All @@ -4962,7 +4961,7 @@ void srtp_stream_list_remove(srtp_stream_list_t list,

srtp_stream_t srtp_stream_list_get(srtp_stream_list_t list, uint32_t ssrc)
{
size_t end = list->size - list->available;
size_t end = list->size;

list_entry *entries = list->entries;

Expand All @@ -4981,25 +4980,25 @@ void srtp_stream_list_for_each(srtp_stream_list_t list,
{
list_entry *entries = list->entries;

size_t available = list->available;
size_t size = list->size;

/*
* the second statement of the expression needs to be recalculated on each
* iteration as the available number of entries may change within the given
* callback.
* Ie: in case the callback calls srtp_stream_list_remove().
*/
for (size_t i = 0; i < list->size - list->available;) {
for (size_t i = 0; i < list->size;) {
if (!callback(entries[i].stream, data)) {
break;
}

// the entry was not removed, increase the counter.
if (available == list->available) {
if (size == list->size) {
++i;
}

available = list->available;
size = list->size;
}
}

Expand Down

0 comments on commit 1515b76

Please sign in to comment.