cancel
Showing results for 
Search instead for 
Did you mean: 

p_malloc() from: mem_utils.c always returns NULL, but I have enough heap... Keil uVision

Zaher
Senior II

I'm trying to utilize some of the "wavprocess" functions (from demo builder package) in one of my projects, but the memory allocation function always returns NULL.

The memory region declared in the external memory space, but I changed it to the internal SRAM because I do not have external memory connected to my STM32F407 board.

mem_TypeDef memory_pool __attribute__((at(0x20000000)));  

I tried it with the CCM enabled/disabled, different sizes of heap, but to no avail. The p_malloc() always returns with NULL.

This is the function concerned:

/**
  * @brief  This function implement a simple memory management algorithme (First fit).
  * @param  size : Requested memory size.
  * @retval Pointer to the allocated region.
  */
void * p_malloc(size_t size)
{
  __IO uint8_t index = 0, counter = 0, start = 0;
  uint8_t PageCount = 0, NewStart = 0;
  if (size > 0)
  {
    /* look for the first contiguous memory that meet requested size. */
    while ( index < (MAX_PAGE_NUMBER + 1))
    {
      if (memory_pool.PageTable[index++] == 0)
      {
        counter++;
        if (size <= (counter * SIZE_OF_PAGE))
        {
          PageCount = counter - 1;
          NewStart = start;
          /* Set memory region state to allocated */
          for (index = 0; index <= PageCount;index++)
          {
            memory_pool.PageTable[NewStart + index] = 1;
          }
          /* Save size */
          memory_pool.size[NewStart] = counter;
          /* return pointer to allocated region. */
          return (void *)((memory_pool.mallocBase + (start  << 10)) + 1 * sizeof(uint32_t));
        }
      }
      else
      {
        start = index;
        counter = 0;
      }
    }
  }
  return NULL;
}

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Zaher
Senior II

/** An Update **/

The standard malloc() has solved the problem and now memory allocated to the buffers without any issue!

View solution in original post

1 REPLY 1
Zaher
Senior II

/** An Update **/

The standard malloc() has solved the problem and now memory allocated to the buffers without any issue!