Radish alpha
H
rad:z3QDZAW2FAfuLvihrhiyDC9fAD8G9
HardenedBSD Package Manager
Radicle
Git
Nuke all sys/queue.h usage and replace it by utlist
Baptiste Daroussin committed 13 years ago
commit 0a179bd7bf9dbd277e7c5678ea9dba247d76c607
parent 3ee9f4f
11 files changed +817 -192
added external/uthash/utlist.h
@@ -0,0 +1,733 @@
+
/*
+
Copyright (c) 2007-2013, Troy D. Hanson   http://uthash.sourceforge.net
+
All rights reserved.
+

+
Redistribution and use in source and binary forms, with or without
+
modification, are permitted provided that the following conditions are met:
+

+
    * Redistributions of source code must retain the above copyright
+
      notice, this list of conditions and the following disclaimer.
+

+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
*/
+

+
#ifndef UTLIST_H
+
#define UTLIST_H
+

+
#define UTLIST_VERSION 1.9.7
+

+
#include <assert.h>
+

+
/* 
+
 * This file contains macros to manipulate singly and doubly-linked lists.
+
 *
+
 * 1. LL_ macros:  singly-linked lists.
+
 * 2. DL_ macros:  doubly-linked lists.
+
 * 3. CDL_ macros: circular doubly-linked lists.
+
 *
+
 * To use singly-linked lists, your structure must have a "next" pointer.
+
 * To use doubly-linked lists, your structure must "prev" and "next" pointers.
+
 * Either way, the pointer to the head of the list must be initialized to NULL.
+
 * 
+
 * ----------------.EXAMPLE -------------------------
+
 * struct item {
+
 *      int id;
+
 *      struct item *prev, *next;
+
 * }
+
 *
+
 * struct item *list = NULL:
+
 *
+
 * int main() {
+
 *      struct item *item;
+
 *      ... allocate and populate item ...
+
 *      DL_APPEND(list, item);
+
 * }
+
 * --------------------------------------------------
+
 *
+
 * For doubly-linked lists, the append and delete macros are O(1)
+
 * For singly-linked lists, append and delete are O(n) but prepend is O(1)
+
 * The sort macro is O(n log(n)) for all types of single/double/circular lists.
+
 */
+

+
/* These macros use decltype or the earlier __typeof GNU extension.
+
   As decltype is only available in newer compilers (VS2010 or gcc 4.3+
+
   when compiling c++ code), this code uses whatever method is needed
+
   or, for VS2008 where neither is available, uses casting workarounds. */
+
#ifdef _MSC_VER            /* MS compiler */
+
#if _MSC_VER >= 1600 && defined(__cplusplus)  /* VS2010 or newer in C++ mode */
+
#define LDECLTYPE(x) decltype(x)
+
#else                     /* VS2008 or older (or VS2010 in C mode) */
+
#define NO_DECLTYPE
+
#define LDECLTYPE(x) char*
+
#endif
+
#else                      /* GNU, Sun and other compilers */
+
#define LDECLTYPE(x) __typeof(x)
+
#endif
+

+
/* for VS2008 we use some workarounds to get around the lack of decltype,
+
 * namely, we always reassign our tmp variable to the list head if we need
+
 * to dereference its prev/next pointers, and save/restore the real head.*/
+
#ifdef NO_DECLTYPE
+
#define _SV(elt,list) _tmp = (char*)(list); {char **_alias = (char**)&(list); *_alias = (elt); }
+
#define _NEXT(elt,list,next) ((char*)((list)->next))
+
#define _NEXTASGN(elt,list,to,next) { char **_alias = (char**)&((list)->next); *_alias=(char*)(to); }
+
/* #define _PREV(elt,list,prev) ((char*)((list)->prev)) */
+
#define _PREVASGN(elt,list,to,prev) { char **_alias = (char**)&((list)->prev); *_alias=(char*)(to); }
+
#define _RS(list) { char **_alias = (char**)&(list); *_alias=_tmp; }
+
#define _CASTASGN(a,b) { char **_alias = (char**)&(a); *_alias=(char*)(b); }
+
#else 
+
#define _SV(elt,list)
+
#define _NEXT(elt,list,next) ((elt)->next)
+
#define _NEXTASGN(elt,list,to,next) ((elt)->next)=(to)
+
/* #define _PREV(elt,list,prev) ((elt)->prev) */
+
#define _PREVASGN(elt,list,to,prev) ((elt)->prev)=(to)
+
#define _RS(list)
+
#define _CASTASGN(a,b) (a)=(b)
+
#endif
+

+
/******************************************************************************
+
 * The sort macro is an adaptation of Simon Tatham's O(n log(n)) mergesort    *
+
 * Unwieldy variable names used here to avoid shadowing passed-in variables.  *
+
 *****************************************************************************/
+
#define LL_SORT(list, cmp)                                                                     \
+
    LL_SORT2(list, cmp, next)
+

+
#define LL_SORT2(list, cmp, next)                                                              \
+
do {                                                                                           \
+
  LDECLTYPE(list) _ls_p;                                                                       \
+
  LDECLTYPE(list) _ls_q;                                                                       \
+
  LDECLTYPE(list) _ls_e;                                                                       \
+
  LDECLTYPE(list) _ls_tail;                                                                    \
+
  LDECLTYPE(list) _ls_oldhead;                                                                 \
+
  LDECLTYPE(list) _tmp;                                                                        \
+
  int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping;                       \
+
  if (list) {                                                                                  \
+
    _ls_insize = 1;                                                                            \
+
    _ls_looping = 1;                                                                           \
+
    while (_ls_looping) {                                                                      \
+
      _CASTASGN(_ls_p,list);                                                                   \
+
      _CASTASGN(_ls_oldhead,list);                                                             \
+
      list = NULL;                                                                             \
+
      _ls_tail = NULL;                                                                         \
+
      _ls_nmerges = 0;                                                                         \
+
      while (_ls_p) {                                                                          \
+
        _ls_nmerges++;                                                                         \
+
        _ls_q = _ls_p;                                                                         \
+
        _ls_psize = 0;                                                                         \
+
        for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) {                                         \
+
          _ls_psize++;                                                                         \
+
          _SV(_ls_q,list); _ls_q = _NEXT(_ls_q,list,next); _RS(list);                          \
+
          if (!_ls_q) break;                                                                   \
+
        }                                                                                      \
+
        _ls_qsize = _ls_insize;                                                                \
+
        while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) {                                    \
+
          if (_ls_psize == 0) {                                                                \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
          } else if (_ls_qsize == 0 || !_ls_q) {                                               \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
          } else if (cmp(_ls_p,_ls_q) <= 0) {                                                  \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
          } else {                                                                             \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
          }                                                                                    \
+
          if (_ls_tail) {                                                                      \
+
            _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list);                \
+
          } else {                                                                             \
+
            _CASTASGN(list,_ls_e);                                                             \
+
          }                                                                                    \
+
          _ls_tail = _ls_e;                                                                    \
+
        }                                                                                      \
+
        _ls_p = _ls_q;                                                                         \
+
      }                                                                                        \
