[docs]defget_max_sinkhole(self,length):""" Find a sinkhole which is large enough to support `length` bytes. This uses first-fit. The first sinkhole (ordered in descending order by their address) which can hold `length` bytes is chosen. If there are more than `length` bytes in the sinkhole, a new sinkhole is created representing the remaining bytes while the old sinkhole is removed. """ordered_sinks=sorted(list(self.sinkholes),key=operator.itemgetter(0),reverse=True)max_pair=Noneforaddr,szinordered_sinks:ifsz>=length:max_pair=(addr,sz)breakifmax_pairisNone:returnNoneremaining=max_pair[1]-lengthmax_addr=max_pair[0]+remainingmax_length=remainingself.sinkholes.remove(max_pair)ifremaining:self.sinkholes.add((max_pair[0],max_length))returnmax_addr
[docs]defadd_sinkhole(self,address,length):""" Add a sinkhole. Allow the possibility for the program to reuse the memory represented by the address length pair. """self.sinkholes.add((address,length))