DPA_InsertPtr

Dimitrie O. Paun dpaun at rogers.com
Sun Oct 20 01:30:03 CDT 2002


from MSDN:
  Inserts a new item at a specified position in a 
  dynamic pointer array (DPA). If neccessary, the 
  DPA expands to accommodate the new item.

Our implementation didn't.

ChangeLog
  Rewrite DPA_InsertPtr in terms on DPA_SetPtr, so that
  it's simpler, cleaner, and more correct.
  It now expands the array to accomodate larger than
  current size indexes.

Index: dlls/comctl32/comctl32undoc.c
===================================================================
RCS file: /var/cvs/wine/dlls/comctl32/comctl32undoc.c,v
retrieving revision 1.67
diff -u -r1.67 comctl32undoc.c
--- dlls/comctl32/comctl32undoc.c	25 Sep 2002 03:19:24 -0000	1.67
+++ dlls/comctl32/comctl32undoc.c	20 Oct 2002 06:23:34 -0000
@@ -1875,60 +1875,20 @@
 INT WINAPI
 DPA_InsertPtr (const HDPA hdpa, INT i, LPVOID p)
 {
-    INT   nNewItems, nSize, nIndex = 0;
-    LPVOID  *lpTemp, *lpDest;
-
     TRACE("(%p %d %p)\n", hdpa, i, p);
 
-    if ((!hdpa) || (i < 0))
-	return -1;
-
-    if (!hdpa->ptrs) {
-	hdpa->ptrs =
-	    (LPVOID*)HeapAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
-				2 * hdpa->nGrow * sizeof(LPVOID));
-	if (!hdpa->ptrs)
-	    return -1;
-	hdpa->nMaxCount = hdpa->nGrow * 2;
-        nIndex = 0;
-    }
-    else {
-	if (hdpa->nItemCount >= hdpa->nMaxCount) {
-	    TRACE("-- resizing\n");
-	    nNewItems = hdpa->nMaxCount + hdpa->nGrow;
-	    nSize = nNewItems * sizeof(LPVOID);
-
-	    lpTemp = (LPVOID*)HeapReAlloc (hdpa->hHeap, HEAP_ZERO_MEMORY,
-					   hdpa->ptrs, nSize);
-	    if (!lpTemp)
-		return -1;
-	    hdpa->nMaxCount = nNewItems;
-	    hdpa->ptrs = lpTemp;
-	}
+    if (!hdpa || i < 0) return -1;
 
-	if (i >= hdpa->nItemCount) {
-	    nIndex = hdpa->nItemCount;
-	    TRACE("-- appending at %d\n", nIndex);
-	}
-	else {
-	    TRACE("-- inserting at %d\n", i);
-	    lpTemp = hdpa->ptrs + i;
-	    lpDest = lpTemp + 1;
-	    nSize  = (hdpa->nItemCount - i) * sizeof(LPVOID);
-	    TRACE("-- move dest=%p src=%p size=%x\n",
-		   lpDest, lpTemp, nSize);
-	    memmove (lpDest, lpTemp, nSize);
-	    nIndex = i;
-	}
-    }
+    if (i >= hdpa->nItemCount)
+	return DPA_SetPtr(hdpa, i, p) ? i : -1;
 
-    /* insert item */
+    /* create empty spot at the end */
+    if (!DPA_SetPtr(hdpa, hdpa->nItemCount, 0)) return -1;
+    memmove (hdpa->ptrs + i + 1, hdpa->ptrs + i, (hdpa->nItemCount - i) * sizeof(LPVOID));
+    hdpa->ptrs[i] = p;
     hdpa->nItemCount++;
-    hdpa->ptrs[nIndex] = p;
-
-    return nIndex;
+    return i;
 }
-
 
 /**************************************************************************
  * DPA_SetPtr [COMCTL32.335]




More information about the wine-patches mailing list