+
      _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,NULL,next); _RS(list);                       \
+
      if (_ls_nmerges <= 1) {                                                                  \
+
        _ls_looping=0;                                                                         \
+
      }                                                                                        \
+
      _ls_insize *= 2;                                                                         \
+
    }                                                                                          \
+
  } else _tmp=NULL; /* quiet gcc unused variable warning */                                    \
+
} while (0)
+

+

+
#define DL_SORT(list, cmp)                                                                     \
+
    DL_SORT2(list, cmp, prev, next)
+

+
#define DL_SORT2(list, cmp, prev, next)                                                        \
+
do {                                                                                           \
+
  LDECLTYPE(list) _ls_p;                                                                       \
+
  LDECLTYPE(list) _ls_q;                                                                       \
+
  LDECLTYPE(list) _ls_e;                                                                       \
+
  LDECLTYPE(list) _ls_tail;                                                                    \
+
  LDECLTYPE(list) _ls_oldhead;                                                                 \
+
  LDECLTYPE(list) _tmp;                                                                        \
+
  int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping;                       \
+
  if (list) {                                                                                  \
+
    _ls_insize = 1;                                                                            \
+
    _ls_looping = 1;                                                                           \
+
    while (_ls_looping) {                                                                      \
+
      _CASTASGN(_ls_p,list);                                                                   \
+
      _CASTASGN(_ls_oldhead,list);                                                             \
+
      list = NULL;                                                                             \
+
      _ls_tail = NULL;                                                                         \
+
      _ls_nmerges = 0;                                                                         \
+
      while (_ls_p) {                                                                          \
+
        _ls_nmerges++;                                                                         \
+
        _ls_q = _ls_p;                                                                         \
+
        _ls_psize = 0;                                                                         \
+
        for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) {                                         \
+
          _ls_psize++;                                                                         \
+
          _SV(_ls_q,list); _ls_q = _NEXT(_ls_q,list,next); _RS(list);                          \
+
          if (!_ls_q) break;                                                                   \
+
        }                                                                                      \
+
        _ls_qsize = _ls_insize;                                                                \
+
        while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) {                                    \
+
          if (_ls_psize == 0) {                                                                \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
          } else if (_ls_qsize == 0 || !_ls_q) {                                               \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
          } else if (cmp(_ls_p,_ls_q) <= 0) {                                                  \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
          } else {                                                                             \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
          }                                                                                    \
+
          if (_ls_tail) {                                                                      \
+
            _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list);                \
+
          } else {                                                                             \
+
            _CASTASGN(list,_ls_e);                                                             \
+
          }                                                                                    \
+
          _SV(_ls_e,list); _PREVASGN(_ls_e,list,_ls_tail,prev); _RS(list);                     \
+
          _ls_tail = _ls_e;                                                                    \
+
        }                                                                                      \
+
        _ls_p = _ls_q;                                                                         \
+
      }                                                                                        \
+
      _CASTASGN(list->prev, _ls_tail);                                                         \
+
      _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,NULL,next); _RS(list);                       \
+
      if (_ls_nmerges <= 1) {                                                                  \
+
        _ls_looping=0;                                                                         \
+
      }                                                                                        \
+
      _ls_insize *= 2;                                                                         \
+
    }                                                                                          \
+
  } else _tmp=NULL; /* quiet gcc unused variable warning */                                    \
+
} while (0)
+

+
#define CDL_SORT(list, cmp)                                                                    \
+
    CDL_SORT2(list, cmp, prev, next)
+

+
#define CDL_SORT2(list, cmp, prev, next)                                                       \
+
do {                                                                                           \
+
  LDECLTYPE(list) _ls_p;                                                                       \
+
  LDECLTYPE(list) _ls_q;                                                                       \
+
  LDECLTYPE(list) _ls_e;                                                                       \
+
  LDECLTYPE(list) _ls_tail;                                                                    \
+
  LDECLTYPE(list) _ls_oldhead;                                                                 \
+
  LDECLTYPE(list) _tmp;                                                                        \
+
  LDECLTYPE(list) _tmp2;                                                                       \
+
  int _ls_insize, _ls_nmerges, _ls_psize, _ls_qsize, _ls_i, _ls_looping;                       \
+
  if (list) {                                                                                  \
+
    _ls_insize = 1;                                                                            \
+
    _ls_looping = 1;                                                                           \
+
    while (_ls_looping) {                                                                      \
+
      _CASTASGN(_ls_p,list);                                                                   \
+
      _CASTASGN(_ls_oldhead,list);                                                             \
+
      list = NULL;                                                                             \
+
      _ls_tail = NULL;                                                                         \
+
      _ls_nmerges = 0;                                                                         \
+
      while (_ls_p) {                                                                          \
+
        _ls_nmerges++;                                                                         \
+
        _ls_q = _ls_p;                                                                         \
+
        _ls_psize = 0;                                                                         \
+
        for (_ls_i = 0; _ls_i < _ls_insize; _ls_i++) {                                         \
+
          _ls_psize++;                                                                         \
+
          _SV(_ls_q,list);                                                                     \
+
          if (_NEXT(_ls_q,list,next) == _ls_oldhead) {                                         \
+
            _ls_q = NULL;                                                                      \
+
          } else {                                                                             \
+
            _ls_q = _NEXT(_ls_q,list,next);                                                    \
+
          }                                                                                    \
+
          _RS(list);                                                                           \
+
          if (!_ls_q) break;                                                                   \
+
        }                                                                                      \
+
        _ls_qsize = _ls_insize;                                                                \
+
        while (_ls_psize > 0 || (_ls_qsize > 0 && _ls_q)) {                                    \
+
          if (_ls_psize == 0) {                                                                \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
            if (_ls_q == _ls_oldhead) { _ls_q = NULL; }                                        \
+
          } else if (_ls_qsize == 0 || !_ls_q) {                                               \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
            if (_ls_p == _ls_oldhead) { _ls_p = NULL; }                                        \
+
          } else if (cmp(_ls_p,_ls_q) <= 0) {                                                  \
+
            _ls_e = _ls_p; _SV(_ls_p,list); _ls_p =                                            \
+
              _NEXT(_ls_p,list,next); _RS(list); _ls_psize--;                                  \
+
            if (_ls_p == _ls_oldhead) { _ls_p = NULL; }                                        \
+
          } else {                                                                             \
+
            _ls_e = _ls_q; _SV(_ls_q,list); _ls_q =                                            \
+
              _NEXT(_ls_q,list,next); _RS(list); _ls_qsize--;                                  \
+
            if (_ls_q == _ls_oldhead) { _ls_q = NULL; }                                        \
+
          }                                                                                    \
+
          if (_ls_tail) {                                                                      \
+
            _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_ls_e,next); _RS(list);                \
+
          } else {                                                                             \
+
            _CASTASGN(list,_ls_e);                                                             \
+
          }                                                                                    \
+
          _SV(_ls_e,list); _PREVASGN(_ls_e,list,_ls_tail,prev); _RS(list);                     \
+
          _ls_tail = _ls_e;                                                                    \
+
        }                                                                                      \
+
        _ls_p = _ls_q;                                                                         \
+
      }                                                                                        \
+
      _CASTASGN(list->prev,_ls_tail);                                                          \
+
      _CASTASGN(_tmp2,list);                                                                   \
+
      _SV(_ls_tail,list); _NEXTASGN(_ls_tail,list,_tmp2,next); _RS(list);                      \
+
      if (_ls_nmerges <= 1) {                                                                  \
+
        _ls_looping=0;                                                                         \
+
      }                                                                                        \
+
      _ls_insize *= 2;                                                                         \
+
    }                                                                                          \
+
  } else _tmp=NULL; /* quiet gcc unused variable warning */                                    \
