diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-02-28 15:31:58 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2013-02-28 15:31:58 +0000 |
commit | 8bf2bd9ff858b953091e9c06fd573e02dcfefe5a (patch) | |
tree | 617198360881b74f55be80d10213f03cdf23dbfa /nuttx/arch/mips | |
parent | f6b940c07fd543138ca4b856731fcf774edebeed (diff) | |
download | px4-nuttx-8bf2bd9ff858b953091e9c06fd573e02dcfefe5a.tar.gz px4-nuttx-8bf2bd9ff858b953091e9c06fd573e02dcfefe5a.tar.bz2 px4-nuttx-8bf2bd9ff858b953091e9c06fd573e02dcfefe5a.zip |
Add debug output when memory allocations fail
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5686 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/arch/mips')
-rw-r--r-- | nuttx/arch/mips/src/common/up_createstack.c | 108 |
1 files changed, 67 insertions, 41 deletions
diff --git a/nuttx/arch/mips/src/common/up_createstack.c b/nuttx/arch/mips/src/common/up_createstack.c index f72701042..02cdd83d7 100644 --- a/nuttx/arch/mips/src/common/up_createstack.c +++ b/nuttx/arch/mips/src/common/up_createstack.c @@ -87,71 +87,97 @@ * Name: up_create_stack * * Description: - * Allocate a stack for a new thread and setup - * up stack-related information in the TCB. + * Allocate a stack for a new thread and setup up stack-related + * information in the TCB. * * The following TCB fields must be initialized: - * adj_stack_size: Stack size after adjustment for hardware, - * processor, etc. This value is retained only for debug - * purposes. + * adj_stack_size: Stack size after adjustment for hardware, processor, + * etc. This value is retained only for debug purposes. * stack_alloc_ptr: Pointer to allocated stack - * adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The - * initial value of the stack pointer. + * adj_stack_ptr: Adjusted stack_alloc_ptr for HW. The initial value of + * the stack pointer. * - * Inputs: + * Input Parameters: * tcb: The TCB of new task - * stack_size: The requested stack size. At least this much - * must be allocated. + * stack_size: The requested stack size. At least this how much must be + * allocated. + * ****************************************************************************/ int up_create_stack(struct tcb_s *tcb, size_t stack_size) { - if (tcb->stack_alloc_ptr && - tcb->adj_stack_size != stack_size) + /* Is there already a stack allocated of a different size? */ + + if (tcb->stack_alloc_ptr && tcb->adj_stack_size != stack_size) { + /* Yes.. free it */ + sched_free(tcb->stack_alloc_ptr); tcb->stack_alloc_ptr = NULL; } - if (!tcb->stack_alloc_ptr) - { -#ifdef CONFIG_DEBUG - tcb->stack_alloc_ptr = (uint32_t*)kzalloc(stack_size); + /* Do we need to allocate a stack? */ + + if (!tcb->stack_alloc_ptr) + { + /* Allocate the stack. If DEBUG is enabled (but not stack debug), + * then create a zeroed stack to make stack dumps easier to trace. + */ + +#if defined(CONFIG_DEBUG) && !defined(CONFIG_DEBUG_STACK) + tcb->stack_alloc_ptr = (uint32_t *)kzalloc(stack_size); #else - tcb->stack_alloc_ptr = (uint32_t*)kmalloc(stack_size); + tcb->stack_alloc_ptr = (uint32_t *)kmalloc(stack_size); +#endif +#ifdef CONFIG_DEBUG + if (!tcb->stack_alloc_ptr) + { + sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); + } #endif - } + } + + /* Did we successfully allocate a stack? */ + + if (tcb->stack_alloc_ptr) + { + size_t top_of_stack; + size_t size_of_stack; + + /* Yes.. If stack debug is enabled, then fill the stack with a + * recognizable value that we can use later to test for high + * water marks. + */ - if (tcb->stack_alloc_ptr) - { - size_t top_of_stack; - size_t size_of_stack; +#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_STACK) + memset(tcb->stack_alloc_ptr, 0xaa, stack_size); +#endif - /* MIPS uses a push-down stack: the stack grows toward lower - * addresses in memory. The stack pointer register points to the - * lowest, valid working address (the "top" of the stack). Items on - * the stack are referenced as positive word offsets from sp. - */ + /* MIPS uses a push-down stack: the stack grows toward lower + * addresses in memory. The stack pointer register points to the + * lowest, valid working address (the "top" of the stack). Items on + * the stack are referenced as positive word offsets from sp. + */ - top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4; + top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4; - /* The MIPS stack must be aligned at word (4 byte) boundaries; for - * floating point use, the stack must be aligned to 8-byte addresses. - * If necessary top_of_stack must be rounded down to the next - * boundary to meet these alignment requirements. - */ + /* The MIPS stack must be aligned at word (4 byte) boundaries; for + * floating point use, the stack must be aligned to 8-byte addresses. + * If necessary top_of_stack must be rounded down to the next + * boundary to meet these alignment requirements. + */ - top_of_stack = STACK_ALIGN_DOWN(top_of_stack); - size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4; + top_of_stack = STACK_ALIGN_DOWN(top_of_stack); + size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4; - /* Save the adjusted stack values in the struct tcb_s */ + /* Save the adjusted stack values in the struct tcb_s */ - tcb->adj_stack_ptr = (uint32_t*)top_of_stack; - tcb->adj_stack_size = size_of_stack; + tcb->adj_stack_ptr = (uint32_t*)top_of_stack; + tcb->adj_stack_size = size_of_stack; - up_ledon(LED_STACKCREATED); - return OK; - } + up_ledon(LED_STACKCREATED); + return OK; + } return ERROR; } |