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