memmgr.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 xPortGetMinimumEverFreeHeapSize(void);
  7. void* malloc(size_t size) {
  8. return pvPortMalloc(size);
  9. }
  10. void free(void* ptr) {
  11. vPortFree(ptr);
  12. }
  13. void* realloc(void* ptr, size_t size) {
  14. if(size == 0) {
  15. vPortFree(ptr);
  16. return NULL;
  17. }
  18. void* p = pvPortMalloc(size);
  19. if(ptr != NULL) {
  20. memcpy(p, ptr, size);
  21. vPortFree(ptr);
  22. }
  23. return p;
  24. }
  25. void* calloc(size_t count, size_t size) {
  26. return pvPortMalloc(count * size);
  27. }
  28. char* strdup(const char* s) {
  29. const char* s_null = s;
  30. if(s_null == NULL) {
  31. return NULL;
  32. }
  33. size_t siz = strlen(s) + 1;
  34. char* y = pvPortMalloc(siz);
  35. memcpy(y, s, siz);
  36. return y;
  37. }
  38. size_t memmgr_get_free_heap(void) {
  39. return xPortGetFreeHeapSize();
  40. }
  41. size_t memmgr_get_minimum_free_heap(void) {
  42. return xPortGetMinimumEverFreeHeapSize();
  43. }
  44. void* __wrap__malloc_r(struct _reent* r, size_t size) {
  45. return pvPortMalloc(size);
  46. }
  47. void __wrap__free_r(struct _reent* r, void* ptr) {
  48. vPortFree(ptr);
  49. }
  50. void* __wrap__calloc_r(struct _reent* r, size_t count, size_t size) {
  51. return calloc(count, size);
  52. }
  53. void* __wrap__realloc_r(struct _reent* r, void* ptr, size_t size) {
  54. return realloc(ptr, size);
  55. }