+
} while (0)
+

+
/******************************************************************************
+
 * singly linked list macros (non-circular)                                   *
+
 *****************************************************************************/
+
#define LL_PREPEND(head,add)                                                                   \
+
    LL_PREPEND2(head,add,next)
+

+
#define LL_PREPEND2(head,add,next)                                                             \
+
do {                                                                                           \
+
  (add)->next = head;                                                                          \
+
  head = add;                                                                                  \
+
} while (0)
+

+
#define LL_CONCAT(head1,head2)                                                                 \
+
    LL_CONCAT2(head1,head2,next)
+

+
#define LL_CONCAT2(head1,head2,next)                                                           \
+
do {                                                                                           \
+
  LDECLTYPE(head1) _tmp;                                                                       \
+
  if (head1) {                                                                                 \
+
    _tmp = head1;                                                                              \
+
    while (_tmp->next) { _tmp = _tmp->next; }                                                  \
+
    _tmp->next=(head2);                                                                        \
+
  } else {                                                                                     \
+
    (head1)=(head2);                                                                           \
+
  }                                                                                            \
+
} while (0)
+

+
#define LL_APPEND(head,add)                                                                    \
+
    LL_APPEND2(head,add,next)
+

+
#define LL_APPEND2(head,add,next)                                                              \
+
do {                                                                                           \
+
  LDECLTYPE(head) _tmp;                                                                        \
+
  (add)->next=NULL;                                                                            \
+
  if (head) {                                                                                  \
+
    _tmp = head;                                                                               \
+
    while (_tmp->next) { _tmp = _tmp->next; }                                                  \
+
    _tmp->next=(add);                                                                          \
+
  } else {                                                                                     \
+
    (head)=(add);                                                                              \
+
  }                                                                                            \
+
} while (0)
+

+
#define LL_DELETE(head,del)                                                                    \
+
    LL_DELETE2(head,del,next)
+

+
#define LL_DELETE2(head,del,next)                                                              \
+
do {                                                                                           \
+
  LDECLTYPE(head) _tmp;                                                                        \
+
  if ((head) == (del)) {                                                                       \
+
    (head)=(head)->next;                                                                       \
+
  } else {                                                                                     \
+
    _tmp = head;                                                                               \
+
    while (_tmp->next && (_tmp->next != (del))) {                                              \
+
      _tmp = _tmp->next;                                                                       \
+
    }                                                                                          \
+
    if (_tmp->next) {                                                                          \
+
      _tmp->next = ((del)->next);                                                              \
+
    }                                                                                          \
+
  }                                                                                            \
+
} while (0)
+

+
/* Here are VS2008 replacements for LL_APPEND and LL_DELETE */
+
#define LL_APPEND_VS2008(head,add)                                                             \
+
    LL_APPEND2_VS2008(head,add,next)
+

+
#define LL_APPEND2_VS2008(head,add,next)                                                       \
+
do {                                                                                           \
+
  if (head) {                                                                                  \
+
    (add)->next = head;     /* use add->next as a temp variable */                             \
+
    while ((add)->next->next) { (add)->next = (add)->next->next; }                             \
+
    (add)->next->next=(add);                                                                   \
+
  } else {                                                                                     \
+
    (head)=(add);                                                                              \
+
  }                                                                                            \
+
  (add)->next=NULL;                                                                            \
+
} while (0)
+

+
#define LL_DELETE_VS2008(head,del)                                                             \
+
    LL_DELETE2_VS2008(head,del,next)
+

+
#define LL_DELETE2_VS2008(head,del,next)                                                       \
+
do {                                                                                           \
+
  if ((head) == (del)) {                                                                       \
+
    (head)=(head)->next;                                                                       \
+
  } else {                                                                                     \
+
    char *_tmp = (char*)(head);                                                                \
+
    while ((head)->next && ((head)->next != (del))) {                                          \
+
      head = (head)->next;                                                                     \
+
    }                                                                                          \
+
    if ((head)->next) {                                                                        \
+
      (head)->next = ((del)->next);                                                            \
+
    }                                                                                          \
+
    {                                                                                          \
+
      char **_head_alias = (char**)&(head);                                                    \
+
      *_head_alias = _tmp;                                                                     \
+
    }                                                                                          \
+
  }                                                                                            \
+
} while (0)
+
#ifdef NO_DECLTYPE
+
#undef LL_APPEND
+
#define LL_APPEND LL_APPEND_VS2008
+
#undef LL_DELETE
+
#define LL_DELETE LL_DELETE_VS2008
+
#undef LL_DELETE2
+
#define LL_DELETE2_VS2008
+
#undef LL_APPEND2
+
#define LL_APPEND2 LL_APPEND2_VS2008
+
#undef LL_CONCAT /* no LL_CONCAT_VS2008 */
+
#undef DL_CONCAT /* no DL_CONCAT_VS2008 */
+
#endif
+
/* end VS2008 replacements */
+

+
#define LL_FOREACH(head,el)                                                                    \
+
    LL_FOREACH2(head,el,next)
+

+
#define LL_FOREACH2(head,el,next)                                                              \
+
    for(el=head;el;el=(el)->next)
+

+
#define LL_FOREACH_SAFE(head,el,tmp)                                                           \
+
    LL_FOREACH_SAFE2(head,el,tmp,next)
+

+
#define LL_FOREACH_SAFE2(head,el,tmp,next)                                                     \
+
  for((el)=(head);(el) && (tmp = (el)->next, 1); (el) = tmp)
