FYI, the new kernel newbie column is out

Alex Beamish talexb-Re5JQEeQqe8AvxtiuMwx3w at public.gmane.org
Wed Jul 8 17:02:16 UTC 2009


With a couple of years of Perl development on top of a couple more
years of C development, I'd propose using a table and some pointers:

======================================
/*  Prototype the registration functions. */

int register_this(void *, char *);
int register_that(void *, char *);
int register_those(void *, char *);
int unregister_this(void *, char *);
int unregister_that(void *, char *);

/*  Define a skull element structure that contains a void pointer for a return
 *  value, as well as pointers to the functions that register and unregister.
 *  */

typedef struct {
  void	*pv;
  int	(*pfiRegister)();
  int	(*pfiUnRegister)();
} SKULL_ELEMENT;

/*  Define an array of skull elements and initialize them with function
 *  pointers or NULLs. */

SKULL_ELEMENT astrStack[3] = {
  NULL, register_this,  unregister_this,
  NULL, register_that,  unregister_that,
  NULL, register_those, NULL,
};

int registration ()
{
    SKULL_ELEMENT *pstr;

    /*  Cycle through each the skull elements .. */

    for ( pstr = astrStack; pstr <= &astrStack[2]; pstr++ ) {

      /*  Try to register a skull using the appropriate pointer and function ..
       *  */

      int err = pstr->pfiRegister ( pstr->pv, "skull" );
      if ( err ) {

	/*  If there was an error, go back through the stack and unregister any
	 *  prior registrations, and return the error.  */

        while ( --pstr > astrStack ) {

	  pstr->pfiUnRegister ( pstr->pv, "skull" );

	}
	return ( err );
      }
    }

    /*  Success!  */

    return ( 0 );
}
======================================

I prefer the usage of 'if ( err )' rather than the somewhat
counterintuitive 'if ( !err )', but that's probably a matter for
discussion over beer, and not a mailing list.

It looks like this code tries to register with three different
functions, and if any of them fail, it unregisters from any that
succeeded. I note that there are no tests for failure on
unregistration -- perhaps they were removed from the example code for
clarity.

I didn't have the time to actually confirm that my code is
functionally the same, but it does compile. I think I last used
function pointers in C back in 1998, so I'm not positive I have the
syntax right, but the meaning should be clear enough.

Cheers,
Alex

-- 
Alex Beamish
Toronto, Ontario
aka talexb
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list