简体   繁体   English

在contiki程序中使用malloc

[英]use of malloc in contiki programs

Consider the following contiki program. 考虑以下contiki计划。

#include<stdio.h>
#include"contiki.h"
#include <stdlib.h>

static char *mem;
static int x;
/*---------------------------------------------------------------------------*/
PROCESS(test, "test");
AUTOSTART_PROCESSES(&test);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test, ev, data)
{
  PROCESS_BEGIN();
  printf("before malloc\n");
  mem=(char*)malloc(10);
  for(x=0;x<10;x++)
     mem[x]=x+1;
  printf("after malloc\n");
  PROCESS_END();
}

when this program is compiled for native/z1/wismote/cooja it executes perfectly fine and both the printf statements are executed, but when compiled for mbxxx target, and then executed on hardware, only the first printf statements is executed and the code gets stuck in the malloc. 当这个程序编译为native / z1 / wismote / cooja时,它执行得很好并且两个printf语句都被执行,但是当编译为mbxxx目标,然后在硬件上执行时,只执行第一个printf语句并且代码被卡住了在malloc。 Any guess or reason behind this behaviour? 这种行为背后的任何猜测或原因? I am also attaching the GDB trace here. 我也在这里附加GDB跟踪。

(gdb) mon reset init
target state: halted
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08000efc msp: 0x20000500
(gdb) b test.c:16
Breakpoint 1 at 0x8000ec8: file test.c, line 16.
(gdb) b test.c:17
Breakpoint 2 at 0x8000ece: file test.c, line 17.
(gdb) b test.c:18
Breakpoint 3 at 0x8000ed8: file test.c, line 18.
(gdb) load
Loading section .isr_vector, size 0x84 lma 0x8000000
Loading section .text, size 0xc5c4 lma 0x8000084
Loading section .data, size 0x660 lma 0x800c648
Start address 0x8000084, load size 52392
Transfer rate: 15 KB/sec, 8732 bytes/write.
(gdb) c
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.

Breakpoint 1, process_thread_test (process_pt=0x2000050c <test+12>, ev=129 '\201', data=0x0) at test.c:16
16      printf("before malloc\n");
(gdb) c
Continuing.

Breakpoint 2, process_thread_test (process_pt=0x2000050c <test+12>, ev=<optimized out>, 
    data=<optimized out>) at test.c:17
17  mem=(char*)malloc(10);
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
Default_Handler () at ../../cpu/stm32w108/hal/micro/cortexm3/stm32w108/crt-stm32w108.c:87
87  {
(gdb) bt
#0  Default_Handler () at ../../cpu/stm32w108/hal/micro/cortexm3/stm32w108/crt-stm32w108.c:87
#1  <signal handler called>
#2  0x08000440 in _malloc_r ()
#3  0x08000ed4 in process_thread_test (process_pt=0x2000050c <certificate_check+12>, ev=<optimized out>, 
    data=<optimized out>) at test.c:17
#4  0x0800272c in call_process (p=0x20000500 <test>, ev=<optimized out>, data=<optimized out>)
    at ../../core/sys/process.c:190
#5  0x080028e6 in process_post_synch (p=<optimized out>, ev=ev@entry=129 '\201', data=<optimized out>)
    at ../../core/sys/process.c:366
#6  0x0800291a in process_start (p=<optimized out>, arg=arg@entry=0x0) at ../../core/sys/process.c:120
#7  0x08002964 in autostart_start (processes=<optimized out>) at ../../core/sys/autostart.c:57
#8  0x08001134 in main () at ../../platform/mbxxx/./contiki-main.c:210
(gdb)

Ahhh... Finally figured out the problem. 啊......终于找到了问题所在。 This particular problem was there because stm32w108 was not configured to use dynamic memory. 这个特殊问题是因为s​​tm32w108未配置为使用动态内存。

All that was needed to be done was, to open the the following file: contiki-2.7/cpu/stm32w108/hal/micro/cortexm3/stm32w108/crt-stm32w108.c and add #define USE_HEAP at the top of the file or before the _sbrk implementation! 所有需要做的是,打开以下文件:contiki-2.7 / cpu / stm32w108 / hal / micro / cortexm3 / stm32w108 / crt-stm32w108.c并在文件顶部或之前添加#define USE_HEAP _sbrk实现! The heap size can also be modified here, not from the linker script, although the stack size 堆大小也可以在这里修改,而不是从链接器脚本修改,尽管堆栈大小

A side note: It is a really bad idea to use dynamic memory allocation in embedded systems, so avoid it! 旁注: 在嵌入式系统中使用动态内存分配是一个非常糟糕的主意,所以要避免它! Its filthy trust me! 它的肮脏相信我! Eventually I will also remove any dynamic memory allocation references! 最后我还将删除所有动态内存分配引用! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM