2013-01-08 21:05:02 +08:00
|
|
|
/*
|
2021-03-08 07:18:51 +08:00
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
2013-01-08 21:05:02 +08:00
|
|
|
*
|
2021-03-08 07:18:51 +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
|
|
|
*/
|
2021-03-08 07:18:51 +08:00
|
|
|
|
2013-01-08 21:05:02 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <finsh.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
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);
|