plist.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2017 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * Upstream site with license notes :
  18. * http://git.sceen.net/rbraun/librbraun.git/
  19. */
  20. #include <kern/list.h>
  21. #include <kern/plist.h>
  22. void
  23. plist_add (struct plist *plist, struct plist_node *pnode)
  24. {
  25. if (plist_empty (plist))
  26. {
  27. list_insert_head (&plist->list, &pnode->node);
  28. list_insert_head (&plist->prio_list, &pnode->prio_node);
  29. return;
  30. }
  31. struct plist_node *next;
  32. list_for_each_entry (&plist->prio_list, next, prio_node)
  33. if (pnode->priority < next->priority)
  34. break;
  35. if (list_end (&plist->prio_list, &next->prio_node) ||
  36. pnode->priority != next->priority)
  37. list_insert_before (&pnode->prio_node, &next->prio_node);
  38. else
  39. list_init (&pnode->prio_node);
  40. list_insert_before (&pnode->node, &next->node);
  41. }
  42. void
  43. plist_remove (struct plist *plist, struct plist_node *pnode)
  44. {
  45. if (!list_node_unlinked (&pnode->prio_node))
  46. {
  47. struct plist_node *next = list_next_entry (pnode, node);
  48. if (!list_end (&plist->list, &next->node) &&
  49. list_node_unlinked (&next->prio_node))
  50. list_insert_after (&pnode->prio_node, &next->prio_node);
  51. list_remove (&pnode->prio_node);
  52. }
  53. list_remove (&pnode->node);
  54. }