+

+
#define LL_SEARCH_SCALAR(head,out,field,val)                                                   \
+
    LL_SEARCH_SCALAR2(head,out,field,val,next)
+

+
#define LL_SEARCH_SCALAR2(head,out,field,val,next)                                             \
+
do {                                                                                           \
+
    LL_FOREACH2(head,out,next) {                                                               \
+
      if ((out)->field == (val)) break;                                                        \
+
    }                                                                                          \
+
} while(0) 
+

+
#define LL_SEARCH(head,out,elt,cmp)                                                            \
+
    LL_SEARCH2(head,out,elt,cmp,next)
+

+
#define LL_SEARCH2(head,out,elt,cmp,next)                                                      \
+
do {                                                                                           \
+
    LL_FOREACH2(head,out,next) {                                                               \
+
      if ((cmp(out,elt))==0) break;                                                            \
+
    }                                                                                          \
+
} while(0) 
+

+
#define LL_REPLACE_ELEM(head, el, add)                                                         \
+
do {                                                                                           \
+
 LDECLTYPE(head) _tmp;                                                                         \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 (add)->next = (el)->next;                                                                     \
+
 if ((head) == (el)) {                                                                         \
+
  (head) = (add);                                                                              \
+
 } else {                                                                                      \
+
  _tmp = head;                                                                                 \
+
  while (_tmp->next && (_tmp->next != (el))) {                                                 \
+
   _tmp = _tmp->next;                                                                          \
+
  }                                                                                            \
+
  if (_tmp->next) {                                                                            \
+
    _tmp->next = (add);                                                                        \
+
  }                                                                                            \
+
 }                                                                                             \
+
} while (0)
+

+
#define LL_PREPEND_ELEM(head, el, add)                                                         \
+
do {                                                                                           \
+
 LDECLTYPE(head) _tmp;                                                                         \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 (add)->next = (el);                                                                           \
+
 if ((head) == (el)) {                                                                         \
+
  (head) = (add);                                                                              \
+
 } else {                                                                                      \
+
  _tmp = head;                                                                                 \
+
  while (_tmp->next && (_tmp->next != (el))) {                                                 \
+
   _tmp = _tmp->next;                                                                          \
+
  }                                                                                            \
+
  if (_tmp->next) {                                                                            \
+
    _tmp->next = (add);                                                                        \
+
  }                                                                                            \
+
 }                                                                                             \
+
} while (0)                                                                                    \
+

+

+
/******************************************************************************
+
 * doubly linked list macros (non-circular)                                   *
+
 *****************************************************************************/
+
#define DL_PREPEND(head,add)                                                                   \
+
    DL_PREPEND2(head,add,prev,next)
+

+
#define DL_PREPEND2(head,add,prev,next)                                                        \
+
do {                                                                                           \
+
 (add)->next = head;                                                                           \
+
 if (head) {                                                                                   \
+
   (add)->prev = (head)->prev;                                                                 \
+
   (head)->prev = (add);                                                                       \
+
 } else {                                                                                      \
+
   (add)->prev = (add);                                                                        \
+
 }                                                                                             \
+
 (head) = (add);                                                                               \
+
} while (0)
+

+
#define DL_APPEND(head,add)                                                                    \
+
    DL_APPEND2(head,add,prev,next)
+

+
#define DL_APPEND2(head,add,prev,next)                                                         \
+
do {                                                                                           \
+
  if (head) {                                                                                  \
+
      (add)->prev = (head)->prev;                                                              \
+
      (head)->prev->next = (add);                                                              \
+
      (head)->prev = (add);                                                                    \
+
      (add)->next = NULL;                                                                      \
+
  } else {                                                                                     \
+
      (head)=(add);                                                                            \
+
      (head)->prev = (head);                                                                   \
+
      (head)->next = NULL;                                                                     \
+
  }                                                                                            \
+
} while (0) 
+

+
#define DL_CONCAT(head1,head2)                                                                 \
+
    DL_CONCAT2(head1,head2,prev,next)
+

+
#define DL_CONCAT2(head1,head2,prev,next)                                                      \
+
do {                                                                                           \
+
  LDECLTYPE(head1) _tmp;                                                                       \
+
  if (head2) {                                                                                 \
+
    if (head1) {                                                                               \
+
        _tmp = (head2)->prev;                                                                  \
+
        (head2)->prev = (head1)->prev;                                                         \
+
        (head1)->prev->next = (head2);                                                         \
+
        (head1)->prev = _tmp;                                                                  \
+
    } else {                                                                                   \
+
        (head1)=(head2);                                                                       \
+
    }                                                                                          \
+
  }                                                                                            \
+
} while (0) 
+

+
#define DL_DELETE(head,del)                                                                    \
+
    DL_DELETE2(head,del,prev,next)
+

+
#define DL_DELETE2(head,del,prev,next)                                                         \
+
do {                                                                                           \
+
  assert((del)->prev != NULL);                                                                 \
+
  if ((del)->prev == (del)) {                                                                  \
+
      (head)=NULL;                                                                             \
+
  } else if ((del)==(head)) {                                                                  \
+
      (del)->next->prev = (del)->prev;                                                         \
+
      (head) = (del)->next;                                                                    \
+
  } else {                                                                                     \
+
      (del)->prev->next = (del)->next;                                                         \
+
      if ((del)->next) {                                                                       \
+
          (del)->next->prev = (del)->prev;                                                     \
+
      } else {                                                                                 \
+
          (head)->prev = (del)->prev;                                                          \
+
      }                                                                                        \
+
  }                                                                                            \
+
} while (0) 
+

+

+
#define DL_FOREACH(head,el)                                                                    \
+
    DL_FOREACH2(head,el,next)
+

+
#define DL_FOREACH2(head,el,next)                                                              \
+
    for(el=head;el;el=(el)->next)
+

+
/* this version is safe for deleting the elements during iteration */
+
#define DL_FOREACH_SAFE(head,el,tmp)                                                           \
+
    DL_FOREACH_SAFE2(head,el,tmp,next)
+

+
#define DL_FOREACH_SAFE2(head,el,tmp,next)                                                     \
+
  for((el)=(head);(el) && (tmp = (el)->next, 1); (el) = tmp)
+

+
/* these are identical to their singly-linked list counterparts */
+
#define DL_SEARCH_SCALAR LL_SEARCH_SCALAR
+
#define DL_SEARCH LL_SEARCH
+
#define DL_SEARCH_SCALAR2 LL_SEARCH_SCALAR2
+
#define DL_SEARCH2 LL_SEARCH2
+

