[hipl-commit] [tiny] Rev 3609: Cleaned-up the modularization code.

  • From: Tim Just <tim.just@xxxxxxxxxxxxxx>
  • To: hipl-commit@xxxxxxxxxxxxx
  • Date: Wed, 3 Mar 2010 18:09:20 +0200

Committer: Tim Just <tim.just@xxxxxxxxxxxxxx>
Date: Wed Mar 03 17:06:50 2010 +0100
Revision: 3609
Revision-id: tim.just@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Branch nick: tiny

Log:
  Cleaned-up the modularization code.
  
  - Changed prefix of generic functions to 'lmod_'
  - Fixed style issues
  - Removed unneeded comparisons with 'NULL'
  - Added license header to hipd/modularization.h
  - Improved doxygen comments

Modified:
  M  hipd/hadb.c
  M  hipd/modularization.c
  M  hipd/modularization.h
  M  lib/modularization/modularization.c
  M  lib/modularization/modularization.h
  M  modules/update/hipd/update.c

=== modified file 'hipd/hadb.c'
--- hipd/hadb.c 2010-03-01 09:27:42 +0000
+++ hipd/hadb.c 2010-03-03 16:06:50 +0000
@@ -592,8 +592,8 @@
     memset(entry->hip_msg_retrans.buf, 0, HIP_MAX_NETWORK_PACKET);
 
     /* Initialize module states */
-    entry->hip_modular_state = hip_init_state();
-    hip_init_state_items(entry->hip_modular_state);
+    entry->hip_modular_state = lmod_init_state();
+    lmod_init_state_items(entry->hip_modular_state);
     HIP_DEBUG("Modular state initialized.\n");
 
 out_err:
@@ -1176,7 +1176,7 @@
         HIP_FREE(ha->rendezvous_addr);
     }
 
