Skip to content

Commit 1b76e9b

Browse files
authored
Clear dirty flag in begin and reallocate only if necessary, add getConstDataPtr method (#2217) (#3849)
1 parent 117bc87 commit 1b76e9b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

libraries/EEPROM/EEPROM.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,21 @@ void EEPROMClass::begin(size_t size) {
5656

5757
size = (size + 3) & (~3);
5858

59-
if (_data) {
59+
//In case begin() is called a 2nd+ time, don't reallocate if size is the same
60+
if(_data && size != _size) {
6061
delete[] _data;
62+
_data = new uint8_t[size];
63+
} else if(!_data) {
64+
_data = new uint8_t[size];
6165
}
6266

63-
_data = new uint8_t[size];
6467
_size = size;
6568

6669
noInterrupts();
6770
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(_data), _size);
6871
interrupts();
72+
73+
_dirty = false; //make sure dirty is cleared in case begin() is called 2nd+ time
6974
}
7075

7176
void EEPROMClass::end() {
@@ -131,6 +136,10 @@ uint8_t * EEPROMClass::getDataPtr() {
131136
return &_data[0];
132137
}
133138

139+
uint8_t const * EEPROMClass::getConstDataPtr() {
140+
return &_data[0];
141+
}
142+
134143
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
135144
EEPROMClass EEPROM;
136145
#endif

libraries/EEPROM/EEPROM.h

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class EEPROMClass {
3838
void end();
3939

4040
uint8_t * getDataPtr();
41+
uint8_t const * getConstDataPtr();
4142

4243
template<typename T>
4344
T &get(int address, T &t) {

0 commit comments

Comments
 (0)