syscalls.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 abort()
  24. {
  25. do
  26. {
  27. __asm__ ("break 0,0");
  28. *((int*) 0) = 0;
  29. } while(true);
  30. }
  31. void* IRAM_ATTR _malloc_r(struct _reent *r, size_t size)
  32. {
  33. return pvPortMalloc(size);
  34. }
  35. void IRAM_ATTR _free_r(struct _reent *r, void* ptr)
  36. {
  37. vPortFree(ptr);
  38. }
  39. void* IRAM_ATTR _realloc_r(struct _reent *r, void* ptr, size_t size)
  40. {
  41. void* new_chunk;
  42. if (size == 0) {
  43. if (ptr) {
  44. vPortFree(ptr);
  45. }
  46. return NULL;
  47. }
  48. new_chunk = pvPortMalloc(size);
  49. if (new_chunk && ptr) {
  50. memcpy(new_chunk, ptr, size);
  51. vPortFree(ptr);
  52. }
  53. // realloc behaviour: don't free original chunk if alloc failed
  54. return new_chunk;
  55. }
  56. void* IRAM_ATTR _calloc_r(struct _reent *r, size_t count, size_t size)
  57. {
  58. void* result = pvPortMalloc(count * size);
  59. if (result)
  60. {
  61. memset(result, 0, count * size);
  62. }
  63. return result;
  64. }
  65. int _system_r(struct _reent *r, const char *str)
  66. {
  67. __errno_r(r) = ENOSYS;
  68. return -1;
  69. }
  70. void _raise_r(struct _reent *r)
  71. {
  72. abort();
  73. }
  74. void* _sbrk_r(struct _reent *r, ptrdiff_t sz)
  75. {
  76. abort();
  77. }
  78. int _getpid_r(struct _reent *r)
  79. {
  80. __errno_r(r) = ENOSYS;
  81. return -1;
  82. }
  83. int _kill_r(struct _reent *r, int pid, int sig)
  84. {
  85. __errno_r(r) = ENOSYS;
  86. return -1;
  87. }
  88. void _exit(int __status)
  89. {
  90. abort();
  91. }