valloc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. #include "valloc.h"
  7. #if defined(RyanJsonTestPlatformQemu)
  8. #include "FreeRTOS.h"
  9. #endif
  10. #define HEADER_SIZE sizeof(int)
  11. #define MALLOC_HEADER_SIZE 0
  12. static int count = 0;
  13. static int use = 0;
  14. static void *vallocPlatformMalloc(size_t size)
  15. {
  16. #if defined(RyanJsonTestPlatformQemu)
  17. return pvPortMalloc(size);
  18. #else
  19. return malloc(size);
  20. #endif
  21. }
  22. static void vallocPlatformFree(void *ptr)
  23. {
  24. #if defined(RyanJsonTestPlatformQemu)
  25. vPortFree(ptr);
  26. #else
  27. free(ptr);
  28. #endif
  29. }
  30. void *v_malloc(size_t size)
  31. {
  32. if (size == 0) { return NULL; }
  33. void *p = vallocPlatformMalloc(size + HEADER_SIZE);
  34. if (!p) { return NULL; }
  35. *(int *)p = (int)size;
  36. count++;
  37. use += (int)size + MALLOC_HEADER_SIZE;
  38. return (char *)p + HEADER_SIZE;
  39. }
  40. void *v_calloc(size_t num, size_t size)
  41. {
  42. size_t total = num * size;
  43. void *p = v_malloc(total);
  44. if (p) { memset(p, 0, total); }
  45. return p;
  46. }
  47. void v_free(void *block)
  48. {
  49. if (!block) { return; }
  50. void *p = (char *)block - HEADER_SIZE;
  51. int size = *(int *)p;
  52. count--;
  53. use -= size + MALLOC_HEADER_SIZE;
  54. vallocPlatformFree(p);
  55. }
  56. void *v_realloc(void *block, size_t size)
  57. {
  58. if (size == 0)
  59. {
  60. v_free(block);
  61. return NULL;
  62. }
  63. int old_size = 0;
  64. void *raw = NULL;
  65. if (block)
  66. {
  67. raw = (char *)block - HEADER_SIZE;
  68. old_size = *(int *)raw;
  69. }
  70. void *p = NULL;
  71. #if defined(RyanJsonTestPlatformQemu)
  72. p = vallocPlatformMalloc(size + HEADER_SIZE);
  73. if (!p) { return NULL; }
  74. if (block && old_size > 0)
  75. {
  76. size_t copySize = (size < (size_t)old_size) ? size : (size_t)old_size;
  77. memcpy((char *)p + HEADER_SIZE, block, copySize);
  78. vallocPlatformFree(raw);
  79. }
  80. #else
  81. p = realloc(raw, size + HEADER_SIZE);
  82. if (!p) { return NULL; }
  83. #endif
  84. *(int *)p = (int)size;
  85. if (!block) { count++; }
  86. use += (int)(size - old_size);
  87. return (char *)p + HEADER_SIZE;
  88. }
  89. int v_mcheck(int *dstCount, int *dstUse)
  90. {
  91. if (dstCount) { *dstCount = count; }
  92. if (dstUse) { *dstUse = use; }
  93. return 0;
  94. }
  95. int32_t vallocGetArea(void)
  96. {
  97. int32_t area2 = 0, use2 = 0;
  98. v_mcheck(&area2, &use2);
  99. return area2;
  100. }
  101. int32_t vallocGetUse(void)
  102. {
  103. int32_t area2 = 0, use2 = 0;
  104. v_mcheck(&area2, &use2);
  105. return use2;
  106. }
  107. void displayMem(void)
  108. {
  109. int32_t area2 = 0, use2 = 0;
  110. v_mcheck(&area2, &use2);
  111. printf("|||----------->>> area = %d, size = %d\r\n", area2, use2);
  112. }