2017-01-27 02:01:51 -05:00
|
|
|
/*
|
2017-03-08 06:44:57 -05:00
|
|
|
Generic test for malloc/free
|
2017-01-27 02:01:51 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <esp_types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "rom/ets_sys.h"
|
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/xtensa_api.h"
|
|
|
|
#include "unity.h"
|
|
|
|
#include "soc/uart_reg.h"
|
|
|
|
#include "soc/dport_reg.h"
|
|
|
|
#include "soc/io_mux_reg.h"
|
2017-09-22 04:02:39 -04:00
|
|
|
#include "esp_heap_caps.h"
|
2017-01-27 02:01:51 -05:00
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
#include "esp_panic.h"
|
2017-09-22 04:02:39 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
|
|
|
|
|
|
|
|
static int **allocatedMem;
|
|
|
|
static int noAllocated;
|
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-01-27 02:01:51 -05:00
|
|
|
static int tryAllocMem() {
|
2017-09-22 04:02:39 -04:00
|
|
|
int i, j;
|
|
|
|
const int allocateMaxK=1024*5; //try to allocate a max of 5MiB
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-09-22 04:02:39 -04:00
|
|
|
allocatedMem=malloc(sizeof(int *)*allocateMaxK);
|
|
|
|
if (!allocatedMem) return 0;
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-09-22 04:02:39 -04:00
|
|
|
for (i=0; i<allocateMaxK; i++) {
|
|
|
|
allocatedMem[i]=malloc(1024);
|
|
|
|
if (allocatedMem[i]==NULL) break;
|
|
|
|
for (j=0; j<1024/4; j++) allocatedMem[i][j]=(0xdeadbeef);
|
2017-03-08 06:44:57 -05:00
|
|
|
}
|
|
|
|
noAllocated=i;
|
2017-09-22 04:02:39 -04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-09-22 04:02:39 -04:00
|
|
|
static void tryAllocMemFree() {
|
|
|
|
int i, j;
|
2017-03-08 06:44:57 -05:00
|
|
|
for (i=0; i<noAllocated; i++) {
|
|
|
|
for (j=0; j<1024/4; j++) {
|
2017-09-22 04:02:39 -04:00
|
|
|
TEST_ASSERT(allocatedMem[i][j]==(0xdeadbeef));
|
2017-03-08 06:44:57 -05:00
|
|
|
}
|
2017-09-22 04:02:39 -04:00
|
|
|
free(allocatedMem[i]);
|
2017-03-08 06:44:57 -05:00
|
|
|
}
|
2017-09-22 04:02:39 -04:00
|
|
|
free(allocatedMem);
|
2017-01-27 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-03 04:03:28 -04:00
|
|
|
TEST_CASE("Malloc/overwrite, then free all available DRAM", "[heap]")
|
2017-01-27 02:01:51 -05:00
|
|
|
{
|
2017-03-08 06:44:57 -05:00
|
|
|
int m1=0, m2=0;
|
|
|
|
m1=tryAllocMem();
|
2017-09-22 04:02:39 -04:00
|
|
|
tryAllocMemFree();
|
2017-03-08 06:44:57 -05:00
|
|
|
m2=tryAllocMem();
|
2017-09-22 04:02:39 -04:00
|
|
|
tryAllocMemFree();
|
2017-03-08 06:44:57 -05:00
|
|
|
printf("Could allocate %dK on first try, %dK on 2nd try.\n", m1, m2);
|
|
|
|
TEST_ASSERT(m1==m2);
|
2017-01-27 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
2017-09-22 04:02:39 -04:00
|
|
|
|
|
|
|
#if CONFIG_SPIRAM_USE_MALLOC
|
|
|
|
|
|
|
|
#if (CONFIG_SPIRAM_MALLOC_RESERVE_INTERNAL > 1024)
|
|
|
|
TEST_CASE("Check if reserved DMA pool still can allocate even when malloc()'ed memory is exhausted", "[heap]")
|
|
|
|
{
|
|
|
|
char** dmaMem=malloc(sizeof(char*)*512);
|
|
|
|
assert(dmaMem);
|
|
|
|
int m=tryAllocMem();
|
|
|
|
int i=0;
|
|
|
|
for (i=0; i<512; i++) {
|
|
|
|
dmaMem[i]=heap_caps_malloc(1024, MALLOC_CAP_DMA);
|
|
|
|
if (dmaMem[i]==NULL) break;
|
|
|
|
}
|
|
|
|
for (int j=0; j<i; j++) free(dmaMem[j]);
|
|
|
|
free(dmaMem);
|
|
|
|
tryAllocMemFree();
|
|
|
|
printf("Could allocate %dK of DMA memory after allocating all of %dK of normal memory.\n", i, m);
|
|
|
|
TEST_ASSERT(i);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|