BABL list API patch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,
 attached is a patch that introduces minimal changes needed
 for removal of the old list routines. It ports several
 lists to the new API and deletes the unused old functions.

 There are still several plain C arrays in the code used as lists, 
 however, these do not depend on the old routines and are
 left untouched by the patch. I can try to identify and port these as
 well. This would certainly improve things from the
 readability point of view, although it wouldn't IMO do much
 about the speed of the code.

 To Sven: Thanks for including me into the AUTHORS file :)

 Regards,
   Jan
Index: babl/babl-model.c
===================================================================
--- babl/babl-model.c	(revision 299)
+++ babl/babl-model.c	(working copy)
@@ -27,7 +27,8 @@ static int
 each_babl_model_destroy (Babl *babl,
                          void *data)
 {
-  babl_free (babl->model.from);
+  if (babl->model.from_list)
+    babl_list_destroy (babl->model.from_list);
   babl_free (babl);
   return 0;  /* continue iterating */
 }
@@ -72,7 +73,7 @@ model_new (const char     *name,
   strcpy (babl->instance.name, name);
   memcpy (babl->model.component, component, sizeof (BablComponent *) * components);
 
-  babl->model.from = NULL;
+  babl->model.from_list  = NULL;
   return babl;
 }
 
Index: babl/babl-sampling.c
===================================================================
--- babl/babl-sampling.c	(revision 299)
+++ babl/babl-sampling.c	(working copy)
@@ -46,7 +46,8 @@ static int
 each_babl_sampling_destroy (Babl *babl,
                             void *data)
 {
-  babl_free (babl->sampling.from);
+  if (babl->sampling.from_list)
+    babl_list_destroy (babl->sampling.from_list);
   return 0;  /* continue iterating */
 }
 
Index: babl/babl-list.c
===================================================================
--- babl/babl-list.c	(revision 299)
+++ babl/babl-list.c	(working copy)
@@ -16,11 +16,8 @@
  * <http://www.gnu.org/licenses/>.
  */
 
-/* Implementation of list data structure. This is a bit superior
- * to the list implementation in babl-util.c.
+/* Implementation of list data structure. 
  * Copyright (C) 2008, Jan Heller
- *
- * TODO: migrate babl to BablList
  */
 
 #include "babl-internal.h"
