[patch] dlib cleanup

123 <[email protected]>
Newsgroups gmane.comp.web.dillo.devel
Message-ID <[email protected]>
malloc.diff: added perror calls for malloc and realloc errors

dstr.diff: dStr* functions cleanup

dlist.diff: replaced recursive QuickSort with qsort from stdlib.h.
Tested with file.dpi where it is used for sorting directory listing.

_______________________________________________
Dillo-dev mailing list
[email protected]
http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
malloc.diff (text/plain, 698 B)
diff -r 564169cca80c dlib/dlib.c
--- a/dlib/dlib.c	Sun Jun 03 00:01:28 2012 +0400
+++ b/dlib/dlib.c	Sun Jun 03 00:09:01 2012 +0400
@@ -43,24 +43,26 @@
 void *dMalloc (size_t size)
 {
   void *value = malloc (size);
-  if (value == 0)
+  if (value == NULL) {
+     perror("malloc");
      exit(1);
+  }
   return value;
 }
 
 void *dRealloc (void *mem, size_t size)
 {
   void *value = realloc (mem, size);
-  if (value == 0)
+  if (value == NULL) {
+     perror("realloc");
      exit(1);
+  }
   return value;
 }
 
 void *dMalloc0 (size_t size)
 {
-  void *value = dMalloc (size);
-  memset (value, 0, size);
-  return value;
+  return memset(dMalloc(size), 0, size);
 }
 
 void dFree (void *mem)
dstr.diff (text/plain, 1.1 KB)
diff -r 7f85637e5e21 dlib/dlib.c
--- a/dlib/dlib.c	Sun Jun 03 00:01:39 2012 +0400
+++ b/dlib/dlib.c	Sun Jun 03 00:09:06 2012 +0400
@@ -78,9 +78,7 @@
 {
    if (s) {
       int len = strlen(s)+1;
-      char *ns = dNew(char, len);
-      memcpy(ns, s, len);
-      return ns;
+      return memcpy(dNew(char, len), s, len);
    }
    return NULL;
 }
@@ -336,11 +334,9 @@
  */
 void dStr_free (Dstr *ds, int all)
 {
-   if (ds) {
-      if (all)
-         dFree(ds->str);
-      dFree(ds);
-   }
+   if (ds && all)
+      dFree(ds->str);
+   dFree(ds);
 }
 
 /*
@@ -350,15 +346,15 @@
 {
    char cs[2];
 
-   if (ds) {
-      if (ds->sz > ds->len + 1) {
-         ds->str[ds->len++] = (Dstr_char_t)c;
-         ds->str[ds->len] = 0;
-      } else {
-         cs[0] = (Dstr_char_t)c;
-         cs[1] = 0;
-         dStr_append_l (ds, cs, 1);
-      }
+   if (!ds)
+      return;
+   if (ds->sz > ds->len + 1) {
+      ds->str[ds->len++] = (Dstr_char_t)c;
+      ds->str[ds->len] = 0;
+   } else {
+      cs[0] = (Dstr_char_t)c;
+      cs[1] = 0;
+      dStr_append_l (ds, cs, 1);
    }
 }
dlist.diff (text/plain, 2 KB)
diff -r ab71015cf724 dlib/dlib.c
--- a/dlib/dlib.c	Sun Jun 03 00:09:03 2012 +0400
+++ b/dlib/dlib.c	Sun Jun 03 00:09:10 2012 +0400
@@ -666,18 +666,15 @@
  */
 int dList_find_idx (Dlist *lp, const void *data)
 {
-   int i, ret = -1;
+   int i;
 
    if (!lp)
-      return ret;
+      return -1;
 
-   for (i = 0; i < lp->len; ++i) {
-      if (lp->list[i] == data) {
-         ret = i;
-         break;
-      }
-   }
-   return ret;
+   for (i = 0; i < lp->len; ++i)
+      if (lp->list[i] == data)
+         return i;
+   return -1;
 }
 
 /*
@@ -688,52 +685,14 @@
 void *dList_find_custom (Dlist *lp, const void *data, dCompareFunc func)
 {
    int i;
-   void *ret = NULL;
 
    if (!lp)
-      return ret;
+      return NULL;
 
-   for (i = 0; i < lp->len; ++i) {
-      if (func(lp->list[i], data) == 0) {
-         ret = lp->list[i];
-         break;
-      }
-   }
-   return ret;
-}
-
-/*
- * QuickSort implementation.
- * This allows for a simple compare function for all the ADT.
- */
-static void QuickSort(void **left, void **right, dCompareFunc compare)
-{
-  void **p = left, **q = right, **t = left;
-
-  while (1) {
-     while (p != t && compare(*p, *t) < 0)
-        ++p;
-     while (q != t && compare(*q, *t) > 0)
-        --q;
-     if (p > q)
-        break;
-     if (p < q) {
-        void *tmp = *p;
-        *p = *q;
-        *q = tmp;
-        if (t == p)
-           t = q;
-        else if (t == q)
-           t = p;
-     }
-     if (++p > --q)
-        break;
-  }
-
-  if (left < q)
-     QuickSort(left, q, compare);
-  if (p < right)
-     QuickSort(p, right, compare);
+   for (i = 0; i < lp->len; ++i)
+      if (func(lp->list[i], data) == 0)
+         return lp->list[i];
+   return NULL;
 }
 
 /*
@@ -741,9 +700,8 @@
  */
 void dList_sort (Dlist *lp, dCompareFunc func)
 {
-   if (lp && lp->len > 1) {
-      QuickSort(lp->list, lp->list + lp->len - 1, func);
-   }
+   if (lp && lp->len > 1)
+      qsort(lp->list, lp->len, sizeof *lp->list, func);
 }
 
 /*
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.