grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

a simple list


From: Guffens, Vincent
Subject: a simple list
Date: Mon, 8 May 2006 00:14:18 +0100

Hi,

I need to use a simple list to register the pci devices, drivers and so on. I notice that there are lists like that already in the code so what would you think about having a list.h file like that ?

/* A very simple list.
 *
 * If you want a list of struct myitem
 * you do
 *
 * struct myitem *item_list;
 *
 * where myitem MUST have its next pointer as the FIRST field
 *
 * and you can then add, delete the EL item,
 * grub_add_list (&item_list, el);
 * grub_del_list (&item_list, el);
 *
 * or call HOOK(item) for each element of the list
 * grub_iterate_list (item_list, hook);
 *
 * This brk version will point el to the list item for which
 * HOOK(EL) returns a non-null value
 * grub_iterate_list_brk (item_list, hook, el);
 *
 */

struct obj {
  struct obj *next; /* MUST BE FIRST */
};

#define grub_del_list(list, el) _grub_del_list((struct obj**) list, (struct obj*) el)
#define grub_add_list(list, el) _grub_add_list((struct obj**) list, (struct obj*) el)
#define grub_find_list(list, el) \
  (typeof(list)) _grub_find_list((struct obj*) list, (struct obj*) el)
#define grub_iterate_list(list, func) \
  {typeof(list) el = list; while (el) {func(el); el=el->next;}}
#define grub_iterate_list_brk(list, func, it) \
  {typeof(list) el = list; it = 0; \
    while (el) {if (func(el)) {it = el; break;} el=el->next; }}

static inline struct obj*  _grub_find_list (struct obj *list, struct obj *el)
{
  struct obj *it = list;
  for (it = list; it; it=it->next)
  {
    if (it == el) return el;
  }
  return 0;
};

static inline void _grub_add_list (struct obj **list, struct obj *el)
{
  if ( (!el) || (_grub_find_list (*list, el)) )
    return;
 
  el->next = *list;
  *list = el;
};

static inline void _grub_del_list (struct obj **list, struct obj *el)
{
  struct obj **p;
  struct obj *q;

  for (p = list, q = *p; q; p = &(q->next), q = q->next)
    if (q == el)
      {
        *p = q->next;
        break;
      }
};


reply via email to

[Prev in Thread] Current Thread [Next in Thread]