2 * Copyright (C) 2018 Cisco Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 // @author Changxue Deng <chadeng@cisco.com>
19 #ifndef __RESOURCE_POOL__
20 #define __RESOURCE_POOL__
27 #include "mmap_file.h"
31 // A singleton class for managing resource/file descriptors using
32 // shared_ptr. All db handles for the same db will share the same
33 // file descriptors so that we won't be running out of file descriptors
34 // when there are a large number of DB handles opened.
40 std::shared_ptr<MmapFileIO> OpenFile(const std::string &fpath, int mode,
41 size_t file_size, bool &map_file,
43 void RemoveResourceByDB(const std::string &db_path);
44 void RemoveResourceByPath(const std::string &path);
46 bool CheckExistence(const std::string &header_path);
47 int AddResourceByPath(const std::string &path, std::shared_ptr<MmapFileIO> resource);
49 static ResourcePool& getInstance() {
50 static ResourcePool instance; // only one instance per process
57 std::map<std::string, std::shared_ptr<MmapFileIO>> file_pool;
58 pthread_mutex_t pool_mutex;