+
#define DL_REPLACE_ELEM(head, el, add)                                                         \
+
do {                                                                                           \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 if ((head) == (el)) {                                                                         \
+
  (head) = (add);                                                                              \
+
  (add)->next = (el)->next;                                                                    \
+
  if ((el)->next == NULL) {                                                                    \
+
   (add)->prev = (add);                                                                        \
+
  } else {                                                                                     \
+
   (add)->prev = (el)->prev;                                                                   \
+
   (add)->next->prev = (add);                                                                  \
+
  }                                                                                            \
+
 } else {                                                                                      \
+
  (add)->next = (el)->next;                                                                    \
+
  (add)->prev = (el)->prev;                                                                    \
+
  (add)->prev->next = (add);                                                                   \
+
  if ((el)->next == NULL) {                                                                    \
+
   (head)->prev = (add);                                                                       \
+
  } else {                                                                                     \
+
   (add)->next->prev = (add);                                                                  \
+
  }                                                                                            \
+
 }                                                                                             \
+
} while (0)
+

+
#define DL_PREPEND_ELEM(head, el, add)                                                         \
+
do {                                                                                           \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 (add)->next = (el);                                                                           \
+
 (add)->prev = (el)->prev;                                                                     \
+
 (el)->prev = (add);                                                                           \
+
 if ((head) == (el)) {                                                                         \
+
  (head) = (add);                                                                              \
+
 } else {                                                                                      \
+
  (add)->prev->next = (add);                                                                   \
+
 }                                                                                             \
+
} while (0)                                                                                    \
+

+

+
/******************************************************************************
+
 * circular doubly linked list macros                                         *
+
 *****************************************************************************/
+
#define CDL_PREPEND(head,add)                                                                  \
+
    CDL_PREPEND2(head,add,prev,next)
+

+
#define CDL_PREPEND2(head,add,prev,next)                                                       \
+
do {                                                                                           \
+
 if (head) {                                                                                   \
+
   (add)->prev = (head)->prev;                                                                 \
+
   (add)->next = (head);                                                                       \
+
   (head)->prev = (add);                                                                       \
+
   (add)->prev->next = (add);                                                                  \
+
 } else {                                                                                      \
+
   (add)->prev = (add);                                                                        \
+
   (add)->next = (add);                                                                        \
+
 }                                                                                             \
+
(head)=(add);                                                                                  \
+
} while (0)
+

+
#define CDL_DELETE(head,del)                                                                   \
+
    CDL_DELETE2(head,del,prev,next)
+

+
#define CDL_DELETE2(head,del,prev,next)                                                        \
+
do {                                                                                           \
+
  if ( ((head)==(del)) && ((head)->next == (head))) {                                          \
+
      (head) = 0L;                                                                             \
+
  } else {                                                                                     \
+
     (del)->next->prev = (del)->prev;                                                          \
+
     (del)->prev->next = (del)->next;                                                          \
+
     if ((del) == (head)) (head)=(del)->next;                                                  \
+
  }                                                                                            \
+
} while (0) 
+

+
#define CDL_FOREACH(head,el)                                                                   \
+
    CDL_FOREACH2(head,el,next)
+

+
#define CDL_FOREACH2(head,el,next)                                                             \
+
    for(el=head;el;el=((el)->next==head ? 0L : (el)->next)) 
+

+
#define CDL_FOREACH_SAFE(head,el,tmp1,tmp2)                                                    \
+
    CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next)
+

+
#define CDL_FOREACH_SAFE2(head,el,tmp1,tmp2,prev,next)                                         \
+
  for((el)=(head), ((tmp1)=(head)?((head)->prev):NULL);                                        \
+
      (el) && ((tmp2)=(el)->next, 1);                                                          \
+
      ((el) = (((el)==(tmp1)) ? 0L : (tmp2))))
+

+
#define CDL_SEARCH_SCALAR(head,out,field,val)                                                  \
+
    CDL_SEARCH_SCALAR2(head,out,field,val,next)
+

+
#define CDL_SEARCH_SCALAR2(head,out,field,val,next)                                            \
+
do {                                                                                           \
+
    CDL_FOREACH2(head,out,next) {                                                              \
+
      if ((out)->field == (val)) break;                                                        \
+
    }                                                                                          \
+
} while(0) 
+

+
#define CDL_SEARCH(head,out,elt,cmp)                                                           \
+
    CDL_SEARCH2(head,out,elt,cmp,next)
+

+
#define CDL_SEARCH2(head,out,elt,cmp,next)                                                     \
+
do {                                                                                           \
+
    CDL_FOREACH2(head,out,next) {                                                              \
+
      if ((cmp(out,elt))==0) break;                                                            \
+
    }                                                                                          \
+
} while(0) 
+

+
#define CDL_REPLACE_ELEM(head, el, add)                                                        \
+
do {                                                                                           \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 if ((el)->next == (el)) {                                                                     \
+
  (add)->next = (add);                                                                         \
+
  (add)->prev = (add);                                                                         \
+
  (head) = (add);                                                                              \
+
 } else {                                                                                      \
+
  (add)->next = (el)->next;                                                                    \
+
  (add)->prev = (el)->prev;                                                                    \
+
  (add)->next->prev = (add);                                                                   \
+
  (add)->prev->next = (add);                                                                   \
+
  if ((head) == (el)) {                                                                        \
+
   (head) = (add);                                                                             \
+
  }                                                                                            \
+
 }                                                                                             \
+
} while (0)
+

+
#define CDL_PREPEND_ELEM(head, el, add)                                                        \
+
do {                                                                                           \
+
 assert(head != NULL);                                                                         \
+
 assert(el != NULL);                                                                           \
+
 assert(add != NULL);                                                                          \
+
 (add)->next = (el);                                                                           \
+
 (add)->prev = (el)->prev;                                                                     \
+
 (el)->prev = (add);                                                                           \
+
 (add)->prev->next = (add);                                                                    \
+
 if ((head) == (el)) {                                                                         \
+
  (head) = (add);                                                                              \
+
 }                                                                                             \
+
} while (0)                                                                                    \
+

+
#endif /* UTLIST_H */
+

modified libpkg/elfhints.c
@@ -30,7 +30,6 @@
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/stat.h>
-
#include <sys/queue.h>

#include <assert.h>
#include <ctype.h>
modified libpkg/fetch.c
@@ -44,11 +44,11 @@

struct http_mirror {
	struct url *url;
-
	STAILQ_ENTRY(http_mirror) next;
+
	struct http_mirror *next;
};

static struct dns_srvinfo *srv_mirrors = NULL;
-
static STAILQ_HEAD(,http_mirror) http_mirrors = STAILQ_HEAD_INITIALIZER(http_mirrors);
+
static struct http_mirror *http_mirrors = NULL;

static void
gethttpmirrors(const char *url) {
@@ -78,7 +78,7 @@ gethttpmirrors(const char *url) {
			if ((u = fetchParseURL(url)) != NULL) {
				m = malloc(sizeof(struct http_mirror));
				m->url = u;
-
				STAILQ_INSERT_TAIL(&http_mirrors, m, next);
+
				LL_APPEND(http_mirrors, m);
			}
		}
	}
