destruct and clone

This is a discussion on destruct and clone within the SNMP Coders forums, part of the Networking and Network Related category; --=-8hqWXQVD1qx2xRhn0iwo Content-Type: text/plain Content-Transfer-Encoding: 7bit I have attached a first version of destruct and clone for ...


Go Back   Usenet Forums > Networking and Network Related > SNMP Coders

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008
Magnus Fromreide
 
Posts: n/a
Default destruct and clone


--=-8hqWXQVD1qx2xRhn0iwo
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

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

--=-8hqWXQVD1qx2xRhn0iwo
Content-Disposition: attachment; filename=patch.diff
Content-Type: text/x-patch; name=patch.diff; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

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;
}

--=-8hqWXQVD1qx2xRhn0iwo
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

-------------------------------------------------------------------------
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/
--=-8hqWXQVD1qx2xRhn0iwo
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Net-snmp-coders mailing list
Net-snmp-coders@lists.sourceforge.net
https://lists.sourceforge.net/lists/...et-snmp-coders

--=-8hqWXQVD1qx2xRhn0iwo--


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:56 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0