freemyipod r482 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r481‎ | r482 | r483 >
Date:16:23, 22 January 2011
Author:theseven
Status:new
Tags:
Comment:
emcore.py: Only allocate as much memory as neccessary for file transfers
Modified paths:
  • /emcore/trunk/tools/emcore.py (modified) (history)

Diff [purge]

Index: emcore/trunk/tools/emcore.py
@@ -891,6 +891,8 @@
892892 except IOError:
893893 raise ArgumentError("Could not open local file for writing.")
894894 try:
 895+ storageinfo = self.emcore.storage_get_info(volume)
 896+ buffsize = min(buffsize, storageinfo.sectorsize * count)
895897 if buffer is None:
896898 buffer = self.emcore.malloc(buffsize)
897899 malloc = True
@@ -899,7 +901,6 @@
900902 malloc = False
901903 try:
902904 self.logger.info("Reading volume %s sectors %X - %X to %s..." % (volume, sector, sector + count - 1, file))
903 - storageinfo = self.emcore.storage_get_info(volume)
904905 while count > 0:
905906 sectors = min(count, int(buffsize / storageinfo.sectorsize))
906907 self.emcore.storage_read_sectors_md(volume, sector, sectors, buffsize, buffer)
@@ -928,6 +929,8 @@
929930 except IOError:
930931 raise ArgumentError("Could not open local file for reading.")
931932 try:
 933+ storageinfo = self.emcore.storage_get_info(volume)
 934+ buffsize = min(buffsize, storageinfo.sectorsize * count)
932935 if buffer is None:
933936 buffer = self.emcore.malloc(buffsize)
934937 malloc = True
@@ -936,7 +939,6 @@
937940 malloc = False
938941 try:
939942 self.logger.info("Writing %s to volume %s sectors %X - %X..." % (file, volume, sector, sector + count - 1))
940 - storageinfo = self.emcore.storage_get_info(volume)
941943 while count > 0:
942944 sectors = min(count, int(buffsize / storageinfo.sectorsize))
943945 bytes = storageinfo.sectorsize * sectors
@@ -1023,26 +1025,27 @@
10241026 except IOError:
10251027 raise ArgumentError("Could not open local file for writing.")
10261028 try:
1027 - if buffer is None:
1028 - buffer = self.emcore.malloc(buffsize)
1029 - malloc = True
1030 - else:
1031 - buffer = self._hexint(buffer)
1032 - malloc = False
 1029+ fd = self.emcore.file_open(remotename, 0)
10331030 try:
1034 - self.logger.info("Downloading file " + remotename + " to " + localname + "...")
1035 - fd = self.emcore.file_open(remotename, 0)
 1031+ size = self.emcore.file_size(fd)
 1032+ buffsize = min(buffsize, size)
 1033+ if buffer is None:
 1034+ buffer = self.emcore.malloc(buffsize)
 1035+ malloc = True
 1036+ else:
 1037+ buffer = self._hexint(buffer)
 1038+ malloc = False
10361039 try:
1037 - size = self.emcore.file_size(fd)
 1040+ self.logger.info("Downloading file " + remotename + " to " + localname + "...")
10381041 while size > 0:
10391042 bytes = self.emcore.file_read(fd, buffsize, buffer)
10401043 f.write(self.emcore.read(buffer, bytes))
10411044 size = size - bytes
10421045 finally:
1043 - self.emcore.file_close(fd)
 1046+ if malloc == True:
 1047+ self.emcore.free(buffer)
10441048 finally:
1045 - if malloc == True:
1046 - self.emcore.free(buffer)
 1049+ self.emcore.file_close(fd)
10471050 finally:
10481051 f.close()
10491052 self.logger.info(" done\n")
@@ -1097,6 +1100,7 @@
10981101 except IOError:
10991102 raise ArgumentError("Could not open local file for reading.")
11001103 try:
 1104+ buffsize = min(buffsize, os.path.getsize(localname))
11011105 if buffer is None:
11021106 buffer = self.emcore.malloc(buffsize)
11031107 malloc = True