utils.py: add compressed writemem

Signed-off-by: Sven Peter <sven@svenpeter.dev>
This commit is contained in:
Sven Peter 2021-02-13 22:28:18 +01:00 committed by Hector Martin
parent b7d30250b2
commit fa02cc9602
2 changed files with 24 additions and 1 deletions

View file

@ -1,3 +1,5 @@
from contextlib import contextmanager
class Heap(object): class Heap(object):
def __init__(self, start, end, block=64): def __init__(self, start, end, block=64):
if start%block: if start%block:
@ -83,3 +85,11 @@ class Heap(object):
print("Heap stats:") print("Heap stats:")
print(" In use: %8dkB"%(inuse * self.block // 1024)) print(" In use: %8dkB"%(inuse * self.block // 1024))
print(" Free: %8dkB"%(free * self.block // 1024)) print(" Free: %8dkB"%(free * self.block // 1024))
@contextmanager
def guarded_malloc(self, size):
addr = self.malloc(size)
try:
yield addr
finally:
self.free(addr)

View file

@ -1,4 +1,4 @@
import serial, os, struct, sys, time, json, os.path import serial, os, struct, sys, time, json, os.path, lzma
from proxy import * from proxy import *
from tgtypes import * from tgtypes import *
import malloc import malloc
@ -95,6 +95,19 @@ class ProxyUtils(object):
raise ProxyError("Exception occurred") raise ProxyError("Exception occurred")
return ret return ret
def compressed_writemem(self, dest, data, progress):
if not len(data):
return
payload = lzma.compress(data)
compressed_size = len(payload)
with self.heap.guarded_malloc(compressed_size) as compressed_addr:
self.iface.writemem(compressed_addr, payload, progress)
decompressed_size = self.proxy.xzdec(compressed_addr, compressed_size, dest, len(data))
assert decompressed_size == len(data)
class RegMonitor(object): class RegMonitor(object):
def __init__(self, utils): def __init__(self, utils):
self.utils = utils self.utils = utils