1 /* 2 * memory.c 3 * 4 * Created on: 2010-11-17 5 * Author: bernard 6 */ 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <finsh.h> 10 #include <errno.h> 11 12 static int errors = 0; merror(const char * msg)13static void merror(const char *msg) 14 { 15 ++errors; 16 printf("Error: %s\n", msg); 17 } 18 libc_mem(void)19int libc_mem(void) 20 { 21 void *p; 22 int save; 23 24 errno = 0; 25 26 p = malloc(-1); 27 save = errno; 28 29 if (p != NULL) 30 merror("malloc (-1) succeeded."); 31 32 if (p == NULL && save != ENOMEM) 33 merror("errno is not set correctly"); 34 35 p = malloc(10); 36 if (p == NULL) 37 merror("malloc (10) failed."); 38 39 /* realloc (p, 0) == free (p). */ 40 p = realloc(p, 0); 41 if (p != NULL) 42 merror("realloc (p, 0) failed."); 43 44 p = malloc(0); 45 if (p == NULL) 46 { 47 printf("malloc(0) returns NULL\n"); 48 } 49 50 p = realloc(p, 0); 51 if (p != NULL) 52 merror("realloc (p, 0) failed."); 53 54 return errors != 0; 55 } 56 FINSH_FUNCTION_EXPORT(libc_mem, memory test for libc); 57