-    hip_free_state(ha->hip_modular_state);
+    lmod_uninit_state(ha->hip_modular_state);
 
     if (ha->locator) {
         free(ha->locator);

=== modified file 'hipd/modularization.c'
--- hipd/modularization.c       2010-03-03 15:24:05 +0000
+++ hipd/modularization.c       2010-03-03 16:06:50 +0000
@@ -65,7 +65,7 @@
                                  const uint32_t priority)
 {
     int err = 0;
-    struct handle_function *new_entry     = NULL;
+    struct handle_function *new_entry = NULL;
 
     HIP_IFEL(packet_type > HIP_MAX_PACKET_TYPE,
              -1,
@@ -74,7 +74,7 @@
              -1,
              "Maximum host association state exceeded.\n");
 
-    HIP_IFEL(((new_entry = malloc(sizeof(struct handle_function))) == NULL),
+    HIP_IFEL(!(new_entry = malloc(sizeof(struct handle_function))),
              -1,
              "Error on allocating memory for a handle function entry.\n");
 
@@ -86,7 +86,7 @@
             lmod_register_function(hip_handle_functions[packet_type][ha_state],
                                    new_entry,
                                    priority);
-    if (hip_handle_functions[packet_type][ha_state] == NULL) {
+    if (!hip_handle_functions[packet_type][ha_state]) {
         HIP_ERROR("Error on registering a handle function.\n");
         err = -1;
     }
@@ -162,9 +162,9 @@
              packet_type,
              ha_state);
 
-    while ((iter = hip_ll_iterate(hip_handle_functions[packet_type][ha_state],
-                                  iter)) != NULL)
-    {
+    while ((iter =
+           hip_ll_iterate(hip_handle_functions[packet_type][ha_state], iter))) 
{
+
         ((struct handle_function *) iter->ptr)->func_ptr(packet_type,
                                                          ha_state,
                                                          ctx);
@@ -182,7 +182,7 @@
  */
 void hip_uninit_handle_functions(void)
 {
-    int i,j;
+    int i, j;
 
     for (i = 0; i < HIP_MAX_PACKET_TYPE; i++) {
         for (j = 0; j < HIP_MAX_HA_STATE; j++) {
@@ -202,9 +202,9 @@
                                 const uint32_t priority)
 {
     int err = 0;
-    struct maint_function *new_entry     = NULL;
+    struct maint_function *new_entry = NULL;
 
-    HIP_IFEL(((new_entry = malloc(sizeof(struct maint_function))) == NULL),
+    HIP_IFEL(!(new_entry = malloc(sizeof(struct maint_function))),
              -1,
              "Error on allocating memory for a handle function entry.\n");
 
@@ -215,7 +215,7 @@
     hip_maintenance_functions = 
lmod_register_function(hip_maintenance_functions,
                                                        new_entry,
                                                        priority);
-    if (hip_maintenance_functions == NULL) {
+    if (!hip_maintenance_functions) {
         HIP_ERROR("Error on registering a maintenance function.\n");
         err = -1;
     }
@@ -237,8 +237,8 @@
     int            err  = 0;
     hip_ll_node_t *iter = NULL;
 
-    if(hip_maintenance_functions != NULL) {
-        while ((iter = hip_ll_iterate(hip_maintenance_functions, iter)) != 
NULL) {
+    if (hip_maintenance_functions) {
+        while ((iter = hip_ll_iterate(hip_maintenance_functions, iter))) {
             ((struct maint_function*) iter->ptr)->func_ptr();
         }
     } else {

=== modified file 'hipd/modularization.h'
--- hipd/modularization.h       2010-03-03 14:56:11 +0000
+++ hipd/modularization.h       2010-03-03 16:06:50 +0000
@@ -1,3 +1,12 @@
+/**
+ * @file
+ * The header file for hipd/modularization.h
+ *
+ * Distributed under <a 
href="http://www.gnu.org/licenses/gpl2.txt";>GNU/GPL</a>.
+ *
+ * @author Tim Just <tim.just@xxxxxxxxxxxxxx>
+ *
+ */
 #ifndef HIP_HIPD_MODULARIZATION_H
 #define HIP_HIPD_MODULARIZATION_H
 
@@ -25,5 +34,4 @@
 
 void hip_uninit_maint_functions(void);
 
-
 #endif /* HIP_HIPD_MODULARIZATION_H */

=== modified file 'lib/modularization/modularization.c'
--- lib/modularization/modularization.c 2010-03-03 14:56:11 +0000
+++ lib/modularization/modularization.c 2010-03-03 16:06:50 +0000
@@ -14,6 +14,9 @@
 #include "modularization.h"
 #include "lib/core/debug.h"
 
+/**
+ * A generic struct for function pointer.
+ */
 struct function {
     uint32_t priority;
     void    *func_ptr;
@@ -21,69 +24,16 @@
 
 
 /**
- * @todo add description
+ * List of initialization functions for the modular state.
+ *
+ * Call lmod_register_state_init_function to add a function to the list and
+ * lmod_init_state_items to initialize all items of a modular state instance.
+ *
  */
 static hip_ll_t *state_init_functions;
 
-hip_ll_t *lmod_register_function(hip_ll_t *list, void *entry, const uint32_t 
priority)
-{
-    int            index    = 0;
-    hip_ll_t      *new_list = NULL;
-    hip_ll_node_t *iter     = NULL;
-
-    if (list == NULL) {
-        if ((new_list = malloc(sizeof(hip_ll_t))) == NULL) {
-            return NULL;
-        }
-        hip_ll_init(new_list);
-        list = new_list;
-    }
-
-    if (entry == NULL) {
-        return NULL;
-    }
-
-    /* Iterate through function list until the desired position is found */
-    while ((iter = hip_ll_iterate(list, iter)) != NULL)
-    {
-        if (priority < ((struct function *) iter->ptr)->priority) {
-            break;
-        } else {
-            index++;
-        }
-    }
-
-    hip_ll_add(list, index, entry);
-
-    return list;
-}
-
-int lmod_unregister_function(hip_ll_t *list, const void *function)
-{
-    int            index = 0;
-    hip_ll_node_t *iter  = NULL;
-
-    if(list == NULL) {
-        return -1;
-    }
-
-    while ((iter = hip_ll_iterate(list, iter)) != NULL) {
-        if (function == ((struct function *) iter->ptr)->func_ptr) {
-            hip_ll_del(list, index, free);
-            break;
-        }
-        index++;
-    }
-
-    return 0;
-}
-
-/******************************************************************************
- * MODULAR STATE                                                              *
- 
******************************************************************************/
-
 /**
- * hip_init_state
+ * lmod_init_state
  *
  * Initializes a new data structure for storage of references to state items.
  * This data structure consists of a pointer set and can be mentioned as global
@@ -92,16 +42,16 @@
  *  @return Success = Pointer to the new data structure
  *          Error   = NULL
  */
-struct modular_state *hip_init_state(void)
+struct modular_state *lmod_init_state(void)
 {
     struct modular_state *state;
 
-    if ((state = malloc(sizeof(struct modular_state))) == NULL) {
+    if (!(state = malloc(sizeof(struct modular_state)))) {
         HIP_ERROR("Error on allocating memory for a modular_state 
instance.\n");
         return NULL;
     }
 
-    if ((state->item_list = malloc(sizeof(hip_ll_t))) == NULL) {
+    if (!(state->item_list = malloc(sizeof(hip_ll_t)))) {
         HIP_ERROR("Error on allocating memory for a linked list.\n");
         return NULL;
     }
@@ -114,7 +64,7 @@
 }
 
 /**
- * hip_register_state_init_function
+ * lmod_register_state_init_function
  *
  * Registers a new state initialization function. These functions are called,
  * when a new host association database entry is created.
@@ -124,7 +74,7 @@
  * @return Success = 0
  *         Error   = -1
  */
-int hip_register_state_init_function(void *func)
+int lmod_register_state_init_function(void *func)
 {
     int err = 0;
     hip_ll_t *new_func_list = NULL;
@@ -132,7 +82,7 @@
     HIP_IFEL(!func, -1, "Invalid init function provided");
 
     if (!state_init_functions) {
-        HIP_IFEL(((new_func_list = malloc(sizeof(hip_ll_t))) == NULL),
+        HIP_IFEL(!(new_func_list = malloc(sizeof(hip_ll_t))),
                  -1,
                  "Error on allocating memory for a linked list.\n");
         hip_ll_init(new_func_list);
@@ -146,22 +96,22 @@
 }
 
 /**
- * hip_init_state_items
+ * lmod_init_state_items
  *
  * Initialize all registered state items. This function is called, when a new
  * host association database entry is created.
  *
- * @note  Call hip_register_state_init_function to add an initialization
+ * @note  Call lmod_register_state_init_function to add an initialization
  *        function.
  *
  * @param *state    Pointer to the modular state data structure.
  */
-void hip_init_state_items(struct modular_state *state)
+void lmod_init_state_items(struct modular_state *state)
 {
     hip_ll_node_t *iter = NULL;
     int (*init_function)(struct modular_state *state) = NULL;
 
-    while ((iter = hip_ll_iterate(state_init_functions, iter)) != NULL) {
+    while ((iter = hip_ll_iterate(state_init_functions, iter))) {
         init_function = iter->ptr;
         init_function(state);
     }
@@ -181,14 +131,14 @@
  *  @param      item_name   String for retrieving the state item by name.
  *  @return Success = id (unsigned int) for retrieving the state by number
  *          Error   = -1
- **/
+ */
 int hip_add_state_item(struct modular_state *state,
                        void *state_item,
                        const char *item_name)
 {
 
     /* Check if identifier already exists */
-    if (-1 != hip_get_state_item_id(state, item_name)) {
+    if (-1 != lmod_get_state_item_id(state, item_name)) {
         return -1;
     }
 
@@ -203,7 +153,7 @@
 }
 
 /**
- * hip_get_state_item
+ * lmod_get_state_item
  *
  * Returns a void pointer to a state item from the global state set using
  * the string identifier.
@@ -212,18 +162,18 @@
  *  @param      item_name   String identifying the state.
  *  @return Success = Pointer to the requested state item (if exists)
  *          Error   = NULL
- **/
-void *hip_get_state_item(struct modular_state *state, const char *item_name)
+ */
+void *lmod_get_state_item(struct modular_state *state, const char *item_name)
 {
     unsigned int state_id;
 
-    state_id = hip_get_state_item_id(state, item_name);
+    state_id = lmod_get_state_item_id(state, item_name);
 
-    return hip_get_state_item_by_id(state, state_id);
+    return lmod_get_state_item_by_id(state, state_id);
 }
 
 /**
- * hip_get_state_item_by_id
+ * lmod_get_state_item_by_id
  *
  * Returns a void pointer to a state item from the global state set using
  * the id (index number).
@@ -232,15 +182,15 @@
  *  @param      id          Index number of the requested state.
  *  @return Success = Pointer to the requested state item (if exists)
  *          Error   = NULL
- **/
-void *hip_get_state_item_by_id(struct modular_state *state,
-                               const unsigned int id)
+ */
+void *lmod_get_state_item_by_id(struct modular_state *state,
+                                const unsigned int id)
 {
     return hip_ll_get(state->item_list, id);
 }
 
 /**
- * hip_get_state_item_id
+ * lmod_get_state_item_id
  *
  * Retrieve a void pointer to a state variable from the global state set using
  * the state item name.
@@ -249,10 +199,10 @@
  *  @param      item_name   String identifying a state.
  *  @return Success = id (index number) of the state item as unsigned int
  *          Error   = -1
- **/
-int hip_get_state_item_id(struct modular_state *state, const char *item_name)
+ */
+int lmod_get_state_item_id(struct modular_state *state, const char *item_name)
 {
-    unsigned int      i;
+    unsigned int i;
 
     for (i = 0; i < state->num_items; i++) {
        if (0 == strcmp(item_name, state->item_names[i])) {
@@ -264,15 +214,15 @@
 }
 
 /**
- * hip_free_state
+ * lmod_uninit_state
  *
  * Free all allocated memory for storage of the global state set.
  *
  *  @param      state       Pointer to the global state.
- **/
-void hip_free_state(struct modular_state *state)
+ */
+void lmod_uninit_state(struct modular_state *state)
 {
-    unsigned int      i;
+    unsigned int i;
 
     hip_ll_uninit(state->item_list, free);
     free(state->item_list);
@@ -284,3 +234,83 @@
     free(state->item_names);
     free(state);
 }
+
+/**
+ * lmod_register_function
+ *
+ * Register a function to the specified list according their priority.
+ *
+ * @param *list Pointer to the list if already exist, NULL otherwise.
+ * @param *entry Pointer to the data structure containing the function pointer.
+ * @param priority Execution priority for the function.
+ *
+ * @note If there already exists a function with the same priority, this
+ *       function will return NULL as error value. Functions need to have 
unique
+ *       priority values.
+ *
+ * @return Success = Pointer to the function list.
+ *         Error   = NULL
+ */
+hip_ll_t *lmod_register_function(hip_ll_t *list, void *entry, const uint32_t 
priority)
+{
+    int            index    = 0;
+    hip_ll_t      *new_list = NULL;
+    hip_ll_node_t *iter     = NULL;
+
+    if (!list) {
+        if (!(new_list = malloc(sizeof(hip_ll_t)))) {
+            return NULL;
+        }
+        hip_ll_init(new_list);
+        list = new_list;
+    }
+
+    if (!entry) {
+        return NULL;
+    }
+
+    while ((iter = hip_ll_iterate(list, iter))) {
+        if (priority == ((struct function *) iter->ptr)->priority) {
+            return NULL;
+        } else if (priority < ((struct function *) iter->ptr)->priority) {
+            break;
+        } else {
+            index++;
+        }
+    }
+
+    hip_ll_add(list, index, entry);
+
+    return list;
+}
+
+/**
+ * lmod_unregister_function
+ *
+ * Unregister a function from the specified list.
+ *
+ * @param *list Pointer to the list from which the function should be removed.
+ * @param *function Pointer to the function to remove.
+ *
+ * @return Success =  0
+ *         Error   = -1
+ */
+int lmod_unregister_function(hip_ll_t *list, const void *function)
+{
+    int            index = 0;
+    hip_ll_node_t *iter  = NULL;
+
+    if (!list) {
+        return -1;
+    }
+
+    while ((iter = hip_ll_iterate(list, iter))) {
+        if (function == ((struct function *) iter->ptr)->func_ptr) {
+            hip_ll_del(list, index, free);
+            break;
+        }
+        index++;
+    }
+
+    return 0;
+}

=== modified file 'lib/modularization/modularization.h'
--- lib/modularization/modularization.h 2010-03-03 14:56:11 +0000
+++ lib/modularization/modularization.h 2010-03-03 16:06:50 +0000
@@ -38,25 +38,25 @@
 hip_ll_t *lmod_register_function(hip_ll_t *list, void *entry, const uint32_t 
priority);
 int lmod_unregister_function(hip_ll_t *list, const void *function);
 
-int hip_register_state_init_function(void *func);
-
-void hip_init_state_items(struct modular_state *state);
-
-struct modular_state *hip_init_state(void);
+int lmod_register_state_init_function(void *func);
+
+void lmod_init_state_items(struct modular_state *state);
+
+struct modular_state *lmod_init_state(void);
 
 int   hip_add_state_item(struct modular_state *state,
                          void *state_item,
                          const char *item_name);
 
-void *hip_get_state_item(struct modular_state *state,
+void *lmod_get_state_item(struct modular_state *state,
                          const char *item_name);
 
-void *hip_get_state_item_by_id(struct modular_state *state,
+void *lmod_get_state_item_by_id(struct modular_state *state,
                                const unsigned int index);
 
-int   hip_get_state_item_id(struct modular_state *state,
+int   lmod_get_state_item_id(struct modular_state *state,
                             const char *item_name);
 
-void  hip_free_state(struct modular_state *state);
+void  lmod_uninit_state(struct modular_state *state);
 
 #endif /* HIP_LIB_MODULARIZATION_MODULARIZATION_H */

=== modified file 'modules/update/hipd/update.c'
--- modules/update/hipd/update.c        2010-03-03 14:29:20 +0000
+++ modules/update/hipd/update.c        2010-03-03 16:06:50 +0000
@@ -150,7 +150,7 @@
         /* hip_update_set_new_spi_in_old(ha, esp_info_old_spi,
          *  esp_info_new_spi, 0);*/
 
-        localstate = hip_get_state_item(ha->hip_modular_state, "update");
+        localstate = lmod_get_state_item(ha->hip_modular_state, "update");
         localstate->update_id_out++;
         HIP_DEBUG("outgoing UPDATE ID=%u\n", 
hip_update_get_out_id(localstate));
         /** @todo Handle this case. */
@@ -287,7 +287,7 @@
     struct in6_addr *address;
     struct update_state *localstate;
 
-    localstate = hip_get_state_item(ha->hip_modular_state, "update");
+    localstate = lmod_get_state_item(ha->hip_modular_state, "update");
 
     HIP_DEBUG("Addresses to send update:\n");
     list_for_each_safe(item, tmp, localstate->addresses_to_send_echo_request, 
i) {
@@ -399,7 +399,7 @@
 
             break;
         case HIP_UPDATE_ECHO_REQUEST:
-            localstate = hip_get_state_item(ha->hip_modular_state, "update");
+            localstate = lmod_get_state_item(ha->hip_modular_state, "update");
 
             list_for_each_safe(item, tmp, 
localstate->addresses_to_send_echo_request, i) {
                 dst_addr = (struct in6_addr *) list_entry(item);
@@ -543,7 +543,7 @@
 
     // Empty the addresses_to_send_echo_request list before adding the
     // new addresses
-    localstate = hip_get_state_item(ha->hip_modular_state, "update");
+    localstate = lmod_get_state_item(ha->hip_modular_state, "update");
     HIP_DEBUG("hip_get_state_item returned localstate: %p\n", localstate);
     hip_remove_addresses_to_send_echo_request(localstate);
 
@@ -716,7 +716,7 @@
         goto out_err;
     }
 
-    localstate = hip_get_state_item(ctx->hadb_entry->hip_modular_state, 
"update");
+    localstate = lmod_get_state_item(ctx->hadb_entry->hip_modular_state, 
"update");
 
     /* RFC 5201 Section 6.12: Receiving UPDATE Packets */
     HIP_DEBUG("previous incoming update id=%u\n", localstate->update_id_in);
@@ -870,7 +870,7 @@
  */
 int hip_update_init(void)
 {
-    hip_register_state_init_function(&hip_update_init_state);
+    lmod_register_state_init_function(&hip_update_init_state);
 
     hip_register_handle_function(HIP_UPDATE,
                                  HIP_STATE_ESTABLISHED,

Other related posts:

  • » [hipl-commit] [tiny] Rev 3609: Cleaned-up the modularization code. - Tim Just