1 | /* sbrk.c -- allocate space on heap on OpenRISC 1000. |
---|
2 | * |
---|
3 | * Copyright (c) 2014 Authors |
---|
4 | * |
---|
5 | * Contributor Stefan Wallentowitz <stefan.wallentowitz@tum.de> |
---|
6 | * |
---|
7 | * The authors hereby grant permission to use, copy, modify, distribute, |
---|
8 | * and license this software and its documentation for any purpose, provided |
---|
9 | * that existing copyright notices are retained in all copies and that this |
---|
10 | * notice is included verbatim in any distributions. No written agreement, |
---|
11 | * license, or royalty fee is required for any of the authorized uses. |
---|
12 | * Modifications to this software may be copyrighted by their authors |
---|
13 | * and need not follow the licensing terms described here, provided that |
---|
14 | * the new terms are clearly indicated on the first page of each file where |
---|
15 | * they apply. |
---|
16 | */ |
---|
17 | |
---|
18 | #include <reent.h> |
---|
19 | |
---|
20 | #include "include/or1k-support.h" |
---|
21 | |
---|
22 | extern uint32_t end; /* Set by linker. */ |
---|
23 | uint32_t _or1k_heap_start = &end; |
---|
24 | uint32_t _or1k_heap_end; |
---|
25 | |
---|
26 | void * |
---|
27 | _sbrk_r (struct _reent * reent, ptrdiff_t incr) |
---|
28 | { |
---|
29 | uint32_t prev_heap_end; |
---|
30 | |
---|
31 | // This needs to be atomic |
---|
32 | |
---|
33 | // Disable interrupts on this core |
---|
34 | uint32_t sr_iee = or1k_interrupts_disable(); |
---|
35 | uint32_t sr_tee = or1k_timer_disable(); |
---|
36 | |
---|
37 | // Initialize heap end to end if not initialized before |
---|
38 | or1k_sync_cas((void*) &_or1k_heap_end, 0, (uint32_t) _or1k_heap_start); |
---|
39 | |
---|
40 | do { |
---|
41 | // Read previous heap end |
---|
42 | prev_heap_end = _or1k_heap_end; |
---|
43 | // and try to set it to the new value as long as it has changed |
---|
44 | } while (or1k_sync_cas((void*) &_or1k_heap_end, |
---|
45 | (uint32_t) prev_heap_end, |
---|
46 | (uint32_t) (prev_heap_end + incr)) != (uint32_t) prev_heap_end); |
---|
47 | |
---|
48 | // Restore interrupts on this core |
---|
49 | or1k_timer_restore(sr_tee); |
---|
50 | or1k_interrupts_restore(sr_iee); |
---|
51 | |
---|
52 | return (void*) prev_heap_end; |
---|
53 | } |
---|