From 91c0a563c3dd5a9e2f4f1b0530cbaf0ff8a03a82 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 10 Feb 2022 21:23:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A1=AE=E4=BF=9Dmmap=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E8=A1=A8=E4=B8=8D=E8=86=A8=E8=83=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Http/HttpBody.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Http/HttpBody.cpp b/src/Http/HttpBody.cpp index 4871eacb..9adb65c3 100644 --- a/src/Http/HttpBody.cpp +++ b/src/Http/HttpBody.cpp @@ -60,13 +60,13 @@ Buffer::Ptr HttpStringBody::readData(size_t size) { #ifdef ENABLE_MMAP static std::shared_ptr getSharedMmap(const string &file_path, const std::shared_ptr &fp, uint64_t max_size) { static mutex s_mtx; - static unordered_map /*mmap*/> s_shared_mmap; + static unordered_map /*mmap*/ > > s_shared_mmap; { lock_guard lck(s_mtx); auto it = s_shared_mmap.find(file_path); if (it != s_shared_mmap.end()) { - auto ret = it->second.lock(); + auto ret = it->second.second.lock(); if (ret) { //命中mmap缓存 return ret; @@ -84,10 +84,19 @@ static std::shared_ptr getSharedMmap(const string &file_path, const std::s WarnL << "mmap " << file_path << " failed:" << get_uv_errmsg(false); return nullptr; } - std::shared_ptr ret(ptr, [max_size, fp](char *ptr) { munmap(ptr, max_size); }); + std::shared_ptr ret(ptr, [max_size, fp, file_path](char *ptr) { + munmap(ptr, max_size); + + //删除mmap记录 + lock_guard lck(s_mtx); + auto it = s_shared_mmap.find(file_path); + if (it != s_shared_mmap.end() && it->second.first == ptr) { + s_shared_mmap.erase(it); + } + }); { lock_guard lck(s_mtx); - s_shared_mmap[file_path] = ret; + s_shared_mmap[file_path] = std::make_pair(ret.get(), ret); } return ret; }