Isis 3 Programmer Reference
CubeManager.cpp
1
6/* SPDX-License-Identifier: CC0-1.0 */
7#include "CubeManager.h"
8
9#include <iostream>
10#include <sys/resource.h>
11#include <cstdlib> // atexit
12#include <memory> // smart pointers
13#include <QCoreApplication>
14
15#include "Cube.h"
16#include "CubeAttribute.h"
17#include "CubeManager.h"
18#include "FileName.h"
19#include "IString.h"
20
21namespace Isis {
22 CubeManager *CubeManager::p_instance = 0;
23
29
30 // Grab the Qcore application instace to check if it has been
31 // instantiated yet. If not, instantiate it.
32 if (QCoreApplication::instance() == 0) {
33 static char **argv = 0;
34
35 if(!argv) {
36 argv = new char*[2];
37 argv[0] = new char[1024];
38 strcpy(argv[0], "CubeManager");
39 argv[1] = 0;
40 }
41
42 static int argc = 1;
43 new QCoreApplication(argc, argv);
44 }
45
46 // Get the maximum allowable number of open files for a process
47 struct rlimit fileLimit;
48
49 if (getrlimit(RLIMIT_NOFILE, &fileLimit) != 0) {
50 QString msg = "Cannot read the maximum allowable open files from system resources.";
51 throw IException(IException::Programmer, msg, _FILEINFO_);
52 }
53
54 // Allow for library files, etc used by this process
55 // So set or file limit to 60% of maximum allowed number of opened files
56 p_maxOpenFiles = fileLimit.rlim_cur * .60;
58
59 // Add the CleanUp() call to QCoreApplication's clean up routine,
60 // this ensures that the static instance of CubeManager is cleaned
61 // up with the rest of ISIS before System application clean up begins
62 // in order to ensure clean destruction. If System deletes CubeManager's
63 // Cubes or any of the individual Cube's memebers before CubeManager gets
64 // deleted, it will cause a segfault.
65 qAddPostRoutine(CleanUp);
66 }
67
75
76
95 Cube *CubeManager::OpenCube(const QString &cubeFileName) {
96 CubeAttributeInput attIn(cubeFileName);
97 IString attri = attIn.toString();
98 IString expName = FileName(cubeFileName).expanded();
99
100 // If there are attributes, we need a plus sign on the name
101 if (attri.size() > 0) {
102 expName += "+";
103 }
104
105 IString fullName = expName + attri;
106 QString fileName(fullName.ToQt());
107 QMap<QString, Cube *>::iterator searchResult = p_cubes.find(fileName);
108
109 if (searchResult == p_cubes.end()) {
110 p_cubes.insert(fileName, new Cube());
111 searchResult = p_cubes.find(fileName);
112
113 // Need to clean up memory if there is a problem opening a cube
114 // This allows the CubeManager class to clean up the dynamically alloc'd
115 // Cube before rethrowing the exception from Cube's open method
116 try {
117 (*searchResult)->open(fileName);
118 }
119 catch (IException &e) {
120 CleanCubes(fileName);
121 throw;
122 }
123 }
124
125 // Keep track of the newly opened cube in our queue
126 p_opened.removeAll(fileName);
127 p_opened.enqueue(fileName);
128
129 // cleanup excess cubes
130 while (p_opened.size() > (int)(p_currentLimit)) {
131 QString needsCleaned = p_opened.dequeue();
132 CleanCubes(needsCleaned);
133 }
134
135 return (*searchResult);
136 }
137
138
146 void CubeManager::CleanCubes(const QString &cubeFileName) {
147
148 QString fileName(FileName(cubeFileName).expanded());
149 QMap<QString, Cube *>::iterator searchResult = p_cubes.find(fileName);
150
151 if (searchResult == p_cubes.end()) {
152 return;
153 }
154
155 delete searchResult.value();
156 searchResult.value() = NULL;
157 p_cubes.erase(searchResult);
158 }
159
160
167
168 while (pos != p_cubes.end()) {
169 delete pos.value();
170 pos.value() = NULL;
171 pos ++;
172 }
173
174 p_cubes.clear();
175 }
176}
Manipulate and parse attributes of input cube filenames.
IO Handler for Isis Cubes.
Definition Cube.h:168
unsigned int p_maxOpenFiles
60% of the maximum number of open files allowed by system resources
static CubeManager * p_instance
There is always at least one instance of CubeManager around.
QMap< QString, Cube * > p_cubes
This keeps track of the open cubes.
Cube * OpenCube(const QString &cubeFileName)
This method opens a cube.
void CleanCubes()
This method removes all cubes from memory.
QQueue< QString > p_opened
This keeps track of cubes that have been opened.
CubeManager()
This initializes a CubeManager object.
static void CleanUp()
This method calls CleanCubes() on the static instance.
unsigned int p_currentLimit
The current limit regarding number of open files allowed.
~CubeManager()
This is the CubeManager destructor.
File name manipulation and expansion.
Definition FileName.h:100
QString expanded() const
Returns a QString of the full file name including the file path, excluding the attributes.
Definition FileName.cpp:196
Isis exception class.
Definition IException.h:91
@ Programmer
This error is for when a programmer made an API call that was illegal.
Definition IException.h:146
Adds specific functionality to C++ strings.
Definition IString.h:165
This is free and unencumbered software released into the public domain.
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16