source: trunk/libs/newlib/src/libgloss/tic6x/sbrk.c @ 684

Last change on this file since 684 was 444, checked in by satin@…, 7 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.8 KB
Line 
1/* sbrk.c -- Implementation of the low-level sbrk() routine
2 *
3 * Copyright (c) 2004 National Semiconductor Corporation
4 *
5 * The authors hereby grant permission to use, copy, modify, distribute,
6 * and license this software and its documentation for any purpose, provided
7 * that existing copyright notices are retained in all copies and that this
8 * notice is included verbatim in any distributions. No written agreement,
9 * license, or royalty fee is required for any of the authorized uses.
10 * Modifications to this software may be copyrighted by their authors
11 * and need not follow the licensing terms described here, provided that
12 * the new terms are clearly indicated on the first page of each file where
13 * they apply.
14 */
15
16#include <errno.h>
17#include <stddef.h> /* where ptrdiff_t is defined */
18#include <stdlib.h>
19
20/* Start of the heap.  */
21extern const char _HEAP_START __attribute__((section(".heap")));
22/* End of the heap (maximum value of heap_ptr).  */
23extern const char _HEAP_MAX __attribute__((section(".heap")));
24
25/* Extend heap space by size bytes.
26   Return start of new space allocated, or -1 for errors
27   Error cases:
28    1. Allocation is not within heap range */
29
30void * sbrk (ptrdiff_t size)
31{
32  /*
33  * The following two memory locations should be defined in the linker script file
34  */
35
36  static const char * heap_ptr;  /* pointer to head of heap */
37  const char * old_heap_ptr;
38  static unsigned char init_sbrk = 0;
39
40  /* heap_ptr is initialized to HEAP_START */
41  if (init_sbrk == 0) 
42  {
43    heap_ptr = &_HEAP_START;
44    init_sbrk = 1;
45  }
46
47  old_heap_ptr = heap_ptr;
48
49  if ((heap_ptr + size) > &_HEAP_MAX)
50  { 
51    /* top of heap is bigger than _HEAP_MAX */
52    errno = ENOMEM;
53    return (void *) -1;
54  }
55
56  /* success: update heap_ptr and return previous value */
57  heap_ptr += size;
58  return (void *)old_heap_ptr;
59}
Note: See TracBrowser for help on using the repository browser.