Sebastian Kuschel reports that pfd_closing can be called for a socket
[u/mdw/putty] / tree234.c
index 0837dc6..f1c0c2e 100644 (file)
--- a/tree234.c
+++ b/tree234.c
 
 #include "tree234.h"
 
-#define smalloc malloc
-#define sfree free
-
-#define mknew(typ) ( (typ *) smalloc (sizeof (typ)) )
-
 #ifdef TEST
 #define LOG(x) (printf x)
+#define snew(type) ((type *)malloc(sizeof(type)))
+#define snewn(n, type) ((type *)malloc((n) * sizeof(type)))
+#define sresize(ptr, n, type)                                         \
+    ((type *)realloc(sizeof((type *)0 == (ptr)) ? (ptr) : (ptr),      \
+                     (n) * sizeof(type)))
+#define sfree(ptr) free(ptr)
 #else
+#include "puttymem.h"
 #define LOG(x)
 #endif
 
@@ -61,7 +63,7 @@ struct node234_Tag {
  */
 tree234 *newtree234(cmpfn234 cmp)
 {
-    tree234 *ret = mknew(tree234);
+    tree234 *ret = snew(tree234);
     LOG(("created tree %p\n", ret));
     ret->root = NULL;
     ret->cmp = cmp;
@@ -128,7 +130,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
 
     LOG(("adding node %p to tree %p\n", e, t));
     if (t->root == NULL) {
-       t->root = mknew(node234);
+       t->root = snew(node234);
        t->root->elems[1] = t->root->elems[2] = NULL;
        t->root->kids[0] = t->root->kids[1] = NULL;
        t->root->kids[2] = t->root->kids[3] = NULL;
@@ -140,6 +142,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
        return orig_e;
     }
 
+    n = NULL; /* placate gcc; will always be set below since t->root != NULL */
     np = &t->root;
     while (*np) {
        int childnum;
@@ -223,7 +226,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
             n->kids[2], n->counts[2], n->elems[2],
             n->kids[3], n->counts[3]));
        LOG(("  need to insert %p/%d [%p] %p/%d at position %d\n",
-            left, lcount, e, right, rcount, np - n->kids));
+            left, lcount, e, right, rcount, (int)(np - n->kids)));
        if (n->elems[1] == NULL) {
            /*
             * Insert in a 2-node; simple.
@@ -300,7 +303,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
            LOG(("  done\n"));
            break;
        } else {
-           node234 *m = mknew(node234);
+           node234 *m = snew(node234);
            m->parent = n->parent;
            LOG(("  splitting a 4-node; created new node %p\n", m));
            /*
@@ -423,7 +426,7 @@ static void *add234_internal(tree234 * t, void *e, int index)
        }
     } else {
        LOG(("  root is overloaded, split into two\n"));
-       t->root = mknew(node234);
+       t->root = snew(node234);
        t->root->kids[0] = left;
        t->root->counts[0] = lcount;
        t->root->elems[0] = e;
@@ -1012,8 +1015,6 @@ void *del234(tree234 * t, void *e)
 
 #include <stdarg.h>
 
-#define srealloc realloc
-
 /*
  * Error reporting function.
  */
@@ -1201,8 +1202,7 @@ void internal_addtest(void *elem, int index, void *realret)
 
     if (arraysize < arraylen + 1) {
        arraysize = arraylen + 1 + 256;
-       array = (array == NULL ? smalloc(arraysize * sizeof(*array)) :
-                srealloc(array, arraysize * sizeof(*array)));
+       array = sresize(array, arraysize, void *);
     }
 
     i = index;
@@ -1475,7 +1475,8 @@ int main(void)
        printf("cleanup: tree size %d\n", count234(tree));
        j = randomnumber(&seed);
        j %= count234(tree);
-       printf("deleting string %s from index %d\n", array[j], j);
+       printf("deleting string %s from index %d\n",
+               (const char *)array[j], j);
        delpostest(j);
     }