Skip to content

add custom allocator to make sure that vector arrays go into PSRAM when availeable #1

@softhack007

Description

@softhack007

std::vector<std::vector<float>> polar_theta; // look-up table for polar angles
std::vector<std::vector<float>> distance; // look-up table for polar distances

void render_polar_lookup_table(float cx, float cy) {
polar_theta.resize(num_x, std::vector<float>(num_y, 0.0f));
distance.resize(num_x, std::vector<float>(num_y, 0.0f));

We need better control of array placement for boards with PSRAM. Normally on esp32, big allocations automatically go into PRSAM. However due to the 2D vector definitions used for the lookup tables, each "row" vector is only 256 bytes and the framework will only put these into PSRAM if there is not enough RAM or PSRAM is very fast (opi_opi mode).

The standard way with vectors seems to be: add a custom allocator to the vector declaration , like the "Mallocator" in this example:
https://learn.microsoft.com/en-us/cpp/standard-library/allocators?view=msvc-170

For compatibility with the vector template class, it seems that operator== and operator!= must be implemented by the Mallocator, too.

The custom allocator should use ps_malloc() and free().

The custom allocator code should only be used on ESP32 with PSRAM, i.e.

#ifdef ESP32
#if defined(BOARD_HAS_PSRAM) && defined(CONFIG_IDF_TARGET_ESP32S3)
[...]
#endif
#endif

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions