@@ -195,21 +195,45 @@ def show_mapinfo(self):
195195 def get_lib_base (self , filename : str ) -> int :
196196 return next ((s for s , _ , _ , info , _ in self .map_info if os .path .split (info )[1 ] == filename ), - 1 )
197197
198- def align (self , addr : int , alignment : int = None ) -> int :
199- """Round up to nearest alignment.
198+ def align (self , value : int , alignment : int = None ) -> int :
199+ """Align a value down to the specified alignment boundary .
200200
201201 Args:
202- addr: address to align
203- alignment: alignment granularity, must be a power of 2
202+ value: a value to align
203+ alignment: alignment boundary; must be a power of 2. if not specified
204+ value will be aligned to page size
205+
206+ Returns: aligned value
207+ """
208+
209+ if alignment is None :
210+ alignment = self .pagesize
211+
212+ # make sure alignment is a power of 2
213+ assert alignment & (alignment - 1 ) == 0
214+
215+ # round down to nearest alignment
216+ return value & ~ (alignment - 1 )
217+
218+ def align_up (self , value : int , alignment : int = None ) -> int :
219+ """Align a value up to the specified alignment boundary.
220+
221+ Args:
222+ value: value to align
223+ alignment: alignment boundary; must be a power of 2. if not specified
224+ value will be aligned to page size
225+
226+ Returns: aligned value
204227 """
205228
206229 if alignment is None :
207230 alignment = self .pagesize
208231
209- # rounds up to nearest alignment
210- mask = self . max_mem_addr & - alignment
232+ # make sure alignment is a power of 2
233+ assert alignment & ( alignment - 1 ) == 0
211234
212- return (addr + (alignment - 1 )) & mask
235+ # round up to nearest alignment
236+ return (value + alignment - 1 ) & ~ (alignment - 1 )
213237
214238 def save (self ):
215239 """Save entire memory content.
0 commit comments