00001 /*********************************************************************** 00002 Memory primitives 00003 00004 (c) 1994, 1995 Innobase Oy 00005 00006 Created 5/30/1994 Heikki Tuuri 00007 ************************************************************************/ 00008 00009 #ifndef ut0mem_h 00010 #define ut0mem_h 00011 00012 #include "univ.i" 00013 #include <string.h> 00014 #include <stdlib.h> 00015 00016 /* The total amount of memory currently allocated from the OS with malloc */ 00017 extern ulint ut_total_allocated_memory; 00018 00019 UNIV_INLINE 00020 void* 00021 ut_memcpy(void* dest, const void* sour, ulint n); 00022 00023 UNIV_INLINE 00024 void* 00025 ut_memmove(void* dest, const void* sour, ulint n); 00026 00027 UNIV_INLINE 00028 int 00029 ut_memcmp(const void* str1, const void* str2, ulint n); 00030 00031 00032 /************************************************************************** 00033 Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is 00034 defined and set_to_zero is TRUE. */ 00035 00036 void* 00037 ut_malloc_low( 00038 /*==========*/ 00039 /* out, own: allocated memory */ 00040 ulint n, /* in: number of bytes to allocate */ 00041 ibool set_to_zero, /* in: TRUE if allocated memory 00042 should be set to zero if 00043 UNIV_SET_MEM_TO_ZERO is defined */ 00044 ibool assert_on_error); /* in: if TRUE, we crash mysqld if 00045 the memory cannot be allocated */ 00046 /************************************************************************** 00047 Allocates memory. Sets it also to zero if UNIV_SET_MEM_TO_ZERO is 00048 defined. */ 00049 00050 void* 00051 ut_malloc( 00052 /*======*/ 00053 /* out, own: allocated memory */ 00054 ulint n); /* in: number of bytes to allocate */ 00055 /************************************************************************** 00056 Tests if malloc of n bytes would succeed. ut_malloc() asserts if memory runs 00057 out. It cannot be used if we want to return an error message. Prints to 00058 stderr a message if fails. */ 00059 00060 ibool 00061 ut_test_malloc( 00062 /*===========*/ 00063 /* out: TRUE if succeeded */ 00064 ulint n); /* in: try to allocate this many bytes */ 00065 /************************************************************************** 00066 Frees a memory bloock allocated with ut_malloc. */ 00067 00068 void 00069 ut_free( 00070 /*====*/ 00071 void* ptr); /* in, own: memory block */ 00072 /************************************************************************** 00073 Implements realloc. This is needed by /pars/lexyy.c. Otherwise, you should not 00074 use this function because the allocation functions in mem0mem.h are the 00075 recommended ones in InnoDB. 00076 00077 man realloc in Linux, 2004: 00078 00079 realloc() changes the size of the memory block pointed to 00080 by ptr to size bytes. The contents will be unchanged to 00081 the minimum of the old and new sizes; newly allocated mem 00082 ory will be uninitialized. If ptr is NULL, the call is 00083 equivalent to malloc(size); if size is equal to zero, the 00084 call is equivalent to free(ptr). Unless ptr is NULL, it 00085 must have been returned by an earlier call to malloc(), 00086 calloc() or realloc(). 00087 00088 RETURN VALUE 00089 realloc() returns a pointer to the newly allocated memory, 00090 which is suitably aligned for any kind of variable and may 00091 be different from ptr, or NULL if the request fails. If 00092 size was equal to 0, either NULL or a pointer suitable to 00093 be passed to free() is returned. If realloc() fails the 00094 original block is left untouched - it is not freed or 00095 moved. */ 00096 00097 void* 00098 ut_realloc( 00099 /*=======*/ 00100 /* out, own: pointer to new mem block or NULL */ 00101 void* ptr, /* in: pointer to old block or NULL */ 00102 ulint size); /* in: desired size */ 00103 /************************************************************************** 00104 Frees in shutdown all allocated memory not freed yet. */ 00105 00106 void 00107 ut_free_all_mem(void); 00108 /*=================*/ 00109 00110 UNIV_INLINE 00111 char* 00112 ut_strcpy(char* dest, const char* sour); 00113 00114 UNIV_INLINE 00115 ulint 00116 ut_strlen(const char* str); 00117 00118 UNIV_INLINE 00119 int 00120 ut_strcmp(const void* str1, const void* str2); 00121 00122 /************************************************************************** 00123 Copies up to size - 1 characters from the NUL-terminated string src to 00124 dst, NUL-terminating the result. Returns strlen(src), so truncation 00125 occurred if the return value >= size. */ 00126 00127 ulint 00128 ut_strlcpy( 00129 /*=======*/ 00130 /* out: strlen(src) */ 00131 char* dst, /* in: destination buffer */ 00132 const char* src, /* in: source buffer */ 00133 ulint size); /* in: size of destination buffer */ 00134 00135 /************************************************************************** 00136 Like ut_strlcpy, but if src doesn't fit in dst completely, copies the last 00137 (size - 1) bytes of src, not the first. */ 00138 00139 ulint 00140 ut_strlcpy_rev( 00141 /*===========*/ 00142 /* out: strlen(src) */ 00143 char* dst, /* in: destination buffer */ 00144 const char* src, /* in: source buffer */ 00145 ulint size); /* in: size of destination buffer */ 00146 00147 /************************************************************************** 00148 Compute strlen(ut_strcpyq(str, q)). */ 00149 UNIV_INLINE 00150 ulint 00151 ut_strlenq( 00152 /*=======*/ 00153 /* out: length of the string when quoted */ 00154 const char* str, /* in: null-terminated string */ 00155 char q); /* in: the quote character */ 00156 00157 /************************************************************************** 00158 Make a quoted copy of a NUL-terminated string. Leading and trailing 00159 quotes will not be included; only embedded quotes will be escaped. 00160 See also ut_strlenq() and ut_memcpyq(). */ 00161 00162 char* 00163 ut_strcpyq( 00164 /*=======*/ 00165 /* out: pointer to end of dest */ 00166 char* dest, /* in: output buffer */ 00167 char q, /* in: the quote character */ 00168 const char* src); /* in: null-terminated string */ 00169 00170 /************************************************************************** 00171 Make a quoted copy of a fixed-length string. Leading and trailing 00172 quotes will not be included; only embedded quotes will be escaped. 00173 See also ut_strlenq() and ut_strcpyq(). */ 00174 00175 char* 00176 ut_memcpyq( 00177 /*=======*/ 00178 /* out: pointer to end of dest */ 00179 char* dest, /* in: output buffer */ 00180 char q, /* in: the quote character */ 00181 const char* src, /* in: string to be quoted */ 00182 ulint len); /* in: length of src */ 00183 00184 /************************************************************************** 00185 Return the number of times s2 occurs in s1. Overlapping instances of s2 00186 are only counted once. */ 00187 00188 ulint 00189 ut_strcount( 00190 /*========*/ 00191 /* out: the number of times s2 occurs in s1 */ 00192 const char* s1, /* in: string to search in */ 00193 const char* s2); /* in: string to search for */ 00194 00195 /************************************************************************** 00196 Replace every occurrence of s1 in str with s2. Overlapping instances of s1 00197 are only replaced once. */ 00198 00199 char * 00200 ut_strreplace( 00201 /*==========*/ 00202 /* out, own: modified string, must be 00203 freed with mem_free() */ 00204 const char* str, /* in: string to operate on */ 00205 const char* s1, /* in: string to replace */ 00206 const char* s2); /* in: string to replace s1 with */ 00207 00208 #ifndef UNIV_NONINL 00209 #include "ut0mem.ic" 00210 #endif 00211 00212 #endif
1.4.7

