您当前的位置: 首页 >  linux

phymat.nico

暂无认证

  • 4浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Linux快速读取文件之文件映射c++实例

phymat.nico 发布时间:2017-10-20 16:46:36 ,浏览量:4

typedef struct file_info_value
	{
		std::string file_name_;
		long size_;
		int fd_;
		char *buff_;
		boost::recursive_mutex cond_mtx_;
		boost::condition_variable_any cond_var_;

		boost::atomic is_mmap_;
		file_info_value(std::string file_name):file_name_(file_name),size_(0),fd_(-1),buff_(nullptr),is_mmap_(false)
		{
			fd_ = open(file_name.c_str(), O_RDONLY);
			if (fd_ == -1)
			{
				size_ = 0;
			}
			else
			{
				struct stat st;
				int r = fstat(fd_, &st);
				if (r == -1)
				{
					size_ = 0;
					close(fd_);
				}
				else
				{
					size_ = st.st_size;
				}
			}
		}
		~file_info_value()
		{
			if (fd_ != -1)
			{
				close(fd_);
				munmap(buff_, size_);
			}
		}
		//if data is too big,create file spilt more file_index,mmap more times(hfrz ptr as start addr)
		int mmap_the_file()
		{
			if (fd_ == -1)
			{
				return -1;
			}
			buff_ = (char *)mmap(NULL, size_, PROT_READ, MAP_PRIVATE, fd_, 0);
			if (buff_ == (void*)-1)
			{
				std::ostringstream oslog;
				oslog             
关注
打赏
1659628745
查看更多评论
0.0545s