memmgr.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "memmgr.h"
  2. #include "common_defines.h"
  3. #include <string.h>
  4. extern void* pvPortMalloc(size_t xSize);
  5. extern void vPortFree(void* pv);
  6. extern size_t xPortGetFreeHeapSize(void);
  7. extern size_t xPortGetTotalHeapSize(void);
  8. extern size_t xPortGetMinimumEverFreeHeapSize(void);
  9. void* malloc(size_t size) {
  10. return pvPortMalloc(size);
  11. }
  12. void free(void* ptr) {
  13. vPortFree(ptr);
  14. }
  15. void* realloc(void* ptr, size_t size) {
  16. if(size == 0) {
  17. vPortFree(ptr);
  18. return NULL;
  19. }
  20. void* p = pvPortMalloc(size);
  21. if(ptr != NULL) {
  22. memcpy(p, ptr, size);
  23. vPortFree(ptr);
  24. }
  25. return p;
  26. }
  27. void* calloc(size_t count, size_t size) {
  28. return pvPortMalloc(count * size);
  29. }
  30. char* strdup(const char* s) {
  31. const char* s_null = s;
  32. if(s_null == NULL) {
  33. return NULL;
  34. }
  35. size_t siz = strlen(s) + 1;
  36. char* y = pvPortMalloc(siz);
  37. memcpy(y, s, siz);
  38. return y;
  39. }
  40. size_t memmgr_get_free_heap(void) {
  41. return xPortGetFreeHeapSize();
  42. }
  43. size_t memmgr_get_total_heap(void) {
  44. return xPortGetTotalHeapSize();
  45. }
  46. size_t memmgr_get_minimum_free_heap(void) {
  47. return xPortGetMinimumEverFreeHeapSize();
  48. }
  49. void* __wrap__malloc_r(struct _reent* r, size_t size) {
  50. UNUSED(r);
  51. return pvPortMalloc(size);
  52. }
  53. void __wrap__free_r(struct _reent* r, void* ptr) {
  54. UNUSED(r);
  55. vPortFree(ptr);
  56. }
  57. void* __wrap__calloc_r(struct _reent* r, size_t count, size_t size) {
  58. UNUSED(r);
  59. return calloc(count, size);
  60. }
  61. void* __wrap__realloc_r(struct _reent* r, void* ptr, size_t size) {
  62. UNUSED(r);
  63. return realloc(ptr, size);
  64. }