Memory optimisation

This commit is contained in:
Damien Nicolet
2019-12-13 01:02:14 +01:00
parent 17432be1d8
commit f763563834
5 changed files with 23 additions and 5 deletions

View File

@@ -18,6 +18,22 @@ EditorController::EditorController(MenuController * menuController, App * python
m_editorView.setTextAreaDelegates(this, this);
}
#ifdef MAX_SCRIPTSIZE
void EditorController::setScript(Script script) {
m_script = script;
Script::Data scriptData = m_script.value();
size_t s=scriptData.size,ms=sizeof(m_areaBuffer);
if (s>ms)
s=ms;
size_t availableScriptSize = s + Ion::Storage::sharedStorage()->availableSize();
if (availableScriptSize>ms)
availableScriptSize=ms;
// assert(sizeof(m_areaBuffer) >= availableScriptSize);
// We cannot use strlcpy as the first char reprensenting the importation status can be 0.
memcpy(m_areaBuffer, (const char *)scriptData.buffer, s);
m_editorView.setText(m_areaBuffer+1, availableScriptSize-1); // 1 char is taken by the importation status flag
}
#else
void EditorController::setScript(Script script) {
m_script = script;
Script::Data scriptData = m_script.value();
@@ -27,7 +43,7 @@ void EditorController::setScript(Script script) {
memcpy(m_areaBuffer, (const char *)scriptData.buffer, scriptData.size);
m_editorView.setText(m_areaBuffer+1, availableScriptSize-1); // 1 char is taken by the importation status flag
}
#endif
// TODO: this should be done in textAreaDidFinishEditing maybe??
bool EditorController::handleEvent(Ion::Events::Event event) {
if (event == Ion::Events::OK || event == Ion::Events::Back || event == Ion::Events::Home) {

View File

@@ -1,6 +1,8 @@
#ifndef CODE_EDITOR_CONTROLLER_H
#define CODE_EDITOR_CONTROLLER_H
#define MAX_SCRIPTSIZE 12288
#include <escher.h>
#include "script.h"
#include "editor_view.h"
@@ -39,7 +41,7 @@ private:
/* m_areaBuffer first character is dedicated to the importation status.
* Thereby, we avoid wasteful copy while adding the Script to the storage
* (in order to add the importation status char before the areaBuffer). */
char m_areaBuffer[Ion::Storage::k_storageSize]; // this could be slightly optimize
char m_areaBuffer[Ion::Storage::k_storageSize>MAX_SCRIPTSIZE?MAX_SCRIPTSIZE:Ion::Storage::k_storageSize]; // this could be slightly optimize
Script m_script;
MenuController * m_menuController;
};