summaryrefslogtreecommitdiff
path: root/examples/libc/memory.c
blob: 4a31b69017f7e09c6742dc745ccae4cc918da0d8 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2010-11-17     Bernard      first version
 */

#include <stdio.h>
#include <stdlib.h>
#include <finsh.h>
#include <sys/errno.h>

static int errors = 0;
static void merror(const char *msg)
{
    ++errors;
    printf("Error: %s\n", msg);
}

int libc_mem(void)
{
    void *p;
    int save;

    errno = 0;

    p = malloc(-1);
    save = errno;

    if (p != NULL)
        merror("malloc (-1) succeeded.");

    if (p == NULL && save != ENOMEM)
        merror("errno is not set correctly");

    p = malloc(10);
    if (p == NULL)
        merror("malloc (10) failed.");

    /* realloc (p, 0) == free (p).  */
    p = realloc(p, 0);
    if (p != NULL)
        merror("realloc (p, 0) failed.");

    p = malloc(0);
    if (p == NULL)
    {
        printf("malloc(0) returns NULL\n");
    }

    p = realloc(p, 0);
    if (p != NULL)
        merror("realloc (p, 0) failed.");

    return errors != 0;
}
FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc);