memory.c 932 字节
Newer Older
B
bernard.xiong@gmail.com 已提交
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
/*
 * memory.c
 *
 *  Created on: 2010-11-17
 *      Author: bernard
 */
#include <stdio.h>
#include <stdlib.h>
#include <finsh.h>
#include <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)
46 47 48
	{
		printf("malloc(0) returns NULL\n");
	}
B
bernard.xiong@gmail.com 已提交
49 50 51 52 53 54 55 56

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

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