memmgr.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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;
  19. p = pvPortMalloc(size);
  20. if(p) {
  21. // TODO implement secure realloc
  22. // insecure, but will do job in our case
  23. if(ptr != NULL) {
  24. memcpy(p, ptr, size);
  25. vPortFree(ptr);
  26. }
  27. }
  28. return p;
  29. }
  30. void* calloc(size_t count, size_t size) {
  31. void* ptr = pvPortMalloc(count * size);
  32. if(ptr) {
  33. // zero the memory
  34. memset(ptr, 0, count * size);
  35. }
  36. return ptr;
  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. }