diff --git a/src/Http/HttpBody.cpp b/src/Http/HttpBody.cpp index c41c4c29..ce6fa2f7 100644 --- a/src/Http/HttpBody.cpp +++ b/src/Http/HttpBody.cpp @@ -9,6 +9,7 @@ */ #include +#include #ifndef _WIN32 #include @@ -60,13 +61,13 @@ Buffer::Ptr HttpStringBody::readData(size_t size) { #ifdef ENABLE_MMAP static mutex s_mtx; -static unordered_map /*mmap*/ > > s_shared_mmap; +static unordered_map /*mmap*/ > > s_shared_mmap; //删除mmap记录 static void delSharedMmap(const string &file_path, char *ptr) { lock_guard lck(s_mtx); auto it = s_shared_mmap.find(file_path); - if (it != s_shared_mmap.end() && it->second.first == ptr) { + if (it != s_shared_mmap.end() && std::get<0>(it->second) == ptr) { s_shared_mmap.erase(it); } } @@ -76,9 +77,10 @@ static std::shared_ptr getSharedMmap(const string &file_path, int64_t &fil lock_guard lck(s_mtx); auto it = s_shared_mmap.find(file_path); if (it != s_shared_mmap.end()) { - auto ret = it->second.second.lock(); + auto ret = std::get<2>(it->second).lock(); if (ret) { //命中mmap缓存 + file_size = std::get<1>(it->second); return ret; } } @@ -123,7 +125,7 @@ static std::shared_ptr getSharedMmap(const string &file_path, int64_t &fil } { lock_guard lck(s_mtx); - s_shared_mmap[file_path] = std::make_pair(ret.get(), ret); + s_shared_mmap[file_path] = std::make_tuple(ret.get(), file_size, ret); } return ret; } @@ -135,7 +137,7 @@ HttpFileBody::HttpFileBody(const string &file_path, bool use_mmap) { _map_addr = getSharedMmap(file_path, _read_to); } #endif - if (!_map_addr) { + if (!_map_addr && _read_to != -1) { _fp.reset(fopen(file_path.data(), "rb"), [](FILE *fp) { if (fp) { fclose(fp);