Device.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <Kernel/Devices/Device.h>
  2. #include <Kernel/FileSystem/InodeMetadata.h>
  3. #include <LibC/errno_numbers.h>
  4. static HashMap<u32, Device*>* s_all_devices;
  5. HashMap<u32, Device*>& Device::all_devices()
  6. {
  7. if (s_all_devices == nullptr)
  8. s_all_devices = new HashMap<u32, Device*>;
  9. return *s_all_devices;
  10. }
  11. void Device::for_each(Function<void(Device&)> callback)
  12. {
  13. for (auto& entry : all_devices())
  14. callback(*entry.value);
  15. }
  16. Device* Device::get_device(unsigned major, unsigned minor)
  17. {
  18. auto it = all_devices().find(encoded_device(major, minor));
  19. if (it == all_devices().end())
  20. return nullptr;
  21. return it->value;
  22. }
  23. Device::Device(unsigned major, unsigned minor)
  24. : m_major(major)
  25. , m_minor(minor)
  26. {
  27. all_devices().set(encoded_device(m_major, m_minor), this);
  28. }
  29. Device::~Device()
  30. {
  31. all_devices().remove(encoded_device(m_major, m_minor));
  32. }
  33. String Device::absolute_path() const
  34. {
  35. return String::format("device:%u,%u (%s)", m_major, m_minor, class_name());
  36. }
  37. String Device::absolute_path(const FileDescription&) const
  38. {
  39. return absolute_path();
  40. }