From 28549e35fe7e0eeba8e169e6488c0f42b4530c3f Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Tue, 2 Feb 2016 22:31:59 +0100 Subject: [PATCH] SNMP: Update threadsync not to use mem_malloc - memory can be instantiated statically since it is only needed once per threadsync instance --- src/apps/snmp/snmp_mib2.c | 2 +- src/apps/snmp/snmp_threadsync.c | 97 +++++++++---------------- src/include/lwip/apps/snmp_mib2.h | 2 +- src/include/lwip/apps/snmp_threadsync.h | 30 ++++++-- 4 files changed, 61 insertions(+), 70 deletions(-) diff --git a/src/apps/snmp/snmp_mib2.c b/src/apps/snmp/snmp_mib2.c index 7ba377f8..bbfa8c1d 100644 --- a/src/apps/snmp/snmp_mib2.c +++ b/src/apps/snmp/snmp_mib2.c @@ -97,7 +97,7 @@ snmp_mib2_lwip_synchronizer(snmp_threadsync_called_fn fn, void* arg) tcpip_callback_with_block(fn, arg, 1); } -struct snmp_threadsync_locks snmp_mib2_lwip_locks; +struct snmp_threadsync_instance snmp_mib2_lwip_locks; #define SYNC_NODE_NAME(node_name) node_name ## _synced #define CREATE_LWIP_SYNC_NODE(oid, node_name) \ diff --git a/src/apps/snmp/snmp_threadsync.c b/src/apps/snmp/snmp_threadsync.c index e016e362..ab312a4e 100644 --- a/src/apps/snmp/snmp_threadsync.c +++ b/src/apps/snmp/snmp_threadsync.c @@ -33,35 +33,16 @@ #include "lwip/apps/snmp_threadsync.h" #include "lwip/apps/snmp_core.h" -#include "lwip/mem.h" #include "lwip/sys.h" #include - -struct threadsync_data -{ - union { - snmp_err_t u8; - u16_t u16; - } retval; - union { - const u32_t *root_oid; - void *value; - } arg1; - union { - u8_t root_oid_len; - u16_t len; - } arg2; - const struct snmp_threadsync_node *threadsync_node; - struct snmp_node_instance proxy_instance; -}; - + static void call_synced_function(struct threadsync_data *call_data, snmp_threadsync_called_fn fn) { - sys_mutex_lock(&call_data->threadsync_node->locks->sem_usage_mutex); - call_data->threadsync_node->locks->sync_fn(fn, call_data); - sys_sem_wait(&call_data->threadsync_node->locks->sem); - sys_mutex_unlock(&call_data->threadsync_node->locks->sem_usage_mutex); + sys_mutex_lock(&call_data->threadsync_node->instance->sem_usage_mutex); + call_data->threadsync_node->instance->sync_fn(fn, call_data); + sys_sem_wait(&call_data->threadsync_node->instance->sem); + sys_mutex_unlock(&call_data->threadsync_node->instance->sem_usage_mutex); } static void @@ -71,7 +52,7 @@ threadsync_get_value_synced(void *ctx) call_data->retval.u16 = call_data->proxy_instance.get_value(&call_data->proxy_instance, call_data->arg1.value); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static u16_t threadsync_get_value(struct snmp_node_instance* instance, void* value) @@ -91,7 +72,7 @@ threadsync_set_test_synced(void *ctx) call_data->retval.u8 = call_data->proxy_instance.set_test(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static snmp_err_t threadsync_set_test(struct snmp_node_instance* instance, u16_t len, void *value) @@ -112,7 +93,7 @@ threadsync_set_value_synced(void *ctx) call_data->retval.u8 = call_data->proxy_instance.set_value(&call_data->proxy_instance, call_data->arg2.len, call_data->arg1.value); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static snmp_err_t threadsync_set_value(struct snmp_node_instance* instance, u16_t len, void *value) @@ -133,7 +114,7 @@ threadsync_release_instance_synced(void* ctx) call_data->proxy_instance.release_instance(&call_data->proxy_instance); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static void threadsync_release_instance(struct snmp_node_instance *instance) @@ -143,8 +124,6 @@ threadsync_release_instance(struct snmp_node_instance *instance) if(call_data->proxy_instance.release_instance != NULL) { call_synced_function(call_data, threadsync_release_instance_synced); } - - mem_free(call_data); } static void @@ -155,7 +134,7 @@ get_instance_synced(void* ctx) call_data->retval.u8 = leaf->get_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static void @@ -166,51 +145,43 @@ get_next_instance_synced(void* ctx) call_data->retval.u8 = leaf->get_next_instance(call_data->arg1.root_oid, call_data->arg2.root_oid_len, &call_data->proxy_instance); - sys_sem_signal(&call_data->threadsync_node->locks->sem); + sys_sem_signal(&call_data->threadsync_node->instance->sem); } static snmp_err_t do_sync(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance, snmp_threadsync_called_fn fn) { const struct snmp_threadsync_node *threadsync_node = (const struct snmp_threadsync_node*)instance->node; - struct threadsync_data *call_data; + struct threadsync_data *call_data = &threadsync_node->instance->data; if(threadsync_node->node.node.oid != threadsync_node->target->node.oid) { LWIP_DEBUGF(SNMP_DEBUG, ("Sync node OID does not match target node OID")); return SNMP_ERR_NOSUCHINSTANCE; } - call_data = (struct threadsync_data*)mem_malloc(sizeof(struct threadsync_data)); - if(call_data != NULL) { - memset(&call_data->proxy_instance, 0, sizeof(call_data->proxy_instance)); + memset(&call_data->proxy_instance, 0, sizeof(call_data->proxy_instance)); - instance->reference.ptr = call_data; - snmp_oid_assign(&call_data->proxy_instance.instance_oid, instance->instance_oid.id, instance->instance_oid.len); + instance->reference.ptr = call_data; + snmp_oid_assign(&call_data->proxy_instance.instance_oid, instance->instance_oid.id, instance->instance_oid.len); - call_data->proxy_instance.node = &threadsync_node->target->node; - call_data->threadsync_node = threadsync_node; + call_data->proxy_instance.node = &threadsync_node->target->node; + call_data->threadsync_node = threadsync_node; - call_data->arg1.root_oid = root_oid; - call_data->arg2.root_oid_len = root_oid_len; - call_synced_function(call_data, fn); + call_data->arg1.root_oid = root_oid; + call_data->arg2.root_oid_len = root_oid_len; + call_synced_function(call_data, fn); - if(call_data->retval.u8 == SNMP_ERR_NOERROR) { - instance->access = call_data->proxy_instance.access; - instance->asn1_type = call_data->proxy_instance.asn1_type; - instance->release_instance = threadsync_release_instance; - instance->get_value = (call_data->proxy_instance.get_value != NULL)? threadsync_get_value : NULL; - instance->set_value = (call_data->proxy_instance.set_value != NULL)? threadsync_set_value : NULL; - instance->set_test = (call_data->proxy_instance.set_test != NULL)? threadsync_set_test : NULL; - snmp_oid_assign(&instance->instance_oid, call_data->proxy_instance.instance_oid.id, call_data->proxy_instance.instance_oid.len); - } else { - mem_free(call_data); - } - - return call_data->retval.u8; - } else { - LWIP_DEBUGF(SNMP_DEBUG, ("Out of memory")); - return SNMP_ERR_NOSUCHINSTANCE; + if(call_data->retval.u8 == SNMP_ERR_NOERROR) { + instance->access = call_data->proxy_instance.access; + instance->asn1_type = call_data->proxy_instance.asn1_type; + instance->release_instance = threadsync_release_instance; + instance->get_value = (call_data->proxy_instance.get_value != NULL)? threadsync_get_value : NULL; + instance->set_value = (call_data->proxy_instance.set_value != NULL)? threadsync_set_value : NULL; + instance->set_test = (call_data->proxy_instance.set_test != NULL)? threadsync_set_test : NULL; + snmp_oid_assign(&instance->instance_oid, call_data->proxy_instance.instance_oid.id, call_data->proxy_instance.instance_oid.len); } + + return call_data->retval.u8; } snmp_err_t @@ -225,11 +196,11 @@ snmp_threadsync_get_next_instance(const u32_t *root_oid, u8_t root_oid_len, stru return do_sync(root_oid, root_oid_len, instance, get_next_instance_synced); } -void snmp_threadsync_init(struct snmp_threadsync_locks *locks, snmp_threadsync_synchronizer_fn sync_fn) +void snmp_threadsync_init(struct snmp_threadsync_instance *instance, snmp_threadsync_synchronizer_fn sync_fn) { - sys_mutex_new(&locks->sem_usage_mutex); - sys_sem_new(&locks->sem, 0); - locks->sync_fn = sync_fn; + sys_mutex_new(&instance->sem_usage_mutex); + sys_sem_new(&instance->sem, 0); + instance->sync_fn = sync_fn; } #endif /* LWIP_SNMP */ diff --git a/src/include/lwip/apps/snmp_mib2.h b/src/include/lwip/apps/snmp_mib2.h index d68f6156..312d0c07 100644 --- a/src/include/lwip/apps/snmp_mib2.h +++ b/src/include/lwip/apps/snmp_mib2.h @@ -54,7 +54,7 @@ extern const struct snmp_mib mib2; #if SNMP_USE_NETCONN #include "lwip/apps/snmp_threadsync.h" void snmp_mib2_lwip_synchronizer(snmp_threadsync_called_fn fn, void* arg); -extern struct snmp_threadsync_locks snmp_mib2_lwip_locks; +extern struct snmp_threadsync_instance snmp_mib2_lwip_locks; #endif #ifndef SNMP_SYSSERVICES diff --git a/src/include/lwip/apps/snmp_threadsync.h b/src/include/lwip/apps/snmp_threadsync.h index c74de27c..f2c8fe55 100644 --- a/src/include/lwip/apps/snmp_threadsync.h +++ b/src/include/lwip/apps/snmp_threadsync.h @@ -47,20 +47,40 @@ extern "C" { typedef void (*snmp_threadsync_called_fn)(void* arg); typedef void (*snmp_threadsync_synchronizer_fn)(snmp_threadsync_called_fn fn, void* arg); -struct snmp_threadsync_locks + +struct threadsync_data +{ + union { + snmp_err_t u8; + u16_t u16; + } retval; + union { + const u32_t *root_oid; + void *value; + } arg1; + union { + u8_t root_oid_len; + u16_t len; + } arg2; + const struct snmp_threadsync_node *threadsync_node; + struct snmp_node_instance proxy_instance; +}; + +struct snmp_threadsync_instance { sys_sem_t sem; sys_mutex_t sem_usage_mutex; snmp_threadsync_synchronizer_fn sync_fn; + struct threadsync_data data; }; /* thread sync node */ struct snmp_threadsync_node { /* inherited "base class" members */ - struct snmp_leaf_node node; - const struct snmp_leaf_node *target; - struct snmp_threadsync_locks *locks; + struct snmp_leaf_node node; + const struct snmp_leaf_node *target; + struct snmp_threadsync_instance *instance; }; snmp_err_t snmp_threadsync_get_instance(const u32_t *root_oid, u8_t root_oid_len, struct snmp_node_instance* instance); @@ -73,7 +93,7 @@ snmp_err_t snmp_threadsync_get_next_instance(const u32_t *root_oid, u8_t root_oi (target), \ (locks) } -void snmp_threadsync_init(struct snmp_threadsync_locks *locks, snmp_threadsync_synchronizer_fn sync_fn); +void snmp_threadsync_init(struct snmp_threadsync_instance *instance, snmp_threadsync_synchronizer_fn sync_fn); #endif /* LWIP_SNMP */