0

I am currently evaluating embOS from SEGGER running on Cortex M4F. It has 128 kilobytes of internal RAM, and 2 megabytes of external RAM, so I know I have plenty of memory.

My program uses some dynamic allocations (yes, I am aware that is not recommended on embedded systems).

When starting my task, I am trying to call malloc/OS_malloc, where the OS_malloc is the thread-safe version provided from embOS. In both cases, malloc failed and returned NULL pointer.

When doing the same malloc/OS_malloc before the OS starts, it works correctly:

**//Malloc here does not fail**
OS_IncDI();                      /* Initially disable interrupts  */
**//Malloc here does not fail**
OS_InitKern();                   /* Initialize OS                  */
**//Malloc here does  fail !!**
OS_InitHW();                     /* Initialize Hardware for OS    */

OS_CREATETASK(&TCBHP, "My Task", HPTask, 50, StackHP); //**<--And off course malloc failes inside teh task also**

OS_Start();

I went and tried using uCOS from MICRIUM, and I see the same behavior. Any ideas why this happens?

hopper
  • 13,060
  • 7
  • 49
  • 53
Fluffy
  • 337
  • 1
  • 5
  • 16
  • That would be one of those things that the OS producers would have to answer - or you'll have to look at the OS source code for malloc. I'm very surprised that malloc "works" before you have properly initialized the OS. – Mats Petersson Jul 16 '13 at 12:47
  • Us guessing is all very well, but really you need to go look at what is actually being allocated and maybe use a debugger to find out why malloc isn't happy. – ams Jul 16 '13 at 12:54
  • Also, did you check `errno`? – ams Jul 16 '13 at 12:54
  • Your system may have plenty of memory but has enough of that memory been assigned to the heap, or wherever malloc attempts to draw from? – kkrambo Jul 16 '13 at 14:20
  • Yes , because when not using the OS , everything works just fine , so everything is configured correctly in the linker and the startup. – Fluffy Jul 16 '13 at 15:57

2 Answers2

1

I think that i am on my way to fix the problem .

it seems that setting in the linker script :

_Min_Heap_Size = 0x19000; /* required amount of heap */ _Min_Stack_Size = 0x200; /* required amount of stack */

instead of :

_Min_Heap_Size = 0x00; /* required amount of heap */ _Min_Stack_Size = 0x200; /* required amount of stack */

Fluffy
  • 337
  • 1
  • 5
  • 16
0

malloc may returns fail in following condition

1)Running out of memory but as you said i have plenty of memory so this is not the case.

2)malloc is not able to allocate contiguous memory of requested size.

I guess option 2 is responsible for your case.

Dayal rai
  • 6,548
  • 22
  • 29
  • I rather guess it is the reason the OP found himself*herself in the meantime: In embedded systems, the RAM area where the malloc() implementation picks its memory from must be provided to that malloc library explicitly. If one starts a project without considering which amount of memory is reserved for "the malloc heap" but sticks to the defaults of some demo example, the day may arrive when the application runs out of heap memory... – HelpingHand Feb 22 '22 at 21:23