MySQL 8.0.40
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, 2024, Oracle and/or its affiliates.
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 designed to work 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 either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License, version 2.0, for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
24
25/**
26 @file include/mysql/service_thd_alloc.h
27 This service provides functions to allocate memory in a connection local
28 memory pool. The memory allocated there will be automatically freed at the
29 end of the statement, don't use it for allocations that should live longer
30 than that. For short living allocations this is more efficient than
31 using my_malloc and friends, and automatic "garbage collection" allows not
32 to think about memory leaks.
33
34 The pool is best for small to medium objects, don't use it for large
35 allocations - they are better served with my_malloc.
36*/
37
38#ifndef MYSQL_ABI_CHECK
39#include <stdlib.h>
40#endif
41
42class THD;
43#define MYSQL_THD THD *
44
46
47extern "C" struct thd_alloc_service_st {
48 void *(*thd_alloc_func)(MYSQL_THD, size_t);
49 void *(*thd_calloc_func)(MYSQL_THD, size_t);
50 char *(*thd_strdup_func)(MYSQL_THD, const char *);
51 char *(*thd_strmake_func)(MYSQL_THD, const char *, size_t);
52 void *(*thd_memdup_func)(MYSQL_THD, const void *, size_t);
53 MYSQL_LEX_STRING *(*thd_make_lex_string_func)(MYSQL_THD, MYSQL_LEX_STRING *,
54 const char *, size_t, int);
56
57#ifdef MYSQL_DYNAMIC_PLUGIN
58
59#define thd_alloc(thd, size) (thd_alloc_service->thd_alloc_func((thd), (size)))
60
61#define thd_calloc(thd, size) \
62 (thd_alloc_service->thd_calloc_func((thd), (size)))
63
64#define thd_strdup(thd, str) (thd_alloc_service->thd_strdup_func((thd), (str)))
65
66#define thd_strmake(thd, str, size) \
67 (thd_alloc_service->thd_strmake_func((thd), (str), (size)))
68
69#define thd_memdup(thd, str, size) \
70 (thd_alloc_service->thd_memdup_func((thd), (str), (size)))
71
72#define thd_make_lex_string(thd, lex_str, str, size, allocate_lex_string) \
73 (thd_alloc_service->thd_make_lex_string_func((thd), (lex_str), (str), \
74 (size), (allocate_lex_string)))
75
76#else
77
78/**
79 Allocate memory in the connection's local memory pool
80
81 @details
82 When properly used in place of @c my_malloc(), this can significantly
83 improve concurrency. Don't use this or related functions to allocate
84 large chunks of memory. Use for temporary storage only. The memory
85 will be freed automatically at the end of the statement; no explicit
86 code is required to prevent memory leaks.
87
88 @see alloc_root()
89*/
90void *thd_alloc(MYSQL_THD thd, size_t size);
91/**
92 @see thd_alloc()
93*/
94void *thd_calloc(MYSQL_THD thd, size_t size);
95/**
96 @see thd_alloc()
97*/
98char *thd_strdup(MYSQL_THD thd, const char *str);
99/**
100 @see thd_alloc()
101*/
102char *thd_strmake(MYSQL_THD thd, const char *str, size_t size);
103/**
104 @see thd_alloc()
105*/
106void *thd_memdup(MYSQL_THD thd, const void *str, size_t size);
107
108/**
109 Create a LEX_STRING in this connection's local memory pool
110
111 @param thd user thread connection handle
112 @param lex_str pointer to LEX_STRING object to be initialized
113 @param str initializer to be copied into lex_str
114 @param size length of str, in bytes
115 @param allocate_lex_string flag: if TRUE, allocate new LEX_STRING object,
116 instead of using lex_str value
117 @return NULL on failure, or pointer to the LEX_STRING object
118
119 @see thd_alloc()
120*/
122 const char *str, size_t size,
123 int allocate_lex_string);
124
125#endif
126
127#define MYSQL_SERVICE_THD_ALLOC_INCLUDED
128#endif
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:34
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1052
#define MYSQL_THD
Definition: service_thd_alloc.h:43
struct thd_alloc_service_st * thd_alloc_service
char * thd_strdup(MYSQL_THD thd, const char *str)
Definition: sql_thd_api.cc:604
void * thd_calloc(MYSQL_THD thd, size_t size)
Definition: sql_thd_api.cc:602
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:612
char * thd_strmake(MYSQL_THD thd, const char *str, size_t size)
Definition: sql_thd_api.cc:608
void * thd_memdup(MYSQL_THD thd, const void *str, size_t size)
Definition: sql_thd_api.cc:621
void * thd_alloc(MYSQL_THD thd, size_t size)
Allocate memory in the connection's local memory pool.
Definition: sql_thd_api.cc:600
Definition: mysql_lex_string.h:35
Definition: service_thd_alloc.h:47