Signed-off-by: Gabriel Ivăncescu gabrielopcode@gmail.com ---
Only increase the item array if we actually have to. Previously, sending 1 to nb_items repeatedly would always increase the array by LB_ARRAY_GRANULARITY, even if there was plenty of space.
dlls/comctl32/listbox.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/dlls/comctl32/listbox.c b/dlls/comctl32/listbox.c index 2137ef8..9cd89ff 100644 --- a/dlls/comctl32/listbox.c +++ b/dlls/comctl32/listbox.c @@ -673,16 +673,20 @@ static LRESULT LISTBOX_InitStorage( LB_DESCR *descr, INT nb_items ) { LB_ITEMDATA *item;
- nb_items += LB_ARRAY_GRANULARITY - 1; - nb_items -= (nb_items % LB_ARRAY_GRANULARITY); if (descr->items) { - nb_items += HeapSize( GetProcessHeap(), 0, descr->items ) / sizeof(*item); - item = HeapReAlloc( GetProcessHeap(), 0, descr->items, - nb_items * sizeof(LB_ITEMDATA)); + nb_items += descr->nb_items; + if (nb_items > HeapSize(GetProcessHeap(), 0, descr->items) / sizeof(*item)) + { + UINT n = nb_items + LB_ARRAY_GRANULARITY - 1; + item = HeapReAlloc(GetProcessHeap(), 0, descr->items, + (n - n % LB_ARRAY_GRANULARITY) * sizeof(*item)); + } + else return LB_OKAY; } else { - item = HeapAlloc( GetProcessHeap(), 0, - nb_items * sizeof(LB_ITEMDATA)); + UINT n = nb_items + LB_ARRAY_GRANULARITY - 1; + item = HeapAlloc(GetProcessHeap(), 0, + (n - n % LB_ARRAY_GRANULARITY) * sizeof(*item)); }
if (!item)
Signed-off-by: Gabriel Ivăncescu gabrielopcode@gmail.com --- dlls/user32/listbox.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/dlls/user32/listbox.c b/dlls/user32/listbox.c index c8bd148..967baf2 100644 --- a/dlls/user32/listbox.c +++ b/dlls/user32/listbox.c @@ -698,16 +698,20 @@ static LRESULT LISTBOX_InitStorage( LB_DESCR *descr, INT nb_items ) { LB_ITEMDATA *item;
- nb_items += LB_ARRAY_GRANULARITY - 1; - nb_items -= (nb_items % LB_ARRAY_GRANULARITY); if (descr->items) { - nb_items += HeapSize( GetProcessHeap(), 0, descr->items ) / sizeof(*item); - item = HeapReAlloc( GetProcessHeap(), 0, descr->items, - nb_items * sizeof(LB_ITEMDATA)); + nb_items += descr->nb_items; + if (nb_items > HeapSize(GetProcessHeap(), 0, descr->items) / sizeof(*item)) + { + UINT n = nb_items + LB_ARRAY_GRANULARITY - 1; + item = HeapReAlloc(GetProcessHeap(), 0, descr->items, + (n - n % LB_ARRAY_GRANULARITY) * sizeof(*item)); + } + else return LB_OKAY; } else { - item = HeapAlloc( GetProcessHeap(), 0, - nb_items * sizeof(LB_ITEMDATA)); + UINT n = nb_items + LB_ARRAY_GRANULARITY - 1; + item = HeapAlloc(GetProcessHeap(), 0, + (n - n % LB_ARRAY_GRANULARITY) * sizeof(*item)); }
if (!item)
On 18 Sep 2018, at 21:10, Gabriel Ivăncescu gabrielopcode@gmail.com wrote:
Signed-off-by: Gabriel Ivăncescu gabrielopcode@gmail.com
Only increase the item array if we actually have to. Previously, sending 1 to nb_items repeatedly would always increase the array by LB_ARRAY_GRANULARITY, even if there was plenty of space.
dlls/comctl32/listbox.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/dlls/comctl32/listbox.c b/dlls/comctl32/listbox.c index 2137ef8..9cd89ff 100644 --- a/dlls/comctl32/listbox.c +++ b/dlls/comctl32/listbox.c @@ -673,16 +673,20 @@ static LRESULT LISTBOX_InitStorage( LB_DESCR *descr, INT nb_items ) { LB_ITEMDATA *item;
- nb_items += LB_ARRAY_GRANULARITY - 1;
- nb_items -= (nb_items % LB_ARRAY_GRANULARITY); if (descr->items) {
nb_items += HeapSize( GetProcessHeap(), 0, descr->items ) / sizeof(*item);
- item = HeapReAlloc( GetProcessHeap(), 0, descr->items,
nb_items * sizeof(LB_ITEMDATA));
nb_items += descr->nb_items;
if (nb_items > HeapSize(GetProcessHeap(), 0, descr->items) / sizeof(*item))
{
UINT n = nb_items + LB_ARRAY_GRANULARITY - 1;
item = HeapReAlloc(GetProcessHeap(), 0, descr->items,
(n - n % LB_ARRAY_GRANULARITY) * sizeof(*item));
I dislike having the calculation split up like this.
Further, I think we can insist that LB_ARRAY_GRANULARITY is a power of two, (perhaps add a comment to that effect above its #define) which should simplify things a bit.
Huw.
On Wed, Sep 19, 2018 at 5:06 PM, Huw Davies huw@codeweavers.com wrote:
I dislike having the calculation split up like this.
Further, I think we can insist that LB_ARRAY_GRANULARITY is a power of two, (perhaps add a comment to that effect above its #define) which should simplify things a bit.
Huw.
Yeah, if it's a power of 2 I can use a bitwise AND operator to simplify it. I'll do that and add a comment.