[PATCH v5 7/8] comctl32/listbox: Expand the item array in a helper function
Gabriel Ivăncescu
gabrielopcode at gmail.com
Thu Nov 22 07:44:43 CST 2018
Signed-off-by: Gabriel Ivăncescu <gabrielopcode at gmail.com>
---
dlls/comctl32/listbox.c | 63 ++++++++++++++++-------------------------
1 file changed, 24 insertions(+), 39 deletions(-)
diff --git a/dlls/comctl32/listbox.c b/dlls/comctl32/listbox.c
index dbcbf4c..3b76e19 100644
--- a/dlls/comctl32/listbox.c
+++ b/dlls/comctl32/listbox.c
@@ -36,6 +36,7 @@
#include "wine/unicode.h"
#include "wine/exception.h"
#include "wine/debug.h"
+#include "wine/heap.h"
#include "comctl32.h"
@@ -126,6 +127,27 @@ static TIMER_DIRECTION LISTBOX_Timer = LB_TIMER_NONE;
static LRESULT LISTBOX_GetItemRect( const LB_DESCR *descr, INT index, RECT *rect );
+static BOOL expand_storage(LB_DESCR *descr, UINT amount)
+{
+ LB_ITEMDATA *p = descr->items;
+ UINT num = descr->nb_items + amount;
+
+ if (num > descr->array_size)
+ {
+ num += LB_ARRAY_GRANULARITY - 1;
+ num -= num % LB_ARRAY_GRANULARITY;
+ p = heap_realloc(p, num * sizeof(LB_ITEMDATA));
+ if (!p)
+ {
+ SEND_NOTIFICATION(descr, LBN_ERRSPACE);
+ return FALSE;
+ }
+ descr->array_size = num;
+ descr->items = p;
+ }
+ return TRUE;
+}
+
static BOOL is_item_selected(LB_DESCR *descr, UINT index)
{
return descr->items[index].selected;
@@ -677,27 +699,8 @@ static void LISTBOX_DrawFocusRect( LB_DESCR *descr, BOOL on )
*/
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 += descr->array_size;
- item = HeapReAlloc( GetProcessHeap(), 0, descr->items,
- nb_items * sizeof(LB_ITEMDATA));
- }
- else {
- item = HeapAlloc( GetProcessHeap(), 0,
- nb_items * sizeof(LB_ITEMDATA));
- }
-
- if (!item)
- {
- SEND_NOTIFICATION( descr, LBN_ERRSPACE );
+ if (!expand_storage(descr, nb_items))
return LB_ERRSPACE;
- }
- descr->array_size = nb_items;
- descr->items = item;
return LB_OKAY;
}
@@ -1518,29 +1521,11 @@ static LRESULT LISTBOX_InsertItem( LB_DESCR *descr, INT index,
LPWSTR str, ULONG_PTR data )
{
LB_ITEMDATA *item;
- INT max_items;
INT oldfocus = descr->focus_item;
if (index == -1) index = descr->nb_items;
else if ((index < 0) || (index > descr->nb_items)) return LB_ERR;
- if (descr->nb_items == descr->array_size)
- {
- /* We need to grow the array */
- max_items = descr->array_size + LB_ARRAY_GRANULARITY;
- if (descr->items)
- item = HeapReAlloc( GetProcessHeap(), 0, descr->items,
- max_items * sizeof(LB_ITEMDATA) );
- else
- item = HeapAlloc( GetProcessHeap(), 0,
- max_items * sizeof(LB_ITEMDATA) );
- if (!item)
- {
- SEND_NOTIFICATION( descr, LBN_ERRSPACE );
- return LB_ERRSPACE;
- }
- descr->array_size = max_items;
- descr->items = item;
- }
+ if (!expand_storage(descr, 1)) return LB_ERR;
/* Insert the item structure */
--
2.19.1
More information about the wine-devel
mailing list