Blue Brain BioExplorer
SharedDataVolume.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2024, EPFL/Blue Brain Project
3  * All rights reserved. Do not distribute without permission.
4  * Responsible Author: Daniel Nachbaur <daniel.nachbaur@epfl.ch>
5  *
6  * This file is part of Blue Brain BioExplorer <https://github.com/BlueBrain/BioExplorer>
7  *
8  * This library is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License version 3.0 as published
10  * by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "SharedDataVolume.h"
23 
25 
26 #include <fcntl.h>
27 #include <fstream>
28 #include <future>
29 #include <sys/mman.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 
33 namespace
34 {
35 const int NO_DESCRIPTOR = -1;
36 }
37 
38 namespace core
39 {
41 {
42  if (_memoryMapPtr)
43  {
44  ::munmap((void*)_memoryMapPtr, _size);
45  _memoryMapPtr = nullptr;
46  }
47  if (_cacheFileDescriptor != NO_DESCRIPTOR)
48  {
49  ::close(_cacheFileDescriptor);
50  _cacheFileDescriptor = NO_DESCRIPTOR;
51  }
52 }
53 
54 void SharedDataVolume::mapData(const std::string& filename)
55 {
56  _cacheFileDescriptor = open(filename.c_str(), O_RDONLY);
57  if (_cacheFileDescriptor == NO_DESCRIPTOR)
58  throw std::runtime_error("Failed to open volume file " + filename);
59 
60  struct stat sb;
61  if (::fstat(_cacheFileDescriptor, &sb) == NO_DESCRIPTOR)
62  {
63  ::close(_cacheFileDescriptor);
64  _cacheFileDescriptor = NO_DESCRIPTOR;
65  throw std::runtime_error("Failed to open volume file " + filename);
66  }
67 
68  _size = sb.st_size;
69  _memoryMapPtr = ::mmap(0, _size, PROT_READ, MAP_PRIVATE, _cacheFileDescriptor, 0);
70  if (_memoryMapPtr == MAP_FAILED)
71  {
72  _memoryMapPtr = nullptr;
73  ::close(_cacheFileDescriptor);
74  _cacheFileDescriptor = NO_DESCRIPTOR;
75  throw std::runtime_error("Failed to open volume file " + filename);
76  }
77 
78  setVoxels(_memoryMapPtr);
79 }
80 
82 {
83  _memoryBuffer.insert(_memoryBuffer.begin(), buffer.begin(), buffer.end());
84  setVoxels(_memoryBuffer.data());
85 }
86 
88 {
89  _memoryBuffer = std::move(buffer);
90  setVoxels(_memoryBuffer.data());
91 }
92 } // namespace core
~SharedDataVolume()
Destructs the SharedDataVolume object. Unmaps the data from memory and closes the mapped file.
virtual PLATFORM_API void setVoxels(const void *voxels)=0
Sets the voxels of the volume.
PLATFORM_API void mapData(const std::string &filename)
Convenience function to map data from file.
std::vector< uint8_t > uint8_ts
Definition: Types.h:49