def alloc_mem(self, size, except_on_fail=True): """allocate memory and return addr or 0 if no more memory""" # align size to 4 bytes size = (size + 3) & ~3 # find best free chunk chunk, left = self._find_best_chunk(size) # out of memory? if chunk == None: if except_on_fail: self.dump_orphans() log_mem_alloc.error("[alloc: NO MEMORY for %06x bytes]" % size) raise VamosInternalError("[alloc: NO MEMORY for %06x bytes]" % size) return 0 # remove chunk from free list # is something left? addr = chunk.addr if left == 0: self._remove_chunk(chunk) else: left_chunk = MemoryChunk(addr + size, left) self._replace_chunk(chunk, left_chunk) # add to valid allocs map self.addrs[addr] = size self.free_bytes -= size # erase memory self.mem.clear_block(addr, size, 0) log_mem_alloc.info("[alloc @%06x-%06x: %06x bytes] %s", addr, addr + size, size, self._stat_info()) if addr % 4: raise VamosInternalError( "Memory pool is invalid, return address not aligned by a long word" ) return addr
def alloc_mem(self, size, except_on_fail=True): """allocate memory and return addr or 0 if no more memory""" # align size to 4 bytes size = (size + 3) & ~3 # find best free chunk chunk, left = self._find_best_chunk(size) # out of memory? if chunk == None: if except_on_fail: self.dump_orphans() log_mem_alloc.error("[alloc: NO MEMORY for %06x bytes]" % size) raise VamosInternalError("[alloc: NO MEMORY for %06x bytes]" % size) return 0 # remove chunk from free list # is something left? addr = chunk.addr if left == 0: self._remove_chunk(chunk) else: left_chunk = MemoryChunk(addr + size, left) self._replace_chunk(chunk, left_chunk) # add to valid allocs map self.addrs[addr] = size self.free_bytes -= size # erase memory self.mem.clear_block(addr, size, 0) log_mem_alloc.info("[alloc @%06x-%06x: %06x bytes] %s", addr, addr+size, size, self._stat_info()) if addr % 4: raise VamosInternalError( "Memory pool is invalid, return address not aligned by a long word") return addr