OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.17.0/] [libgloss/] [or32/] [sbrk.c] - Diff between revs 158 and 180

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 158 Rev 180
Line 1... Line 1...
 
/* sbrk.c. Implementation of the _sbrk syscall for newlib
 
 
 
   Copyright (C) 2004, Jacob Bower
 
   Copyright (C) 2010, Embecosm Limited <info@embecosm.com>
 
 
 
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
 
 
 
   This file is part of Newlib.
 
 
 
   The original work by Jacob Bower is provided as-is without any kind of
 
   warranty. Use it at your own risk!
 
 
 
   All subsequent work is bound by version 3 of the GPL as follows.
 
 
 
   This program is free software; you can redistribute it and/or modify it
 
   under the terms of the GNU General Public License as published by the Free
 
   Software Foundation; either version 3 of the License, or (at your option)
 
   any later version.
 
 
 
   This program is distributed in the hope that it will be useful, but WITHOUT
 
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
   more details.
 
 
 
   You should have received a copy of the GNU General Public License along
 
   with this program.  If not, see <http:#www.gnu.org/licenses/>.             */
 
/* -------------------------------------------------------------------------- */
 
/* This program is commented throughout in a fashion suitable for processing
 
   with Doxygen.                                                              */
 
/* -------------------------------------------------------------------------- */
 
 
#include <errno.h>
#include <errno.h>
 
 
/*
/*! Reserved stack space inbytes. */
 * While the heap grows upwards, the stack grows downwards.
#define STACK_BUFFER  65536
 * Evnetually these two things may colide and sbrk()
 
 * won't even be able to return properly. To mitigate this
 
 * we reserve upto STACK_BUFFER _words_ at the top of memory.
 
 * Note this doesn't actually solve the problem, it just
 
 * provides an error margin. The real solution is to use
 
 * an OS with a proper virtual memory manager.
 
 */
 
#define STACK_BUFFER 16384
 
 
 
 
/*! Define NULL if not yet defined. */
#ifndef NULL
#ifndef NULL
#define NULL ((void *)0)
#define NULL ((void *)0)
#endif
#endif
 
 
 
 
/*
/* -------------------------------------------------------------------------- */
 * sbrk -- changes heap size size. Get nbytes more
/*!Extend the heap
 *         RAM. We just increment a pointer in what's
 
 *         left of memory on the board.
   We just increment a pointer in what's left of memory on the board.
 */
 
void *sbrk(int nbytes)
   While the heap grows upwards, the stack grows downwards.  Eventually these
 
   two things may colide and sbrk() won't even be able to return properly.
 
 
 
   To mitigate this we reserve upto STACK_BUFFER _words_ at the top of memory.
 
   Note this doesn't actually solve the problem, it just provides an error
 
   margin. The real solution is to use an OS with a proper virtual memory
 
   manager.
 
 
 
   Remember that this function is *not* reentrant, so no static state should
 
   be held.
 
 
 
   @todo  We break this rule with heap_ptr. This needs to be clean, so that a
 
          re-entrant call to sbrk (e.g. in an ISR) is certain to work.
 
 
 
   @param[in] nbytes  The number of bytes to be allocated.
 
 
 
   @return  The previous heap end on success, -1 on failure with an error
 
            code in errno.                                                    */
 
/* -------------------------------------------------------------------------- */
 
void *
 
_sbrk (int nbytes)
{
{
        /* symbols defined by linker map */
  /* Symbols defined by linker map */
        extern int _end;   // start of free memory
  extern int  end;              /* start of free memory */
        extern int _stack; // end of free memory
  extern int  stack;            /* end of free memory */
 
 
        static unsigned long *heap_ptr = NULL;
 
        void *base;
 
        int nwords = (nbytes + 3) / 4;
 
 
 
        if (heap_ptr == NULL)
 
                heap_ptr = (unsigned long *)&_end;
 
 
 
        if ( ((unsigned long *)&_stack - (heap_ptr + nwords)) > STACK_BUFFER ) {
 
                base = heap_ptr;
 
                heap_ptr += nwords;
 
 
 
                return (void *)base;
  /* The statically held previous end of the stack, with its initialization. */
        } else {
  static void *heap_ptr = (void *)&end;         /* Previous end */
                errno = ENOMEM;
 
//              write(1, "Failed to extend heap.", 23);
 
 
 
                return (void *)-1;
  if (((void *) &stack - (heap_ptr + nbytes)) > STACK_BUFFER )
 
    {
 
      void * base  = heap_ptr;
 
      heap_ptr    += nbytes;
 
 
 
      return  base;
        }
        }
 
  else
 
    {
 
      errno = ENOMEM;
 
      return  (void *) -1;
}
}
 
}       /* _sbrk () */
 
 
 No newline at end of file
 No newline at end of file

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.