SimpleRegion.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SimpleRegion.h"
  27. namespace UserspaceEmulator {
  28. SimpleRegion::SimpleRegion(u32 base, u32 size)
  29. : Region(base, size)
  30. {
  31. m_data = (u8*)calloc(1, size);
  32. m_shadow_data = (u8*)calloc(1, size);
  33. }
  34. SimpleRegion::~SimpleRegion()
  35. {
  36. free(m_shadow_data);
  37. free(m_data);
  38. }
  39. ValueWithShadow<u8> SimpleRegion::read8(FlatPtr offset)
  40. {
  41. ASSERT(offset < size());
  42. return { *reinterpret_cast<const u8*>(m_data + offset), *reinterpret_cast<const u8*>(m_shadow_data + offset) };
  43. }
  44. ValueWithShadow<u16> SimpleRegion::read16(u32 offset)
  45. {
  46. ASSERT(offset + 1 < size());
  47. return { *reinterpret_cast<const u16*>(m_data + offset), *reinterpret_cast<const u16*>(m_shadow_data + offset) };
  48. }
  49. ValueWithShadow<u32> SimpleRegion::read32(u32 offset)
  50. {
  51. ASSERT(offset + 3 < size());
  52. return { *reinterpret_cast<const u32*>(m_data + offset), *reinterpret_cast<const u32*>(m_shadow_data + offset) };
  53. }
  54. void SimpleRegion::write8(u32 offset, ValueWithShadow<u8> value)
  55. {
  56. ASSERT(offset < size());
  57. *reinterpret_cast<u8*>(m_data + offset) = value.value();
  58. *reinterpret_cast<u8*>(m_shadow_data + offset) = value.shadow();
  59. }
  60. void SimpleRegion::write16(u32 offset, ValueWithShadow<u16> value)
  61. {
  62. ASSERT(offset + 1 < size());
  63. *reinterpret_cast<u16*>(m_data + offset) = value.value();
  64. *reinterpret_cast<u16*>(m_shadow_data + offset) = value.shadow();
  65. }
  66. void SimpleRegion::write32(u32 offset, ValueWithShadow<u32> value)
  67. {
  68. ASSERT(offset + 3 < size());
  69. *reinterpret_cast<u32*>(m_data + offset) = value.value();
  70. *reinterpret_cast<u32*>(m_shadow_data + offset) = value.shadow();
  71. }
  72. u8* SimpleRegion::cacheable_ptr(u32 offset)
  73. {
  74. return m_data + offset;
  75. }
  76. }