@@ -1083,6 +1083,17 @@ bool WiFiClientSecureCtx::_installClientX509Validator() {
10831083 return true ;
10841084}
10851085
1086+ std::shared_ptr<unsigned char > WiFiClientSecureCtx::_alloc_iobuf (size_t sz)
1087+ { // Allocate buffer with preference to IRAM
1088+ HeapSelectIram primary;
1089+ auto sptr = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [sz], std::default_delete<unsigned char []>());
1090+ if (!sptr) {
1091+ HeapSelectDram alternate;
1092+ sptr = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [sz], std::default_delete<unsigned char []>());
1093+ }
1094+ return sptr;
1095+ }
1096+
10861097// Called by connect() to do the actual SSL setup and handshake.
10871098// Returns if the SSL handshake succeeded.
10881099bool WiFiClientSecureCtx::_connectSSL (const char * hostName) {
@@ -1099,17 +1110,12 @@ bool WiFiClientSecureCtx::_connectSSL(const char* hostName) {
10991110
11001111 _sc = std::make_shared<br_ssl_client_context>();
11011112 _eng = &_sc->eng ; // Allocation/deallocation taken care of by the _sc shared_ptr
1102- // C This was borrowed from @earlephilhower PoC, to exemplify the use of IRAM.
1103- // C Is this something we want to keep in the final release?
1104- { // ESP.setIramHeap(); would be an alternative to using a class to set a scope for IRAM usage.
1105- HeapSelectIram ephemeral;
1106- _iobuf_in = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_in_size], std::default_delete<unsigned char []>());
1107- _iobuf_out = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_out_size], std::default_delete<unsigned char []>());
1108- DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1109- DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1110- DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1111- DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
1112- } // ESP.resetHeap();
1113+ _iobuf_in = _alloc_iobuf (_iobuf_in_size);
1114+ _iobuf_out = _alloc_iobuf (_iobuf_out_size);
1115+ DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1116+ DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1117+ DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1118+ DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
11131119
11141120 if (!_sc || !_iobuf_in || !_iobuf_out) {
11151121 _freeSSL (); // Frees _sc, _iobuf*
@@ -1225,15 +1231,12 @@ bool WiFiClientSecureCtx::_connectSSLServerRSA(const X509List *chain,
12251231 _oom_err = false ;
12261232 _sc_svr = std::make_shared<br_ssl_server_context>();
12271233 _eng = &_sc_svr->eng ; // Allocation/deallocation taken care of by the _sc shared_ptr
1228- { // ESP.setIramHeap();
1229- HeapSelectIram ephemeral;
1230- _iobuf_in = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_in_size], std::default_delete<unsigned char []>());
1231- _iobuf_out = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_out_size], std::default_delete<unsigned char []>());
1232- DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1233- DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1234- DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1235- DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
1236- } // ESP.resetHeap();
1234+ _iobuf_in = _alloc_iobuf (_iobuf_in_size);
1235+ _iobuf_out = _alloc_iobuf (_iobuf_out_size);
1236+ DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1237+ DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1238+ DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1239+ DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
12371240
12381241 if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
12391242 _freeSSL ();
@@ -1272,15 +1275,12 @@ bool WiFiClientSecureCtx::_connectSSLServerEC(const X509List *chain,
12721275 _oom_err = false ;
12731276 _sc_svr = std::make_shared<br_ssl_server_context>();
12741277 _eng = &_sc_svr->eng ; // Allocation/deallocation taken care of by the _sc shared_ptr
1275- { // ESP.setIramHeap();
1276- HeapSelectIram ephemeral;
1277- _iobuf_in = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_in_size], std::default_delete<unsigned char []>());
1278- _iobuf_out = std::shared_ptr<unsigned char >(new (std::nothrow) unsigned char [_iobuf_out_size], std::default_delete<unsigned char []>());
1279- DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1280- DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1281- DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1282- DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
1283- } // ESP.resetHeap();
1278+ _iobuf_in = _alloc_iobuf (_iobuf_in_size);
1279+ _iobuf_out = _alloc_iobuf (_iobuf_out_size);
1280+ DBG_MMU_PRINTF (" \n _iobuf_in: %p\n " , _iobuf_in.get ());
1281+ DBG_MMU_PRINTF ( " _iobuf_out: %p\n " , _iobuf_out.get ());
1282+ DBG_MMU_PRINTF ( " _iobuf_in_size: %u\n " , _iobuf_in_size);
1283+ DBG_MMU_PRINTF ( " _iobuf_out_size: %u\n " , _iobuf_out_size);
12841284
12851285 if (!_sc_svr || !_iobuf_in || !_iobuf_out) {
12861286 _freeSSL ();
0 commit comments