1

我正在尝试使用 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();
}

在构建项目时,会出现一个错误,指出“ In function _sbrk_r: undefined reference to _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 的使用并且无法更改。

谢谢。

4

1 回答 1

1

简而言之,您几乎可以肯定不需要也不想malloc()在 Contiki-NG 微控制器上使用。请改用 Contiki 的memb 模块

长答案是您可以_heap使用链接描述文件添加符号。检查 Contiki-NG 提供的示例链接器脚本以了解如何执行此操作。例如,脚本arch/cpu/cc26x0-cc13x0/cc26xx.ld(对于 cc26x0-cc13x0 平台)_heap紧跟在 BSS 段之后:

.bss :
{
    /* ... */
} > SRAM

_end = .;  /* End of the .bss segment. */
/* ... */
_stack = .;
_heap = _stack;
于 2020-08-24T11:28:54.267 回答