1/*****************************************************************************
2
3Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify it under
6the terms of the GNU General Public License as published by the Free Software
7Foundation; version 2 of the License.
8
9This program is distributed in the hope that it will be useful, but WITHOUT
10ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12
13You should have received a copy of the GNU General Public License along with
14this program; if not, write to the Free Software Foundation, Inc.,
1551 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
16
17*****************************************************************************/
18
19/*******************************************************************//**
20@file include/ut0mem.h
21Memory primitives
22
23Created 5/30/1994 Heikki Tuuri
24************************************************************************/
25
26#ifndef ut0mem_h
27#define ut0mem_h
28
29#include "univ.i"
30#include "os0event.h"
31#include "ut0mutex.h"
32
33/** Wrapper for memcpy(3). Copy memory area when the source and
34target are not overlapping.
35@param[in,out] dest copy to
36@param[in] src copy from
37@param[in] n number of bytes to copy
38@return dest */
39UNIV_INLINE
40void*
41ut_memcpy(void* dest, const void* src, ulint n);
42
43/** Wrapper for memmove(3). Copy memory area when the source and
44target are overlapping.
45@param[in,out] dest Move to
46@param[in] src Move from
47@param[in] n number of bytes to move
48@return dest */
49UNIV_INLINE
50void*
51ut_memmove(void* dest, const void* sour, ulint n);
52
53/** Wrapper for memcmp(3). Compare memory areas.
54@param[in] str1 first memory block to compare
55@param[in] str2 second memory block to compare
56@param[in] n number of bytes to compare
57@return negative, 0, or positive if str1 is smaller, equal,
58 or greater than str2, respectively. */
59UNIV_INLINE
60int
61ut_memcmp(const void* str1, const void* str2, ulint n);
62
63/** Wrapper for strcpy(3). Copy a NUL-terminated string.
64@param[in,out] dest Destination to copy to
65@param[in] src Source to copy from
66@return dest */
67UNIV_INLINE
68char*
69ut_strcpy(char* dest, const char* src);
70
71/** Wrapper for strlen(3). Determine the length of a NUL-terminated string.
72@param[in] str string
73@return length of the string in bytes, excluding the terminating NUL */
74UNIV_INLINE
75ulint
76ut_strlen(const char* str);
77
78/** Wrapper for strcmp(3). Compare NUL-terminated strings.
79@param[in] str1 first string to compare
80@param[in] str2 second string to compare
81@return negative, 0, or positive if str1 is smaller, equal,
82 or greater than str2, respectively. */
83UNIV_INLINE
84int
85ut_strcmp(const char* str1, const char* str2);
86
87/**********************************************************************//**
88Copies up to size - 1 characters from the NUL-terminated string src to
89dst, NUL-terminating the result. Returns strlen(src), so truncation
90occurred if the return value >= size.
91@return strlen(src) */
92ulint
93ut_strlcpy(
94/*=======*/
95 char* dst, /*!< in: destination buffer */
96 const char* src, /*!< in: source buffer */
97 ulint size); /*!< in: size of destination buffer */
98
99/**********************************************************************//**
100Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last
101(size - 1) bytes of src, not the first.
102@return strlen(src) */
103ulint
104ut_strlcpy_rev(
105/*===========*/
106 char* dst, /*!< in: destination buffer */
107 const char* src, /*!< in: source buffer */
108 ulint size); /*!< in: size of destination buffer */
109
110/********************************************************************
111Concatenate 3 strings.*/
112char*
113ut_str3cat(
114/*=======*/
115 /* out, own: concatenated string, must be
116 freed with ut_free() */
117 const char* s1, /* in: string 1 */
118 const char* s2, /* in: string 2 */
119 const char* s3); /* in: string 3 */
120
121/**********************************************************************//**
122Converts a raw binary data to a NUL-terminated hex string. The output is
123truncated if there is not enough space in "hex", make sure "hex_size" is at
124least (2 * raw_size + 1) if you do not want this to happen. Returns the
125actual number of characters written to "hex" (including the NUL).
126@return number of chars written */
127UNIV_INLINE
128ulint
129ut_raw_to_hex(
130/*==========*/
131 const void* raw, /*!< in: raw data */
132 ulint raw_size, /*!< in: "raw" length in bytes */
133 char* hex, /*!< out: hex string */
134 ulint hex_size); /*!< in: "hex" size in bytes */
135
136/*******************************************************************//**
137Adds single quotes to the start and end of string and escapes any quotes
138by doubling them. Returns the number of bytes that were written to "buf"
139(including the terminating NUL). If buf_size is too small then the
140trailing bytes from "str" are discarded.
141@return number of bytes that were written */
142UNIV_INLINE
143ulint
144ut_str_sql_format(
145/*==============*/
146 const char* str, /*!< in: string */
147 ulint str_len, /*!< in: string length in bytes */
148 char* buf, /*!< out: output buffer */
149 ulint buf_size); /*!< in: output buffer size
150 in bytes */
151
152#include "ut0mem.ic"
153
154#endif
155