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-05-03 04:03:28 -04:00
|
|
|
#include "esp_panic.h"
|
|
|
|
|
2017-01-27 02:01:51 -05:00
|
|
|
static int tryAllocMem() {
|
2017-03-08 06:44:57 -05:00
|
|
|
int **mem;
|
|
|
|
int i, noAllocated, j;
|
2017-05-03 04:03:28 -04:00
|
|
|
|
|
|
|
mem=malloc(sizeof(int *)*1024);
|
2017-03-08 06:44:57 -05:00
|
|
|
if (!mem) return 0;
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-03-08 06:44:57 -05:00
|
|
|
for (i=0; i<1024; i++) {
|
|
|
|
mem[i]=malloc(1024);
|
|
|
|
if (mem[i]==NULL) break;
|
|
|
|
for (j=0; j<1024/4; j++) mem[i][j]=(0xdeadbeef);
|
|
|
|
}
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-03-08 06:44:57 -05:00
|
|
|
noAllocated=i;
|
2017-05-03 04:03:28 -04:00
|
|
|
|
2017-03-08 06:44:57 -05:00
|
|
|
for (i=0; i<noAllocated; i++) {
|
|
|
|
for (j=0; j<1024/4; j++) {
|
|
|
|
TEST_ASSERT(mem[i][j]==(0xdeadbeef));
|
|
|
|
}
|
|
|
|
free(mem[i]);
|
|
|
|
}
|
|
|
|
free(mem);
|
|
|
|
return noAllocated;
|
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();
|
|
|
|
m2=tryAllocMem();
|
|
|
|
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
|
|
|
}
|
|
|
|
|