memory.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2010-11-17 Bernard first version
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/errno.h>
  13. static int errors = 0;
  14. static void merror(const char *msg)
  15. {
  16. ++errors;
  17. printf("Error: %s\n", msg);
  18. }
  19. int libc_mem(void)
  20. {
  21. void *p;
  22. int save;
  23. errno = 0;
  24. p = malloc(-1);
  25. save = errno;
  26. if (p != NULL)
  27. merror("malloc (-1) succeeded.");
  28. if (p == NULL && save != ENOMEM)
  29. merror("errno is not set correctly");
  30. p = malloc(10);
  31. if (p == NULL)
  32. merror("malloc (10) failed.");
  33. /* realloc (p, 0) == free (p). */
  34. p = realloc(p, 0);
  35. if (p != NULL)
  36. merror("realloc (p, 0) failed.");
  37. p = malloc(0);
  38. if (p == NULL)
  39. {
  40. printf("malloc(0) returns NULL\n");
  41. }
  42. p = realloc(p, 0);
  43. if (p != NULL)
  44. merror("realloc (p, 0) failed.");
  45. return errors != 0;
  46. }