2727#include < nvs.h>
2828#include < esp_partition.h>
2929#include < esp_log.h>
30+ #include < new>
3031
3132EEPROMClass::EEPROMClass (void ) : _handle(0 ), _data(0 ), _size(0 ), _dirty(false ), _name(" eeprom" ) {}
3233
@@ -59,31 +60,31 @@ bool EEPROMClass::begin(size_t size) {
5960 }
6061 if (size < key_size) { // truncate
6162 log_w (" truncating EEPROM from %d to %d" , key_size, size);
62- uint8_t *key_data = ( uint8_t *) malloc ( key_size) ;
63+ uint8_t *key_data = new (std::nothrow) uint8_t [ key_size] ;
6364 if (!key_data) {
6465 log_e (" Not enough memory to truncate EEPROM!" );
6566 return false ;
6667 }
6768 nvs_get_blob (_handle, _name, key_data, &key_size);
6869 nvs_set_blob (_handle, _name, key_data, size);
6970 nvs_commit (_handle);
70- free ( key_data) ;
71+ delete[] key_data;
7172 } else if (size > key_size) { // expand or new
7273 size_t expand_size = size - key_size;
73- uint8_t *expand_key = ( uint8_t *) malloc ( expand_size) ;
74+ uint8_t *expand_key = new (std::nothrow) uint8_t [ expand_size] ;
7475 if (!expand_key) {
7576 log_e (" Not enough memory to expand EEPROM!" );
7677 return false ;
7778 }
7879 // check for adequate free space
7980 if (nvs_set_blob (_handle, " expand" , expand_key, expand_size)) {
8081 log_e (" Not enough space to expand EEPROM from %d to %d" , key_size, size);
81- free ( expand_key) ;
82+ delete[] expand_key;
8283 return false ;
8384 }
84- free ( expand_key) ;
85+ delete[] expand_key;
8586 nvs_erase_key (_handle, " expand" );
86- uint8_t *key_data = ( uint8_t *) malloc ( size) ;
87+ uint8_t *key_data = new (std::nothrow) uint8_t [ size] ;
8788 if (!key_data) {
8889 log_e (" Not enough memory to expand EEPROM!" );
8990 return false ;
@@ -99,15 +100,15 @@ bool EEPROMClass::begin(size_t size) {
99100 }
100101 nvs_commit (_handle);
101102 nvs_set_blob (_handle, _name, key_data, size);
102- free ( key_data) ;
103+ delete[] key_data;
103104 nvs_commit (_handle);
104105 }
105106
106107 if (_data) {
107108 delete[] _data;
108109 }
109110
110- _data = ( uint8_t *) malloc ( size) ;
111+ _data = new (std::nothrow) uint8_t [ size] ;
111112 if (!_data) {
112113 log_e (" Not enough memory for %d bytes in EEPROM" , size);
113114 return false ;
@@ -212,7 +213,7 @@ uint16_t EEPROMClass::convert(bool clear, const char *EEPROMname, const char *nv
212213 }
213214
214215 size_t size = mypart->size ;
215- uint8_t *data = ( uint8_t *) malloc ( size) ;
216+ uint8_t *data = new (std::nothrow) uint8_t [ size] ;
216217 if (!data) {
217218 log_e (" Not enough memory to convert EEPROM!" );
218219 goto exit;
@@ -255,7 +256,7 @@ uint16_t EEPROMClass::convert(bool clear, const char *EEPROMname, const char *nv
255256 }
256257 }
257258exit:
258- free ( data) ;
259+ delete[] data;
259260 return result;
260261}
261262
@@ -509,9 +510,9 @@ size_t EEPROMClass::writeBytes(int address, const void *value, size_t len) {
509510 return len;
510511}
511512
512- template <class T > T EEPROMClass::writeAll (int address, const T &value) {
513+ template <class T > size_t EEPROMClass::writeAll (int address, const T &value) {
513514 if (address < 0 || address + sizeof (T) > _size) {
514- return value ;
515+ return 0 ;
515516 }
516517
517518 memcpy (_data + address, (const uint8_t *)&value, sizeof (T));
0 commit comments