syscalls.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <sys/types.h>
  17. #include <unistd.h>
  18. #include <errno.h>
  19. #include <sys/reent.h>
  20. #include <stdlib.h>
  21. #include "esp_attr.h"
  22. #include "freertos/FreeRTOS.h"
  23. void* IRAM_ATTR _malloc_r(struct _reent *r, size_t size)
  24. {
  25. return pvPortMalloc(size);
  26. }
  27. void IRAM_ATTR _free_r(struct _reent *r, void* ptr)
  28. {
  29. vPortFree(ptr);
  30. }
  31. void* IRAM_ATTR _realloc_r(struct _reent *r, void* ptr, size_t size)
  32. {
  33. void* new_chunk;
  34. if (size == 0) {
  35. if (ptr) {
  36. vPortFree(ptr);
  37. }
  38. return NULL;
  39. }
  40. new_chunk = pvPortMalloc(size);
  41. if (new_chunk && ptr) {
  42. memcpy(new_chunk, ptr, size);
  43. vPortFree(ptr);
  44. }
  45. // realloc behaviour: don't free original chunk if alloc failed
  46. return new_chunk;
  47. }
  48. void* IRAM_ATTR _calloc_r(struct _reent *r, size_t count, size_t size)
  49. {
  50. void* result = pvPortMalloc(count * size);
  51. if (result)
  52. {
  53. memset(result, 0, count * size);
  54. }
  55. return result;
  56. }
  57. int _system_r(struct _reent *r, const char *str)
  58. {
  59. __errno_r(r) = ENOSYS;
  60. return -1;
  61. }
  62. void _raise_r(struct _reent *r)
  63. {
  64. abort();
  65. }
  66. void* _sbrk_r(struct _reent *r, ptrdiff_t sz)
  67. {
  68. abort();
  69. }
  70. int _getpid_r(struct _reent *r)
  71. {
  72. __errno_r(r) = ENOSYS;
  73. return -1;
  74. }
  75. int _kill_r(struct _reent *r, int pid, int sig)
  76. {
  77. __errno_r(r) = ENOSYS;
  78. return -1;
  79. }
  80. void _exit(int __status)
  81. {
  82. abort();
  83. }