blob: 77a288a7d7dc4a2899f75fae9339c56ea5a7ed91 [file] [log] [blame]
Imre Kise6d73412021-10-18 14:01:47 +02001// SPDX-License-Identifier: BSD-3-Clause
2/*
3 * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
4 */
5
6#include "compiler.h"
7#include "optee_sp_user_defines.h"
8#include <errno.h>
9#include <stdint.h>
10#include <unistd.h>
11
12/* Allocating heap area */
13#ifndef OPTEE_SP_HEAP_SIZE
14#error "OPTEE_SP_HEAP_SIZE is not defined in SP"
15#endif
16
17static uint8_t sp_heap[OPTEE_SP_HEAP_SIZE] __aligned(16);
18static uint8_t *program_break = sp_heap;
19
20/**
21 * Basic sbrk implementation which increases the program break through the
22 * sp_heap buffer.
23 */
24void *_sbrk(ptrdiff_t incr)
25{
26 uint8_t *previous_break = program_break;
27 uint8_t *new_break = program_break + incr;
28
29 if ((new_break < sp_heap) || (new_break > (sp_heap + sizeof(sp_heap)))) {
30 errno = ENOMEM;
31 return (void *)(uintptr_t) -1;
32 }
33
34 program_break += incr;
35
36 return (void *) previous_break;
37}