arduino-pico/cores/rp2040/_freertos.cpp
AngeloGioacchino Del Regno 44eeebbc6f
cores/rp2040: _freertos.cpp: Avoid memory leak and unnecessary allocation (#948)
There's a memory leak around the corner: creating the mutex means
that we allocate memory for it, but if we cannot find any free slot in the
FMMap array, we're simply returning a nullptr without ever freeing the
previously allocated memory.

Instead of allocating it out of the free-slot check, do it inside.
While at it, also check if the mutex creation succeeded before assigning
it to a FMMap slot.
2022-10-31 14:29:00 -07:00

56 lines
1.7 KiB
C++

/*
_freertos.cpp - Internal core definitions for FreeRTOS
Copyright (c) 2022 Earle F. Philhower, III <earlephilhower@yahoo.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "_freertos.h"
#include "pico/mutex.h"
#include <stdlib.h>
typedef struct {
mutex_t *src;
SemaphoreHandle_t dst;
} FMMap;
static FMMap *_map = nullptr;
extern "C" SemaphoreHandle_t __get_freertos_mutex_for_ptr(mutex_t *m) {
if (!_map) {
_map = (FMMap *)calloc(sizeof(FMMap), 16);
}
// Pre-existing map
for (int i = 0; i < 16; i++) {
if (m == _map[i].src) {
return _map[i].dst;
}
}
for (int i = 0; i < 16; i++) {
if (_map[i].src == nullptr) {
// Make a new mutex
SemaphoreHandle_t fm = __freertos_mutex_create();
if (fm == nullptr) {
return nullptr;
}
_map[i].src = m;
_map[i].dst = fm;
return fm;
}
}
return nullptr; // Need to make space for more mutex maps!
}