kdelibs/kdecore

Eray Ozkural kde-optimize@mail.kde.org
Thu, 20 Mar 2003 19:17:29 +0100 (CET)


CVS commit by exa: 

Keep track of capacity and actual size separately in RArray. RArray does have size semantics!! (Which I neglected painfully). Also use calloc in constructor. Both suggested by Dirk.

CCMAIL: kde-optimize@kde.org

Note: If this still has breakage, please don't hesitate to revert it. If it's correct, I can tell why it's supposed to be faster in detail.

Best Regards!!

__
Eray


  M +13 -12    netwm.cpp   1.89
  M +1 -0      netwm_p.h   1.12


--- kdelibs/kdecore/netwm.cpp  #1.88:1.89
@@ -448,9 +448,7 @@ fprintf(stderr, "NETWM: Warning readIcon
 template <class Z>
 NETRArray<Z>::NETRArray()
-  : sz( 2 )
+  : sz(0),  capacity(2)
 {
-    // new/delete and malloc/free are not compatible
-    d = (Z*) malloc(sizeof(Z)*sz); // allocate 2 elts
-    memset( (void*) d, 0, sizeof(Z)*sz );
+    d = (Z*) calloc(capacity, sizeof(Z)); // allocate 2 elts and set to zero
 }
 
@@ -464,21 +462,24 @@ NETRArray<Z>::~NETRArray() {
 template <class Z>
 void NETRArray<Z>::reset() {
-    sz = 2;
-    d = (Z*) realloc(d, sizeof(Z)*sz);
-    memset( (void*) d, 0, sizeof(Z)*sz );
+    sz = 0;
+    capacity = 2;
+    d = (Z*) realloc(d, sizeof(Z)*capacity);
+    memset( (void*) d, 0, sizeof(Z)*capacity );
 }
 
 template <class Z>
 Z &NETRArray<Z>::operator[](int index) {
-    if (index >= sz) {
+    if (index >= capacity) {
         // allocate space for the new data
         // open table has amortized O(1) access time
         // when N elements appended consecutively -- exa
-        int newsize = max(2*sz,  index+1);
+        int newcapacity = max(2*capacity,  index+1);
         // copy into new larger memory block using realloc
-        d = (Z*) realloc(d, sizeof(Z)*newsize);
-        memset( (void*) &d[sz], 0, sizeof(Z)*(newsize-sz) );
-        sz = newsize;
+        d = (Z*) realloc(d, sizeof(Z)*newcapacity);
+        memset( (void*) &d[capacity], 0, sizeof(Z)*(newcapacity-capacity) );
+        capacity = newcapacity;
     }
+    if (index >= sz)            // at this point capacity>index
+        sz = index;
 
     return d[index];

--- kdelibs/kdecore/netwm_p.h  #1.11:1.12
@@ -70,4 +70,5 @@ public:
 private:
     int sz;
+    int capacity;
     Z *d;
 };