memory_map.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #[derive(Debug)]
  2. #[repr(C)]
  3. pub struct MemoryMapTag {
  4. typ: u32,
  5. size: u32,
  6. entry_size: u32,
  7. entry_version: u32,
  8. first_area: MemoryArea,
  9. }
  10. impl MemoryMapTag {
  11. pub fn memory_areas(&self) -> MemoryAreaIter {
  12. let self_ptr = self as *const MemoryMapTag;
  13. let start_area = (&self.first_area) as *const MemoryArea;
  14. MemoryAreaIter {
  15. current_area: start_area,
  16. last_area: (self_ptr as u64 + (self.size - self.entry_size) as u64)
  17. as *const MemoryArea,
  18. entry_size: self.entry_size,
  19. }
  20. }
  21. }
  22. #[derive(Debug)]
  23. #[repr(C)]
  24. pub struct MemoryArea {
  25. pub base_addr: u64,
  26. pub length: u64,
  27. typ: u32,
  28. _reserved: u32,
  29. }
  30. #[derive(Clone, Debug)]
  31. pub struct MemoryAreaIter {
  32. current_area: *const MemoryArea,
  33. last_area: *const MemoryArea,
  34. entry_size: u32,
  35. }
  36. impl Iterator for MemoryAreaIter {
  37. type Item = &'static MemoryArea;
  38. fn next(&mut self) -> Option<&'static MemoryArea> {
  39. if self.current_area > self.last_area {
  40. None
  41. } else {
  42. let area = unsafe{&*self.current_area};
  43. self.current_area = ((self.current_area as u64) + self.entry_size as u64)
  44. as *const MemoryArea;
  45. if area.typ == 1 {
  46. Some(area)
  47. } else {self.next()}
  48. }
  49. }
  50. }