From ca1dee24586c07e78459e4170225142cde9ba7f0 Mon Sep 17 00:00:00 2001 From: Sebastian Andrzej Siewior Date: Sat, 2 Jul 2011 20:06:58 +0200 Subject: [PATCH 2/3] mmap() dst file instead of malloc() + read() it Bug: https://bugs.debian.org/632585 This drops the memory pressure since the OS may now drop and reload parts of the old and new file on demand. Signed-off-by: Sebastian Andrzej Siewior --- bsdiff.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/bsdiff.c b/bsdiff.c index f25304548101..f975e3722cba 100644 --- a/bsdiff.c +++ b/bsdiff.c @@ -238,12 +238,16 @@ int main(int argc,char *argv[]) /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ - if(((fd=open(argv[2],O_RDONLY,0))<0) || - ((newsize=lseek(fd,0,SEEK_END))==-1) || - ((new=malloc(newsize+1))==NULL) || - (lseek(fd,0,SEEK_SET)!=0) || - (read(fd,new,newsize)!=newsize) || - (close(fd)==-1)) err(1,"%s",argv[2]); + fd = open(argv[2], O_RDONLY, 0); + if (fd < 0) + err(1, "open %s", argv[2]); + newsize = lseek(fd, 0, SEEK_END); + if (newsize == -1) + err(1, "lseek %s", argv[2]); + new = mmap(NULL, newsize, PROT_READ, MAP_SHARED | MAP_POPULATE, fd, 0); + if (new == MAP_FAILED) + err(1, "mmap %s", argv[2]); + close(fd); if(((db=malloc(newsize+1))==NULL) || ((eb=malloc(newsize+1))==NULL)) err(1,NULL); @@ -403,7 +407,7 @@ int main(int argc,char *argv[]) free(eb); free(I); munmap(old, oldsize); - free(new); + munmap(new, newsize); return 0; } -- 2.9.3