syscall_mem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2014-08-03 bernard Add file header
  9. * 2021-11-13 Meco Man implement no-heap warning
  10. */
  11. #include <rtthread.h>
  12. #include <stddef.h>
  13. #ifndef RT_USING_HEAP
  14. #define DBG_TAG "armlibc.syscall.mem"
  15. #define DBG_LVL DBG_INFO
  16. #include <rtdbg.h>
  17. #define _NO_HEAP_ERROR() do{LOG_E("Please enable RT_USING_HEAP");\
  18. RT_ASSERT(0);\
  19. }while(0)
  20. #endif /* RT_USING_HEAP */
  21. #ifdef __CC_ARM
  22. /* avoid the heap and heap-using library functions supplied by arm */
  23. #pragma import(__use_no_heap)
  24. #endif /* __CC_ARM */
  25. /**
  26. * @brief Allocate memory block
  27. *
  28. * Allocates a block of size bytes of memory, returning a pointer to the
  29. * beginning of the block. The content of the newly allocated block of
  30. * memory is not initialized, remaining with indeterminate values.
  31. *
  32. * @param[in] n the size of the memory block, in bytes.
  33. *
  34. * @return On success, a pointer to the memory block allocated by the function.
  35. * If the system is configured without heap (RT_USING_HEAP is not defined),
  36. * the function will assert and return RT_NULL.
  37. *
  38. * @note The returned pointer is always suitably aligned for any built-in type.
  39. */
  40. void *malloc(size_t n)
  41. {
  42. #ifdef RT_USING_HEAP
  43. return rt_malloc(n);
  44. #else
  45. _NO_HEAP_ERROR();
  46. return RT_NULL;
  47. #endif
  48. }
  49. RTM_EXPORT(malloc);
  50. /**
  51. * @brief Reallocate memory block
  52. *
  53. * Changes the size of the memory block pointed to by rmem.
  54. * The function may move the memory block to a new location
  55. * (whose address is returned by the function).
  56. * The content of the memory block is preserved up to the
  57. * lesser of the new and old sizes, even if the block is
  58. * moved to a new location. If the new size is larger,
  59. * the value of the newly allocated portion is indeterminate.
  60. *
  61. * @param[in,out] rmem pointer to a memory block previously allocated with
  62. * malloc, calloc or realloc to be reallocated.
  63. * If this is RT_NULL, a new block is allocated and
  64. * a pointer to it is returned by the function.
  65. * @param[in] newsize new size for the memory block, in bytes.
  66. *
  67. * @return A pointer to the reallocated memory block, which may be either
  68. * the same as the rmem pointer or a new location.
  69. * If the system is configured without heap (RT_USING_HEAP is not defined),
  70. * the function will assert and return RT_NULL.
  71. */
  72. void *realloc(void *rmem, size_t newsize)
  73. {
  74. #ifdef RT_USING_HEAP
  75. return rt_realloc(rmem, newsize);
  76. #else
  77. _NO_HEAP_ERROR();
  78. return RT_NULL;
  79. #endif
  80. }
  81. RTM_EXPORT(realloc);
  82. /**
  83. * @brief Allocate and zero-initialize array
  84. *
  85. * Allocates a block of memory for an array of nelem elements, each of them
  86. * elsize bytes long, and initializes all its bits to zero.
  87. * The effective result is the allocation of a zero-initialized memory block
  88. * of (nelem*elsize) bytes.
  89. *
  90. * @param[in] nelem number of elements to allocate.
  91. * @param[in] elsize size of each element.
  92. *
  93. * @return On success, a pointer to the memory block allocated by the function.
  94. * If the system is configured without heap (RT_USING_HEAP is not defined),
  95. * the function will assert and return RT_NULL.
  96. */
  97. void *calloc(size_t nelem, size_t elsize)
  98. {
  99. #ifdef RT_USING_HEAP
  100. return rt_calloc(nelem, elsize);
  101. #else
  102. _NO_HEAP_ERROR();
  103. return RT_NULL;
  104. #endif
  105. }
  106. RTM_EXPORT(calloc);
  107. /**
  108. * @brief Deallocate memory block
  109. *
  110. * A block of memory previously allocated by a call to malloc, calloc or realloc
  111. * is deallocated, making it available again for further allocations.
  112. *
  113. * @param[in] rmem pointer to a memory block previously allocated with malloc,
  114. * calloc or realloc to be deallocated. If a null pointer is
  115. * passed as argument, no action occurs.
  116. *
  117. * @note If the system is configured without heap (RT_USING_HEAP is not defined),
  118. * the function will assert.
  119. */
  120. void free(void *rmem)
  121. {
  122. #ifdef RT_USING_HEAP
  123. rt_free(rmem);
  124. #else
  125. _NO_HEAP_ERROR();
  126. #endif
  127. }
  128. RTM_EXPORT(free);