rt-thread/examples/libc/memory.c

61 lines
1.1 KiB
C
Raw Normal View History

2013-01-08 21:05:02 +08:00
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
2013-01-08 21:05:02 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2010-11-17 Bernard first version
2013-01-08 21:05:02 +08:00
*/
2013-01-08 21:05:02 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <finsh.h>
2021-05-22 02:50:35 +08:00
#include <sys/errno.h>
2013-01-08 21:05:02 +08:00
static int errors = 0;
static void merror(const char *msg)
{
2021-03-07 08:34:04 +08:00
++errors;
printf("Error: %s\n", msg);
2013-01-08 21:05:02 +08:00
}
int libc_mem(void)
{
2021-03-07 08:34:04 +08:00
void *p;
int save;
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
errno = 0;
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
p = malloc(-1);
save = errno;
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
if (p != NULL)
merror("malloc (-1) succeeded.");
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
if (p == NULL && save != ENOMEM)
merror("errno is not set correctly");
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
p = malloc(10);
if (p == NULL)
merror("malloc (10) failed.");
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
/* realloc (p, 0) == free (p). */
p = realloc(p, 0);
if (p != NULL)
merror("realloc (p, 0) failed.");
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
p = malloc(0);
if (p == NULL)
{
printf("malloc(0) returns NULL\n");
}
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
p = realloc(p, 0);
if (p != NULL)
merror("realloc (p, 0) failed.");
2013-01-08 21:05:02 +08:00
2021-03-07 08:34:04 +08:00
return errors != 0;
2013-01-08 21:05:02 +08:00
}
FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc);