]> git.lizzy.rs Git - nothing.git/commitdiff
realloc and destroy_lt fixes
authorTravis Staloch <twostepted@gmail.com>
Sat, 18 May 2019 18:28:26 +0000 (11:28 -0700)
committerTravis Staloch <twostepted@gmail.com>
Sat, 18 May 2019 18:28:26 +0000 (11:28 -0700)
src/system/lt.h

index 7931efe17de0ee2b1ab8b64584437ffaea455f60..12b07be27ae0481d2b0c3ab0c8db6aac99055c00 100644 (file)
@@ -29,7 +29,7 @@ static inline void destroy_lt(Lt *lt)
 {
     trace_assert(lt);
 
-    for (Slot *p = lt->slots_end; p != lt->slots; --p) {
+    for (Slot *p = lt->slots_end - 1; p >= lt->slots; --p) {
         if (p->res) {
             p->dtor(p->res);
         }
@@ -45,8 +45,8 @@ static inline void destroy_lt(Lt *lt)
 static inline void *lt_push(Lt *lt, void *res, Dtor dtor)
 {
     trace_assert(lt);
-
-    if ((size_t)(lt->slots_end - lt->slots) >= lt->capacity) {
+    size_t size = (size_t)(lt->slots_end - lt->slots);
+    if (size >= lt->capacity) {
         if (lt->capacity == 0) {
             lt->capacity = LT_INITIAL_CAPACITY;
             lt->slots = calloc(LT_INITIAL_CAPACITY, sizeof(Slot));
@@ -54,7 +54,7 @@ static inline void *lt_push(Lt *lt, void *res, Dtor dtor)
         } else {
             lt->capacity *= 2;
             lt->slots = realloc(lt->slots, lt->capacity * sizeof(Slot));
-            lt->slots_end = lt->slots;
+            lt->slots_end = lt->slots + size;
         }
     }