Message ID | 20210306100552.33784-7-luc.vanoostenryck@gmail.com (mailing list archive) |
---|---|
State | Mainlined, archived |
Headers | show |
Series | small changes to ptrlist API | expand |
On 06/03/2021 10:05, Luc Van Oostenryck wrote: > 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 to array with one more entries and check s/allocate to array/allocate an array/ > that the return value is one less than the maximum size. > > Change this, so that this function returns the total number of > entries in the list, much like it's done for snprintf(). But this requires setting max == 0, right? This isn't documented. > > Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> > --- > ptrlist.c | 10 +++++----- > simplify.c | 4 ++-- > 2 files changed, 7 insertions(+), 7 deletions(-) > > diff --git a/ptrlist.c b/ptrlist.c > index 0f0b3f6d818f..ecfbc07b2b6d 100644 > --- a/ptrlist.c > +++ b/ptrlist.c > @@ -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 nunmber of entries in the list. s/nunmber/number/ Somewhere here, the behaviour with max == 0 on input needs to be documented. ATB, Ramsay Jones > // > // The array to linearize into (@arr) should really > // be ``void *x[]``, but we want to let people fill in any kind > @@ -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; > diff --git a/simplify.c b/simplify.c > index cf5b3d748808..207af8edf28f 100644 > --- a/simplify.c > +++ b/simplify.c > @@ -108,7 +108,7 @@ 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; > @@ -116,7 +116,7 @@ static int if_convert_phi(struct instruction *insn) > 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; >
On Sat, Mar 06, 2021 at 04:43:21PM +0000, Ramsay Jones wrote: > On 06/03/2021 10:05, Luc Van Oostenryck wrote: > > Change this, so that this function returns the total number of > > entries in the list, much like it's done for snprintf(). > > But this requires setting max == 0, right? This isn't documented. No no, there is nothing special with max == 0 at the interface level. If the list contains 3 elements but you're only interested in the cases it contains 2, you now call: ... array[2]; int nbr = linearize_ptr_list(list, array, 2); and it'll only fill 2 elements but it will return 3, so can you write: if (nbr == 2) { ... do stuff ... } IOW, it returns the number of elements that would have been written to the array if 'max' would have been infinite. Previously, it would have returned 2 because the return value was capped to 'max' / it returned he number of elements written. So you had no idea if the list effectively contained only 2 elements of if there was some more and so you had to call it with one extra element to check this: ... array[3]; int nbr = linearize_ptr_list(list, array, 3); if (nbr == 2) { ... } Thanks for noticing all these typos, -- Luc
diff --git a/ptrlist.c b/ptrlist.c index 0f0b3f6d818f..ecfbc07b2b6d 100644 --- a/ptrlist.c +++ b/ptrlist.c @@ -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 nunmber 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 @@ -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; diff --git a/simplify.c b/simplify.c index cf5b3d748808..207af8edf28f 100644 --- a/simplify.c +++ b/simplify.c @@ -108,7 +108,7 @@ 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; @@ -116,7 +116,7 @@ static int if_convert_phi(struct instruction *insn) 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;
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 to array with one more entries and check that the return value is one less than the maximum size. Change this, so that this function returns the total number of entries in the list, much like it's done for snprintf(). Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@gmail.com> --- ptrlist.c | 10 +++++----- simplify.c | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-)