about summary refs log tree commit diff stats
path: root/subx/010---vm.cc
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2018-11-23 00:21:41 -0800
committerKartik Agaram <vc@akkartik.com>2018-11-23 00:22:24 -0800
commit4a99a6e0dd3000d8e595ec7b4b04f637e66ded6d (patch)
treeb6d0b7f2145e4370c22fd0142950e648b406b524 /subx/010---vm.cc
parent6ee77ba7bb47bc4b17df67d2683bdbb84f326e8e (diff)
downloadmu-4a99a6e0dd3000d8e595ec7b4b04f637e66ded6d.tar.gz
4761
Bugfix: I forgot about ELF segment offsets when implementing VMAs. Eventually
segments grew large enough that I started seeing overlaps.
Diffstat (limited to 'subx/010---vm.cc')
-rw-r--r--subx/010---vm.cc11
1 files changed, 5 insertions, 6 deletions
diff --git a/subx/010---vm.cc b/subx/010---vm.cc
index 47b2c3da..7e5b8d79 100644
--- a/subx/010---vm.cc
+++ b/subx/010---vm.cc
@@ -112,9 +112,10 @@ SF = ZF = OF = false;
 //:: simulated RAM
 
 :(before "End Types")
-const uint32_t INITIAL_SEGMENT_SIZE = 0x1000000 - 1;
-// Subtract one just so we can start the first segment at address 1 without
-// overflowing the first segment. Other segments will learn to adjust.
+const uint32_t SEGMENT_ALIGNMENT = 0x1000000;
+inline uint32_t align_upwards(uint32_t x, uint32_t align) {
+  return (x+align-1) & -(align);
+}
 
 // Like in real-world Linux, we'll allocate RAM for our programs in disjoint
 // slabs called VMAs or Virtual Memory Areas.
@@ -123,7 +124,7 @@ struct vma {
   uint32_t end;  // exclusive
   vector<uint8_t> _data;
   vma(uint32_t s, uint32_t e) :start(s), end(e) {}
-  vma(uint32_t s) :start(s), end(s+INITIAL_SEGMENT_SIZE) {}
+  vma(uint32_t s) :start(s), end(align_upwards(s+1, SEGMENT_ALIGNMENT)) {}
   bool match(uint32_t a) {
     return a >= start && a < end;
   }
@@ -136,9 +137,7 @@ struct vma {
     if (_data.size() <= result_index) {
       const int align = 0x1000;
       uint32_t result_size = result_index + 1;  // size needed for result_index to be valid
-      #define align_upwards(x, align)  (((x)+(align)-1) & -(align))
       uint32_t new_size = align_upwards(result_size, align);
-      #undef align_upwards
       // grow at least 2x to maintain some amortized complexity guarantees
       if (new_size < _data.size() * 2)
         new_size = _data.size() * 2;