memmgr.c 1.3 KB

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