Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
freertos_drivers/nxp/stack_malloc.c
Go to the documentation of this file.
1
35#include <string.h>
36#include <stdlib.h>
37#include "utils/blinker.h"
38#include "utils/constants.hxx"
39
42extern char __ETHRAM_segment_start__;
47extern char __stacks_min__;
48
49void* usb_malloc(unsigned long length);
50
53DECLARE_CONST(use_separate_stack_segment);
54
63void* stack_malloc(unsigned long length)
64{
65 if (config_use_separate_stack_segment() == CONSTANT_TRUE) {
66 char* old_stack_start = sstack_start;
67 char* new_stack_start = sstack_start + length;
68 if (new_stack_start > &__stacks_min__)
69 {
71 }
72 sstack_start = new_stack_start;
73 return old_stack_start;
74 } else {
75 return usb_malloc(length);
76 }
77}
78
81extern char __USBRAM_segment_start__;
84extern char __USBRAM_segment_end__;
87
96void* usb_malloc(unsigned long length)
97{
98 // Aligns to 4 bytes.
99 length += 3; length &= ~3;
100 char* old_ublock_start = ublock_start;
101 char* new_ublock_start = ublock_start + length;
102 if (new_ublock_start > &__USBRAM_segment_end__)
103 {
105 }
106 ublock_start = new_ublock_start;
107 return old_ublock_start;
108}
109
114void *buffer_malloc(size_t length)
115{
116 /* We do a trick here to ensure that the compiler will output a stack frame
117 * for this function. We want to avoid tail-chain optimization in this
118 * function or else it disappears from the stack traces done for memory
119 * tracing. */
120 void *volatile v = usb_malloc(length);
121 return v;
122}
void diewith(uint32_t pattern)
Sets a blinking pattern and never returns.
#define BLINK_DIE_OUTOFMEMSTACK
Out of stack memory space (used only where there is a separate memory block for stack).
Definition blinker.h:65
#define DECLARE_CONST(name)
Declares a constant value.
Definition constants.hxx:60
#define CONSTANT_TRUE
We cannot compare constants to zero, so we use 1 and 2 as constant values for booleans.
char __stacks_min__
Linker symbol defining the first byte that is used by the interrupt stack in the stack segment.
static char * ublock_start
Pointer to the next free (unallocated) byte in the USB_RAM segment.
char __ETHRAM_segment_start__
Linker symbol defining the address where the ethernet RAM segment starts in the 32-bit address space.
void * usb_malloc(unsigned long length)
Custom malloc function for USB space.
char __USBRAM_segment_end__
Linker symbol defining the address where the USB RAM segment ends in the 32-bit address space.
void * buffer_malloc(size_t length)
Allocates a buffer.
static char * sstack_start
Next byte that's free in the stack memory allocation segment.
void * stack_malloc(unsigned long length)
Custom malloc function for stack spaces.
char __USBRAM_segment_start__
Linker symbol defining the address where the USB RAM segment starts in the 32-bit address space.