From 8bf2bd9ff858b953091e9c06fd573e02dcfefe5a Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 28 Feb 2013 15:31:58 +0000 Subject: 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 --- nuttx/arch/sh/src/common/up_createstack.c | 110 +++++++++++++++++++----------- 1 file changed, 70 insertions(+), 40 deletions(-) (limited to 'nuttx/arch/sh') diff --git a/nuttx/arch/sh/src/common/up_createstack.c b/nuttx/arch/sh/src/common/up_createstack.c index f868750de..296245a49 100644 --- a/nuttx/arch/sh/src/common/up_createstack.c +++ b/nuttx/arch/sh/src/common/up_createstack.c @@ -1,7 +1,7 @@ /**************************************************************************** * arch/sh/src/common/up_createstack.c * - * Copyright (C) 2008-2009 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2009, 2013 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -66,67 +66,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) - { - tcb->stack_alloc_ptr = (uint32_t*)kzalloc(stack_size); - } + /* Do we need to allocate a stack? */ - if (tcb->stack_alloc_ptr) - { - size_t top_of_stack; - size_t size_of_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); +#endif +#ifdef CONFIG_DEBUG + if (!tcb->stack_alloc_ptr) + { + sdbg("ERROR: Failed to allocate stack, size %d\n", stack_size); + } +#endif + } - /* The Arm7Tdmi uses a push-down stack: the stack grows - * toward loweraddresses in memory. The stack pointer - * register, points to the lowest, valid work address - * (the "top" of the stack). Items on the stack are - * referenced as positive word offsets from sp. - */ + /* Did we successfully allocate a stack? */ - top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4; + if (tcb->stack_alloc_ptr) + { + size_t top_of_stack; + size_t size_of_stack; - /* The Arm7Tdmi stack must be aligned at word (4 byte) - * boundaries. If necessary top_of_stack must be rounded - * down to the next boundary - */ + /* 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. + */ - top_of_stack &= ~3; - size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4; +#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_STACK) + memset(tcb->stack_alloc_ptr, 0xaa, stack_size); +#endif - /* Save the adjusted stack values in the struct tcb_s */ + /* The SH family uses a push-down stack: the stack grows + * toward loweraddresses in memory. The stack pointer + * register, points to the lowest, valid work address + * (the "top" of the stack). Items on the stack are + * referenced as positive word offsets from sp. + */ - tcb->adj_stack_ptr = (uint32_t*)top_of_stack; - tcb->adj_stack_size = size_of_stack; + top_of_stack = (uint32_t)tcb->stack_alloc_ptr + stack_size - 4; - up_ledon(LED_STACKCREATED); - return OK; - } + /* The SH stack must be aligned at word (4 byte) + * boundaries. If necessary top_of_stack must be rounded + * down to the next boundary + */ + + top_of_stack &= ~3; + size_of_stack = top_of_stack - (uint32_t)tcb->stack_alloc_ptr + 4; + + /* 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; + + up_ledon(LED_STACKCREATED); + return OK; + } return ERROR; } -- cgit v1.2.3