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