service_thd_alloc.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #ifndef MYSQL_SERVICE_THD_ALLOC_INCLUDED
  2. /* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License, version 2.0,
  5. as published by the Free Software Foundation.
  6. This program is also distributed with certain software (including
  7. but not limited to OpenSSL) that is licensed under separate terms,
  8. as designated in a particular file or component or in included license
  9. documentation. The authors of MySQL hereby grant you an additional
  10. permission to link the program and your derivative works with the
  11. separately licensed software that they have included with MySQL.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License, version 2.0, for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  19. /**
  20. @file
  21. This service provdes functions to allocate memory in a connection local
  22. memory pool. The memory allocated there will be automatically freed at the
  23. end of the statement, don't use it for allocations that should live longer
  24. than that. For short living allocations this is more efficient than
  25. using my_malloc and friends, and automatic "garbage collection" allows not
  26. to think about memory leaks.
  27. The pool is best for small to medium objects, don't use it for large
  28. allocations - they are better served with my_malloc.
  29. */
  30. #ifndef MYSQL_ABI_CHECK
  31. #include <stdlib.h>
  32. #endif
  33. #ifdef __cplusplus
  34. class THD;
  35. #define MYSQL_THD THD*
  36. #else
  37. #define MYSQL_THD void*
  38. #endif
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. #include <mysql/mysql_lex_string.h>
  43. extern struct thd_alloc_service_st {
  44. void *(*thd_alloc_func)(MYSQL_THD, size_t);
  45. void *(*thd_calloc_func)(MYSQL_THD, size_t);
  46. char *(*thd_strdup_func)(MYSQL_THD, const char *);
  47. char *(*thd_strmake_func)(MYSQL_THD, const char *, size_t);
  48. void *(*thd_memdup_func)(MYSQL_THD, const void*, size_t);
  49. MYSQL_LEX_STRING *(*thd_make_lex_string_func)(MYSQL_THD, MYSQL_LEX_STRING *,
  50. const char *, size_t, int);
  51. } *thd_alloc_service;
  52. #ifdef MYSQL_DYNAMIC_PLUGIN
  53. #define thd_alloc(thd,size) (thd_alloc_service->thd_alloc_func((thd), (size)))
  54. #define thd_calloc(thd,size) (thd_alloc_service->thd_calloc_func((thd), (size)))
  55. #define thd_strdup(thd,str) (thd_alloc_service->thd_strdup_func((thd), (str)))
  56. #define thd_strmake(thd,str,size) \
  57. (thd_alloc_service->thd_strmake_func((thd), (str), (size)))
  58. #define thd_memdup(thd,str,size) \
  59. (thd_alloc_service->thd_memdup_func((thd), (str), (size)))
  60. #define thd_make_lex_string(thd, lex_str, str, size, allocate_lex_string) \
  61. (thd_alloc_service->thd_make_lex_string_func((thd), (lex_str), (str), \
  62. (size), (allocate_lex_string)))
  63. #else
  64. /**
  65. Allocate memory in the connection's local memory pool
  66. @details
  67. When properly used in place of @c my_malloc(), this can significantly
  68. improve concurrency. Don't use this or related functions to allocate
  69. large chunks of memory. Use for temporary storage only. The memory
  70. will be freed automatically at the end of the statement; no explicit
  71. code is required to prevent memory leaks.
  72. @see alloc_root()
  73. */
  74. void *thd_alloc(MYSQL_THD thd, size_t size);
  75. /**
  76. @see thd_alloc()
  77. */
  78. void *thd_calloc(MYSQL_THD thd, size_t size);
  79. /**
  80. @see thd_alloc()
  81. */
  82. char *thd_strdup(MYSQL_THD thd, const char *str);
  83. /**
  84. @see thd_alloc()
  85. */
  86. char *thd_strmake(MYSQL_THD thd, const char *str, size_t size);
  87. /**
  88. @see thd_alloc()
  89. */
  90. void *thd_memdup(MYSQL_THD thd, const void* str, size_t size);
  91. /**
  92. Create a LEX_STRING in this connection's local memory pool
  93. @param thd user thread connection handle
  94. @param lex_str pointer to LEX_STRING object to be initialized
  95. @param str initializer to be copied into lex_str
  96. @param size length of str, in bytes
  97. @param allocate_lex_string flag: if TRUE, allocate new LEX_STRING object,
  98. instead of using lex_str value
  99. @return NULL on failure, or pointer to the LEX_STRING object
  100. @see thd_alloc()
  101. */
  102. MYSQL_LEX_STRING *thd_make_lex_string(MYSQL_THD thd, MYSQL_LEX_STRING *lex_str,
  103. const char *str, size_t size,
  104. int allocate_lex_string);
  105. #endif
  106. #ifdef __cplusplus
  107. }
  108. #endif
  109. #define MYSQL_SERVICE_THD_ALLOC_INCLUDED
  110. #endif