问题描述
我正在尝试使用适用于IoT平台的Contiki-ng操作系统编写一个简单的程序。第一次测试是可以的,但是在尝试分配动态内存时发生了一些奇怪的事情。我正在使用的测试代码如下:
#include "contiki.h"
#include <stdio.h>
#include <stdlib.h>
PROCESS(main_process,"main_process");
AUTOSTART_PROCESSES(&main_process);
PROCESS_THREAD(main_process,ev,data) {
uint8_t *nonce;
int i;
PROCESS_BEGIN();
nonce = (uint8_t *) malloc(32 * sizeof(uint8_t));
if (nonce != NULL) {
for (i = 0; i < 32; i++)
nonce[i] = 0;
printf("Todo OK\n");
} else {
printf("Todo mal\n");
}
PROCESS_END();
}
在构建项目时,出现错误,指出“ 功能_sbrk_r:对_sbrk的未定义引用”。在互联网上浏览了一些论坛后,我设法通过在项目的Makefile中包含以下行来解决此问题:
MODULES += os/lib/newlib
这解决了问题,因为在os / lib / newlib路径中有一个文件syscalls.c,该文件实现了提到的_sbrk函数。但是,现在出现以下错误:
我没有在互联网上找到任何遇到相同问题的人,所以我被困在这里。另外,我不知道为什么,但是从那以后,从Makefile中删除“ MODULES + = os / lib / newlib”行无济于事,并且_eheap和_heap的问题仍然存在,而没有返回未定义的_sbrk错误。但是,我相信这个奇怪的错误是由于Eclipse IDE清理项目时出现了问题。必要时,来自syscalls.c的代码如下:
#include <sys/types.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
/*---------------------------------------------------------------------------*/
#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/
/**
* \brief Enlarges the allocated heap space
* \param incr Number of bytes by which to increase the heap space
* \return The prevIoUs end of heap on success (which is also a pointer to the
* start of the newly allocated memory if \p incr is positive),or
* <tt>(caddr_t)-1</tt> with \c errno set to \c ENOMEM on error
*/
caddr_t
_sbrk(int incr)
{
/*
* Newlib's _sbrk_r() assumes that this global errno variable is used here,* which is different from the errno deFinition provided by <errno.h>.
*/
#undef errno
extern int errno;
/* Heap boundaries from linker script. */
extern uint8_t _heap;
extern uint8_t _eheap;
static uint8_t *heap_end = &_heap;
uint8_t *prev_heap_end = heap_end;
if(heap_end + incr > &_eheap) {
PRINTF("Out of heap space!\n");
errno = ENOMEM;
return (caddr_t)-1;
}
heap_end += incr;
return (caddr_t)prev_heap_end;
}
我希望有人能帮助我。我已经读过Contiki提供了更多使用动态内存的方法,但是我需要这样做,因为代码的其他部分依赖于malloc的使用并且不能更改。
谢谢。
解决方法
简而言之,您几乎肯定不需要在Contiki-NG微控制器上使用malloc()
。请改用Contiki的memb module。
长答案是可以使用链接器脚本添加_heap
符号。查看Contiki-NG提供的示例链接脚本,以了解如何执行此操作。例如,脚本arch/cpu/cc26x0-cc13x0/cc26xx.ld
(用于cc26x0-cc13x0平台)将_heap
放在BSS段之后:
.bss :
{
/* ... */
} > SRAM
_end = .; /* End of the .bss segment. */
/* ... */
_stack = .;
_heap = _stack;