@@ -161,16 +161,16 @@ pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
				snprintf(zone, sizeof(zone),
				    "%s://%s", u->scheme, u->host);
				pthread_mutex_lock(&mirror_mtx);
-
				if (STAILQ_EMPTY(&http_mirrors))
+
				if (http_mirrors == NULL)
					gethttpmirrors(zone);
				pthread_mutex_unlock(&mirror_mtx);
-
				http_current = STAILQ_FIRST(&http_mirrors);
+
				http_current = http_mirrors;
			}
		}

		if (srv && srv_mirrors != NULL)
			strlcpy(u->host, srv_current->host, sizeof(u->host));
-
		else if (http && !STAILQ_EMPTY(&http_mirrors)) {
+
		else if (http && http_mirrors != NULL) {
			strlcpy(u->scheme, http_current->url->scheme, sizeof(u->scheme));
			strlcpy(u->host, http_current->url->host, sizeof(u->host));
			snprintf(docpath, MAXPATHLEN, "%s%s", http_current->url->doc, doc);
@@ -191,10 +191,10 @@ pkg_fetch_file_to_fd(const char *url, int dest, time_t t)
				srv_current = srv_current->next;
				if (srv_current == NULL)
					srv_current = srv_mirrors;
-
			} else if (http && !STAILQ_EMPTY(&http_mirrors)) {
-
				http_current = STAILQ_NEXT(http_current, next);
+
			} else if (http && http_mirrors != NULL) {
+
				http_current = http_mirrors->next;
				if (http_current == NULL)
-
					http_current = STAILQ_FIRST(&http_mirrors);
+
					http_current = http_mirrors;
			} else {
				sleep(1);
			}
modified libpkg/pkg_config.c
@@ -25,8 +25,6 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
#include <sys/queue.h>
-

#include <assert.h>
#include <ctype.h>
#include <dlfcn.h>
modified libpkg/pkg_jobs.c
@@ -36,10 +36,6 @@
#include <stdlib.h>
#include <string.h>

-
#ifdef __DragonFly__
-
#define STAILQ_FOREACH_SAFE STAILQ_FOREACH_MUTABLE
-
#endif
-

#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
@@ -62,7 +58,6 @@ pkg_jobs_new(struct pkg_jobs **j, pkg_jobs_t t, struct pkgdb *db)
	(*j)->type = t;
	(*j)->solved = false;
	(*j)->flags = PKG_FLAG_NONE;
-
	STAILQ_INIT(&(*j)->patterns);

	return (EPKG_OK);
}
@@ -85,8 +80,6 @@ pkg_jobs_set_repository(struct pkg_jobs *j, const char *name)
void
pkg_jobs_free(struct pkg_jobs *j)
{
-
	struct job_pattern *jp;
-

	if (j == NULL)
		return;

@@ -94,7 +87,7 @@ pkg_jobs_free(struct pkg_jobs *j)
		pkgdb_release_lock(j->db);

	HASH_FREE(j->jobs, pkg, pkg_free);
-
	LIST_FREE(&j->patterns, jp, free);
+
	LL_FREE(j->patterns, job_pattern, free);

	free(j);
}
@@ -114,7 +107,7 @@ pkg_jobs_add(struct pkg_jobs *j, match_t match, char **argv, int argc)
	jp->pattern = argv;
	jp->nb = argc;
	jp->match = match;
-
	STAILQ_INSERT_TAIL(&j->patterns, jp, next);
+
	LL_APPEND(j->patterns, jp);

	return (EPKG_OK);
}
@@ -131,7 +124,7 @@ jobs_solve_deinstall(struct pkg_jobs *j)
	if ((j->flags & PKG_FLAG_RECURSIVE) == PKG_FLAG_RECURSIVE)
		recursive = true;

