I have attached a first version of destruct and clone for
netsnmp_mib_handlers in order to demonstrate the concept.

I would really like to get comments on it.

The patch do incidentally fix a memory leak in the agent, and there are
more similar low hanging fruit available.

/MF
Index: include/net-snmp/agent/agent_handler.h
===================================================================
--- include/net-snmp/agent/agent_handler.h	(revision 16840)
+++ include/net-snmp/agent/agent_handler.h	(working copy)
@@ -64,6 +64,15 @@ typedef struct netsnmp_mib_handler_s {
 
         struct netsnmp_mib_handler_s *next;
         struct netsnmp_mib_handler_s *prev;
+
+        /** delete the myvoid member - default is to do nothing */
+        void (*delete_method)(void *);
+
+        /** deep copy the myvoid member - default is to copy the pointer
+         *  returns 0 on success, -1 on failure
+         */
+        int (*clone_method)(void **dest, void *src);
+
 } netsnmp_mib_handler;
 
 /*
Index: agent/helpers/scalar_group.c
===================================================================
--- agent/helpers/scalar_group.c	(revision 16840)
+++ agent/helpers/scalar_group.c	(working copy)
@@ -16,6 +16,22 @@
 #include <net-snmp/agent/serialize.h>
 #include <net-snmp/agent/read_only.h>
 
+static int
+clone_scalar_group(netsnmp_scalar_group** dest, netsnmp_scalar_group* src)
+{
+    if (src) {
+        netsnmp_scalar_group *t = SNMP_MALLOC_TYPEDEF(netsnmp_scalar_group);
+        if(t != NULL) {
+            t->lbound = src->lbound;
+            t->ubound = src->ubound;
+            *dest = t;
+            return 0;
+        } else
+            return -1;
+    } else
+        return 0;
+}
+
 /** @defgroup scalar_group_group scalar_group
  *  Process groups of scalars.
  *  @ingroup leaf
@@ -39,6 +55,8 @@ netsnmp_get_scalar_group_handler(oid fir
 	    sgroup->lbound = first;
 	    sgroup->ubound = last;
             ret->myvoid = (void *)sgroup;
+            ret->delete_method = free;
+            ret->clone_method = clone_scalar_group;
 	}
     }
     return ret;
Index: agent/agent_handler.c
===================================================================
--- agent/agent_handler.c	(revision 16840)
+++ agent/agent_handler.c	(working copy)
@@ -579,6 +579,10 @@ netsnmp_handler_free(netsnmp_mib_handler
          *  defined. About 30 functions down the stack, starting
          *  in clear_context() -> clear_subtree()
          */
+        if(handler->delete_method) {
+            handler->delete_method(handler->myvoid);
+            handler->myvoid = NULL;
+        }
         SNMP_FREE(handler->handler_name);
         SNMP_FREE(handler);
     }
@@ -599,7 +603,13 @@ netsnmp_handler_dup(netsnmp_mib_handler 
     h = _clone_handler(handler);
 
     if (h != NULL) {
-        h->myvoid = handler->myvoid;
+        if (handler->clone_method) {
+            if(handler->clone_method(&h->myvoid, handler->myvoid) != 0) {
+                netsnmp_handler_free(h);
+                return NULL;
+            }
+        } else
+            h->myvoid = handler->myvoid;
 
         if (handler->next != NULL) {
             h->next = netsnmp_handler_dup(handler->next);
@@ -873,8 +883,11 @@ _clone_handler(netsnmp_mib_handler *it)
         return NULL;
 
     dup = netsnmp_create_handler(it->handler_name, it->access_method);
-    if(NULL != dup)
+    if(NULL != dup) {
         dup->flags = it->flags;
+        dup->delete_method = it->delete_method;
+        dup->clone_method = it->clone_method;
+    }
 
     return dup;
 }
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/net-snmp-coders

Reply via email to