service_thd_alloc.h 4.3 KB

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