kuid.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright (c) 2022 Agustina Arzille.
  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. * Kernel unique-id (KUID) management.
  18. */
  19. #ifndef KERN_KUID_H
  20. #define KERN_KUID_H
  21. #include <stdint.h>
  22. #include <kern/init.h>
  23. enum
  24. {
  25. KUID_TASK,
  26. KUID_THREAD,
  27. KUID_MAX_CLS
  28. };
  29. struct kuid_head
  30. {
  31. uint32_t id;
  32. size_t nr_refs;
  33. };
  34. static inline void
  35. kuid_head_init (struct kuid_head *head)
  36. {
  37. head->id = 0;
  38. head->nr_refs = 1;
  39. }
  40. // Allocate a KUID of a particular class.
  41. int kuid_alloc (struct kuid_head *head, int cls);
  42. // Find the kuid structure that matches a numeric ID of a particular class.
  43. struct kuid_head* kuid_find (uint32_t id, int cls);
  44. // Remove a previously allocated KUID for a particular class.
  45. int kuid_remove (struct kuid_head *kuid, int cls);
  46. // Helper for types that embed a kuid structure.
  47. #define kuid_find_type(id, type, member, cls) \
  48. ({ \
  49. _Auto head_ = kuid_find (id, cls); \
  50. head_ ? structof (head_, type, member) : 0; \
  51. })
  52. /*
  53. * This init operation provides :
  54. * - KUID management operational.
  55. */
  56. INIT_OP_DECLARE (kuid_setup);
  57. #endif