| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 1994, 2016, Oracle and/or its affiliates. All Rights Reserved. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify it under |
| 6 | the terms of the GNU General Public License as published by the Free Software |
| 7 | Foundation; version 2 of the License. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 11 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License along with |
| 14 | this program; if not, write to the Free Software Foundation, Inc., |
| 15 | 51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA |
| 16 | |
| 17 | *****************************************************************************/ |
| 18 | |
| 19 | /*******************************************************************//** |
| 20 | @file include/ut0mem.h |
| 21 | Memory primitives |
| 22 | |
| 23 | Created 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 |
| 34 | target 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 */ |
| 39 | UNIV_INLINE |
| 40 | void* |
| 41 | ut_memcpy(void* dest, const void* src, ulint n); |
| 42 | |
| 43 | /** Wrapper for memmove(3). Copy memory area when the source and |
| 44 | target 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 */ |
| 49 | UNIV_INLINE |
| 50 | void* |
| 51 | ut_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. */ |
| 59 | UNIV_INLINE |
| 60 | int |
| 61 | ut_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 */ |
| 67 | UNIV_INLINE |
| 68 | char* |
| 69 | ut_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 */ |
| 74 | UNIV_INLINE |
| 75 | ulint |
| 76 | ut_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. */ |
| 83 | UNIV_INLINE |
| 84 | int |
| 85 | ut_strcmp(const char* str1, const char* str2); |
| 86 | |
| 87 | /**********************************************************************//** |
| 88 | Copies up to size - 1 characters from the NUL-terminated string src to |
| 89 | dst, NUL-terminating the result. Returns strlen(src), so truncation |
| 90 | occurred if the return value >= size. |
| 91 | @return strlen(src) */ |
| 92 | ulint |
| 93 | ut_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 | /**********************************************************************//** |
| 100 | Like 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) */ |
| 103 | ulint |
| 104 | ut_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 | /******************************************************************** |
| 111 | Concatenate 3 strings.*/ |
| 112 | char* |
| 113 | ut_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 | /**********************************************************************//** |
| 122 | Converts a raw binary data to a NUL-terminated hex string. The output is |
| 123 | truncated if there is not enough space in "hex", make sure "hex_size" is at |
| 124 | least (2 * raw_size + 1) if you do not want this to happen. Returns the |
| 125 | actual number of characters written to "hex" (including the NUL). |
| 126 | @return number of chars written */ |
| 127 | UNIV_INLINE |
| 128 | ulint |
| 129 | ut_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 | /*******************************************************************//** |
| 137 | Adds single quotes to the start and end of string and escapes any quotes |
| 138 | by 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 |
| 140 | trailing bytes from "str" are discarded. |
| 141 | @return number of bytes that were written */ |
| 142 | UNIV_INLINE |
| 143 | ulint |
| 144 | ut_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 | |