memmgr.c 1.5 KB

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