From e0b68536a5c405e8e028c65c3d4e22b5b99218bd Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 14 Oct 2022 14:38:12 +1100 Subject: [PATCH] Use a fixed transaction buffer size --- main/i2c.cpp | 10 ++++++++-- main/i2c.hpp | 1 + 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/main/i2c.cpp b/main/i2c.cpp index 4ae3b267..9b218f3f 100644 --- a/main/i2c.cpp +++ b/main/i2c.cpp @@ -1,16 +1,22 @@ #include "i2c.hpp" +#include #include "assert.h" +#include "driver/i2c.h" namespace gay_ipod { I2CTransaction::I2CTransaction() { - handle_ = i2c_cmd_link_create(); + // Use a fixed size buffer to avoid many many tiny allocations. + // TODO: make this static and tune the size. possibly make it optional too? + // threading. + buffer_ = (uint8_t*) calloc(sizeof(uint8_t), I2C_LINK_RECOMMENDED_SIZE(10)); + handle_ = i2c_cmd_link_create_static(buffer_, I2C_LINK_RECOMMENDED_SIZE(10)); assert(handle_ != NULL && "failed to create command link"); } I2CTransaction::~I2CTransaction() { - i2c_cmd_link_delete(handle_); + free(buffer_); } esp_err_t I2CTransaction::Execute() { diff --git a/main/i2c.hpp b/main/i2c.hpp index 6b2de577..0993a305 100644 --- a/main/i2c.hpp +++ b/main/i2c.hpp @@ -78,6 +78,7 @@ class I2CTransaction { private: i2c_cmd_handle_t handle_; + uint8_t *buffer_; }; } // namespace gay_ipod