memory.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*!
  2. \ingroup Memory
  3. \brief This function calls the custom malloc function, if one has been
  4. defined, or simply calls the default C malloc function if no custom
  5. function exists. It is not called directly by wolfSSL, but instead
  6. generally called by using XMALLOC, which may be replaced by
  7. wolfSSL_Malloc during preprocessing.
  8. \return Success On successfully allocating the desired memory,
  9. returns a void* to that location
  10. \return NULL Returned when there is a failure to allocate memory
  11. \param size size, in bytes, of the memory to allocate
  12. _Example_
  13. \code
  14. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  15. \endcode
  16. \sa wolfSSL_Free
  17. \sa wolfSSL_Realloc
  18. \sa XMALLOC
  19. \sa XFREE
  20. \sa XREALLOC
  21. */
  22. WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type, const char* func, unsigned int line);
  23. /*!
  24. \ingroup Memory
  25. \brief This function calls a custom free function, if one has been
  26. defined, or simply calls the default C free function if no custom
  27. function exists. It is not called directly by wolfSSL, but instead
  28. generally called by using XFREE, which may be replaced by wolfSSL_Free
  29. during preprocessing.
  30. \return none No returns.
  31. \param ptr pointer to the memory to free
  32. _Example_
  33. \code
  34. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  35. // process data as desired
  36. ...
  37. if(tenInts) {
  38. wolfSSL_Free(tenInts);
  39. }
  40. \endcode
  41. \sa wolfSSL_Malloc
  42. \sa wolfSSL_Realloc
  43. \sa XMALLOC
  44. \sa XFREE
  45. \sa XREALLOC
  46. */
  47. WOLFSSL_API void wolfSSL_Free(void *ptr, void* heap, int type, const char* func, unsigned int line);
  48. /*!
  49. \ingroup Memory
  50. \brief This function calls a custom realloc function, if one has been
  51. defined, or simply calls the default C realloc function if no custom
  52. function exists. It is not called directly by wolfSSL, but instead
  53. generally called by using XREALLOC, which may be replaced by
  54. wolfSSL_Realloc during preprocessing.
  55. \return Success On successfully reallocating the desired memory,
  56. returns a void* to that location
  57. \return NULL Returned when there is a failure to reallocate memory
  58. \param ptr pointer to the memory to the memory to reallocate
  59. \param size desired size after reallocation
  60. _Example_
  61. \code
  62. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  63. int* twentyInts = (int*)realloc(tenInts, sizeof(tenInts)*2);
  64. \endcode
  65. \sa wolfSSL_Malloc
  66. \sa wolfSSL_Free
  67. \sa XMALLOC
  68. \sa XFREE
  69. \sa XREALLOC
  70. */
  71. WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type, const char* func, unsigned int line);
  72. /*!
  73. \ingroup Memory
  74. \brief This function is similar to malloc(), but calls the memory
  75. allocation function which wolfSSL has been configured to use. By default,
  76. wolfSSL uses malloc(). This can be changed using the wolfSSL memory
  77. abstraction layer - see wolfSSL_SetAllocators().
  78. \return pointer If successful, this function returns a pointer to
  79. allocated memory.
  80. \return error If there is an error, NULL will be returned.
  81. \return other Specific return values may be dependent on the underlying
  82. memory allocation function being used (if not using the default malloc()).
  83. \param size number of bytes to allocate.
  84. _Example_
  85. \code
  86. char* buffer;
  87. buffer = (char*) wolfSSL_Malloc(20);
  88. if (buffer == NULL) {
  89. // failed to allocate memory
  90. }
  91. \endcode
  92. \sa wolfSSL_Free
  93. \sa wolfSSL_Realloc
  94. \sa wolfSSL_SetAllocators
  95. */
  96. WOLFSSL_API void* wolfSSL_Malloc(size_t size, void* heap, int type);
  97. /*!
  98. \ingroup Memory
  99. \brief This function is similar to realloc(), but calls the memory
  100. re-allocation function which wolfSSL has been configured to use.
  101. By default, wolfSSL uses realloc(). This can be changed using the
  102. wolfSSL memory abstraction layer - see wolfSSL_SetAllocators().
  103. \return pointer If successful, this function returns a pointer to
  104. re-allocated memory. This may be the same pointer as ptr, or a
  105. new pointer location.
  106. \return Null If there is an error, NULL will be returned.
  107. \return other Specific return values may be dependent on the
  108. underlying memory re-allocation function being used
  109. (if not using the default realloc()).
  110. \param ptr pointer to the previously-allocated memory, to be reallocated.
  111. \param size number of bytes to allocate.
  112. _Example_
  113. \code
  114. char* buffer;
  115. buffer = (char*) wolfSSL_Realloc(30);
  116. if (buffer == NULL) {
  117. // failed to re-allocate memory
  118. }
  119. \endcode
  120. \sa wolfSSL_Free
  121. \sa wolfSSL_Malloc
  122. \sa wolfSSL_SetAllocators
  123. */
  124. WOLFSSL_API void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
  125. /*!
  126. \ingroup Memory
  127. \brief This function is similar to free(), but calls the memory free
  128. function which wolfSSL has been configured to use. By default, wolfSSL
  129. uses free(). This can be changed using the wolfSSL memory abstraction
  130. layer - see wolfSSL_SetAllocators().
  131. \return none No returns.
  132. \param ptr pointer to the memory to be freed.
  133. _Example_
  134. \code
  135. char* buffer;
  136. ...
  137. wolfSSL_Free(buffer);
  138. \endcode
  139. \sa wolfSSL_Alloc
  140. \sa wolfSSL_Realloc
  141. \sa wolfSSL_SetAllocators
  142. */
  143. WOLFSSL_API void wolfSSL_Free(void *ptr, const char* func, unsigned int line);
  144. /*!
  145. \ingroup Memory
  146. \brief This function registers the allocation functions used by wolfSSL.
  147. By default, if the system supports it, malloc/free and realloc are used.
  148. Using this function allows the user at runtime to install their own
  149. memory handlers.
  150. \return Success If successful this function will return 0.
  151. \return BAD_FUNC_ARG is the error that will be returned if a
  152. function pointer is not provided.
  153. \param malloc_function memory allocation function for wolfSSL to use.
  154. Function signature must match wolfSSL_Malloc_cb prototype, above.
  155. \param free_function memory free function for wolfSSL to use. Function
  156. signature must match wolfSSL_Free_cb prototype, above.
  157. \param realloc_function memory re-allocation function for wolfSSL to use.
  158. Function signature must match wolfSSL_Realloc_cb prototype, above.
  159. _Example_
  160. \code
  161. int ret = 0;
  162. // Memory function prototypes
  163. void* MyMalloc(size_t size);
  164. void MyFree(void* ptr);
  165. void* MyRealloc(void* ptr, size_t size);
  166. // Register custom memory functions with wolfSSL
  167. ret = wolfSSL_SetAllocators(MyMalloc, MyFree, MyRealloc);
  168. if (ret != 0) {
  169. // failed to set memory functions
  170. }
  171. void* MyMalloc(size_t size)
  172. {
  173. // custom malloc function
  174. }
  175. void MyFree(void* ptr)
  176. {
  177. // custom free function
  178. }
  179. void* MyRealloc(void* ptr, size_t size)
  180. {
  181. // custom realloc function
  182. }
  183. \endcode
  184. \sa none
  185. */
  186. WOLFSSL_API int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
  187. wolfSSL_Free_cb,
  188. wolfSSL_Realloc_cb);
  189. /*!
  190. \ingroup Memory
  191. \brief This function is available when static memory feature is used
  192. (--enable-staticmemory). It gives the optimum buffer size for memory
  193. “buckets”. This allows for a way to compute buffer size so that no
  194. extra unused memory is left at the end after it has been partitioned.
  195. The returned value, if positive, is the computed buffer size to use.
  196. \return Success On successfully completing buffer size calculations a
  197. positive value is returned. This returned value is for optimum buffer size.
  198. \return Failure All negative values are considered to be error cases.
  199. \param buffer pointer to buffer
  200. \param size size of buffer
  201. \param type desired type of memory ie WOLFMEM_GENERAL or WOLFMEM_IO_POOL
  202. _Example_
  203. \code
  204. byte buffer[1000];
  205. word32 size = sizeof(buffer);
  206. int optimum;
  207. optimum = wolfSSL_StaticBufferSz(buffer, size, WOLFMEM_GENERAL);
  208. if (optimum < 0) { //handle error case }
  209. printf(“The optimum buffer size to make use of all memory is %d\n”,
  210. optimum);
  211. ...
  212. \endcode
  213. \sa wolfSSL_Malloc
  214. \sa wolfSSL_Free
  215. */
  216. WOLFSSL_API int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
  217. /*!
  218. \ingroup Memory
  219. \brief This function is available when static memory feature is used
  220. (--enable-staticmemory). It gives the size of padding needed for each
  221. partition of memory. This padding size will be the size needed to
  222. contain a memory management structure along with any extra for
  223. memory alignment.
  224. \return On successfully memory padding calculation the return value will
  225. be a positive value
  226. \return All negative values are considered error cases.
  227. \param none No parameters.
  228. _Example_
  229. \code
  230. int padding;
  231. padding = wolfSSL_MemoryPaddingSz();
  232. if (padding < 0) { //handle error case }
  233. printf(“The padding size needed for each \”bucket\” of memory is %d\n”,
  234. padding);
  235. // calculation of buffer for IO POOL size is number of buckets
  236. // times (padding + WOLFMEM_IO_SZ)
  237. ...
  238. \endcode
  239. \sa wolfSSL_Malloc
  240. \sa wolfSSL_Free
  241. */
  242. WOLFSSL_API int wolfSSL_MemoryPaddingSz(void);