summaryrefslogtreecommitdiff
path: root/bsp/hpmicro/libraries/hpm_sdk/utils/hpm_sbrk.c
blob: 8c4a9590fb6f7e1728994cf0154f040cd79a1d7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
 * Copyright (c) 2021 hpmicro
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#ifdef __GNUC__
#include <stdio.h>
#include <errno.h>
#include "hpm_common.h"

void *_sbrk(int incr)
{
    extern char __heap_start__, __heap_end__;
    static char *heap_end;
    char *prev_heap_end;
    void *ret;

    if (heap_end == NULL)
    {
        heap_end = &__heap_start__;
    }

    prev_heap_end = heap_end;

    if ((unsigned int)heap_end + (unsigned int)incr > (unsigned int)(&__heap_end__))
    {
        errno = ENOMEM;

        ret = (void *)-1;
    }
    else
    {
        heap_end = (char *)((unsigned int)heap_end + (unsigned int)incr);

        ret = (void *)prev_heap_end;
    }

    return ret;
}
#endif