2
0

memory.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*!
  2. \ingroup Memory
  3. \brief This function is similar to malloc(), but calls the memory
  4. allocation function which wolfSSL has been configured to use. By default,
  5. wolfSSL uses malloc(). This can be changed using the wolfSSL memory
  6. abstraction layer - see wolfSSL_SetAllocators(). Note wolfSSL_Malloc is not
  7. called directly by wolfSSL, but instead called by macro XMALLOC.
  8. For the default build only the size argument exists. If using
  9. WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  10. \return pointer If successful, this function returns a pointer to
  11. allocated memory.
  12. \return error If there is an error, NULL will be returned.
  13. \param size size, in bytes, of the memory to allocate
  14. \param heap heap hint to use for memory. Can be NULL
  15. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  16. _Example_
  17. \code
  18. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  19. \endcode
  20. \sa wolfSSL_Free
  21. \sa wolfSSL_Realloc
  22. \sa wolfSSL_SetAllocators
  23. \sa XMALLOC
  24. \sa XFREE
  25. \sa XREALLOC
  26. */
  27. void* wolfSSL_Malloc(size_t size, void* heap, int type);
  28. /*!
  29. \ingroup Memory
  30. \brief This function is similar to free(), but calls the memory free
  31. function which wolfSSL has been configured to use. By default, wolfSSL
  32. uses free(). This can be changed using the wolfSSL memory abstraction
  33. layer - see wolfSSL_SetAllocators(). Note wolfSSL_Free is not
  34. called directly by wolfSSL, but instead called by macro XFREE.
  35. For the default build only the ptr argument exists. If using
  36. WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  37. \return none No returns.
  38. \param ptr pointer to the memory to be freed.
  39. \param heap heap hint to use for memory. Can be NULL
  40. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  41. _Example_
  42. \code
  43. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  44. // process data as desired
  45. ...
  46. if(tenInts) {
  47. wolfSSL_Free(tenInts);
  48. }
  49. \endcode
  50. \sa wolfSSL_Alloc
  51. \sa wolfSSL_Realloc
  52. \sa wolfSSL_SetAllocators
  53. \sa XMALLOC
  54. \sa XFREE
  55. \sa XREALLOC
  56. */
  57. void wolfSSL_Free(void *ptr, void* heap, int type);
  58. /*!
  59. \ingroup Memory
  60. \brief This function is similar to realloc(), but calls the memory
  61. re-allocation function which wolfSSL has been configured to use.
  62. By default, wolfSSL uses realloc(). This can be changed using the
  63. wolfSSL memory abstraction layer - see wolfSSL_SetAllocators().
  64. Note wolfSSL_Realloc is not called directly by wolfSSL, but instead called
  65. by macro XREALLOC. For the default build only the size argument exists.
  66. If using WOLFSSL_STATIC_MEMORY build then heap and type arguments are included.
  67. \return pointer If successful, this function returns a pointer to
  68. re-allocated memory. This may be the same pointer as ptr, or a
  69. new pointer location.
  70. \return Null If there is an error, NULL will be returned.
  71. \param ptr pointer to the previously-allocated memory, to be reallocated.
  72. \param size number of bytes to allocate.
  73. \param heap heap hint to use for memory. Can be NULL
  74. \param type dynamic type (see DYNAMIC_TYPE_ list in types.h)
  75. _Example_
  76. \code
  77. int* tenInts = (int*)wolfSSL_Malloc(sizeof(int)*10);
  78. int* twentyInts = (int*)wolfSSL_Realloc(tenInts, sizeof(int)*20);
  79. \endcode
  80. \sa wolfSSL_Free
  81. \sa wolfSSL_Malloc
  82. \sa wolfSSL_SetAllocators
  83. \sa XMALLOC
  84. \sa XFREE
  85. \sa XREALLOC
  86. */
  87. void* wolfSSL_Realloc(void *ptr, size_t size, void* heap, int type);
  88. /*!
  89. \ingroup Memory
  90. \brief This function registers the allocation functions used by wolfSSL.
  91. By default, if the system supports it, malloc/free and realloc are used.
  92. Using this function allows the user at runtime to install their own
  93. memory handlers.
  94. \return Success If successful this function will return 0.
  95. \return BAD_FUNC_ARG is the error that will be returned if a
  96. function pointer is not provided.
  97. \param malloc_function memory allocation function for wolfSSL to use.
  98. Function signature must match wolfSSL_Malloc_cb prototype, above.
  99. \param free_function memory free function for wolfSSL to use. Function
  100. signature must match wolfSSL_Free_cb prototype, above.
  101. \param realloc_function memory re-allocation function for wolfSSL to use.
  102. Function signature must match wolfSSL_Realloc_cb prototype, above.
  103. _Example_
  104. \code
  105. static void* MyMalloc(size_t size)
  106. {
  107. // custom malloc function
  108. }
  109. static void MyFree(void* ptr)
  110. {
  111. // custom free function
  112. }
  113. static void* MyRealloc(void* ptr, size_t size)
  114. {
  115. // custom realloc function
  116. }
  117. // Register custom memory functions with wolfSSL
  118. int ret = wolfSSL_SetAllocators(MyMalloc, MyFree, MyRealloc);
  119. if (ret != 0) {
  120. // failed to set memory functions
  121. }
  122. \endcode
  123. \sa none
  124. */
  125. int wolfSSL_SetAllocators(wolfSSL_Malloc_cb,
  126. wolfSSL_Free_cb,
  127. wolfSSL_Realloc_cb);
  128. /*!
  129. \ingroup Memory
  130. \brief This function is available when static memory feature is used
  131. (--enable-staticmemory). It gives the optimum buffer size for memory
  132. “buckets”. This allows for a way to compute buffer size so that no
  133. extra unused memory is left at the end after it has been partitioned.
  134. The returned value, if positive, is the computed buffer size to use.
  135. \return Success On successfully completing buffer size calculations a
  136. positive value is returned. This returned value is for optimum buffer size.
  137. \return Failure All negative values are considered to be error cases.
  138. \param buffer pointer to buffer
  139. \param size size of buffer
  140. \param type desired type of memory ie WOLFMEM_GENERAL or WOLFMEM_IO_POOL
  141. _Example_
  142. \code
  143. byte buffer[1000];
  144. word32 size = sizeof(buffer);
  145. int optimum;
  146. optimum = wolfSSL_StaticBufferSz(buffer, size, WOLFMEM_GENERAL);
  147. if (optimum < 0) { //handle error case }
  148. printf(“The optimum buffer size to make use of all memory is %d\n”,
  149. optimum);
  150. ...
  151. \endcode
  152. \sa wolfSSL_Malloc
  153. \sa wolfSSL_Free
  154. */
  155. int wolfSSL_StaticBufferSz(byte* buffer, word32 sz, int flag);
  156. /*!
  157. \ingroup Memory
  158. \brief This function is available when static memory feature is used
  159. (--enable-staticmemory). It gives the size of padding needed for each
  160. partition of memory. This padding size will be the size needed to
  161. contain a memory management structure along with any extra for
  162. memory alignment.
  163. \return On successfully memory padding calculation the return value will
  164. be a positive value
  165. \return All negative values are considered error cases.
  166. \param none No parameters.
  167. _Example_
  168. \code
  169. int padding;
  170. padding = wolfSSL_MemoryPaddingSz();
  171. if (padding < 0) { //handle error case }
  172. printf(“The padding size needed for each \”bucket\” of memory is %d\n”,
  173. padding);
  174. // calculation of buffer for IO POOL size is number of buckets
  175. // times (padding + WOLFMEM_IO_SZ)
  176. ...
  177. \endcode
  178. \sa wolfSSL_Malloc
  179. \sa wolfSSL_Free
  180. */
  181. int wolfSSL_MemoryPaddingSz(void);