Skip to content

Commit a976386

Browse files
committed
Align write data to page size
1 parent 8625a65 commit a976386

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

qt/main_window.cpp

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828

2929
void MainWindow::initBufTable()
3030
{
31-
buffer = nullptr;
32-
bufferSize = 0;
33-
3431
ui->bufferTableView->setModel(&bufferTableModel);
3532
QHeaderView *verticalHeader = ui->bufferTableView->verticalHeader();
3633
verticalHeader->setSectionResizeMode(QHeaderView::Fixed);
@@ -43,8 +40,7 @@ void MainWindow::initBufTable()
4340
void MainWindow::resetBufTable()
4441
{
4542
bufferTableModel.setBuffer(nullptr, 0);
46-
bufferSize = 0;
47-
delete [] buffer;
43+
buffer.clear();
4844
}
4945

5046
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
@@ -89,7 +85,6 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
8985

9086
MainWindow::~MainWindow()
9187
{
92-
delete [] buffer;
9388
Logger::putInstance();
9489
delete ui;
9590
}
@@ -113,14 +108,8 @@ void MainWindow::slotFileOpen()
113108

114109
resetBufTable();
115110
fileSize = file.size();
116-
buffer = new (std::nothrow) uint8_t[fileSize];
117-
if (!buffer)
118-
{
119-
qCritical() << "Failed to allocate memory for read buffer";
120-
goto Exit;
121-
}
122-
123-
ret = file.read((char *)buffer, fileSize);
111+
buffer.resize(static_cast<int>(fileSize));
112+
ret = file.read(reinterpret_cast<char *>(buffer.data()), fileSize);
124113
if (ret < 0)
125114
{
126115
qCritical() << "Failed to read file:" << fileName << ", error:" <<
@@ -134,8 +123,8 @@ void MainWindow::slotFileOpen()
134123
goto Exit;
135124
}
136125

137-
bufferSize = fileSize;
138-
bufferTableModel.setBuffer(buffer, fileSize);
126+
bufferTableModel.setBuffer(buffer.data(),
127+
static_cast<uint32_t>(buffer.size()));
139128

140129
Exit:
141130
file.close();
@@ -278,20 +267,18 @@ void MainWindow::slotProgErase()
278267

279268
void MainWindow::slotProgReadCompleted(int status)
280269
{
281-
int index = ui->chipSelectComboBox->currentIndex();
282-
uint32_t readSize = chipDb.sizeGetById(CHIP_INDEX2ID(index));
283-
284270
disconnect(prog, SIGNAL(readChipCompleted(int)), this,
285271
SLOT(slotProgReadCompleted(int)));
286272

287273
if (status)
288274
{
289-
delete [] buffer;
275+
buffer.clear();
290276
return;
291277
}
292278

293279
qInfo() << "Data has been successfully read";
294-
bufferTableModel.setBuffer(buffer, readSize);
280+
bufferTableModel.setBuffer(buffer.data(),
281+
static_cast<uint32_t>(buffer.size()));
295282
}
296283

297284
void MainWindow::slotProgRead()
@@ -306,19 +293,15 @@ void MainWindow::slotProgRead()
306293
}
307294

308295
resetBufTable();
309-
buffer = new (std::nothrow) uint8_t[readSize];
310-
if (!buffer)
311-
{
312-
qCritical() << "Failed to allocate memory for read buffer";
313-
return;
314-
}
296+
buffer.clear();
297+
buffer.resize(static_cast<int>(readSize));
315298

316299
qInfo() << "Reading data ...";
317300

318301
connect(prog, SIGNAL(readChipCompleted(int)), this,
319302
SLOT(slotProgReadCompleted(int)));
320303

321-
prog->readChip(buffer, START_ADDRESS, readSize, true);
304+
prog->readChip(buffer.data(), START_ADDRESS, readSize, true);
322305
}
323306

324307
void MainWindow::slotProgWriteCompleted(int status)
@@ -335,8 +318,9 @@ void MainWindow::slotProgWrite()
335318
int index;
336319
QString name;
337320
uint32_t pageSize;
321+
uint32_t bufferSize;
338322

339-
if (!bufferSize)
323+
if (buffer.isEmpty())
340324
{
341325
qInfo() << "Write buffer is empty";
342326
return;
@@ -355,12 +339,19 @@ void MainWindow::slotProgWrite()
355339
return;
356340
}
357341

342+
bufferSize = static_cast<uint32_t>(buffer.size());
343+
if (bufferSize & (pageSize - 1))
344+
{
345+
bufferSize = (bufferSize + pageSize - 1) & ~(pageSize - 1);
346+
buffer.resize(static_cast<int>(bufferSize));
347+
}
348+
358349
qInfo() << "Writing data ...";
359350

360351
connect(prog, SIGNAL(writeChipCompleted(int)), this,
361352
SLOT(slotProgWriteCompleted(int)));
362353

363-
prog->writeChip(buffer, START_ADDRESS, bufferSize, pageSize);
354+
prog->writeChip(buffer.data(), START_ADDRESS, bufferSize, pageSize);
364355
}
365356

366357
void MainWindow::slotProgReadBadBlocksCompleted(int status)

qt/main_window.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "programmer.h"
1010
#include "buffer_table_model.h"
1111
#include <QMainWindow>
12+
#include <QVector>
1213

1314
namespace Ui {
1415
class MainWindow;
@@ -25,8 +26,7 @@ class MainWindow : public QMainWindow
2526

2627
private:
2728
Ui::MainWindow *ui;
28-
uint8_t *buffer;
29-
uint32_t bufferSize;
29+
QVector<uint8_t> buffer;
3030
BufferTableModel bufferTableModel;
3131
ChipId chipId;
3232
ChipDb chipDb;

0 commit comments

Comments
 (0)