Skip to content

Commit

Permalink
add remaining distance to next region
Browse files Browse the repository at this point in the history
  • Loading branch information
cracked-machine committed Feb 4, 2024
1 parent 91b29c0 commit 4127f75
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
6 changes: 3 additions & 3 deletions doc/example/report.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![memory map diagram](report.png)
|name|origin|size|remaining|
|:-|:-|:-|:-|
|<span style='color:darkolivegreen'>dtb</span>|16|336|TODO|
|<span style='color:grey'>kernel</span>|16|96|TODO|
|<span style='color:darkmagenta'>rootfs</span>|80|16|TODO|
|<span style='color:lime'>dtb</span>|0x90|0x100|0x258|
|<span style='color:seagreen'>kernel</span>|0x10|0x60|0x0|
|<span style='color:turquoise'>rootfs</span>|0x70|0x10|0x10|
Binary file modified doc/example/report.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 10 additions & 7 deletions mmdiagram/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
import mmdiagram.types
import warnings

height = 1000
"""height of the diagram image"""
width = 500
"""width of the diagram image"""

@typeguard.typechecked
class Diagram:
def __init__(self):
self._height = 500
"""height of the diagram image"""
self._width = 500
"""width of the diagram image"""
self._legend_width = 50
"""width of the area used for text annotations/legend"""
self._region_list = None
Expand All @@ -38,7 +38,7 @@ def __init__(self):
def _create_diagram(self, region_list: List[mmdiagram.types.Region]):

# init the main image
img_main = PIL.Image.new("RGB", (self._width, self._height), color=(255, 255, 255))
img_main = PIL.Image.new("RGB", (width, height), color=(255, 255, 255))

# this is the x-axis drawing offset for each region
# we increment this each time we draw a region to clearly show overlaps
Expand All @@ -56,12 +56,12 @@ def _create_diagram(self, region_list: List[mmdiagram.types.Region]):
region_offset = region_offset + 5

# init the layer
region_img = PIL.Image.new("RGBA", (self._width - self._legend_width, region.size), color=(255, 255, 0, 5))
region_img = PIL.Image.new("RGBA", (width - self._legend_width, region.size), color=(255, 255, 0, 5))
region_canvas = PIL.ImageDraw.Draw(region_img)

# draw the region graphic
region_canvas.rectangle(
(0, 0, self._width - 1, region.origin + region.size),
(0, 0, width - 1, region.origin + region.size),
fill=region.colour,
outline="black",
width=1,
Expand Down Expand Up @@ -120,6 +120,9 @@ def _process_input(self) -> List[mmdiagram.types.Region]:
else:
region_list.append(mmdiagram.types.Region(name, origin, size))

for r in region_list:
r.calc_nearest_region(region_list)

return region_list

def _batched(self, iterable, n):
Expand Down
31 changes: 27 additions & 4 deletions mmdiagram/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import typeguard
import random
import PIL.ImageColor
from typing import Dict

from typing import List, Dict
import mmdiagram.generator

@typeguard.typechecked
class Region:
Expand All @@ -21,7 +21,7 @@ def __init__(self, name: str, origin: str, size: str):
"""size in bytes"""
self.colour = self._pick_available_colour()
"""random colour for region block"""
self.remain = self._calc_remaining()
self.remain: str = None
"""Number of bytes until next region block"""

# both 'lightslategray' and 'lightslategrey' are the same colour
Expand Down Expand Up @@ -65,13 +65,36 @@ def _pick_available_colour(self):
print(f"\t### {len(Region._remaining_colours)} colours left ###")
return chosen_colour_name

def _calc_remaining(self):
def calc_nearest_region(self, region_list: List['Region']):
"""Calculate the remaining number of bytes until next region block
TODO iterate all other region blocks, determine which is nearest,
calc the distance from origin + size of current region block
Returns:
None: TODO
"""
region_distances = {}
print(f"Calculating distances for {self.name}:")
this_region_end = 0
for next_region in region_list:
# skip calculating distance from yourself.
if self.name == next_region.name:
continue
this_region_end: int = self.origin + self.size
next_region_end: int = next_region.origin + next_region.size
if self.origin > next_region_end:
continue
next_region_distance: int = next_region.origin - this_region_end
print(f"\t{next_region_distance} to {next_region.name}")
if next_region_distance >= 0:
region_distances[next_region.name] = next_region_distance

print(region_distances)
if region_distances:
lowest = min(region_distances, key=region_distances.get)
self.remain = hex(region_distances[lowest])
else:
self.remain = hex(mmdiagram.generator.height - this_region_end)
return "TODO"
6 changes: 3 additions & 3 deletions tests/test_mmdiagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ def test_generate_doc_example():
'0x10',
'0x60',
'rootfs',
'0x50',
'0x70',
'0x10',
'dtb',
'0x10',
'0x150',
'0x90',
'0x100',
"-o",
"doc/example/report.md"]):
mmdiagram.generator.Diagram()
Expand Down

0 comments on commit 4127f75

Please sign in to comment.