Skip to content

Commit

Permalink
ptrlist: change return value of linearize_ptr_list()/ptr_list_to_array()
Browse files Browse the repository at this point in the history
The function linearize_ptr_list() is annoying to use because it
returns the number of elements put in the array. So, if you need
to know if the list contained the expected number of entries,
you need to allocate an array with one extra entry and check
that the return value is one less than this size.

So, change the function to return the total number of entries
in the list. In other words, the return value corresponds now to
the number of entries that could be copied if the size would be
unlimited, much like it's done for snprintf().
The number of entries effectively copied stays, of course,
limited by the size specified for the array.

Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com>
  • Loading branch information
lucvoo committed Mar 8, 2021
1 parent c50f527 commit f94f4a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions ptrlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ void *ptr_list_nth_entry(struct ptr_list *list, unsigned int idx)
// @head: the list to be linearized
// @arr: a ``void*`` array to fill with @head's entries
// @max: the maximum number of entries to store into @arr
// @return: the number of entries linearized.
// @return: the number of entries in the list.
//
// Linearize the entries of a list up to a total of @max,
// and return the nr of entries linearized.
// and return the number of entries in the list.
//
// The array to linearize into (@arr) should really
// be ``void *x[]``, but we want to let people fill in any kind
Expand All @@ -170,14 +170,14 @@ int linearize_ptr_list(struct ptr_list *head, void **arr, int max)

do {
int i = list->nr;
nr += i;
if (max == 0)
continue;
if (i > max)
i = max;
memcpy(arr, list->list, i*sizeof(void *));
arr += i;
nr += i;
max -= i;
if (!max)
break;
} while ((list = list->next) != head);
}
return nr;
Expand Down
4 changes: 2 additions & 2 deletions simplify.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ static int get_phisources(struct instruction *sources[], int nbr, struct instruc
static int if_convert_phi(struct instruction *insn)
{
struct instruction *array[2];
struct basic_block *parents[3];
struct basic_block *parents[2];
struct basic_block *bb, *bb1, *bb2, *source;
struct instruction *br;
pseudo_t p1, p2;

bb = insn->bb;
if (get_phisources(array, 2, insn))
return 0;
if (ptr_list_to_array(bb->parents, parents, 3) != 2)
if (ptr_list_to_array(bb->parents, parents, 2) != 2)
return 0;
p1 = array[0]->phi_src;
bb1 = array[0]->bb;
Expand Down

0 comments on commit f94f4a8

Please sign in to comment.