Skip to content

Commit

Permalink
New API to wrap an initialized array with an array list (#1040)
Browse files Browse the repository at this point in the history
* New API to wrap an initialized array with an array list


---------

Co-authored-by: Bret Ambrose <bambrose@amazon.com>
  • Loading branch information
bretambrose and Bret Ambrose authored Jul 10, 2023
1 parent 1778c79 commit 87a1b56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions include/aws/common/array_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,21 @@ void aws_array_list_init_static(
size_t item_count,
size_t item_size);

/**
* Initializes an array list with a preallocated array of *already-initialized* elements. item_count is the number of
* elements in the array, and item_size is the size in bytes of each element.
*
* Once initialized, nothing further can be added to the list, since it will be full and cannot resize.
*
* Primary use case is to treat an already-initialized C array as an array list.
*/
AWS_STATIC_IMPL
void aws_array_list_init_static_from_initialized(
struct aws_array_list *AWS_RESTRICT list,
void *raw_array,
size_t item_count,
size_t item_size);

/**
* Set of properties of a valid aws_array_list.
*/
Expand Down
13 changes: 13 additions & 0 deletions include/aws/common/array_list.inl
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ void aws_array_list_init_static(
AWS_POSTCONDITION(aws_array_list_is_valid(list));
}

AWS_STATIC_IMPL
void aws_array_list_init_static_from_initialized(
struct aws_array_list *AWS_RESTRICT list,
void *raw_array,
size_t item_count,
size_t item_size) {

aws_array_list_init_static(list, raw_array, item_count, item_size);
list->length = item_count;

AWS_POSTCONDITION(aws_array_list_is_valid(list));
}

AWS_STATIC_IMPL
bool aws_array_list_is_valid(const struct aws_array_list *AWS_RESTRICT list) {
if (!list) {
Expand Down

0 comments on commit 87a1b56

Please sign in to comment.