From 984715a018acd4985b814f50331b3931ef18aa22 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Fri, 11 Feb 2022 13:40:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=8E=B7=E5=8F=96=E5=85=B1?= =?UTF-8?q?=E4=BA=ABmmap=E5=A4=A7=E5=B0=8Fbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpBody.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) 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);