Skip to content

Commit

Permalink
changed random malloc seed to environment variable; rearranged added …
Browse files Browse the repository at this point in the history
…code
  • Loading branch information
Samantha committed Oct 19, 2016
1 parent 5eedf8e commit e371afa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
20 changes: 13 additions & 7 deletions include/pico_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#else
#include <linux/types.h>
#endif
#ifdef __linux__
#include <execinfo.h>
#endif


#if defined __IAR_SYSTEMS_ICC__ || defined ATOP
# define PACKED_STRUCT_DEF __packed struct
Expand Down Expand Up @@ -231,7 +235,10 @@ static inline uint64_t long_long_be(uint64_t le)
# include "arch/pico_posix.h"
#endif

static inline void log(const char* filename, const char* message)


#ifdef CHECK_MEM
static inline void log_malloc(const char* filename, const char* message)
{
FILE * file;
file = fopen(filename, "a");
Expand All @@ -258,15 +265,14 @@ static inline void append_backtrace(const char* filename)
fclose(file);
}

#ifdef PICO_SUPPORT_MM
#define PICO_ZALLOC(x) pico_mem_zalloc(x)
#define PICO_FREE(x) pico_mem_free(x)
#elif defined CHECK_MEM
#define PICO_ZALLOC(x) \
((((double)(rand())/(double)RAND_MAX) < 0.4) \
? (log("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \
: (log("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x)))
? (log_malloc("mem_test.log", "Malloc FAILED\n"), append_backtrace("mem_test.log"), NULL) \
: (log_malloc("mem_test.log", "Malloc Succeeded\n"), append_backtrace("mem_test.log"), pico_zalloc(x)))
#define PICO_FREE(x) pico_free(x)
#elif defined PICO_SUPPORT_MM
#define PICO_ZALLOC(x) pico_mem_zalloc(x)
#define PICO_FREE(x) pico_mem_free(x)
#else
#define PICO_ZALLOC(x) pico_zalloc(x)
#define PICO_FREE(x) pico_free(x)
Expand Down
13 changes: 11 additions & 2 deletions stack/pico_stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -856,9 +856,18 @@ uint32_t pico_timer_add_hashed(pico_time expire, void (*timer)(pico_time, void *

int MOCKABLE pico_stack_init(void)
{
#if ((defined CHECK_MEM) && (defined MEM_TEST_SEED))
srand(MEM_TEST_SEED);

#ifdef CHECK_MEM
//New log for malloc information
fopen("mem_test.log", "w");

//Set rand seed
char *buf = getenv("MEM_TEST_SEED");
if(buf!=NULL){
srand(atoi(buf));
}
else
srand(1);
#endif

#ifdef PICO_SUPPORT_ETH
Expand Down

0 comments on commit e371afa

Please sign in to comment.