diff options
author | Dmitry Isaikin <isaikin@corp.mail.ru> | 2016-02-03 14:23:54 +0300 |
---|---|---|
committer | Dmitry Isaikin <isaikin@corp.mail.ru> | 2016-02-03 14:23:54 +0300 |
commit | ae46781112a85801114c1307e8744cd2f481da69 (patch) | |
tree | 8fbf67a1c9becd93b9a1ece67f55b10e6a6312fa /src/core | |
parent | 5ba469e045e88217eedaa13cfb3ff27ac469617a (diff) |
Implement Data::dataWithContentsOfFile via mmap
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/basetypes/MCData.cpp | 30 | ||||
-rw-r--r-- | src/core/basetypes/MCData.h | 1 |
2 files changed, 12 insertions, 19 deletions
diff --git a/src/core/basetypes/MCData.cpp b/src/core/basetypes/MCData.cpp index 9c6d0cae..0553b29d 100644 --- a/src/core/basetypes/MCData.cpp +++ b/src/core/basetypes/MCData.cpp @@ -7,6 +7,7 @@ #include <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <sys/mman.h> #include <pthread.h> #if USE_UCHARDET #include <uchardet/uchardet.h> @@ -522,20 +523,17 @@ void Data::takeBytesOwnership(char * bytes, unsigned int length, BytesDeallocato mBytesDeallocator = bytesDeallocator; } -void Data::takeBytesOwnership(char * bytes, unsigned int length) -{ - reset(); - mBytes = (char *) bytes; - mLength = length; +static void mmapDeallocator(char * bytes, unsigned int length) { + if (bytes) { + munmap(bytes, length); + } } Data * Data::dataWithContentsOfFile(String * filename) { int r; - size_t read_items; struct stat stat_buf; FILE * f; - char * buf; Data * data; f = fopen(filename->fileSystemRepresentation(), "rb"); @@ -548,21 +546,17 @@ Data * Data::dataWithContentsOfFile(String * filename) fclose(f); return NULL; } - - buf = (char *) malloc((size_t) stat_buf.st_size); - - read_items = fread(buf, 1, (size_t) stat_buf.st_size, f); - if ((off_t) read_items != stat_buf.st_size) { - free(buf); - fclose(f); + + unsigned int length = (unsigned int)stat_buf.st_size; + void * bytes = mmap(NULL, length, PROT_READ, MAP_PRIVATE, fileno(f), 0); + fclose(f); + + if (bytes == MAP_FAILED) { return NULL; } data = Data::data(); - data->takeBytesOwnership(buf, (unsigned int) stat_buf.st_size); - - fclose(f); - + data->takeBytesOwnership((char *)bytes, length, mmapDeallocator); return data; } diff --git a/src/core/basetypes/MCData.h b/src/core/basetypes/MCData.h index 4151757e..5bda3f6e 100644 --- a/src/core/basetypes/MCData.h +++ b/src/core/basetypes/MCData.h @@ -81,7 +81,6 @@ namespace mailcore { void init(); void reset(); String * charsetWithFilteredHTMLWithoutHint(bool filterHTML); - void takeBytesOwnership(char * bytes, unsigned int length); }; |