You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
10 KiB

  1. /*****************************************************************************
  2. Copyright (c) 1994, 2009, Innobase Oy. All Rights Reserved.
  3. This program is free software; you can redistribute it and/or modify it under
  4. the terms of the GNU General Public License as published by the Free Software
  5. Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License along with
  10. this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  11. Place, Suite 330, Boston, MA 02111-1307 USA
  12. *****************************************************************************/
  13. /*******************************************************************//**
  14. @file include/ut0mem.h
  15. Memory primitives
  16. Created 5/30/1994 Heikki Tuuri
  17. ************************************************************************/
  18. #ifndef ut0mem_h
  19. #define ut0mem_h
  20. #include "univ.i"
  21. #include <string.h>
  22. #ifndef UNIV_HOTBACKUP
  23. # include "os0sync.h"
  24. /** The total amount of memory currently allocated from the operating
  25. system with os_mem_alloc_large() or malloc(). Does not count malloc()
  26. if srv_use_sys_malloc is set. Protected by ut_list_mutex. */
  27. extern ulint ut_total_allocated_memory;
  28. /** Mutex protecting ut_total_allocated_memory and ut_mem_block_list */
  29. extern os_fast_mutex_t ut_list_mutex;
  30. #endif /* !UNIV_HOTBACKUP */
  31. /** Wrapper for memcpy(3). Copy memory area when the source and
  32. target are not overlapping.
  33. * @param dest in: copy to
  34. * @param sour in: copy from
  35. * @param n in: number of bytes to copy
  36. * @return dest */
  37. UNIV_INLINE
  38. void*
  39. ut_memcpy(void* dest, const void* sour, ulint n);
  40. /** Wrapper for memmove(3). Copy memory area when the source and
  41. target are overlapping.
  42. * @param dest in: copy to
  43. * @param sour in: copy from
  44. * @param n in: number of bytes to copy
  45. * @return dest */
  46. UNIV_INLINE
  47. void*
  48. ut_memmove(void* dest, const void* sour, ulint n);
  49. /** Wrapper for memcmp(3). Compare memory areas.
  50. * @param str1 in: first memory block to compare
  51. * @param str2 in: second memory block to compare
  52. * @param n in: number of bytes to compare
  53. * @return negative, 0, or positive if str1 is smaller, equal,
  54. or greater than str2, respectively. */
  55. UNIV_INLINE
  56. int
  57. ut_memcmp(const void* str1, const void* str2, ulint n);
  58. /**********************************************************************//**
  59. Initializes the mem block list at database startup. */
  60. UNIV_INTERN
  61. void
  62. ut_mem_init(void);
  63. /*=============*/
  64. /**********************************************************************//**
  65. Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
  66. defined and set_to_zero is TRUE.
  67. @return own: allocated memory */
  68. UNIV_INTERN
  69. void*
  70. ut_malloc_low(
  71. /*==========*/
  72. ulint n, /*!< in: number of bytes to allocate */
  73. ibool set_to_zero, /*!< in: TRUE if allocated memory
  74. should be set to zero if
  75. UNIV_SET_MEM_TO_ZERO is defined */
  76. ibool assert_on_error); /*!< in: if TRUE, we crash mysqld if
  77. the memory cannot be allocated */
  78. /**********************************************************************//**
  79. Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
  80. defined.
  81. @return own: allocated memory */
  82. UNIV_INTERN
  83. void*
  84. ut_malloc(
  85. /*======*/
  86. ulint n); /*!< in: number of bytes to allocate */
  87. #ifndef UNIV_HOTBACKUP
  88. /**********************************************************************//**
  89. Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
  90. out. It cannot be used if we want to return an error message. Prints to
  91. stderr a message if fails.
  92. @return TRUE if succeeded */
  93. UNIV_INTERN
  94. ibool
  95. ut_test_malloc(
  96. /*===========*/
  97. ulint n); /*!< in: try to allocate this many bytes */
  98. #endif /* !UNIV_HOTBACKUP */
  99. /**********************************************************************//**
  100. Frees a memory block allocated with ut_malloc. */
  101. UNIV_INTERN
  102. void
  103. ut_free(
  104. /*====*/
  105. void* ptr); /*!< in, own: memory block */
  106. #ifndef UNIV_HOTBACKUP
  107. /**********************************************************************//**
  108. Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
  109. use this function because the allocation functions in mem0mem.h are the
  110. recommended ones in InnoDB.
  111. man realloc in Linux, 2004:
  112. realloc() changes the size of the memory block pointed to
  113. by ptr to size bytes. The contents will be unchanged to
  114. the minimum of the old and new sizes; newly allocated mem
  115. ory will be uninitialized. If ptr is NULL, the call is
  116. equivalent to malloc(size); if size is equal to zero, the
  117. call is equivalent to free(ptr). Unless ptr is NULL, it
  118. must have been returned by an earlier call to malloc(),
  119. calloc() or realloc().
  120. RETURN VALUE
  121. realloc() returns a pointer to the newly allocated memory,
  122. which is suitably aligned for any kind of variable and may
  123. be different from ptr, or NULL if the request fails. If
  124. size was equal to 0, either NULL or a pointer suitable to
  125. be passed to free() is returned. If realloc() fails the
  126. original block is left untouched - it is not freed or
  127. moved.
  128. @return own: pointer to new mem block or NULL */
  129. UNIV_INTERN
  130. void*
  131. ut_realloc(
  132. /*=======*/
  133. void* ptr, /*!< in: pointer to old block or NULL */
  134. ulint size); /*!< in: desired size */
  135. /**********************************************************************//**
  136. Frees in shutdown all allocated memory not freed yet. */
  137. UNIV_INTERN
  138. void
  139. ut_free_all_mem(void);
  140. /*=================*/
  141. #endif /* !UNIV_HOTBACKUP */
  142. /** Wrapper for strcpy(3). Copy a NUL-terminated string.
  143. * @param dest in: copy to
  144. * @param sour in: copy from
  145. * @return dest */
  146. UNIV_INLINE
  147. char*
  148. ut_strcpy(char* dest, const char* sour);
  149. /** Wrapper for strlen(3). Determine the length of a NUL-terminated string.
  150. * @param str in: string
  151. * @return length of the string in bytes, excluding the terminating NUL */
  152. UNIV_INLINE
  153. ulint
  154. ut_strlen(const char* str);
  155. /** Wrapper for strcmp(3). Compare NUL-terminated strings.
  156. * @param str1 in: first string to compare
  157. * @param str2 in: second string to compare
  158. * @return negative, 0, or positive if str1 is smaller, equal,
  159. or greater than str2, respectively. */
  160. UNIV_INLINE
  161. int
  162. ut_strcmp(const char* str1, const char* str2);
  163. /**********************************************************************//**
  164. Copies up to size - 1 characters from the NUL-terminated string src to
  165. dst, NUL-terminating the result. Returns strlen(src), so truncation
  166. occurred if the return value >= size.
  167. @return strlen(src) */
  168. UNIV_INTERN
  169. ulint
  170. ut_strlcpy(
  171. /*=======*/
  172. char* dst, /*!< in: destination buffer */
  173. const char* src, /*!< in: source buffer */
  174. ulint size); /*!< in: size of destination buffer */
  175. /**********************************************************************//**
  176. Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
  177. (size - 1) bytes of src, not the first.
  178. @return strlen(src) */
  179. UNIV_INTERN
  180. ulint
  181. ut_strlcpy_rev(
  182. /*===========*/
  183. char* dst, /*!< in: destination buffer */
  184. const char* src, /*!< in: source buffer */
  185. ulint size); /*!< in: size of destination buffer */
  186. /**********************************************************************//**
  187. Compute strlen(ut_strcpyq(str, q)).
  188. @return length of the string when quoted */
  189. UNIV_INLINE
  190. ulint
  191. ut_strlenq(
  192. /*=======*/
  193. const char* str, /*!< in: null-terminated string */
  194. char q); /*!< in: the quote character */
  195. /**********************************************************************//**
  196. Make a quoted copy of a NUL-terminated string. Leading and trailing
  197. quotes will not be included; only embedded quotes will be escaped.
  198. See also ut_strlenq() and ut_memcpyq().
  199. @return pointer to end of dest */
  200. UNIV_INTERN
  201. char*
  202. ut_strcpyq(
  203. /*=======*/
  204. char* dest, /*!< in: output buffer */
  205. char q, /*!< in: the quote character */
  206. const char* src); /*!< in: null-terminated string */
  207. /**********************************************************************//**
  208. Make a quoted copy of a fixed-length string. Leading and trailing
  209. quotes will not be included; only embedded quotes will be escaped.
  210. See also ut_strlenq() and ut_strcpyq().
  211. @return pointer to end of dest */
  212. UNIV_INTERN
  213. char*
  214. ut_memcpyq(
  215. /*=======*/
  216. char* dest, /*!< in: output buffer */
  217. char q, /*!< in: the quote character */
  218. const char* src, /*!< in: string to be quoted */
  219. ulint len); /*!< in: length of src */
  220. /**********************************************************************//**
  221. Return the number of times s2 occurs in s1. Overlapping instances of s2
  222. are only counted once.
  223. @return the number of times s2 occurs in s1 */
  224. UNIV_INTERN
  225. ulint
  226. ut_strcount(
  227. /*========*/
  228. const char* s1, /*!< in: string to search in */
  229. const char* s2); /*!< in: string to search for */
  230. /**********************************************************************//**
  231. Replace every occurrence of s1 in str with s2. Overlapping instances of s1
  232. are only replaced once.
  233. @return own: modified string, must be freed with mem_free() */
  234. UNIV_INTERN
  235. char*
  236. ut_strreplace(
  237. /*==========*/
  238. const char* str, /*!< in: string to operate on */
  239. const char* s1, /*!< in: string to replace */
  240. const char* s2); /*!< in: string to replace s1 with */
  241. /**********************************************************************//**
  242. Converts a raw binary data to a NUL-terminated hex string. The output is
  243. truncated if there is not enough space in "hex", make sure "hex_size" is at
  244. least (2 * raw_size + 1) if you do not want this to happen. Returns the
  245. actual number of characters written to "hex" (including the NUL).
  246. @return number of chars written */
  247. UNIV_INLINE
  248. ulint
  249. ut_raw_to_hex(
  250. /*==========*/
  251. const void* raw, /*!< in: raw data */
  252. ulint raw_size, /*!< in: "raw" length in bytes */
  253. char* hex, /*!< out: hex string */
  254. ulint hex_size); /*!< in: "hex" size in bytes */
  255. /*******************************************************************//**
  256. Adds single quotes to the start and end of string and escapes any quotes
  257. by doubling them. Returns the number of bytes that were written to "buf"
  258. (including the terminating NUL). If buf_size is too small then the
  259. trailing bytes from "str" are discarded.
  260. @return number of bytes that were written */
  261. UNIV_INLINE
  262. ulint
  263. ut_str_sql_format(
  264. /*==============*/
  265. const char* str, /*!< in: string */
  266. ulint str_len, /*!< in: string length in bytes */
  267. char* buf, /*!< out: output buffer */
  268. ulint buf_size); /*!< in: output buffer size
  269. in bytes */
  270. #ifndef UNIV_NONINL
  271. #include "ut0mem.ic"
  272. #endif
  273. #endif