Files
Upsilon/apps/graph/cartesian_function_store.cpp
Émilie Feral 9b119942a2 [apps] Cap the size of stores (memory issue)
Change-Id: I69dbb593b42615d601b0adcf7580c740ecd22e4b
2017-05-09 17:00:32 +02:00

114 lines
3.1 KiB
C++

#include "cartesian_function_store.h"
extern "C" {
#include <assert.h>
#include <stddef.h>
}
#include <ion.h>
namespace Graph {
constexpr int CartesianFunctionStore::k_maxNumberOfFunctions;
constexpr KDColor CartesianFunctionStore::k_defaultColors[k_maxNumberOfFunctions];
constexpr const char * CartesianFunctionStore::k_functionNames[k_maxNumberOfFunctions];
CartesianFunctionStore::CartesianFunctionStore() :
Shared::FunctionStore()
{
addEmptyFunction();
}
uint32_t CartesianFunctionStore::storeChecksum() {
size_t dataLengthInBytes = m_numberOfFunctions*sizeof(CartesianFunction);
assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4
return Ion::crc32((uint32_t *)m_functions, dataLengthInBytes>>2);
}
CartesianFunction * CartesianFunctionStore::functionAtIndex(int i) {
assert(i>=0 && i<m_numberOfFunctions);
return &m_functions[i];
}
CartesianFunction * CartesianFunctionStore::activeFunctionAtIndex(int i) {
return (CartesianFunction *)Shared::FunctionStore::activeFunctionAtIndex(i);
}
CartesianFunction * CartesianFunctionStore::definedFunctionAtIndex(int i) {
return (CartesianFunction *)Shared::FunctionStore::definedFunctionAtIndex(i);
}
CartesianFunction * CartesianFunctionStore::addEmptyFunction() {
assert(m_numberOfFunctions < k_maxNumberOfFunctions);
const char * name = firstAvailableName();
KDColor color = firstAvailableColor();
CartesianFunction addedFunction(name, color);
m_functions[m_numberOfFunctions] = addedFunction;
CartesianFunction * result = &m_functions[m_numberOfFunctions];
m_numberOfFunctions++;
return result;
}
void CartesianFunctionStore::removeFunction(Shared::Function * f) {
int i = 0;
while (&m_functions[i] != f && i < m_numberOfFunctions) {
i++;
}
assert(i>=0 && i<m_numberOfFunctions);
m_numberOfFunctions--;
for (int j = i; j<m_numberOfFunctions; j++) {
m_functions[j] = m_functions[j+1];
}
CartesianFunction emptyFunction("", KDColorBlack);
m_functions[m_numberOfFunctions] = emptyFunction;
}
int CartesianFunctionStore::maxNumberOfFunctions() {
return k_maxNumberOfFunctions;
}
char CartesianFunctionStore::symbol() const {
return 'x';
}
const char * CartesianFunctionStore::firstAvailableName() {
for (int k = 0; k < k_maxNumberOfFunctions; k++) {
int j = 0;
while (j < m_numberOfFunctions) {
if (m_functions[j].name() == k_functionNames[k]) {
break;
}
j++;
}
if (j == m_numberOfFunctions) {
return k_functionNames[k];
}
}
return k_functionNames[0];
}
const KDColor CartesianFunctionStore::firstAvailableColor() {
for (int k = 0; k < k_maxNumberOfFunctions; k++) {
int j = 0;
while (j < m_numberOfFunctions) {
if (m_functions[j].color() == k_defaultColors[k]) {
break;
}
j++;
}
if (j == m_numberOfFunctions) {
return k_defaultColors[k];
}
}
return k_defaultColors[0];
}
void CartesianFunctionStore::removeAll() {
for (int i = 0; i < m_numberOfFunctions; i++) {
CartesianFunction emptyFunction("", KDColorBlack);
m_functions[i] = emptyFunction;
}
m_numberOfFunctions = 0;
addEmptyFunction();
}
}