Parcourir la source

misc/grow_array.h: implement GrowArray_InitEmpty() and GrowArray_DoubleUpLimit()

ambrop7 il y a 13 ans
Parent
commit
6630a0c47e
1 fichiers modifiés avec 59 ajouts et 3 suppressions
  1. 59 3
      misc/grow_array.h

+ 59 - 3
misc/grow_array.h

@@ -1,3 +1,32 @@
+/**
+ * @file grow_array.h
+ * @author Ambroz Bizjak <ambrop7@gmail.com>
+ * 
+ * @section LICENSE
+ * 
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the author nor the
+ *    names of its contributors may be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ * 
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
 // Preprocessor inputs:
 // GROWARRAY_NAME - prefix of of functions to define
 // GROWARRAY_OBJECT_TYPE - type of structure where array and capacity sizes are
@@ -15,12 +44,16 @@
 
 #define GrowArrayObject GROWARRAY_OBJECT_TYPE
 #define GrowArray_Init MERGE(GROWARRAY_NAME, _Init)
+#define GrowArray_InitEmpty MERGE(GROWARRAY_NAME, _InitEmpty)
 #define GrowArray_Free MERGE(GROWARRAY_NAME, _Free)
 #define GrowArray_DoubleUp MERGE(GROWARRAY_NAME, _DoubleUp)
+#define GrowArray_DoubleUpLimit MERGE(GROWARRAY_NAME, _DoubleUpLimit)
 
 static int GrowArray_Init (GrowArrayObject *o, size_t capacity) WARN_UNUSED;
+static void GrowArray_InitEmpty (GrowArrayObject *o);
 static void GrowArray_Free (GrowArrayObject *o);
 static int GrowArray_DoubleUp (GrowArrayObject *o) WARN_UNUSED;
+static int GrowArray_DoubleUpLimit (GrowArrayObject *o, size_t limit) WARN_UNUSED;
 
 static int GrowArray_Init (GrowArrayObject *o, size_t capacity)
 {
@@ -39,20 +72,41 @@ static int GrowArray_Init (GrowArrayObject *o, size_t capacity)
     return 1;
 }
 
+static void GrowArray_InitEmpty (GrowArrayObject *o)
+{
+    o->GROWARRAY_ARRAY_MEMBER = NULL;
+    o->GROWARRAY_CAPACITY_MEMBER = 0;
+}
+
 static void GrowArray_Free (GrowArrayObject *o)
 {
-    BFree(o->GROWARRAY_ARRAY_MEMBER);
+    if (o->GROWARRAY_ARRAY_MEMBER) {
+        BFree(o->GROWARRAY_ARRAY_MEMBER);
+    }
 }
 
 static int GrowArray_DoubleUp (GrowArrayObject *o)
 {
-    ASSERT(o->GROWARRAY_CAPACITY_MEMBER > 0)
-    
+    return GrowArray_DoubleUpLimit(o, SIZE_MAX);
+}
+
+static int GrowArray_DoubleUpLimit (GrowArrayObject *o, size_t limit)
+{
     if (o->GROWARRAY_CAPACITY_MEMBER > SIZE_MAX / 2 || o->GROWARRAY_CAPACITY_MEMBER > GROWARRAY_MAX_CAPACITY / 2) {
         return 0;
     }
     
     size_t newcap = 2 * o->GROWARRAY_CAPACITY_MEMBER;
+    if (newcap == 0) {
+        newcap = 1;
+    }
+    
+    if (newcap > limit) {
+        newcap = limit;
+        if (newcap == o->GROWARRAY_CAPACITY_MEMBER) {
+            return 0;
+        }
+    }
     
     void *newarr = BAllocArray(newcap, sizeof(o->GROWARRAY_ARRAY_MEMBER[0]));
     if (!newarr) {
@@ -77,5 +131,7 @@ static int GrowArray_DoubleUp (GrowArrayObject *o)
 
 #undef GrowArrayObject
 #undef GrowArray_Init
+#undef GrowArray_InitEmpty
 #undef GrowArray_Free
 #undef GrowArray_DoubleUp
+#undef GrowArray_DoubleUpLimit