All of lore.kernel.org
 help / color / mirror / Atom feed
From: Marco Gerards <mgerards@xs4all.nl>
To: The development of GRUB 2 <grub-devel@gnu.org>
Subject: Re: Handlers
Date: Fri, 29 Aug 2008 16:46:32 +0200	[thread overview]
Message-ID: <87d4jrj2uv.fsf@xs4all.nl> (raw)
In-Reply-To: <87ljygkl3a.wl%neal@walfield.org> (Neal H. Walfield's message of "Fri, 29 Aug 2008 15:27:21 +0200")

"Neal H. Walfield" <neal@walfield.org> writes:

> I know know why you call this a handler; it seems to me that it is
> just a semi-generic list package.  Am I missing something?

Perhaps.  I can better explain how this can be used and give a small
example.  GRUB has several types of handlers.  I hope a handler is the
right word, please correct me if it's not.  Examples of handlers are
filesystems, terminals, partitioning schemes, commands, etc.  A
handler usually consists of a struct with function pointers and a
pointer to the next handler of its kind.

Let's focus on filesystems.  To implement the filesystem handler, we
defined 3 basic functions.  You need to be able to register and
unregister filesystems.  It should be possible to iterate over all
filesystems.  grub_file_open, for example, iterates over all
filesystems to see if it detects a certain filesystem layout on some
disk.

What we currently did is rewriting grub_*_register, grub_*_unregister
and grub_*_iterate.  It's boring to rewrite such functions all the
time and this results in duplicated code.

So it is not a list in the classical sense, it does not contain data.

> You can find a slighly more flexible and generic implementation here:
>
>   http://cvs.savannah.gnu.org/viewvc/hurd-l4/viengoos/list.h?root=hurd&view=markup
>
> I've been using that for a while and am quite satisfied with it.
>
> Perhaps you'll find it useful.

It certainly looks good, thanks for the suggestion.  However, I do not
think we have the same goals.  For example, I focus on size and do not
need many features.

> Two comments:
>
> At Fri, 29 Aug 2008 14:36:56 +0200,
> Marco Gerards wrote:
>> +void
>> +grub_handler_unregister (grub_handler_t *head, grub_handler_t handler)
>> +{
>> +  grub_handler_t *p, q;
>> +
>> +  for (p = head, q = *p; q; p = &(q->next), q = q->next)
>                     ^^^^^^                     ^^^^^^^^^^^
>
> This is a bit inconsistent.
>
>   for (p = head, q = *head; q; p = &(q->next), q = q->next)
>
> or
>
>   for (p = head, q = *p; q; p = &(q->next), q = *p)
>
>
> Or, more succinctly:
>
>   for (p = head; (q = *p); p = &(q->next))

You are right, I simply blindly copied this from existing code.  I can
make this more consistent.

>> +int
>> +grub_handler_iterate (grub_handler_t head,
>> +		      int (*hook) (const grub_handler_t handler))
>> +{
>> +  grub_handler_t p;
>> +
>> +  for (p = head; p; p = p->next)
>> +    if (hook (p))
>> +      break;
>> +
>> +  return 0;
>> +}
>
> A suggestion: when HOOK returns a non-zero value, return that from the
> function:
>
>      for (p = head; p; p = p->next)
>        {
>          int ret = hook (p);
>          if (ret)
>            return ret;
>        }
>      return 0;

Well spotted!  Normally we use:

if (hook (p))
  return 1;

This is what happens when you blindly copy existing code, it might not
meet the behavior you expect :-/

--
Marco




  reply	other threads:[~2008-08-29 14:41 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-29 12:36 Handlers Marco Gerards
2008-08-29 13:27 ` Handlers Neal H. Walfield
2008-08-29 14:46   ` Marco Gerards [this message]
2008-08-29 19:13     ` Handlers Neal H. Walfield
2008-08-29 14:58 ` Handlers Vesa Jääskeläinen
2008-08-29 15:10   ` Handlers Marco Gerards
2008-08-29 19:43     ` Handlers Vesa Jääskeläinen
2008-08-30  1:27       ` Handlers Marco Gerards
2008-08-30 14:55         ` Handlers Vesa Jääskeläinen
2008-08-30 12:45 ` Handlers Robert Millan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d4jrj2uv.fsf@xs4all.nl \
    --to=mgerards@xs4all.nl \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.