MySQL 8.3.0
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, 2023, 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 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
41class THD;
42#define MYSQL_THD THD *
43
45
46extern "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*/
89void *thd_alloc(MYSQL_THD thd, size_t size);
90/**
91 @see thd_alloc()
92*/
93void *thd_calloc(MYSQL_THD thd, size_t size);
94/**
95 @see thd_alloc()
96*/
97char *thd_strdup(MYSQL_THD thd, const char *str);
98/**
99 @see thd_alloc()
100*/
101char *thd_strmake(MYSQL_THD thd, const char *str, size_t size);
102/**
103 @see thd_alloc()
104*/
105void *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
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:35
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1065
#define MYSQL_THD
Definition: service_thd_alloc.h:42
struct thd_alloc_service_st * thd_alloc_service
char * thd_strdup(MYSQL_THD thd, const char *str)
Definition: sql_thd_api.cc:612
void * thd_calloc(MYSQL_THD thd, size_t size)
Definition: sql_thd_api.cc:610
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:620
char * thd_strmake(MYSQL_THD thd, const char *str, size_t size)
Definition: sql_thd_api.cc:616
void * thd_memdup(MYSQL_THD thd, const void *str, size_t size)
Definition: sql_thd_api.cc:629
void * thd_alloc(MYSQL_THD thd, size_t size)
Allocate memory in the connection's local memory pool.
Definition: sql_thd_api.cc:608
Definition: mysql_lex_string.h:34
Definition: service_thd_alloc.h:46