1
by brian
clean slate |
1 |
/***********************************************************************
|
2 |
Memory primitives
|
|
3 |
||
4 |
(c) 1994, 1995 Innobase Oy
|
|
5 |
||
6 |
Created 5/30/1994 Heikki Tuuri
|
|
7 |
************************************************************************/
|
|
8 |
||
9 |
#ifndef ut0mem_h
|
|
10 |
#define ut0mem_h
|
|
11 |
||
12 |
#include "univ.i" |
|
13 |
#include <string.h> |
|
14 |
#include <stdlib.h> |
|
15 |
||
16 |
/* The total amount of memory currently allocated from the OS with malloc */
|
|
17 |
extern ulint ut_total_allocated_memory; |
|
18 |
||
19 |
UNIV_INLINE
|
|
20 |
void* |
|
21 |
ut_memcpy(void* dest, const void* sour, ulint n); |
|
22 |
||
23 |
UNIV_INLINE
|
|
24 |
void* |
|
25 |
ut_memmove(void* dest, const void* sour, ulint n); |
|
26 |
||
27 |
UNIV_INLINE
|
|
28 |
int
|
|
29 |
ut_memcmp(const void* str1, const void* str2, ulint n); |
|
30 |
||
31 |
||
32 |
/**************************************************************************
|
|
33 |
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
|
|
34 |
defined and set_to_zero is TRUE. */
|
|
35 |
||
36 |
void* |
|
37 |
ut_malloc_low( |
|
38 |
/*==========*/
|
|
39 |
/* out, own: allocated memory */
|
|
40 |
ulint n, /* in: number of bytes to allocate */ |
|
41 |
ibool set_to_zero, /* in: TRUE if allocated memory |
|
42 |
should be set to zero if
|
|
43 |
UNIV_SET_MEM_TO_ZERO is defined */
|
|
44 |
ibool assert_on_error); /* in: if TRUE, we crash mysqld if |
|
45 |
the memory cannot be allocated */
|
|
46 |
/**************************************************************************
|
|
47 |
Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is
|
|
48 |
defined. */
|
|
49 |
||
50 |
void* |
|
51 |
ut_malloc( |
|
52 |
/*======*/
|
|
53 |
/* out, own: allocated memory */
|
|
54 |
ulint n); /* in: number of bytes to allocate */ |
|
55 |
/**************************************************************************
|
|
56 |
Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs
|
|
57 |
out. It cannot be used if we want to return an error message. Prints to
|
|
58 |
stderr a message if fails. */
|
|
59 |
||
60 |
ibool
|
|
61 |
ut_test_malloc( |
|
62 |
/*===========*/
|
|
63 |
/* out: TRUE if succeeded */
|
|
64 |
ulint n); /* in: try to allocate this many bytes */ |
|
65 |
/**************************************************************************
|
|
66 |
Frees a memory block allocated with ut_malloc. */
|
|
67 |
||
68 |
void
|
|
69 |
ut_free( |
|
70 |
/*====*/
|
|
71 |
void* ptr); /* in, own: memory block */ |
|
72 |
/**************************************************************************
|
|
73 |
Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not
|
|
74 |
use this function because the allocation functions in mem0mem.h are the
|
|
75 |
recommended ones in InnoDB.
|
|
76 |
||
77 |
man realloc in Linux, 2004:
|
|
78 |
||
79 |
realloc() changes the size of the memory block pointed to
|
|
80 |
by ptr to size bytes. The contents will be unchanged to
|
|
81 |
the minimum of the old and new sizes; newly allocated memĀ
|
|
82 |
ory will be uninitialized. If ptr is NULL, the call is
|
|
83 |
equivalent to malloc(size); if size is equal to zero, the
|
|
84 |
call is equivalent to free(ptr). Unless ptr is NULL, it
|
|
85 |
must have been returned by an earlier call to malloc(),
|
|
86 |
calloc() or realloc().
|
|
87 |
||
88 |
RETURN VALUE
|
|
89 |
realloc() returns a pointer to the newly allocated memory,
|
|
90 |
which is suitably aligned for any kind of variable and may
|
|
91 |
be different from ptr, or NULL if the request fails. If
|
|
92 |
size was equal to 0, either NULL or a pointer suitable to
|
|
93 |
be passed to free() is returned. If realloc() fails the
|
|
94 |
original block is left untouched - it is not freed or
|
|
95 |
moved. */
|
|
96 |
||
97 |
void* |
|
98 |
ut_realloc( |
|
99 |
/*=======*/
|
|
100 |
/* out, own: pointer to new mem block or NULL */
|
|
101 |
void* ptr, /* in: pointer to old block or NULL */ |
|
102 |
ulint size); /* in: desired size */ |
|
103 |
/**************************************************************************
|
|
104 |
Frees in shutdown all allocated memory not freed yet. */
|
|
105 |
||
106 |
void
|
|
107 |
ut_free_all_mem(void); |
|
108 |
/*=================*/
|
|
109 |
||
110 |
UNIV_INLINE
|
|
111 |
char* |
|
112 |
ut_strcpy(char* dest, const char* sour); |
|
113 |
||
114 |
UNIV_INLINE
|
|
115 |
ulint
|
|
116 |
ut_strlen(const char* str); |
|
117 |
||
118 |
UNIV_INLINE
|
|
119 |
int
|
|
120 |
ut_strcmp(const void* str1, const void* str2); |
|
121 |
||
122 |
/**************************************************************************
|
|
123 |
Copies up to size - 1 characters from the NUL-terminated string src to
|
|
124 |
dst, NUL-terminating the result. Returns strlen(src), so truncation
|
|
125 |
occurred if the return value >= size. */
|
|
126 |
||
127 |
ulint
|
|
128 |
ut_strlcpy( |
|
129 |
/*=======*/
|
|
130 |
/* out: strlen(src) */ |
|
131 |
char* dst, /* in: destination buffer */ |
|
132 |
const char* src, /* in: source buffer */ |
|
133 |
ulint size); /* in: size of destination buffer */ |
|
134 |
||
135 |
/**************************************************************************
|
|
136 |
Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
|
|
137 |
(size - 1) bytes of src, not the first. */
|
|
138 |
||
139 |
ulint
|
|
140 |
ut_strlcpy_rev( |
|
141 |
/*===========*/
|
|
142 |
/* out: strlen(src) */ |
|
143 |
char* dst, /* in: destination buffer */ |
|
144 |
const char* src, /* in: source buffer */ |
|
145 |
ulint size); /* in: size of destination buffer */ |
|
146 |
||
147 |
/**************************************************************************
|
|
148 |
Compute strlen(ut_strcpyq(str, q)). */
|
|
149 |
UNIV_INLINE
|
|
150 |
ulint
|
|
151 |
ut_strlenq( |
|
152 |
/*=======*/
|
|
153 |
/* out: length of the string when quoted */
|
|
154 |
const char* str, /* in: null-terminated string */ |
|
155 |
char q); /* in: the quote character */ |
|
156 |
||
157 |
/**************************************************************************
|
|
158 |
Make a quoted copy of a NUL-terminated string. Leading and trailing
|
|
159 |
quotes will not be included; only embedded quotes will be escaped.
|
|
160 |
See also ut_strlenq() and ut_memcpyq(). */
|
|
161 |
||
162 |
char* |
|
163 |
ut_strcpyq( |
|
164 |
/*=======*/
|
|
165 |
/* out: pointer to end of dest */
|
|
166 |
char* dest, /* in: output buffer */ |
|
167 |
char q, /* in: the quote character */ |
|
168 |
const char* src); /* in: null-terminated string */ |
|
169 |
||
170 |
/**************************************************************************
|
|
171 |
Make a quoted copy of a fixed-length string. Leading and trailing
|
|
172 |
quotes will not be included; only embedded quotes will be escaped.
|
|
173 |
See also ut_strlenq() and ut_strcpyq(). */
|
|
174 |
||
175 |
char* |
|
176 |
ut_memcpyq( |
|
177 |
/*=======*/
|
|
178 |
/* out: pointer to end of dest */
|
|
179 |
char* dest, /* in: output buffer */ |
|
180 |
char q, /* in: the quote character */ |
|
181 |
const char* src, /* in: string to be quoted */ |
|
182 |
ulint len); /* in: length of src */ |
|
183 |
||
184 |
/**************************************************************************
|
|
185 |
Return the number of times s2 occurs in s1. Overlapping instances of s2
|
|
186 |
are only counted once. */
|
|
187 |
||
188 |
ulint
|
|
189 |
ut_strcount( |
|
190 |
/*========*/
|
|
191 |
/* out: the number of times s2 occurs in s1 */
|
|
192 |
const char* s1, /* in: string to search in */ |
|
193 |
const char* s2); /* in: string to search for */ |
|
194 |
||
195 |
/**************************************************************************
|
|
196 |
Replace every occurrence of s1 in str with s2. Overlapping instances of s1
|
|
197 |
are only replaced once. */
|
|
198 |
||
199 |
char * |
|
200 |
ut_strreplace( |
|
201 |
/*==========*/
|
|
202 |
/* out, own: modified string, must be
|
|
203 |
freed with mem_free() */ |
|
204 |
const char* str, /* in: string to operate on */ |
|
205 |
const char* s1, /* in: string to replace */ |
|
206 |
const char* s2); /* in: string to replace s1 with */ |
|
207 |
||
208 |
#ifndef UNIV_NONINL
|
|
209 |
#include "ut0mem.ic" |
|
210 |
#endif
|
|
211 |
||
212 |
#endif
|