-
	STAILQ_FOREACH(jp, &j->patterns, next) {
+
	LL_FOREACH(j->patterns, jp) {
		if ((it = pkgdb_query_delete(j->db, jp->match, jp->nb,
		    jp->pattern, recursive)) == NULL)
			return (EPKG_FATAL);
@@ -211,7 +204,7 @@ jobs_solve_install(struct pkg_jobs *j)
	if ((j->flags & PKG_FLAG_RECURSIVE) == PKG_FLAG_RECURSIVE)
		recursive = true;

-
	STAILQ_FOREACH(jp, &j->patterns, next) {
+
	LL_FOREACH(j->patterns, jp) {
		if ((it = pkgdb_query_installs(j->db, jp->match, jp->nb,
		    jp->pattern, j->reponame, force, recursive)) == NULL)
			return (EPKG_FATAL);
@@ -242,7 +235,7 @@ jobs_solve_fetch(struct pkg_jobs *j)
	if ((j->flags & PKG_FLAG_WITH_DEPS) != 0)
		flag |= PKG_LOAD_DEPS;

-
	STAILQ_FOREACH(jp, &j->patterns, next) {
+
	LL_FOREACH(j->patterns, jp) {
		if ((it = pkgdb_query_fetch(j->db, jp->match, jp->nb,
		    jp->pattern, j->reponame, flag)) == NULL)
			return (EPKG_FATAL);
@@ -355,7 +348,7 @@ pkg_jobs_install(struct pkg_jobs *j)
	struct pkg *newpkg = NULL;
	struct pkg *pkg_temp = NULL;
	struct pkgdb_it *it = NULL;
-
	STAILQ_HEAD(,pkg) pkg_queue;
+
	struct pkg *pkg_queue = NULL;
	char path[MAXPATHLEN + 1];
	const char *cachedir = NULL;
	int flags = 0;
@@ -364,8 +357,6 @@ pkg_jobs_install(struct pkg_jobs *j)
	    PKG_LOAD_DIRS;
	bool handle_rc = false;

-
	STAILQ_INIT(&pkg_queue);
-

	/* Fetch */
	if (pkg_jobs_fetch(j) != EPKG_OK)
		return (EPKG_FATAL);
@@ -401,7 +392,7 @@ pkg_jobs_install(struct pkg_jobs *j)
						goto cleanup; /* Bail out */
					}

-
					STAILQ_INSERT_TAIL(&pkg_queue, pkg, next);
+
					LL_APPEND(pkg_queue, pkg);
					pkg_script_run(pkg,
					    PKG_SCRIPT_PRE_DEINSTALL);
					pkg_get(pkg, PKG_ORIGIN, &origin);
@@ -435,7 +426,7 @@ pkg_jobs_install(struct pkg_jobs *j)
					goto cleanup; /* Bail out */
				}

-
				STAILQ_INSERT_TAIL(&pkg_queue, pkg, next);
+
				LL_APPEND(pkg_queue, pkg);
				pkg_script_run(pkg, PKG_SCRIPT_PRE_DEINSTALL);
				pkg_get(pkg, PKG_ORIGIN, &origin);
				/*
@@ -458,13 +449,13 @@ pkg_jobs_install(struct pkg_jobs *j)
		} else {
			pkg_emit_install_begin(newpkg);
		}
-
		STAILQ_FOREACH(pkg, &pkg_queue, next)
+
		LL_FOREACH(pkg_queue, pkg)
			pkg_jobs_keep_files_to_del(pkg, newpkg);

-
		STAILQ_FOREACH_SAFE(pkg, &pkg_queue, next, pkg_temp) {
+
		LL_FOREACH_SAFE(pkg_queue, pkg, pkg_temp) {
			pkg_get(pkg, PKG_ORIGIN, &origin);
			if (strcmp(pkgorigin, origin) == 0) {
-
				STAILQ_REMOVE(&pkg_queue, pkg, pkg, next);
+
				LL_DELETE(pkg_queue, pkg);
				pkg_delete_files(pkg, 1);
				pkg_script_run(pkg, PKG_SCRIPT_POST_DEINSTALL);
				pkg_delete_dirs(j->db, pkg, 0);
@@ -489,7 +480,7 @@ pkg_jobs_install(struct pkg_jobs *j)
		else
			pkg_emit_install_finished(newpkg);

-
		if (STAILQ_EMPTY(&pkg_queue)) {
+
		if (pkg_queue == NULL) {
			pkgdb_transaction_commit(j->db->sqlite, "upgrade");
			pkgdb_transaction_begin(j->db->sqlite, "upgrade");
		}
modified libpkg/pkg_ports.c
@@ -46,7 +46,7 @@
struct keyword {
	/* 64 is more than enough for this */
	char keyword[64];
-
	STAILQ_HEAD(, action) actions;
+
	struct action *actions;
	UT_hash_handle hh;
};

@@ -81,7 +81,7 @@ struct file_attr {

struct action {
	int (*perform)(struct plist *, char *, struct file_attr *);
-
	STAILQ_ENTRY(action) next;
+
	struct action *next;
};

static int setprefix(struct plist *, char *, struct file_attr *);
@@ -531,142 +531,51 @@ unexec(struct plist *p, char *line, struct file_attr *a)
	return (meta_exec(p, line, a, true));
}

+
static struct keyact {
+
	const char *key;
+
	int (*action)(struct plist *, char *, struct file_attr *);
+
} keyacts[] = {
+
	{ "cwd", setprefix },
+
	{ "ignore", ignore_next },
+
	{ "comment", comment_key },
+
	{ "dirrm", dirrm },
+
	{ "dirrmtry", dirrmtry },
+
	{ "mode", setmod },
+
	{ "owner", setowner },
+
	{ "group", setgroup },
+
	{ "exec", exec },
+
	{ "unexec", unexec },
+
	/* old pkg compat */
+
	{ "name", name_key },
+
	{ "pkgdep", pkgdep },
+
	{ NULL, NULL },
+
};
+

static void
populate_keywords(struct plist *p)
{
	struct keyword *k;
	struct action *a;
+
	int i;

-
	/* @cwd */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "cwd", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = setprefix;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @ignore */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "ignore", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = ignore_next;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @comment */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "comment", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = comment_key;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @dirrm */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "dirrm", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = dirrm;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @dirrmtry */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "dirrmtry", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = dirrmtry;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @mode */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "mode", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = setmod;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @owner */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "owner", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = setowner;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @group */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "group", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = setgroup;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @exec */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "exec", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = exec;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @unexec */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "unexec", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = unexec;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* old pkg compat */
-

-
	/* @name */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "name", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = name_key;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
-

-
	/* @pkgdep */
-
	k = malloc(sizeof(struct keyword));
-
	a = malloc(sizeof(struct action));
-
	strlcpy(k->keyword, "pkgdep", sizeof(k->keyword));
-
	STAILQ_INIT(&k->actions);
-
	a->perform = pkgdep;
-
	STAILQ_INSERT_TAIL(&k->actions, a, next);
-
	HASH_ADD_STR(p->keywords, keyword, k);
+
	for (i = 0; keyacts[i].key != NULL; i++) {
+
		k = calloc(1, sizeof(struct keyword));
+
		a = malloc(sizeof(struct action));
+
		strlcpy(k->keyword, keyacts[i].key, sizeof(k->keyword));
+
		a->perform = keyacts[i].action;
+
		LL_APPEND(k->actions, a);
+
		HASH_ADD_STR(p->keywords, keyword, k);
+
	}
}

static void
keyword_free(struct keyword *k)
{
-
	struct action *a;
-

-
	LIST_FREE(&k->actions, a, free);
+
	LL_FREE(k->actions, action, free);

	free(k);
}

-
static void
-
plist_free(struct plist *p)
-
{
-
	struct keyword *k, *tmp;
-
	HASH_ITER(hh, p->keywords, k, tmp) {
-
		HASH_DEL(p->keywords, k);
-
		keyword_free(k);
-
	}
-
}

static int
parse_actions(yaml_document_t *doc, yaml_node_t *node, struct plist *p,
@@ -974,7 +883,7 @@ parse_keywords(struct plist *plist, char *keyword, char *line)

	HASH_FIND_STR(plist->keywords, keyword, k);
	if (k != NULL) {
-
		STAILQ_FOREACH(a, &k->actions, next) {
+
		LL_FOREACH(k->actions, a) {
			ret = a->perform(plist, line, attr);
			if (ret != EPKG_OK)
				return (ret);
@@ -1117,7 +1026,7 @@ ports_parse_plist(struct pkg *pkg, char *plist, const char *stage)
	free(hardlinks.inodes);

	free(plist_buf);
-
	plist_free(&pplist);
+
	HASH_FREE(pplist.keywords, keyword, keyword_free);

	return (ret);
}
modified libpkg/pkg_repo.c
@@ -637,7 +637,7 @@ pkg_create_repo(char *path, bool force,
	thd_data.stop = false;
	thd_data.fts = fts;
	pthread_mutex_init(&thd_data.fts_m, NULL);
-
	STAILQ_INIT(&thd_data.results);
+
	thd_data.results = NULL;
	thd_data.thd_finished = 0;
	pthread_mutex_init(&thd_data.results_m, NULL);
	pthread_cond_init(&thd_data.has_result, NULL);
@@ -658,14 +658,14 @@ pkg_create_repo(char *path, bool force,
		lic_t licenselogic;

		pthread_mutex_lock(&thd_data.results_m);
-
		while ((r = STAILQ_FIRST(&thd_data.results)) == NULL) {
+
		while ((r = thd_data.results) == NULL) {
			if (thd_data.thd_finished == num_workers) {
				break;
			}
			pthread_cond_wait(&thd_data.has_result, &thd_data.results_m);
		}
		if (r != NULL) {
-
			STAILQ_REMOVE_HEAD(&thd_data.results, next);
+
			LL_DELETE(thd_data.results, thd_data.results);
			thd_data.num_results--;
			pthread_cond_signal(&thd_data.has_room);
		}
@@ -916,7 +916,7 @@ read_pkg_file(void *data)
		while (d->num_results >= d->max_results) {
			pthread_cond_wait(&d->has_room, &d->results_m);
		}
-
		STAILQ_INSERT_TAIL(&d->results, r, next);
+
		LL_APPEND(d->results, r);
		d->num_results++;
		pthread_cond_signal(&d->has_result);
		pthread_mutex_unlock(&d->results_m);
modified libpkg/plugins.c
@@ -26,7 +26,6 @@
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

-
#include <sys/queue.h>
#include <sys/types.h>
#include <sys/stat.h>

@@ -60,10 +59,10 @@ struct pkg_plugin {
	struct plugin_hook *hooks;
	struct pkg_config *conf;
	struct pkg_config *conf_by_key;
-
	STAILQ_ENTRY(pkg_plugin) next;
+
	struct pkg_plugin *next;
};

-
static STAILQ_HEAD(, pkg_plugin) ph = STAILQ_HEAD_INITIALIZER(ph);
+
static struct pkg_plugin *plugins = NULL;

static int pkg_plugin_free(void);
static int pkg_plugin_hook_free(struct pkg_plugin *p);
@@ -100,9 +99,7 @@ plug_free(struct pkg_plugin *p)
static int
pkg_plugin_free(void)
{
-
	struct pkg_plugin *p = NULL;
-

-
	LIST_FREE(&ph, p, plug_free);
+
	LL_FREE(plugins, pkg_plugin, plug_free);

	return (EPKG_OK);
}
@@ -362,12 +359,10 @@ pkg_plugin_conf_add_list(struct pkg_plugin *p, int id, const char *key)
int
pkg_plugins(struct pkg_plugin **plugin)
{
-
	assert(&ph != NULL);
-
	
	if ((*plugin) == NULL)
-
		(*plugin) = STAILQ_FIRST(&ph);
+
		(*plugin) = plugins;
	else
-
		(*plugin) = STAILQ_NEXT((*plugin), next);
+
		(*plugin) = (*plugin)->next;

	if ((*plugin) == NULL)
		return (EPKG_END);
@@ -417,7 +412,7 @@ pkg_plugins_init(void)
		}
		pkg_plugin_set(p, PKG_PLUGIN_PLUGINFILE, pluginfile);
		if (init_func(p) == EPKG_OK) {
-
			STAILQ_INSERT_TAIL(&ph, p, next);
+
			LL_APPEND(plugins, p);
		} else {
			dlclose(p->lh);
			free(p);
modified libpkg/private/ldconfig.h
@@ -30,7 +30,6 @@
#define LDCONFIG_H 1

#include <sys/cdefs.h>
-
#include <sys/queue.h>

extern int	insecure;	/* -i flag, needed here for elfhints.c */

modified libpkg/private/pkg.h
@@ -29,7 +29,6 @@
#define _PKG_PRIVATE_H

#include <sys/param.h>
-
#include <sys/queue.h>
#include <sys/sbuf.h>
#include <sys/types.h>

@@ -39,6 +38,7 @@
#include <openssl/md5.h>
#include <stdbool.h>
#include <uthash.h>
+
#include <utlist.h>

#include <yaml.h>
#include "private/utils.h"
@@ -60,6 +60,16 @@
	data = NULL;                               \
} while (0)

+
#define LL_FREE(head, type, free_func) do {   \
+
	struct type *l1, *l2;                 \
+
	LL_FOREACH_SAFE(head, l1, l2) {       \
+
		LL_DELETE(head, l1);          \
+
		if (free_func != NULL)        \
+
			free_func(l1);        \
+
	}                                     \
+
	head = NULL;                          \
+
} while (0)
+

#define HASH_NEXT(hash, data) do {            \
		if (data == NULL)             \
			data = hash;          \
@@ -71,14 +81,6 @@
			return (EPKG_OK);     \
	} while (0)

-
#define LIST_FREE(head, data, free_func) do { \
-
	while (!STAILQ_EMPTY(head)) { \
-
		data = STAILQ_FIRST(head); \
-
		STAILQ_REMOVE_HEAD(head, next); \
-
		free_func(data); \
-
	}  \
-
	} while (0)
-

extern pthread_mutex_t mirror_mtx;

struct pkg {
@@ -105,7 +107,7 @@ struct pkg {
	lic_t		 licenselogic;
	pkg_t		 type;
	UT_hash_handle	 hh;
-
	STAILQ_ENTRY(pkg) next;
+
	struct pkg	*next;
};

struct pkg_dep {
@@ -160,24 +162,24 @@ struct pkg_jobs {
	pkg_flags	 flags;
	bool		 solved;
	const char *	 reponame;
-
	STAILQ_HEAD(,job_pattern) patterns;
+
	struct job_pattern *patterns;
};

struct job_pattern {
	char		**pattern;
	int		nb;
	match_t		match;
-
	STAILQ_ENTRY(job_pattern) next;
+
	struct job_pattern *next;
};

-
struct pkg_jobs_node {
+
/*struct pkg_jobs_node {
	struct pkg	*pkg;
	size_t		 nrefs;
-
	struct pkg_jobs_node	**parents; /* rdeps */
+
	struct pkg_jobs_node	**parents;
	size_t		 parents_len;
	size_t		 parents_cap;
	LIST_ENTRY(pkg_jobs_node) entries;
-
};
+
}; */

struct pkg_user {
	char		 name[MAXLOGNAME+1];
modified libpkg/private/thd_repo.h
@@ -27,7 +27,6 @@
#ifndef _PKG_THD_REPO_H
#define _PKG_THD_REPO_H

-
#include <sys/queue.h>
#include <sys/types.h>
#include <pthread.h>

@@ -37,7 +36,7 @@ struct pkg_result {
	char cksum[SHA256_DIGEST_LENGTH * 2 + 1];
	off_t size;
	int retcode; /* to pass errors */
-
	STAILQ_ENTRY(pkg_result) next;
+
	struct pkg_result *next;
};

struct thd_data {
@@ -57,7 +56,7 @@ struct thd_data {
	pthread_mutex_t results_m;
	pthread_cond_t has_result;
	pthread_cond_t has_room;
-
	STAILQ_HEAD(results, pkg_result) results;
+
	struct pkg_result *results;
	unsigned int num_results;
	int thd_finished;
};