dlbeer / quirc

QR decoder library
Other
865 stars 285 forks source link

Add support for allocating heap instead of stack memory #123

Open MU-Software opened 2 years ago

MU-Software commented 2 years ago

The main purpose of this PR is to use this library in a special case with more heap space than stack.
ex) ESP32 can have external memory (PSRAM), but that can be used only for heap allocation. Previously, using this library on a ESP32 without any modification causes a stack corruption.
(sizeof(struct datastream) might take 8901bytes(1+2+2+8896), and ESP32's default task stack size is 4 KB.) We can solve the problem by moving some stack variables to heap.

This can be enabled by defining QUIRC_USE_HEAP macro, define this before import, or add -D QUIRC_USE_HEAP flag while compiling.

dlbeer commented 2 years ago

On Sun, Jan 09, 2022 at 09:35:38AM -0800, MUsoftware wrote:

The main purpose of this PR is to use this library in a special case with more heap space than stack.
ex) ESP32 can have external memory (PSRAM), but that can be used only for heap allocation. Previously, using this library on a ESP32 without any modification causes a stack corruption.
(sizeof(struct datastream) might take 8911bytes, and ESP32's default task stack size is 4 KB.) We can solve the problem by moving some stack variables to heap.

This can be enabled by defining QUIRC_USE_HEAP macro, define this before import, or add -D QUIRC_USE_HEAP flag while compiling. You can view, comment on, or merge this pull request online at:

I've just had a chance to look over the patch now. I understand the problem, however:

My suggestion would be to either:

One other very quick-and-dirty alternative is to just mark the problematic buffers as static. That's a bit ugly, but I'm guessing you have only one instance of the object on your memory-constrained system?

-- Daniel Beer @.***> http://dlbeer.co.nz/ PGP: BA6E 0B26 1F89 246C E3F3 C910 1E58 C43A 160A 553B