@@ -30,11 +27,19 @@
 BablList *
 babl_list_init (void)
 {
+  return babl_list_init_with_size (BABL_LIST_INITIAL_SIZE);
+}
+
+BablList *
+babl_list_init_with_size (int initial_size)
+{
   BablList *list = babl_calloc (sizeof (BablList), 1);
 
   babl_assert (list);
 
-  list->size = BABL_LIST_INITIAL_SIZE;
+  if (initial_size == 0)
+    initial_size = 1;
+  list->size = initial_size;
   list->count = 0;
   list->items = NULL;
   if (list->size) 
@@ -81,13 +86,10 @@ babl_list_insert (BablList *list,
     list->items[list->count++] = item;
 }
 
-/* TODO: Rename babl_list_each_temp to babl_list_each after the list migration
- */
-
 void
-babl_list_each_temp (BablList         *list,
-                     BablEachFunction each_fun,
-                     void             *user_data)
+babl_list_each (BablList         *list,
+                BablEachFunction each_fun,
+                void             *user_data)
 {
   int i;
 
Index: babl/babl-list.h
===================================================================
--- babl/babl-list.h	(revision 299)
+++ babl/babl-list.h	(working copy)
@@ -20,12 +20,13 @@
 #define _BABL_LIST_H
 
 #ifndef _BABL_CLASSES_H
+/* babl-classes.h contains forward declaration
+ * typedef struct _BablList BablList;
+ */
 #error  babl-list.h is only to be included after babl-classes.h
 #endif
 
 
-typedef struct _BablList BablList;
-
 typedef struct _BablList
 {
   int  count;
@@ -37,6 +38,9 @@ typedef struct _BablList
 BablList *
 babl_list_init (void);
 
+BablList *
+babl_list_init_with_size (int initial_size);
+
 void
 babl_list_destroy (BablList *list);
 
@@ -48,9 +52,9 @@ babl_list_insert (BablList *list,
                   Babl     *item);
 
 void
-babl_list_each_temp (BablList      *list,
-                     BablEachFunction each_fun,
-                     void             *user_data);
+babl_list_each (BablList      *list,
+                BablEachFunction each_fun,
+                void             *user_data);
 
 
 #endif
Index: babl/babl-internal.h
===================================================================
--- babl/babl-internal.h	(revision 299)
+++ babl/babl-internal.h	(working copy)
@@ -25,6 +25,7 @@
 
 #define BABL_MAX_COMPONENTS       32
 #define BABL_HARD_MAX_PATH_LENGTH  8
+#define BABL_CONVERSIONS           5
 
 #include <stdlib.h>
 #include <stdio.h>
Index: babl/babl-fish-path.c
===================================================================
--- babl/babl-fish-path.c	(revision 299)
+++ babl/babl-fish-path.c	(working copy)
@@ -141,10 +141,10 @@ get_conversion_chain (const Babl      *f
       temp_chain[temp_conversions] = NULL;
       babl_assert (from);
       babl_assert (from->class_type == BABL_FORMAT);
-      if (!from->format.from)
+      if (!from->format.from_list)
         return 0;
 
-      babl_list_each ((void **) from->format.from,
+      babl_list_each (from->format.from_list,
                       chain_gen_each,
                       &context);
     }
@@ -152,12 +152,11 @@ get_conversion_chain (const Babl      *f
     {
       if (BABL (temp_chain[temp_conversions - 1]) &&
           BABL (temp_chain[temp_conversions - 1]->destination)->
-          format.from)
+          format.from_list)
 
         babl_list_each (
-          (void **)
           BABL (temp_chain[temp_conversions - 1]->destination)->
-          format.from,
+          format.from_list,
           chain_gen_each,
           &context);
     }
@@ -250,15 +249,16 @@ static inline Babl *
 assert_conversion_find (void *source,
                         void *destination)
 {
-  int    i = 0;
-  Babl **conversion;
+  int      i = 0;
+  BablList *conversion_list;
+  Babl     *conversion;
 
-  conversion = (void *) BABL (source)->type.from;
-  while (conversion && conversion[i])
+  conversion_list = BABL (source)->type.from_list;
+  for (i = 0; i < babl_list_size (conversion_list); i++)
     {
-      if (conversion[i]->conversion.destination == destination)
-        return (Babl *) conversion[i];
-      i++;
+      conversion = BABL (conversion_list->items[i]);
+      if (conversion->conversion.destination == destination)
+        return conversion;
     }
   babl_fatal ("failed, aborting");
   return NULL;
Index: babl/babl-format.c
===================================================================
--- babl/babl-format.c	(revision 299)
+++ babl/babl-format.c	(working copy)
@@ -27,7 +27,8 @@ static int
 each_babl_format_destroy (Babl *babl,
                           void *data)
 {
-  babl_free (babl->format.from);
+  if (babl->format.from_list)
+    babl_list_destroy (babl->format.from_list);
   babl_free (babl);
 
   return 0;  /* continue iterating */
@@ -74,7 +75,7 @@ format_new (const char     *name,
                       sizeof (int) * (components) +
                       sizeof (int) * (components));
 
-  babl->format.from      = NULL;
+  babl->format.from_list = NULL;
   babl->format.component = (void *) (((char *) babl) + sizeof (BablFormat));
   babl->format.type      = (void *) (((char *) babl->format.component) + sizeof (BablComponent *) * (components));
   babl->format.sampling  = (void *) (((char *) babl->format.type) + sizeof (BablType *) * (components));
@@ -110,7 +111,7 @@ format_new (const char     *name,
 
 static char buf[512] = "";
 
-static const char *
+static char *
 create_name (BablModel      *model,
              int             components,
              BablComponent **component,
Index: babl/babl-introspect.c
===================================================================
--- babl/babl-introspect.c	(revision 299)
+++ babl/babl-introspect.c	(working copy)
@@ -78,37 +78,22 @@ babl_introspect (Babl *babl)
 }
 
 #ifdef BABL_LOG
-static int list_length (void **list)
-{
-  void **ptr;
-  int    len = 0;
-
-  ptr = list;
-  while (NULL != *ptr)
-    {
-      ptr++;
-      len++;
-    }
-  return len;
-}
-
 
 static void
 item_conversions_introspect (Babl *babl)
 {
-  void **ptr;
-
-  if (babl->type.from)
-    babl_log ("\t\tconversions from %s: %i",
-              babl->instance.name, list_length ((void **) (babl->type.from)));
-
-  ptr = (void **) babl->type.from;
+  int i;
+  BablList *list;
 
-  while (ptr && NULL != *ptr)
+  list = babl->type.from_list;
+  if (list) 
     {
-      babl_log ("\t\t\t'%s'", ((Babl *) (*ptr))->instance.name);
-      ptr++;
-    }
+      babl_log ("\t\tconversions from %s: %i",
+                babl->instance.name, babl_list_size (list));
+
+      for (i = 0; i < babl_list_size (list); i++)
+        babl_log ("\t\t\t'%s'", BABL (list->items[i])->instance.name);
+   }
 }
 
 static void
Index: babl/babl-classes.h
===================================================================
--- babl/babl-classes.h	(revision 299)
+++ babl/babl-classes.h	(working copy)
@@ -76,6 +76,7 @@ typedef enum {
 
 
 typedef union _Babl Babl;
+typedef struct _BablList BablList;
 
 /* common header for any item inserted into database */
 typedef struct
@@ -109,7 +110,7 @@ BablConversion {
 typedef struct
 {
   BablInstance     instance;
-  BablConversion **from;  /*< NULL terminated list of conversions from class */
+  BablList         *from_list;
   int              bits;  /*< number of bits used to represent the data type
                             (initially restricted to a multiple of 8) */
   double           min_val;
@@ -136,7 +137,7 @@ typedef struct
 typedef struct
 {
   BablInstance     instance;
-  BablConversion **from; /*< NULL terminated list of conversions from class */
+  BablList         *from_list;
   int              horizontal;
   int              vertical;
   char             name[4];
@@ -154,7 +155,7 @@ typedef struct
 typedef struct
 {
   BablInstance     instance;
-  BablConversion **from; /*< NULL terminated list of conversions from class */
+  BablList         *from_list;
   int              components;
   BablComponent  **component;
   BablType       **type; /*< must be doubles, used here for convenience in code */
@@ -163,7 +164,7 @@ typedef struct
 typedef struct
 {
   BablInstance     instance;
-  BablConversion **from; /*< NULL terminated list of conversions from class */
+  BablList         *from_list;
   int              components;
   BablComponent  **component;
   BablType       **type;
Index: babl/babl-sanity.c
===================================================================
--- babl/babl-sanity.c	(revision 299)
+++ babl/babl-sanity.c	(working copy)
@@ -36,22 +36,20 @@ type_sanity (Babl *babl,
 {
   /* ensure that every type has reference conversions to
    * and from double */
-  void **ptr;
-  int    ok;
+  int      ok, i;
+  BablList *list;
 
   ok = 0;
-  if (babl->type.from)
+  list = babl->type.from_list;
+  if (list)
     {
-      ptr = (void **) babl->type.from;
-
-      while (ptr && NULL != *ptr)
+      for (i = 0; i < babl_list_size (list); i++)
         {
-          if (babl_conversion_destination ((Babl *) (*ptr)) == babl_type_id (BABL_DOUBLE))
+          if (babl_conversion_destination ((Babl *) list->items[i]) == babl_type_id (BABL_DOUBLE))
             {
               ok = 1;
               break;
             }
-          ptr++;
         }
     }
   if (!ok)
@@ -71,22 +69,20 @@ model_sanity (Babl *babl,
 {
   /* ensure that every type has reference conversions to
    * and from rgba */
-  void **ptr;
-  int    ok;
+  int      ok, i;
+  BablList *list;
 
   ok = 0;
-  if (babl->model.from)
+  list = babl->model.from_list;
+  if (list)
     {
-      ptr = (void **) babl->model.from;
-
-      while (ptr && NULL != *ptr)
+      for (i = 0; i < babl_list_size (list); i++)
         {
-          if (babl_conversion_destination ((Babl *) (*ptr)) == babl_model_id (BABL_RGBA))
+          if (babl_conversion_destination ((Babl *) list->items[i]) == babl_model_id (BABL_RGBA))
             {
               ok = 1;
               break;
             }
-          ptr++;
         }
     }
   if (!ok)
Index: babl/babl-util.c
===================================================================
--- babl/babl-util.c	(revision 299)
+++ babl/babl-util.c	(working copy)
@@ -16,66 +16,9 @@
  * <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
 #include <math.h>
-#include "babl-memory.h"
 #include "babl-internal.h"
 
-static int list_length (void **list)
-{
-  void **ptr;
-  int    len = 0;
-
-  ptr = list;
-  while (NULL != *ptr)
-    {
-      ptr++;
-      len++;
-    }
-  return len;
-}
-
-void
-babl_add_ptr_to_list (void ***list,
-                      void   *new)
-{
-  int orig_len = 0;
-
-  if (*list)
-    {
-      orig_len = list_length (*list);
-    }
-
-  *list = babl_realloc ((*list),
-                        sizeof (void *) * (orig_len + 2));
-
-  if (!(*list))
-    {
-      babl_fatal ("failed to realloc");
-    }
-
-  (*list)[orig_len]     = new;
-  (*list)[orig_len + 1] = NULL;
-}
-
-void
-babl_list_each (void           **list,
-                BablEachFunction each_fun,
-                void            *user_data)
-{
-  int i;
-
-  if (!list)
-    return;
-  for (i = 0; i < list_length (list); i++)
-    {
-      if (each_fun ((Babl *) list[i], user_data))
-        break;
-    }
-}
-
 #include <sys/time.h>
 #include <time.h>
 
Index: babl/babl-type.c
===================================================================
--- babl/babl-type.c	(revision 299)
+++ babl/babl-type.c	(working copy)
@@ -28,7 +28,8 @@ static int
 each_babl_type_destroy (Babl *babl,
                         void *data)
 {
-  babl_free (babl->type.from);
+  if (babl->type.from_list)
+    babl_list_destroy (babl->type.from_list);
   babl_free (babl);
   return 0;  /* continue iterating */
 }
@@ -44,13 +45,13 @@ type_new (const char *name,
   babl_assert (bits != 0);
   babl_assert (bits % 8 == 0);
 
-  babl                = babl_malloc (sizeof (BablType) + strlen (name) + 1);
-  babl->instance.name = (void *) ((char *) babl + sizeof (BablType));
-  babl->class_type    = BABL_TYPE;
-  babl->instance.id   = id;
+  babl                 = babl_malloc (sizeof (BablType) + strlen (name) + 1);
+  babl->instance.name  = (void *) ((char *) babl + sizeof (BablType));
+  babl->class_type     = BABL_TYPE;
+  babl->instance.id    = id;
   strcpy (babl->instance.name, name);
-  babl->type.bits = bits;
-  babl->type.from = NULL;
+  babl->type.bits      = bits;
+  babl->type.from_list = NULL;
 
   return babl;
 }
Index: babl/babl-util.h
===================================================================
--- babl/babl-util.h	(revision 299)
+++ babl/babl-util.h	(working copy)
@@ -19,16 +19,6 @@
 #ifndef _BABL_UTIL_H
 #define _BABL_UTIL_H
 
-#include <math.h>
-
-void   babl_add_ptr_to_list (void       ***list,
-                             void         *new);
-
-void
-babl_list_each (void             **list,
-                BablEachFunction   each_fun,
-                void              *user_data);
-
 long
 babl_ticks     (void);
 
Index: babl/babl-conversion.c
===================================================================
--- babl/babl-conversion.c	(revision 299)
+++ babl/babl-conversion.c	(working copy)
@@ -275,7 +275,9 @@ babl_conversion_new (void *first_arg,
    * id/name, inserting newly created class into database.
    */
   babl_db_insert (db, babl);
-  babl_add_ptr_to_list ((void ***) ((Babl *) &(source->type.from)), babl);
+  if (!source->type.from_list)
+    source->type.from_list = babl_list_init_with_size (BABL_CONVERSIONS);
+  babl_list_insert (source->type.from_list, babl);  
   return babl;
 }
 
Index: babl/babl-db.c
===================================================================
--- babl/babl-db.c	(revision 299)
+++ babl/babl-db.c	(working copy)
@@ -111,7 +111,7 @@ babl_db_each (BablDb          *db,
               BablEachFunction each_fun,
               void            *user_data)
 {
-  babl_list_each_temp (db->babl_list, each_fun, user_data);
+  babl_list_each (db->babl_list, each_fun, user_data);
 }
 
 
Index: babl/babl-fish.c
===================================================================
--- babl/babl-fish.c	(revision 299)
+++ babl/babl-fish.c	(working copy)
@@ -41,7 +41,7 @@ babl_conversion_find (const void *source
 {
   void *data = (void*)destination;
 
-  babl_list_each ((void *) BABL (source)->type.from, match_conversion, &data);
+  babl_list_each (BABL (source)->type.from_list, match_conversion, &data);
   if (data == (void*)destination) /* didn't change */
     return NULL;
   return data;
_______________________________________________
Gegl-developer mailing list
Gegl-developer@xxxxxxxxxxxxxxxxxxxxxx
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer

[Index of Archives]     [Yosemite News]     [Yosemite Photos]     [gtk]     [GIMP Users]     [KDE]     [Gimp's Home]     [Gimp on Windows]     [Steve's Art]

  Powered by Linux