@@ -194,27 +194,27 @@ static term nif_dht_read(Context *ctx, int argc, term argv[])
194194 term pin = argv [0 ];
195195 VALIDATE_VALUE (pin , term_is_integer );
196196
197- if (UNLIKELY (memory_ensure_free (ctx , 20 ) != MEMORY_GC_OK )) {
198- RAISE_ERROR (OUT_OF_MEMORY_ATOM );
199- }
200-
201197 uint8_t buf [5 ];
202198 memset (buf , 0 , 5 );
203199 int err = read_into (term_to_int (pin ), buf );
204200 if (err == PROTOCOL_ERROR ) {
205- if (UNLIKELY (memory_ensure_free (ctx , 3 ) != MEMORY_GC_OK )) {
201+ if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE ( 2 ) ) != MEMORY_GC_OK )) {
206202 RAISE_ERROR (OUT_OF_MEMORY_ATOM );
207203 } else {
208- term error_tuple = term_alloc_tuple (2 , ctx );
204+ term error_tuple = term_alloc_tuple (2 , & ctx -> heap );
209205 term_put_tuple_element (error_tuple , 0 , ERROR_ATOM );
210- term_put_tuple_element (error_tuple , 1 , context_make_atom (ctx , dht_bad_read ));
206+ term_put_tuple_element (error_tuple , 1 , globalcontext_make_atom (ctx -> global , dht_bad_read ));
211207 return error_tuple ;
212208 }
213209 } else {
214- term ok_tuple = term_alloc_tuple (2 , ctx );
215- term_put_tuple_element (ok_tuple , 0 , OK_ATOM );
216- term_put_tuple_element (ok_tuple , 1 , term_from_literal_binary (buf , 5 , ctx ));
217- return ok_tuple ;
210+ if (UNLIKELY (memory_ensure_free (ctx , TUPLE_SIZE (2 ) + term_binary_heap_size (5 )) != MEMORY_GC_OK )) {
211+ RAISE_ERROR (OUT_OF_MEMORY_ATOM );
212+ } else {
213+ term ok_tuple = term_alloc_tuple (2 , & ctx -> heap );
214+ term_put_tuple_element (ok_tuple , 0 , OK_ATOM );
215+ term_put_tuple_element (ok_tuple , 1 , term_from_literal_binary (buf , 5 , & ctx -> heap , ctx -> global ));
216+ return ok_tuple ;
217+ }
218218 }
219219}
220220
@@ -241,5 +241,5 @@ const struct Nif *atomvm_dht_get_nif(const char *nifname)
241241
242242#include <sdkconfig.h>
243243#ifdef CONFIG_AVM_DHT_ENABLE
244- REGISTER_NIF_COLLECTION (atomvm_dht , atomvm_dht_init , atomvm_dht_get_nif )
244+ REGISTER_NIF_COLLECTION (atomvm_dht , atomvm_dht_init , NULL , atomvm_dht_get_nif )
245245#endif
0 commit comments