Skip to content

Commit

Permalink
fix: Free not used bitmaps and use atomic var !minor
Browse files Browse the repository at this point in the history
  • Loading branch information
lcgamboa committed Jun 29, 2024
1 parent e66b25e commit 34d91b3
Show file tree
Hide file tree
Showing 28 changed files with 153 additions and 82 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ PACKAGE=picsimlab
MAINVER=0
MINORVER=9
VERSION=0.9.2
DATE=240622
DATE=240629
VERSION_STABLE=0.9.1
4 changes: 2 additions & 2 deletions src/boards/bsim_qemu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ int bsim_qemu::MInit(const char* processor, const char* _fname, float freq_) {
StartThread();
while (!qemu_started) {
#ifndef _WIN_
usleep(100);
usleep(1000);
#else
Sleep(1);
#endif
Expand Down Expand Up @@ -931,7 +931,7 @@ void bsim_qemu::EvThreadRun(void) {
qemu_started = 1;
PICSimLab.SystemCmd(PSC_MUTEXUNLOCK, (const char*)&mtx_qinitId);
#ifndef _WIN_
usleep(100);
usleep(1000);
#else
Sleep(1);
#endif
Expand Down
9 changes: 5 additions & 4 deletions src/boards/bsim_qemu.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#ifndef BOARD_QEMU_H
#define BOARD_QEMU_H

#include <atomic>
#include "../devices/bitbang_i2c.h"
#include "../devices/bitbang_out.h"
#include "../devices/bitbang_pwm.h"
Expand Down Expand Up @@ -62,12 +63,12 @@ class bsim_qemu : virtual public board {
unsigned char MGetPin(int pin) override;
const picpin* MGetPinsValues(void) override;
void MStep(void) override;
void MStepResume(void) override{};
void MStepResume(void) override {};
void MReset(int flags) override;
void EvThreadRun(void) override;
int GetDefaultClock(void) override { return 1; };
int GetInc_ns(void) { return inc_ns; };
virtual void PinsExtraConfig(int cfg){};
virtual void PinsExtraConfig(int cfg) {};
user_timer_t timer;
virtual void Run_CPU_ns(uint64_t time) = 0;
bitbang_i2c_t master_i2c[2];
Expand All @@ -87,7 +88,7 @@ class bsim_qemu : virtual public board {
const char* IcountToMipsItens(char* buffer);
unsigned int ns_count;
void pins_reset(void);
virtual void BoardOptions(int* argc, char** argv){};
virtual void BoardOptions(int* argc, char** argv) {};
virtual const short int* GetPinMap(void) = 0;
int icount;
#ifdef _WIN_
Expand All @@ -106,7 +107,7 @@ class bsim_qemu : virtual public board {
char fname_bak[2048];
unsigned short ADCvalues[16];
int mtx_qinitId;
int qemu_started;
std::atomic_int qemu_started;
QEMUSimType SimType;
std::string cmdline;
int use_cmdline_extra;
Expand Down
2 changes: 2 additions & 0 deletions src/lib/oscilloscope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,8 @@ int COscilloscope::WindowCmd(const int id, const char* ControlName, const PICSim
const char* Value, void* ReturnBuff) {
if (Oscilloscope.OnWindowCmd) {
return (*Oscilloscope.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff);
} else {
printf("Error: COscilloscope::WindowCmd missing !\n");
}
return -1;
}
5 changes: 5 additions & 0 deletions src/lib/part.cc
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,11 @@ void part::LoadPartImage(void) {
SpareParts.SetPartOnDraw(id);
std::string iname = PICSimLab.GetSharePath() + "parts/" + Type + "/" + GetPictureFileName();

if (BitmapId >= 0) {
SpareParts.CanvasCmd({.cmd = CC_FREEBITMAP, .FreeBitmap{BitmapId}});
BitmapId = -1;
}

int bmp = SpareParts.CanvasCmd({.cmd = CC_LOADIMAGE, .LoadImage{iname.c_str(), Scale, 0, Orientation}});

if (bmp >= 0) {
Expand Down
43 changes: 24 additions & 19 deletions src/lib/part.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,17 @@ class part {
/**
* @brief Called every start of CPU process
*/
virtual void PreProcess(void){};
virtual void PreProcess(void) {};

/**
* @brief Called every CPU step
*/
virtual void Process(void){};
virtual void Process(void) {};

/**
* @brief Called every end of CPU process
*/
virtual void PostProcess(void){};
virtual void PostProcess(void) {};

/**
* @brief Return the filename of part picture
Expand Down Expand Up @@ -126,12 +126,12 @@ class part {
/**
* @brief Reset part status
*/
virtual void Reset(void){};
virtual void Reset(void) {};

/**
* @brief Stop part simulation
*/
virtual void Stop(void){};
virtual void Stop(void) {};

/**
* @brief Event handler on the part
Expand All @@ -142,7 +142,7 @@ class part {
* @brief Event handler on the part
*/
virtual void OnMouseButtonPress(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y,
unsigned int state){};
unsigned int state) {};

/**
* @brief Event handler on the part
Expand All @@ -153,7 +153,7 @@ class part {
* @brief Event handler on the part
*/
virtual void OnMouseButtonRelease(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y,
unsigned int state){};
unsigned int state) {};

/**
* @brief Event handler on the part
Expand All @@ -164,17 +164,17 @@ class part {
* @brief Event handler on the part
*/
virtual void OnMouseMove(unsigned int inputId, unsigned int button, unsigned int x, unsigned int y,
unsigned int state){};
unsigned int state) {};

/**
* @brief Event handler on the part
*/
virtual void EvKeyPress(unsigned int key, unsigned int mask){};
virtual void EvKeyPress(unsigned int key, unsigned int mask) {};

/**
* @brief Event handler on the part
*/
virtual void EvKeyRelease(unsigned int key, unsigned int mask){};
virtual void EvKeyRelease(unsigned int key, unsigned int mask) {};

/**
* @brief Called to save part preferences in configuration file
Expand Down Expand Up @@ -209,33 +209,33 @@ class part {
/**
* @brief Used by properties window combos
*/
virtual void ComboChange(const char* controlname, std::string value){};
virtual void ComboChange(const char* controlname, std::string value) {};

/**
* @brief Used by properties window spin
*/
virtual void SpinChange(const char* controlname, int value){};
virtual void SpinChange(const char* controlname, int value) {};

/**
* @brief Used by windows of parts
*/
virtual void ButtonEvent(const char* controlname, unsigned int button, unsigned int x, unsigned int y,
unsigned int state){};
unsigned int state) {};

/**
* @brief Used by windows of parts
*/
virtual void KeyEvent(const char* controlname, unsigned int keysym, unsigned int ukeysym, unsigned int state){};
virtual void KeyEvent(const char* controlname, unsigned int keysym, unsigned int ukeysym, unsigned int state) {};

/**
* @brief Used by windows of parts
*/
virtual void Event(const char* controlname){};
virtual void Event(const char* controlname) {};

/**
* @brief Used by properties window filedialogs
*/
virtual void filedialog_EvOnClose(int retId){};
virtual void filedialog_EvOnClose(int retId) {};

/**
* @brief Called once on part creation
Expand All @@ -251,12 +251,12 @@ class part {
/**
* @brief Called once on part after initialization
*/
virtual void PostInit(void){};
virtual void PostInit(void) {};

/**
* @brief Called once on part destruction
*/
virtual ~part(void){};
virtual ~part(void) {};

/**
* @brief Return the Bitmap of part
Expand Down Expand Up @@ -333,6 +333,11 @@ class part {
*/
int GetId(void) { return id; };

/**
* @brief Set the part ID
*/
void SetId(int id_) { id = id_; };

/**
* @brief Return if part need Draw update
*/
Expand Down Expand Up @@ -365,7 +370,7 @@ class part {
/**
* @brief Register remote control variables
*/
virtual void RegisterRemoteControl(void){};
virtual void RegisterRemoteControl(void) {};

int id; ///< part ID
input_t input[MAX_IDS]; ///< input map elements
Expand Down
20 changes: 12 additions & 8 deletions src/lib/picsimlab.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ CPICSimLab::CPICSimLab() {
lab = DEFAULT_BOARD;
lab_ = DEFAULT_BOARD;
Workspacefn = "";
status.status = 0;
status = 0;
scale = 1.0;
need_resize = 0;
tgo = 0;
Expand Down Expand Up @@ -805,14 +805,14 @@ void CPICSimLab::SaveWorkspace(std::string fnpzw) {

void CPICSimLab::SetSimulationRun(int run) {
if (run) {
status.st[0] &= ~ST_DI;
status &= ~ST_DI;
} else {
status.st[0] |= ST_DI;
status |= ST_DI;
}
}

int CPICSimLab::GetSimulationRun(void) {
return (status.st[0] & ST_DI) == 0;
return (status & ST_DI) == 0;
}

void CPICSimLab::Configure(const char* home, int use_default_board, int create, const char* lfile,
Expand Down Expand Up @@ -1129,14 +1129,14 @@ int CPICSimLab::LoadHexFile(std::string fname) {

pa = GetMcuPwr();
SetMcuPwr(0);
while (PICSimLab.status.st[1] & ST_TH)
while (PICSimLab.status & ST_TH)
usleep(100); // wait thread

status.st[0] |= ST_DI;
status |= ST_DI;
msleep(BASETIMER);
if (tgo)
tgo = 1;
while (status.status & 0x0401) {
while (status & (ST_TH | ST_T1)) {
msleep(1);
WindowCmd(PW_MAIN, NULL, PWA_APPPROCESSEVENTS, NULL);
}
Expand Down Expand Up @@ -1175,7 +1175,7 @@ int CPICSimLab::LoadHexFile(std::string fname) {
ret = !GetMcuRun();

SetMcuPwr(pa);
status.st[0] &= ~ST_DI;
status &= ~ST_DI;

#ifdef NO_DEBUG
UpdateStatus(PS_DEBUG, " ");
Expand Down Expand Up @@ -1208,13 +1208,17 @@ int CPICSimLab::WindowCmd(const int id, const char* ControlName, const PICSimLab
void* ReturnBuff) {
if (PICSimLab.OnWindowCmd) {
return (*PICSimLab.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff);
} else {
printf("Error: CPICSimLab::WindowCmd missing !\n");
}
return -1;
}

int CPICSimLab::SystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff) {
if (PICSimLab.OnSystemCmd) {
return (*PICSimLab.OnSystemCmd)(cmd, Arg, ReturnBuff);
} else {
printf("Error: CPICSimLab::SystemCmd missing !\n");
}
return -1;
}
8 changes: 3 additions & 5 deletions src/lib/picsimlab.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

extern char SERIALDEVICE[100];

#include <atomic>
#include "board.h"
#include "draw.h"
#include "types.h"
Expand Down Expand Up @@ -258,12 +259,9 @@ class CPICSimLab {

static int SystemCmd(const PICSimLabSystemCmd cmd, const char* Arg, void* ReturnBuff = NULL);

union {
char st[2];
unsigned short int status;
} status;
std::atomic<unsigned short int> status;

int tgo;
std::atomic<int> tgo;

int plWidth;
int plHeight;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/serial_port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ unsigned long serial_port_rec_tout(serialfd_t serialfd, unsigned char* c) {
#else
long nbytes;
do {
usleep(100);
usleep(1000);
nbytes = read(serialfd, c, 1);
if (nbytes < 0)
nbytes = 0;
Expand Down
11 changes: 9 additions & 2 deletions src/lib/spareparts.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@ part* CSpareParts::AddPart(const char* partname, const int x, const int y, const

void CSpareParts::DeleteParts(void) {
int partsc_ = partsc;
partsc = 0; // for disable process
partsc = 0; // for disable draw process
partsc_aup = 0;
useAlias = 0;

for (int i = 0; i < partsc_; i++) {
delete parts[i];
parts[i] = NULL;
}
}

Expand Down Expand Up @@ -504,15 +505,17 @@ bool CSpareParts::LoadConfig(std::string fname, const int disable_debug) {

void CSpareParts::DeletePart(const int partn) {
int partsc_ = partsc;
partsc = 0; // disable process
partsc = 0; // disable draw process
partsc_aup = 0;

delete parts[partn];

for (int i = partn; i < partsc_ - 1; i++) {
parts[i] = parts[i + 1];
parts[i]->SetId(i);
}
partsc_--;
parts[partsc_] = NULL;

partsc = partsc_;
}
Expand Down Expand Up @@ -680,6 +683,8 @@ int CSpareParts::WPropCmd(const char* ControlName, const PICSimLabWindowAction a
void* ReturnBuff) {
if (SpareParts.OnWindowCmd) {
return (*SpareParts.OnWindowCmd)(PW_WPROP, ControlName, action, Value, ReturnBuff);
} else {
printf("Error: CSpareParts::WindowCmd missing !\n");
}
return -1;
}
Expand All @@ -688,6 +693,8 @@ int CSpareParts::WindowCmd(const int id, const char* ControlName, const PICSimLa
void* ReturnBuff) {
if (SpareParts.OnWindowCmd) {
return (*SpareParts.OnWindowCmd)(id, ControlName, action, Value, ReturnBuff);
} else {
printf("Error: CSpareParts::WindowCmd missing !\n");
}
return -1;
}
Loading

0 comments on commit 34d91b3

Please sign in to comment.