2
0

resetcontainer.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Reset container
  3. *
  4. * Copyright (c) 2024 Linaro, Ltd
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. */
  9. /*
  10. * The "reset container" is an object which implements the Resettable
  11. * interface. It contains a list of arbitrary other objects which also
  12. * implement Resettable. Resetting the reset container resets all the
  13. * objects in it.
  14. */
  15. #include "qemu/osdep.h"
  16. #include "hw/resettable.h"
  17. #include "hw/core/resetcontainer.h"
  18. struct ResettableContainer {
  19. Object parent;
  20. ResettableState reset_state;
  21. GPtrArray *children;
  22. };
  23. OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ResettableContainer, resettable_container, RESETTABLE_CONTAINER, OBJECT, { TYPE_RESETTABLE_INTERFACE }, { })
  24. void resettable_container_add(ResettableContainer *rc, Object *obj)
  25. {
  26. INTERFACE_CHECK(void, obj, TYPE_RESETTABLE_INTERFACE);
  27. g_ptr_array_add(rc->children, obj);
  28. }
  29. void resettable_container_remove(ResettableContainer *rc, Object *obj)
  30. {
  31. g_ptr_array_remove(rc->children, obj);
  32. }
  33. static ResettableState *resettable_container_get_state(Object *obj)
  34. {
  35. ResettableContainer *rc = RESETTABLE_CONTAINER(obj);
  36. return &rc->reset_state;
  37. }
  38. static void resettable_container_child_foreach(Object *obj,
  39. ResettableChildCallback cb,
  40. void *opaque, ResetType type)
  41. {
  42. ResettableContainer *rc = RESETTABLE_CONTAINER(obj);
  43. unsigned int len = rc->children->len;
  44. for (unsigned int i = 0; i < len; i++) {
  45. cb(g_ptr_array_index(rc->children, i), opaque, type);
  46. /* Detect callbacks trying to unregister themselves */
  47. assert(len == rc->children->len);
  48. }
  49. }
  50. static void resettable_container_init(Object *obj)
  51. {
  52. ResettableContainer *rc = RESETTABLE_CONTAINER(obj);
  53. rc->children = g_ptr_array_new();
  54. }
  55. static void resettable_container_finalize(Object *obj)
  56. {
  57. }
  58. static void resettable_container_class_init(ObjectClass *klass, void *data)
  59. {
  60. ResettableClass *rc = RESETTABLE_CLASS(klass);
  61. rc->get_state = resettable_container_get_state;
  62. rc->child_foreach = resettable_container_child_foreach;
  63. }