Browse Source

mmap_read/mmap_shared on zero length files no longer fail but return a

zero length buffer
master
Felix von Leitner 7 years ago
parent
commit
a965db504b
  1. 2
      CHANGES
  2. 9
      mmap/mmap_read.c
  3. 9
      mmap/mmap_shared.c
  4. 2
      mmap/mmap_unmap.c

2
CHANGES

@ -24,6 +24,8 @@
add io_fd_flags so the caller can tell io_fd whether the socket is blocking
(saves one fcntl syscall)
more constness for stralloc and buffer
mmap_read/mmap_shared on zero length files no longer fail but return a
zero length buffer
0.29:
save 8 bytes in taia.h for 64-bit systems

9
mmap/mmap_read.c

@ -30,9 +30,12 @@ extern const char* mmap_read(const char* filename,size_t * filesize) {
register off_t o=lseek(fd,0,SEEK_END);
if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; }
*filesize=(size_t)o;
map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0);
if (map==(char*)-1)
map=0;
if (o>0) {
map=mmap(0,*filesize,PROT_READ,MAP_SHARED,fd,0);
if (map==(char*)-1)
map=0;
} else
map="";
close(fd);
return map;
}

9
mmap/mmap_shared.c

@ -30,9 +30,12 @@ extern char* mmap_shared(const char* filename,size_t * filesize) {
register off_t o=lseek(fd,0,SEEK_END);
if (sizeof(off_t)!=sizeof(size_t) && o > (off_t)(size_t)-1) { close(fd); return 0; }
*filesize=(size_t)o;
map=mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (map==(char*)-1)
map=0;
if (o) {
map=mmap(0,*filesize,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
if (map==(char*)-1)
map=0;
} else
map="";
close(fd);
return map;
}

2
mmap/mmap_unmap.c

@ -13,6 +13,6 @@ int mmap_unmap(const char* mapped,size_t maplen) {
(void)maplen;
return UnmapViewOfFile(mapped)?0:-1;
#else
return munmap((char*)mapped,maplen);
return maplen ? munmap((char*)mapped,maplen) : 0;
#endif
}

Loading…
Cancel
Save