OSDN Git Service

First version
[st-ro/stro.git] / 3rdparty / mysql / include / my_sys.h
1 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
2
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; version 2 of the License.
6
7    This program is distributed in the hope that it will be useful,
8    but WITHOUT ANY WARRANTY; without even the implied warranty of
9    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10    GNU General Public License for more details.
11
12    You should have received a copy of the GNU General Public License
13    along with this program; if not, write to the Free Software
14    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
15
16 #ifndef _my_sys_h
17 #define _my_sys_h
18
19 #include "my_global.h"                  /* C_MODE_START, C_MODE_END */
20 #include "m_ctype.h"                    /* for CHARSET_INFO */
21
22 #include "my_thread.h"                  /* Needed for psi.h */
23 #include "mysql/psi/psi.h"
24 #include "mysql/service_mysql_alloc.h"
25 #include "mysql/psi/mysql_memory.h"
26 #include "mysql/psi/mysql_thread.h"
27
28 #ifdef HAVE_ALLOCA_H
29 #include <alloca.h>
30 #endif
31 #ifdef _WIN32
32 #include <malloc.h>
33 #endif
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
37
38 C_MODE_START
39
40 #ifdef HAVE_VALGRIND
41 # include <valgrind/valgrind.h>
42 # define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) VALGRIND_MALLOCLIKE_BLOCK(p1, p2, p3, p4)
43 # define MEM_FREELIKE_BLOCK(p1, p2) VALGRIND_FREELIKE_BLOCK(p1, p2)
44 # include <valgrind/memcheck.h>
45 # define MEM_UNDEFINED(a,len) VALGRIND_MAKE_MEM_UNDEFINED(a,len)
46 # define MEM_NOACCESS(a,len) VALGRIND_MAKE_MEM_NOACCESS(a,len)
47 # define MEM_CHECK_ADDRESSABLE(a,len) VALGRIND_CHECK_MEM_IS_ADDRESSABLE(a,len)
48 #else /* HAVE_VALGRIND */
49 # define MEM_MALLOCLIKE_BLOCK(p1, p2, p3, p4) do {} while (0)
50 # define MEM_FREELIKE_BLOCK(p1, p2) do {} while (0)
51 # define MEM_UNDEFINED(a,len) ((void) 0)
52 # define MEM_NOACCESS(a,len) ((void) 0)
53 # define MEM_CHECK_ADDRESSABLE(a,len) ((void) 0)
54 #endif /* HAVE_VALGRIND */
55
56 #include <typelib.h>
57
58 #define MY_INIT(name)   { my_progname= name; my_init(); }
59
60 /**
61   Max length of an error message generated by mysys utilities.
62   Some mysys functions produce error messages. These mostly go
63   to stderr.
64   This constant defines the size of the buffer used to format
65   the message. It should be kept in sync with MYSQL_ERRMSG_SIZE,
66   since sometimes mysys errors are stored in the server diagnostics
67   area, and we would like to avoid unexpected truncation.
68 */
69 #define MYSYS_ERRMSG_SIZE   (512)
70 #define MYSYS_STRERROR_SIZE (128)
71
72 #define MY_FILE_ERROR   ((size_t) -1)
73
74         /* General bitmaps for my_func's */
75 #define MY_FFNF         1       /* Fatal if file not found */
76 #define MY_FNABP        2       /* Fatal if not all bytes read/writen */
77 #define MY_NABP         4       /* Error if not all bytes read/writen */
78 #define MY_FAE          8       /* Fatal if any error */
79 #define MY_WME          16      /* Write message on error */
80 #define MY_WAIT_IF_FULL 32      /* Wait and try again if disk full error */
81 #define MY_IGNORE_BADFD 32      /* my_sync: ignore 'bad descriptor' errors */
82 #define MY_SYNC_DIR     8192    /* my_create/delete/rename: sync directory */
83 #define MY_UNUSED       64      /* Unused (was support for RAID) */
84 #define MY_FULL_IO     512      /* For my_read - loop intil I/O is complete */
85 #define MY_DONT_CHECK_FILESIZE 128 /* Option to init_io_cache() */
86 #define MY_LINK_WARNING 32      /* my_redel() gives warning if links */
87 #define MY_COPYTIME     64      /* my_redel() copys time */
88 #define MY_DELETE_OLD   256     /* my_create_with_symlink() */
89 #define MY_RESOLVE_LINK 128     /* my_realpath(); Only resolve links */
90 #define MY_HOLD_ORIGINAL_MODES 128  /* my_copy() holds to file modes */
91 #define MY_REDEL_MAKE_BACKUP 256
92 #define MY_REDEL_NO_COPY_STAT 512 /* my_redel() doesn't call my_copystat() */
93 #define MY_SEEK_NOT_DONE 32     /* my_lock may have to do a seek */
94 #define MY_DONT_WAIT    64      /* my_lock() don't wait if can't lock */
95 #define MY_ZEROFILL     32      /* my_malloc(), fill array with zero */
96 #define MY_ALLOW_ZERO_PTR 64    /* my_realloc() ; zero ptr -> malloc */
97 #define MY_FREE_ON_ERROR 128    /* my_realloc() ; Free old ptr on error */
98 #define MY_HOLD_ON_ERROR 256    /* my_realloc() ; Return old ptr on error */
99 #define MY_DONT_OVERWRITE_FILE 1024     /* my_copy: Don't overwrite file */
100 #define MY_THREADSAFE 2048      /* my_seek(): lock fd mutex */
101 #define MY_SYNC       4096      /* my_copy(): sync dst file */
102
103 #define MYF_RW MYF(MY_WME+MY_NABP)              /* For my_read & my_write */
104
105 #define MY_CHECK_ERROR  1       /* Params to my_end; Check open-close */
106 #define MY_GIVE_INFO    2       /* Give time info about process*/
107 #define MY_DONT_FREE_DBUG 4     /* Do not call DBUG_END() in my_end() */
108
109 /* Flags for my_error() */
110 #define ME_BELL         4       /* DEPRECATED: Ring bell then printing message */
111 #define ME_ERRORLOG     64      /* Write the error message to error log */
112 #define ME_FATALERROR   1024    /* Fatal statement error */
113
114         /* Bits in last argument to fn_format */
115 #define MY_REPLACE_DIR          1       /* replace dir in name with 'dir' */
116 #define MY_REPLACE_EXT          2       /* replace extension with 'ext' */
117 #define MY_UNPACK_FILENAME      4       /* Unpack name (~ -> home) */
118 #define MY_PACK_FILENAME        8       /* Pack name (home -> ~) */
119 #define MY_RESOLVE_SYMLINKS     16      /* Resolve all symbolic links */
120 #define MY_RETURN_REAL_PATH     32      /* return full path for file */
121 #define MY_SAFE_PATH            64      /* Return NULL if too long path */
122 #define MY_RELATIVE_PATH        128     /* name is relative to 'dir' */
123 #define MY_APPEND_EXT           256     /* add 'ext' as additional extension*/
124
125
126         /* My seek flags */
127 #define MY_SEEK_SET     0
128 #define MY_SEEK_CUR     1
129 #define MY_SEEK_END     2
130
131         /* Some constants */
132 #define MY_WAIT_FOR_USER_TO_FIX_PANIC   60      /* in seconds */
133 #define MY_WAIT_GIVE_USER_A_MESSAGE     10      /* Every 10 times of prev */
134 #define MIN_COMPRESS_LENGTH             50      /* Don't compress small bl. */
135 #define DFLT_INIT_HITS  3
136
137         /* root_alloc flags */
138 #define MY_KEEP_PREALLOC        1
139 #define MY_MARK_BLOCKS_FREE     2  /* move used to free list and reuse them */
140
141         /* Internal error numbers (for assembler functions) */
142 #define MY_ERRNO_EDOM           33
143 #define MY_ERRNO_ERANGE         34
144
145         /* Bits for get_date timeflag */
146 #define GETDATE_DATE_TIME       1
147 #define GETDATE_SHORT_DATE      2
148 #define GETDATE_HHMMSSTIME      4
149 #define GETDATE_GMT             8
150 #define GETDATE_FIXEDLENGTH     16
151
152         /* defines when allocating data */
153 extern void *my_multi_malloc(PSI_memory_key key, myf flags, ...);
154
155 /*
156   Switch to my_malloc() if the memory block to be allocated is bigger than
157   max_alloca_sz.
158 */
159 extern PSI_memory_key key_memory_max_alloca;
160 #define my_safe_alloca(size, max_alloca_sz) ((size <= max_alloca_sz) ? \
161                                              my_alloca(size) : \
162                                              my_malloc(key_memory_max_alloca, size, MYF(0)))
163 #define my_safe_afree(ptr, size, max_alloca_sz) if (size > max_alloca_sz) \
164                                                my_free(ptr)
165
166 #if !defined(DBUG_OFF) || defined(HAVE_VALGRIND)
167 /**
168   Put bad content in memory to be sure it will segfault if dereferenced.
169   With Valgrind, verify that memory is addressable, and mark it undefined.
170   We cache value of B because if B is expression which depends on A, memset()
171   trashes value of B.
172 */
173 #define TRASH(A,B) do {                                                 \
174     const size_t l= (B);                                                \
175     MEM_CHECK_ADDRESSABLE(A, l);                                        \
176     memset(A, 0x8F, l);                                                 \
177     MEM_UNDEFINED(A, l);                                                \
178   } while (0)
179 #else
180 #define TRASH(A,B) do {} while(0)
181 #endif
182 #if defined(ENABLED_DEBUG_SYNC)
183 extern void (*debug_sync_C_callback_ptr)(const char *, size_t);
184 #define DEBUG_SYNC_C(_sync_point_name_) do {                            \
185     if (debug_sync_C_callback_ptr != NULL)                              \
186       (*debug_sync_C_callback_ptr)(STRING_WITH_LEN(_sync_point_name_)); } \
187   while(0)
188 #define DEBUG_SYNC_C_IF_THD(thd, _sync_point_name_) do {                \
189     if (debug_sync_C_callback_ptr != NULL && thd)                       \
190       (*debug_sync_C_callback_ptr)(STRING_WITH_LEN(_sync_point_name_)); } \
191   while(0)
192 #else
193 #define DEBUG_SYNC_C(_sync_point_name_)
194 #define DEBUG_SYNC_C_IF_THD(thd, _sync_point_name_)
195 #endif /* defined(ENABLED_DEBUG_SYNC) */
196
197 #ifdef HAVE_LINUX_LARGE_PAGES
198 extern uint my_get_large_page_size(void);
199 extern uchar * my_large_malloc(PSI_memory_key key, size_t size, myf my_flags);
200 extern void my_large_free(uchar *ptr);
201 extern my_bool my_use_large_pages;
202 extern uint    my_large_page_size;
203 #else
204 #define my_get_large_page_size() (0)
205 #define my_large_malloc(A,B,C) my_malloc((A),(B),(C))
206 #define my_large_free(A) my_free((A))
207 #endif /* HAVE_LINUX_LARGE_PAGES */
208
209 #define my_alloca(SZ) alloca((size_t) (SZ))
210
211 #include <errno.h>                      /* errno is a define */
212
213 extern char *home_dir;                  /* Home directory for user */
214 extern const char *my_progname;         /* program-name (printed in errors) */
215 extern char curr_dir[];         /* Current directory for user */
216 extern void (*error_handler_hook)(uint my_err, const char *str,myf MyFlags);
217 extern void (*fatal_error_handler_hook)(uint my_err, const char *str,
218                                        myf MyFlags);
219 extern void (*local_message_hook)(enum loglevel ll,
220                                   const char *format, va_list args);
221 extern uint my_file_limit;
222 extern MYSQL_PLUGIN_IMPORT ulong my_thread_stack_size;
223
224 /*
225   Hooks for reporting execution stage information. The server implementation
226   of these will also set THD::current_cond/current_mutex.
227   By having hooks, we avoid direct dependencies on server code.
228 */
229 extern void (*enter_cond_hook)(void *opaque_thd,
230                                mysql_cond_t *cond,
231                                mysql_mutex_t *mutex,
232                                const PSI_stage_info *stage,
233                                PSI_stage_info *old_stage,
234                                const char *src_function,
235                                const char *src_file,
236                                int src_line);
237
238 extern void (*exit_cond_hook)(void *opaque_thd,
239                               const PSI_stage_info *stage,
240                               const char *src_function,
241                               const char *src_file,
242                               int src_line);
243
244 /*
245   Hook for checking if the thread has been killed.
246 */
247 extern int (*is_killed_hook)(const void *opaque_thd);
248
249 /* charsets */
250 #define MY_ALL_CHARSETS_SIZE 2048
251 extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *default_charset_info;
252 extern MYSQL_PLUGIN_IMPORT CHARSET_INFO *all_charsets[MY_ALL_CHARSETS_SIZE];
253 extern CHARSET_INFO compiled_charsets[];
254
255 /* statistics */
256 extern ulong    my_file_opened,my_stream_opened, my_tmp_file_created;
257 extern ulong    my_file_total_opened;
258 extern my_bool  my_init_done;
259
260 extern MYSQL_PLUGIN_IMPORT int my_umask;                /* Default creation mask  */
261 extern int my_umask_dir;
262
263 extern ulong    my_default_record_cache_size;
264 extern my_bool  my_disable_locking,
265                 my_enable_symlinks;
266 extern char     wild_many,wild_one,wild_prefix;
267 extern const char *charsets_dir;
268
269 enum cache_type
270 {
271   TYPE_NOT_SET= 0, READ_CACHE, WRITE_CACHE,
272   SEQ_READ_APPEND               /* sequential read or append */,
273   READ_FIFO, READ_NET,WRITE_NET};
274
275 enum flush_type
276 {
277   FLUSH_KEEP,           /* flush block and keep it in the cache */
278   FLUSH_RELEASE,        /* flush block and remove it from the cache */
279   FLUSH_IGNORE_CHANGED, /* remove block from the cache */
280   /*
281     As my_disable_flush_pagecache_blocks is always 0, the following option
282     is strictly equivalent to FLUSH_KEEP
283   */
284   FLUSH_FORCE_WRITE
285 };
286
287 enum file_type
288 {
289   UNOPEN = 0, FILE_BY_OPEN, FILE_BY_CREATE, STREAM_BY_FOPEN, STREAM_BY_FDOPEN,
290   FILE_BY_MKSTEMP, FILE_BY_DUP
291 };
292
293 struct st_my_file_info
294 {
295   char  *name;
296 #ifdef _WIN32
297   HANDLE fhandle;   /* win32 file handle */
298   int    oflag;     /* open flags, e.g O_APPEND */
299 #endif
300   enum   file_type      type;
301 };
302
303 extern struct st_my_file_info *my_file_info;
304
305 /* needed for client-only build */
306 #ifndef PSI_FILE_KEY_DEFINED
307 typedef unsigned int PSI_file_key;
308 #define PSI_FILE_KEY_DEFINED
309 #endif
310
311 typedef struct st_dynamic_array
312 {
313   uchar *buffer;
314   uint elements,max_element;
315   uint alloc_increment;
316   uint size_of_element;
317   PSI_memory_key m_psi_key;
318 } DYNAMIC_ARRAY;
319
320 typedef struct st_my_tmpdir
321 {
322   char **list;
323   uint cur, max;
324   mysql_mutex_t mutex;
325 } MY_TMPDIR;
326
327 typedef struct st_dynamic_string
328 {
329   char *str;
330   size_t length,max_length,alloc_increment;
331 } DYNAMIC_STRING;
332
333 struct st_io_cache;
334 typedef int (*IO_CACHE_CALLBACK)(struct st_io_cache*);
335
336 typedef struct st_io_cache_share
337 {
338   mysql_mutex_t       mutex;           /* To sync on reads into buffer. */
339   mysql_cond_t        cond;            /* To wait for signals. */
340   mysql_cond_t        cond_writer;     /* For a synchronized writer. */
341   /* Offset in file corresponding to the first byte of buffer. */
342   my_off_t              pos_in_file;
343   /* If a synchronized write cache is the source of the data. */
344   struct st_io_cache    *source_cache;
345   uchar                 *buffer;         /* The read buffer. */
346   uchar                 *read_end;       /* Behind last valid byte of buffer. */
347   int                   running_threads; /* threads not in lock. */
348   int                   total_threads;   /* threads sharing the cache. */
349   int                   error;           /* Last error. */
350 } IO_CACHE_SHARE;
351
352 typedef struct st_io_cache              /* Used when cacheing files */
353 {
354   /* Offset in file corresponding to the first byte of uchar* buffer. */
355   my_off_t pos_in_file;
356   /*
357     The offset of end of file for READ_CACHE and WRITE_CACHE.
358     For SEQ_READ_APPEND it the maximum of the actual end of file and
359     the position represented by read_end.
360   */
361   my_off_t end_of_file;
362   /* Points to current read position in the buffer */
363   uchar *read_pos;
364   /* the non-inclusive boundary in the buffer for the currently valid read */
365   uchar  *read_end;
366   uchar  *buffer;                               /* The read buffer */
367   /* Used in ASYNC_IO */
368   uchar  *request_pos;
369
370   /* Only used in WRITE caches and in SEQ_READ_APPEND to buffer writes */
371   uchar  *write_buffer;
372   /*
373     Only used in SEQ_READ_APPEND, and points to the current read position
374     in the write buffer. Note that reads in SEQ_READ_APPEND caches can
375     happen from both read buffer (uchar* buffer) and write buffer
376     (uchar* write_buffer).
377   */
378   uchar *append_read_pos;
379   /* Points to current write position in the write buffer */
380   uchar *write_pos;
381   /* The non-inclusive boundary of the valid write area */
382   uchar *write_end;
383
384   /*
385     Current_pos and current_end are convenience variables used by
386     my_b_tell() and other routines that need to know the current offset
387     current_pos points to &write_pos, and current_end to &write_end in a
388     WRITE_CACHE, and &read_pos and &read_end respectively otherwise
389   */
390   uchar  **current_pos, **current_end;
391
392   /*
393     The lock is for append buffer used in SEQ_READ_APPEND cache
394     need mutex copying from append buffer to read buffer.
395   */
396   mysql_mutex_t append_buffer_lock;
397   /*
398     The following is used when several threads are reading the
399     same file in parallel. They are synchronized on disk
400     accesses reading the cached part of the file asynchronously.
401     It should be set to NULL to disable the feature.  Only
402     READ_CACHE mode is supported.
403   */
404   IO_CACHE_SHARE *share;
405
406   /*
407     A caller will use my_b_read() macro to read from the cache
408     if the data is already in cache, it will be simply copied with
409     memcpy() and internal variables will be accordinging updated with
410     no functions invoked. However, if the data is not fully in the cache,
411     my_b_read() will call read_function to fetch the data. read_function
412     must never be invoked directly.
413   */
414   int (*read_function)(struct st_io_cache *,uchar *,size_t);
415   /*
416     Same idea as in the case of read_function, except my_b_write() needs to
417     be replaced with my_b_append() for a SEQ_READ_APPEND cache
418   */
419   int (*write_function)(struct st_io_cache *,const uchar *,size_t);
420   /*
421     Specifies the type of the cache. Depending on the type of the cache
422     certain operations might not be available and yield unpredicatable
423     results. Details to be documented later
424   */
425   enum cache_type type;
426   /*
427     Callbacks when the actual read I/O happens. These were added and
428     are currently used for binary logging of LOAD DATA INFILE - when a
429     block is read from the file, we create a block create/append event, and
430     when IO_CACHE is closed, we create an end event. These functions could,
431     of course be used for other things
432   */
433   IO_CACHE_CALLBACK pre_read;
434   IO_CACHE_CALLBACK post_read;
435   IO_CACHE_CALLBACK pre_close;
436   /*
437     Counts the number of times, when we were forced to use disk. We use it to
438     increase the binlog_cache_disk_use and binlog_stmt_cache_disk_use status
439     variables.
440   */
441   ulong disk_writes;
442   void* arg;                            /* for use by pre/post_read */
443   char *file_name;                      /* if used with 'open_cached_file' */
444   char *dir,*prefix;
445   File file; /* file descriptor */
446   PSI_file_key file_key; /* instrumented file key */
447
448   /*
449     seek_not_done is set by my_b_seek() to inform the upcoming read/write
450     operation that a seek needs to be preformed prior to the actual I/O
451     error is 0 if the cache operation was successful, -1 if there was a
452     "hard" error, and the actual number of I/O-ed bytes if the read/write was
453     partial.
454   */
455   int   seek_not_done,error;
456   /* buffer_length is memory size allocated for buffer or write_buffer */
457   size_t        buffer_length;
458   /* read_length is the same as buffer_length except when we use async io */
459   size_t  read_length;
460   myf   myflags;                        /* Flags used to my_read/my_write */
461   /*
462     alloced_buffer is 1 if the buffer was allocated by init_io_cache() and
463     0 if it was supplied by the user.
464     Currently READ_NET is the only one that will use a buffer allocated
465     somewhere else
466   */
467   my_bool alloced_buffer;
468 } IO_CACHE;
469
470 typedef int (*qsort2_cmp)(const void *, const void *, const void *);
471
472 typedef void (*my_error_reporter)(enum loglevel level, const char *format, ...)
473   MY_ATTRIBUTE((format(printf, 2, 3)));
474
475 extern my_error_reporter my_charset_error_reporter;
476
477 /* defines for mf_iocache */
478 extern PSI_file_key key_file_io_cache;
479
480 /* Test if buffer is inited */
481 #define my_b_clear(info) (info)->buffer=0
482 #define my_b_inited(info) (info)->buffer
483 #define my_b_EOF INT_MIN
484
485 #define my_b_read(info,Buffer,Count) \
486   ((info)->read_pos + (Count) <= (info)->read_end ?\
487    (memcpy(Buffer,(info)->read_pos,(size_t) (Count)), \
488     ((info)->read_pos+=(Count)),0) :\
489    (*(info)->read_function)((info),Buffer,Count))
490
491 #define my_b_write(info,Buffer,Count) \
492  ((info)->write_pos + (Count) <=(info)->write_end ?\
493   (memcpy((info)->write_pos, (Buffer), (size_t)(Count)),\
494    ((info)->write_pos+=(Count)),0) : \
495    (*(info)->write_function)((info),(uchar *)(Buffer),(Count)))
496
497 #define my_b_get(info) \
498   ((info)->read_pos != (info)->read_end ?\
499    ((info)->read_pos++, (int) (uchar) (info)->read_pos[-1]) :\
500    _my_b_get(info))
501
502 #define my_b_tell(info) ((info)->pos_in_file + \
503                          (size_t) (*(info)->current_pos - (info)->request_pos))
504
505 #define my_b_get_buffer_start(info) (info)->request_pos
506 #define my_b_get_bytes_in_buffer(info) (char*) (info)->read_end -   \
507   (char*) my_b_get_buffer_start(info)
508 #define my_b_get_pos_in_file(info) (info)->pos_in_file
509
510 /* tell write offset in the SEQ_APPEND cache */
511 int      my_b_copy_to_file(IO_CACHE *cache, FILE *file);
512 my_off_t my_b_append_tell(IO_CACHE* info);
513 my_off_t my_b_safe_tell(IO_CACHE* info); /* picks the correct tell() */
514
515 #define my_b_bytes_in_cache(info) (size_t) (*(info)->current_end - \
516                                           *(info)->current_pos)
517
518 typedef uint32 ha_checksum;
519
520 #include <my_alloc.h>
521
522
523         /* Prototypes for mysys and my_func functions */
524
525 extern int my_copy(const char *from,const char *to,myf MyFlags);
526 extern int my_delete(const char *name,myf MyFlags);
527 extern int my_getwd(char * buf,size_t size,myf MyFlags);
528 extern int my_setwd(const char *dir,myf MyFlags);
529 extern int my_lock(File fd,int op,my_off_t start, my_off_t length,myf MyFlags);
530 extern void *my_once_alloc(size_t Size,myf MyFlags);
531 extern void my_once_free(void);
532 extern char *my_once_strdup(const char *src,myf myflags);
533 extern void *my_once_memdup(const void *src, size_t len, myf myflags);
534 extern File my_open(const char *FileName,int Flags,myf MyFlags);
535 extern File my_register_filename(File fd, const char *FileName,
536                                  enum file_type type_of_file,
537                                  uint error_message_number, myf MyFlags);
538 extern File my_create(const char *FileName,int CreateFlags,
539                       int AccessFlags, myf MyFlags);
540 extern int my_close(File Filedes,myf MyFlags);
541 extern int my_mkdir(const char *dir, int Flags, myf MyFlags);
542 extern int my_readlink(char *to, const char *filename, myf MyFlags);
543 extern int my_is_symlink(const char *filename);
544 extern int my_realpath(char *to, const char *filename, myf MyFlags);
545 extern File my_create_with_symlink(const char *linkname, const char *filename,
546                                    int createflags, int access_flags,
547                                    myf MyFlags);
548 extern int my_delete_with_symlink(const char *name, myf MyFlags);
549 extern int my_rename_with_symlink(const char *from,const char *to,myf MyFlags);
550 extern int my_symlink(const char *content, const char *linkname, myf MyFlags);
551 extern size_t my_read(File Filedes,uchar *Buffer,size_t Count,myf MyFlags);
552 extern size_t my_pread(File Filedes,uchar *Buffer,size_t Count,my_off_t offset,
553                      myf MyFlags);
554 extern int my_rename(const char *from,const char *to,myf MyFlags);
555 extern my_off_t my_seek(File fd,my_off_t pos,int whence,myf MyFlags);
556 extern my_off_t my_tell(File fd,myf MyFlags);
557 extern size_t my_write(File Filedes,const uchar *Buffer,size_t Count,
558                      myf MyFlags);
559 extern size_t my_pwrite(File Filedes,const uchar *Buffer,size_t Count,
560                       my_off_t offset,myf MyFlags);
561 extern size_t my_fread(FILE *stream,uchar *Buffer,size_t Count,myf MyFlags);
562 extern size_t my_fwrite(FILE *stream,const uchar *Buffer,size_t Count,
563                       myf MyFlags);
564 extern my_off_t my_fseek(FILE *stream,my_off_t pos,int whence,myf MyFlags);
565 extern my_off_t my_ftell(FILE *stream,myf MyFlags);
566
567 /* implemented in my_syslog.c */
568
569 // Maximum size of message  that will be logged.
570 #define MAX_SYSLOG_MESSAGE_SIZE 1024
571
572 /* Platform-independent SysLog support */
573
574 /* facilities on unixoid syslog. harmless on systemd / Win platforms. */
575 typedef struct st_syslog_facility { int id; const char *name; } SYSLOG_FACILITY;
576 extern SYSLOG_FACILITY syslog_facility[];
577
578 enum my_syslog_options { MY_SYSLOG_PIDS= 1 };
579
580 int my_openlog(const char *eventSourceName, int option, int facility);
581 int my_closelog();
582 int my_syslog(const CHARSET_INFO *cs, enum loglevel level, const char *msg);
583
584 /* implemented in my_memmem.c */
585 extern void *my_memmem(const void *haystack, size_t haystacklen,
586                        const void *needle, size_t needlelen);
587
588
589 #ifdef _WIN32
590 extern int      my_access(const char *path, int amode);
591 #else
592 #define my_access access
593 #endif
594
595 extern int check_if_legal_filename(const char *path);
596 extern int check_if_legal_tablename(const char *path);
597
598 #ifdef _WIN32
599 extern my_bool is_filename_allowed(const char *name, size_t length,
600                    my_bool allow_current_dir);
601 #else /* _WIN32 */
602 # define is_filename_allowed(name, length, allow_cwd) (TRUE)
603 #endif /* _WIN32 */
604
605 #ifdef _WIN32
606 extern int nt_share_delete(const char *name,myf MyFlags);
607 #define my_delete_allow_opened(fname,flags)  nt_share_delete((fname),(flags))
608 #else
609 #define my_delete_allow_opened(fname,flags)  my_delete((fname),(flags))
610 #endif
611
612 #ifdef _WIN32
613 /* Windows-only functions (CRT equivalents)*/
614 extern HANDLE   my_get_osfhandle(File fd);
615 extern void     my_osmaperr(unsigned long last_error);
616 #endif
617
618 extern const char* get_global_errmsg(int nr);
619 extern void wait_for_free_space(const char *filename, int errors);
620 extern FILE *my_fopen(const char *FileName,int Flags,myf MyFlags);
621 extern FILE *my_fdopen(File Filedes,const char *name, int Flags,myf MyFlags);
622 extern FILE *my_freopen(const char *path, const char *mode, FILE *stream);
623 extern int my_fclose(FILE *fd,myf MyFlags);
624 extern File my_fileno(FILE *fd);
625 extern int my_chsize(File fd,my_off_t newlength, int filler, myf MyFlags);
626 extern void thr_set_sync_wait_callback(void (*before_sync)(void),
627                                        void (*after_sync)(void));
628 extern int my_sync(File fd, myf my_flags);
629 extern int my_sync_dir(const char *dir_name, myf my_flags);
630 extern int my_sync_dir_by_file(const char *file_name, myf my_flags);
631 extern char *my_strerror(char *buf, size_t len, int errnum);
632 extern const char *my_get_err_msg(int nr);
633 extern void my_error(int nr,myf MyFlags, ...);
634 extern void my_printf_error(uint my_err, const char *format,
635                             myf MyFlags, ...)
636   MY_ATTRIBUTE((format(printf, 2, 4)));
637 extern void my_printv_error(uint error, const char *format, myf MyFlags,
638                             va_list ap);
639 extern int my_error_register(const char* (*get_errmsg) (int),
640                              int first, int last);
641 extern my_bool my_error_unregister(int first, int last);
642 extern void my_message(uint my_err, const char *str,myf MyFlags);
643 extern void my_message_stderr(uint my_err, const char *str, myf MyFlags);
644 void my_message_local_stderr(enum loglevel ll,
645                              const char *format, va_list args);
646 extern void my_message_local(enum loglevel ll, const char *format, ...);
647 extern my_bool my_init(void);
648 extern void my_end(int infoflag);
649 extern int my_redel(const char *from, const char *to, int MyFlags);
650 extern int my_copystat(const char *from, const char *to, int MyFlags);
651 extern char * my_filename(File fd);
652 extern MY_MODE get_file_perm(ulong perm_flags);
653 extern my_bool my_chmod(const char *filename, ulong perm_flags, myf my_flags);
654
655 #ifdef EXTRA_DEBUG
656 void my_print_open_files(void);
657 #else
658 #define my_print_open_files()
659 #endif
660
661 extern my_bool init_tmpdir(MY_TMPDIR *tmpdir, const char *pathlist);
662 extern char *my_tmpdir(MY_TMPDIR *tmpdir);
663 extern void free_tmpdir(MY_TMPDIR *tmpdir);
664
665 extern size_t dirname_part(char * to,const char *name, size_t *to_res_length);
666 extern size_t dirname_length(const char *name);
667 #define base_name(A) (A+dirname_length(A))
668 extern int test_if_hard_path(const char *dir_name);
669 extern my_bool has_path(const char *name);
670 extern char *convert_dirname(char *to, const char *from, const char *from_end);
671 extern void to_unix_path(char * name);
672 extern char * fn_ext(const char *name);
673 extern char * fn_same(char * toname,const char *name,int flag);
674 extern char * fn_format(char * to,const char *name,const char *dir,
675                            const char *form, uint flag);
676 extern size_t strlength(const char *str);
677 extern void pack_dirname(char * to,const char *from);
678 extern size_t normalize_dirname(char * to, const char *from);
679 extern size_t unpack_dirname(char * to,const char *from);
680 extern size_t cleanup_dirname(char * to,const char *from);
681 extern size_t system_filename(char * to,const char *from);
682 extern size_t unpack_filename(char * to,const char *from);
683 extern char * intern_filename(char * to,const char *from);
684 extern char * directory_file_name(char * dst, const char *src);
685 extern int pack_filename(char * to, const char *name, size_t max_length);
686 extern char * my_path(char * to,const char *progname,
687                          const char *own_pathname_part);
688 extern char * my_load_path(char * to, const char *path,
689                               const char *own_path_prefix);
690 extern int wild_compare(const char *str,const char *wildstr,
691                         pbool str_is_pattern);
692 extern my_bool array_append_string_unique(const char *str,
693                                           const char **array, size_t size);
694 extern void get_date(char * to,int timeflag,time_t use_time);
695 extern void soundex(CHARSET_INFO *, char * out_pntr, char * in_pntr,
696                     pbool remove_garbage);
697
698 extern my_bool radixsort_is_appliccable(uint n_items, size_t size_of_element);
699 extern void radixsort_for_str_ptr(uchar* base[], uint number_of_elements,
700                                   size_t size_of_element,uchar *buffer[]);
701 extern void my_qsort(void *base_ptr, size_t total_elems, size_t size,
702                      qsort_cmp cmp);
703 extern void my_qsort2(void *base_ptr, size_t total_elems, size_t size,
704                       qsort2_cmp cmp, const void *cmp_argument);
705 void my_store_ptr(uchar *buff, size_t pack_length, my_off_t pos);
706 my_off_t my_get_ptr(uchar *ptr, size_t pack_length);
707 extern int init_io_cache_ext(IO_CACHE *info,File file,size_t cachesize,
708                              enum cache_type type,my_off_t seek_offset,
709                              pbool use_async_io, myf cache_myflags,
710                              PSI_file_key file_key);
711 extern int init_io_cache(IO_CACHE *info,File file,size_t cachesize,
712                          enum cache_type type,my_off_t seek_offset,
713                          pbool use_async_io, myf cache_myflags);
714 extern my_bool reinit_io_cache(IO_CACHE *info,enum cache_type type,
715                                my_off_t seek_offset,pbool use_async_io,
716                                pbool clear_cache);
717 extern void setup_io_cache(IO_CACHE* info);
718 extern int _my_b_read(IO_CACHE *info,uchar *Buffer,size_t Count);
719 extern int _my_b_read_r(IO_CACHE *info,uchar *Buffer,size_t Count);
720 extern void init_io_cache_share(IO_CACHE *read_cache, IO_CACHE_SHARE *cshare,
721                                 IO_CACHE *write_cache, uint num_threads);
722 extern void remove_io_thread(IO_CACHE *info);
723 extern int _my_b_seq_read(IO_CACHE *info,uchar *Buffer,size_t Count);
724 extern int _my_b_net_read(IO_CACHE *info,uchar *Buffer,size_t Count);
725 extern int _my_b_get(IO_CACHE *info);
726 extern int _my_b_write(IO_CACHE *info,const uchar *Buffer,size_t Count);
727 extern int my_b_append(IO_CACHE *info,const uchar *Buffer,size_t Count);
728 extern int my_b_safe_write(IO_CACHE *info,const uchar *Buffer,size_t Count);
729
730 extern int my_block_write(IO_CACHE *info, const uchar *Buffer,
731                           size_t Count, my_off_t pos);
732 extern int my_b_flush_io_cache(IO_CACHE *info, int need_append_buffer_lock);
733
734 #define flush_io_cache(info) my_b_flush_io_cache((info),1)
735
736 extern int end_io_cache(IO_CACHE *info);
737 extern size_t my_b_fill(IO_CACHE *info);
738 extern void my_b_seek(IO_CACHE *info,my_off_t pos);
739 extern size_t my_b_gets(IO_CACHE *info, char *to, size_t max_length);
740 extern my_off_t my_b_filelength(IO_CACHE *info);
741 extern size_t my_b_printf(IO_CACHE *info, const char* fmt, ...)
742   MY_ATTRIBUTE((format(printf, 2, 3)));
743 extern size_t my_b_vprintf(IO_CACHE *info, const char* fmt, va_list ap);
744 extern my_bool open_cached_file(IO_CACHE *cache,const char *dir,
745                                  const char *prefix, size_t cache_size,
746                                  myf cache_myflags);
747 extern my_bool real_open_cached_file(IO_CACHE *cache);
748 extern void close_cached_file(IO_CACHE *cache);
749 File create_temp_file(char *to, const char *dir, const char *pfx,
750                       int mode, myf MyFlags);
751
752 // Use Prealloced_array or std::vector or something similar in C++
753 #if defined(__cplusplus)
754
755 #define init_dynamic_array please_use_an_appropriately_typed_container
756 #define my_init_dynamic_array please_use_an_appropriately_typed_container
757
758 #else
759
760 extern my_bool my_init_dynamic_array(DYNAMIC_ARRAY *array,
761                                      PSI_memory_key key,
762                                      uint element_size,
763                                      void *init_buffer,
764                                      uint init_alloc,
765                                      uint alloc_increment);
766 /* init_dynamic_array() function is deprecated */
767 extern my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
768                                   uint init_alloc, uint alloc_increment);
769 #define dynamic_element(array,array_index,type) \
770   ((type)((array)->buffer) +(array_index))
771
772 #endif  /* __cplusplus */
773
774 /* Some functions are still in use in C++, because HASH uses DYNAMIC_ARRAY */
775 extern my_bool insert_dynamic(DYNAMIC_ARRAY *array, const void *element);
776 extern void *alloc_dynamic(DYNAMIC_ARRAY *array);
777 extern void *pop_dynamic(DYNAMIC_ARRAY*);
778 extern void get_dynamic(DYNAMIC_ARRAY *array, void *element, uint array_index);
779 extern void claim_dynamic(DYNAMIC_ARRAY *array);
780 extern void delete_dynamic(DYNAMIC_ARRAY *array);
781 extern void freeze_size(DYNAMIC_ARRAY *array);
782 static inline void reset_dynamic(DYNAMIC_ARRAY *array)
783 {
784   array->elements= 0;
785 }
786
787 extern my_bool init_dynamic_string(DYNAMIC_STRING *str, const char *init_str,
788                                    size_t init_alloc,size_t alloc_increment);
789 extern my_bool dynstr_append(DYNAMIC_STRING *str, const char *append);
790 my_bool dynstr_append_mem(DYNAMIC_STRING *str, const char *append,
791                           size_t length);
792 extern my_bool dynstr_append_os_quoted(DYNAMIC_STRING *str, const char *append,
793                                        ...);
794 extern my_bool dynstr_set(DYNAMIC_STRING *str, const char *init_str);
795 extern my_bool dynstr_realloc(DYNAMIC_STRING *str, size_t additional_size);
796 extern my_bool dynstr_trunc(DYNAMIC_STRING *str, size_t n);
797 extern void dynstr_free(DYNAMIC_STRING *str);
798 #define alloc_root_inited(A) ((A)->min_malloc != 0)
799 #define ALLOC_ROOT_MIN_BLOCK_SIZE (MALLOC_OVERHEAD + sizeof(USED_MEM) + 8)
800 #define clear_alloc_root(A) do { (A)->free= (A)->used= (A)->pre_alloc= 0; (A)->min_malloc=0;} while(0)
801 extern void init_alloc_root(PSI_memory_key key,
802                             MEM_ROOT *mem_root, size_t block_size,
803                             size_t pre_alloc_size);
804 extern void *alloc_root(MEM_ROOT *mem_root, size_t Size);
805 extern void *multi_alloc_root(MEM_ROOT *mem_root, ...);
806 extern void claim_root(MEM_ROOT *root);
807 extern void free_root(MEM_ROOT *root, myf MyFLAGS);
808 extern void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
809                                 size_t prealloc_size);
810 extern char *strdup_root(MEM_ROOT *root,const char *str);
811 static inline char *safe_strdup_root(MEM_ROOT *root, const char *str)
812 {
813   return str ? strdup_root(root, str) : 0;
814 }
815 extern char *strmake_root(MEM_ROOT *root,const char *str,size_t len);
816 extern void *memdup_root(MEM_ROOT *root,const void *str, size_t len);
817 extern my_bool my_compress(uchar *, size_t *, size_t *);
818 extern my_bool my_uncompress(uchar *, size_t , size_t *);
819 extern uchar *my_compress_alloc(const uchar *packet, size_t *len,
820                                 size_t *complen);
821 extern int packfrm(uchar *, size_t, uchar **, size_t *);
822 extern int unpackfrm(uchar **, size_t *, const uchar *);
823
824 extern ha_checksum my_checksum(ha_checksum crc, const uchar *mem,
825                                size_t count);
826
827 /* Wait a given number of microseconds */
828 static inline void my_sleep(time_t m_seconds)
829 {
830 #if defined(_WIN32)
831   Sleep((DWORD)m_seconds/1000+1);      /* Sleep() has millisecond arg */
832 #else
833   struct timeval t;
834   t.tv_sec=  m_seconds / 1000000L;
835   t.tv_usec= m_seconds % 1000000L;
836   select(0,0,0,0,&t); /* sleep */
837 #endif
838 }
839
840 extern ulong crc32(ulong crc, const uchar *buf, uint len);
841 extern uint my_set_max_open_files(uint files);
842 void my_free_open_file_info(void);
843
844 extern time_t my_time(myf flags);
845 extern ulonglong my_getsystime(void);
846 extern ulonglong my_micro_time();
847 extern my_bool my_gethwaddr(uchar *to);
848
849 #ifdef HAVE_SYS_MMAN_H
850 #include <sys/mman.h>
851
852 #ifndef MAP_NOSYNC
853 #define MAP_NOSYNC      0
854 #endif
855
856 #ifdef HAVE_MMAP64
857 #define my_mmap(a,b,c,d,e,f)    mmap64(a,b,c,d,e,f)
858 #else
859 #define my_mmap(a,b,c,d,e,f)    mmap(a,b,c,d,e,f)
860 #endif
861 #define my_munmap(a,b)          munmap((a),(b))
862
863 #else
864 /* not a complete set of mmap() flags, but only those that nesessary */
865 #define PROT_READ        1
866 #define PROT_WRITE       2
867 #define MAP_NORESERVE    0
868 #define MAP_SHARED       0x0001
869 #define MAP_PRIVATE      0x0002
870 #define MAP_NOSYNC       0x0800
871 #define MAP_FAILED       ((void *)-1)
872 #define MS_SYNC          0x0000
873
874 void *my_mmap(void *, size_t, int, int, int, my_off_t);
875 int my_munmap(void *, size_t);
876 #endif
877
878 /* my_getpagesize */
879 static inline int my_getpagesize()
880 {
881 #ifndef _WIN32
882   return getpagesize();
883 #else
884   SYSTEM_INFO si;
885   GetSystemInfo(&si);
886   return (int)si.dwPageSize;
887 #endif
888 }
889
890 int my_msync(int, void *, size_t, int);
891
892 /* character sets */
893 extern void my_charset_loader_init_mysys(MY_CHARSET_LOADER *loader);
894 extern uint get_charset_number(const char *cs_name, uint cs_flags);
895 extern uint get_collation_number(const char *name);
896 extern const char *get_charset_name(uint cs_number);
897
898 extern CHARSET_INFO *get_charset(uint cs_number, myf flags);
899 extern CHARSET_INFO *get_charset_by_name(const char *cs_name, myf flags);
900 extern CHARSET_INFO *my_collation_get_by_name(MY_CHARSET_LOADER *loader,
901                                               const char *name, myf flags);
902 extern CHARSET_INFO *get_charset_by_csname(const char *cs_name,
903                                            uint cs_flags, myf my_flags);
904 extern CHARSET_INFO *my_charset_get_by_name(MY_CHARSET_LOADER *loader,
905                                             const char *name,
906                                             uint cs_flags, myf my_flags);
907 extern my_bool resolve_charset(const char *cs_name,
908                                const CHARSET_INFO *default_cs,
909                                const CHARSET_INFO **cs);
910 extern my_bool resolve_collation(const char *cl_name,
911                                  const CHARSET_INFO *default_cl,
912                                  const CHARSET_INFO **cl);
913 extern void free_charsets(void);
914 extern char *get_charsets_dir(char *buf);
915 extern my_bool my_charset_same(const CHARSET_INFO *cs1,
916                                const CHARSET_INFO *cs2);
917 extern my_bool init_compiled_charsets(myf flags);
918 extern void add_compiled_collation(CHARSET_INFO *cs);
919 extern size_t escape_string_for_mysql(const CHARSET_INFO *charset_info,
920                                       char *to, size_t to_length,
921                                       const char *from, size_t length);
922 #ifdef _WIN32
923 /* File system character set */
924 extern CHARSET_INFO *fs_character_set(void);
925 #endif
926 extern size_t escape_quotes_for_mysql(CHARSET_INFO *charset_info,
927                                   char *to, size_t to_length,
928                                   const char *from, size_t length, char quote);
929 #ifdef _WIN32
930 extern my_bool have_tcpip;              /* Is set if tcpip is used */
931
932 /* implemented in my_windac.c */
933
934 int my_security_attr_create(SECURITY_ATTRIBUTES **psa, const char **perror,
935                             DWORD owner_rights, DWORD everybody_rights);
936
937 void my_security_attr_free(SECURITY_ATTRIBUTES *sa);
938
939 /* implemented in my_conio.c */
940 my_bool my_win_is_console(FILE *file);
941 char *my_win_console_readline(const CHARSET_INFO *cs, char *mbbuf, size_t mbbufsize,
942                               size_t *nread);
943 void my_win_console_write(const CHARSET_INFO *cs, const char *data, size_t datalen);
944 void my_win_console_fputs(const CHARSET_INFO *cs, const char *data);
945 void my_win_console_putc(const CHARSET_INFO *cs, int c);
946 void my_win_console_vfprintf(const CHARSET_INFO *cs, const char *fmt, va_list args);
947 int my_win_translate_command_line_args(const CHARSET_INFO *cs, int *ac, char ***av);
948 #endif /* _WIN32 */
949
950 #ifdef HAVE_PSI_INTERFACE
951 extern MYSQL_PLUGIN_IMPORT struct PSI_bootstrap *PSI_hook;
952 extern void set_psi_server(PSI *psi);
953 void my_init_mysys_psi_keys(void);
954 #endif
955
956 struct st_mysql_file;
957 extern struct st_mysql_file *mysql_stdin;
958
959 C_MODE_END
960 #endif /* _my_sys_h */