memmgr.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. char* strdup(const char* s) {
  39. const char* s_null = s;
  40. if(s_null == NULL) {
  41. return NULL;
  42. }
  43. size_t siz = strlen(s) + 1;
  44. char* y = malloc(siz);
  45. if(y != NULL) {
  46. memcpy(y, s, siz);
  47. } else {
  48. return NULL;
  49. }
  50. return y;
  51. }
  52. size_t memmgr_get_free_heap(void) {
  53. return xPortGetFreeHeapSize();
  54. }
  55. size_t memmgr_get_minimum_free_heap(void) {
  56. return xPortGetMinimumEverFreeHeapSize();
  57. }