Skip to content

Commit

Permalink
Bug#21881278: SWITCH MYSYS LIBRARY FROM C TO C++
Browse files Browse the repository at this point in the history
This patch switches all mysys/ files from .c to .cc.
By doing this we get stricter compile checks (e.g. type checks)
and it makes it possible to use C++ features for future
bug fixes and extensions inside mysys.
  • Loading branch information
jhauglid committed Sep 29, 2015
1 parent 90f1d8c commit c7a720b
Show file tree
Hide file tree
Showing 118 changed files with 461 additions and 309 deletions.
2 changes: 2 additions & 0 deletions client/check/mysqlcheck.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ static void usage(void)
} /* usage */


extern "C" {
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
char *argument)
Expand Down Expand Up @@ -355,6 +356,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
}
return 0;
}
}


static int get_options(int *argc, char ***argv)
Expand Down
2 changes: 1 addition & 1 deletion client/check/mysqlcheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,4 @@ class Program
}
}

#endif
#endif
6 changes: 3 additions & 3 deletions client/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2809,12 +2809,12 @@ C_MODE_END

#if defined(USE_NEW_READLINE_INTERFACE)
static int fake_magic_space(int, int);
extern "C" char *no_completion(const char*,int)
char *no_completion(const char*,int)
#elif defined(USE_LIBEDIT_INTERFACE)
static int fake_magic_space(const char *, int);
extern "C" int no_completion(const char*,int)
int no_completion(const char*,int)
#else
extern "C" char *no_completion()
char *no_completion()
#endif
{
return 0; /* No filename completion */
Expand Down
4 changes: 4 additions & 0 deletions client/mysql_config_editor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ enum commands {
MY_CONFIG_HELP
};

extern "C" {
struct my_command_data {
const int id;
const char *name;
Expand All @@ -109,6 +110,7 @@ struct my_command_data {
const struct my_option *opt,
char *argument);
};
}


/* mysql_config_editor utility options. */
Expand Down Expand Up @@ -212,6 +214,7 @@ static struct my_option my_help_command_options[]=
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
};

extern "C" {
static my_bool
my_program_get_one_option(int optid,
const struct my_option *opt __attribute__((unused)),
Expand Down Expand Up @@ -321,6 +324,7 @@ my_reset_command_get_one_option(int optid,
}
return 0;
}
}

static struct my_command_data command_data[]=
{
Expand Down
2 changes: 2 additions & 0 deletions client/mysql_secure_installation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ static void free_resources()
free_defaults(defaults_argv);
}

extern "C" {
static my_bool
my_arguments_get_one_option(int optid,
const struct my_option *opt __attribute__((unused)),
Expand Down Expand Up @@ -164,6 +165,7 @@ my_arguments_get_one_option(int optid,
}
return 0;
}
}


/* Initialize options for the given connection handle. */
Expand Down
2 changes: 2 additions & 0 deletions client/mysql_ssl_rsa_setup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ void usage(void)
}


extern "C" {
static my_bool
my_arguments_get_one_option(int optid,
const struct my_option *opt __attribute__((unused)),
Expand All @@ -387,6 +388,7 @@ my_arguments_get_one_option(int optid,
}
return 0;
}
}


static inline
Expand Down
2 changes: 2 additions & 0 deletions client/mysqlslap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,7 @@ static void usage(void)
}


extern "C" {
static my_bool
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
char *argument)
Expand Down Expand Up @@ -820,6 +821,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
}
DBUG_RETURN(0);
}
}


size_t
Expand Down
33 changes: 4 additions & 29 deletions include/my_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,37 +626,12 @@ struct timespec {
#endif /* !HAVE_STRUCT_TIMESPEC */

C_MODE_START
extern ulonglong my_getsystime(void);
C_MODE_END
ulonglong my_getsystime(void);

static inline void set_timespec_nsec(struct timespec *abstime, ulonglong nsec)
{
#ifdef HAVE_STRUCT_TIMESPEC
ulonglong now= my_getsystime() + (nsec / 100);
ulonglong tv_sec= now / 10000000ULL;
#if SIZEOF_TIME_T < SIZEOF_LONG_LONG
/* Ensure that the number of seconds don't overflow. */
tv_sec= MY_MIN(tv_sec, ((ulonglong)INT_MAX32));
#endif
abstime->tv_sec= (time_t)tv_sec;
abstime->tv_nsec= (now % 10000000ULL) * 100 + (nsec % 100);
#else /* !HAVE_STRUCT_TIMESPEC */
ulonglong max_timeout_msec= (nsec / 1000000);
union ft64 tv;
GetSystemTimeAsFileTime(&tv.ft);
abstime->tv.i64= tv.i64 + (__int64)(nsec / 100);
#if SIZEOF_LONG < SIZEOF_LONG_LONG
/* Ensure that the msec value doesn't overflow. */
max_timeout_msec= MY_MIN(max_timeout_msec, ((ulonglong)INT_MAX32));
#endif
abstime->max_timeout_msec= (long)max_timeout_msec;
#endif /* !HAVE_STRUCT_TIMESPEC */
}
void set_timespec_nsec(struct timespec *abstime, ulonglong nsec);

static inline void set_timespec(struct timespec *abstime, ulonglong sec)
{
set_timespec_nsec(abstime, sec * 1000000000ULL);
}
void set_timespec(struct timespec *abstime, ulonglong sec);
C_MODE_END

/**
Compare two timespec structs.
Expand Down
14 changes: 1 addition & 13 deletions include/my_sys.h
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,6 @@ File create_temp_file(char *to, const char *dir, const char *pfx,
int mode, myf MyFlags);

// Use Prealloced_array or std::vector or something similar in C++
#if defined(__cplusplus)

#define init_dynamic_array please_use_an_appropriately_typed_container
#define my_init_dynamic_array please_use_an_appropriately_typed_container

#else

extern my_bool my_init_dynamic_array(DYNAMIC_ARRAY *array,
PSI_memory_key key,
uint element_size,
Expand All @@ -762,8 +755,6 @@ extern my_bool init_dynamic_array(DYNAMIC_ARRAY *array, uint element_size,
#define dynamic_element(array,array_index,type) \
((type)((array)->buffer) +(array_index))

#endif /* __cplusplus */

/* Some functions are still in use in C++, because HASH uses DYNAMIC_ARRAY */
extern my_bool insert_dynamic(DYNAMIC_ARRAY *array, const void *element);
extern void *alloc_dynamic(DYNAMIC_ARRAY *array);
Expand Down Expand Up @@ -801,10 +792,7 @@ extern void free_root(MEM_ROOT *root, myf MyFLAGS);
extern void reset_root_defaults(MEM_ROOT *mem_root, size_t block_size,
size_t prealloc_size);
extern char *strdup_root(MEM_ROOT *root,const char *str);
static inline char *safe_strdup_root(MEM_ROOT *root, const char *str)
{
return str ? strdup_root(root, str) : 0;
}
extern char *safe_strdup_root(MEM_ROOT *root, const char *str);
extern char *strmake_root(MEM_ROOT *root,const char *str,size_t len);
extern void *memdup_root(MEM_ROOT *root,const void *str, size_t len);
extern void set_memroot_max_capacity(MEM_ROOT *mem_root, size_t size);
Expand Down
3 changes: 2 additions & 1 deletion include/thr_lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <my_list.h>
#include "mysql/psi/mysql_thread.h"

extern mysql_mutex_t THR_LOCK_lock;

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -134,7 +136,6 @@ typedef struct st_thr_lock {


extern LIST *thr_lock_thread_list;
extern mysql_mutex_t THR_LOCK_lock;

void thr_lock_info_init(THR_LOCK_INFO *info, my_thread_id thread_id,
mysql_cond_t *suspend);
Expand Down
134 changes: 102 additions & 32 deletions mysys/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,123 @@

INCLUDE_DIRECTORIES(${ZLIB_INCLUDE_DIR} ${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/mysys)

SET(MYSYS_SOURCES array.c charset-def.c charset.c checksum.c
errors.c hash.c list.c mf_cache.c mf_dirname.c mf_fn_ext.c
mf_format.c mf_getdate.c mf_iocache.c mf_iocache2.c mf_keycache.c
mf_keycaches.c mf_loadpath.c mf_pack.c mf_path.c mf_qsort.c mf_qsort2.c
mf_radix.c mf_same.c mf_soundex.c mf_arr_appstr.c
mf_tempfile.c mf_unixpath.c mf_wcomp.c mulalloc.c my_access.c
my_alloc.c my_bit.c my_bitmap.c my_chsize.c
my_compress.c my_copy.c my_create.c my_delete.c
my_div.c my_error.c my_file.c my_fopen.c my_fstream.c
my_gethwaddr.c my_getsystime.c my_getwd.c my_compare.c my_init.c
my_lib.c my_lock.c my_malloc.c my_mess.c
my_mkdir.c my_mmap.c my_once.c my_open.c my_pread.c
my_read.c my_redel.c my_rename.c my_seek.c
my_static.c my_symlink.c my_symlink2.c my_sync.c my_thr_init.c
my_write.c ptr_cmp.c queues.c stacktrace.c
string.c thr_cond.c thr_lock.c thr_mutex.c
thr_rwlock.c tree.c typelib.c base64.c my_memmem.c
lf_alloc-pin.c lf_dynarray.c lf_hash.c
my_rdtsc.c psi_noop.c my_syslog.c
my_chmod.c my_thread.c)
SET(MYSYS_SOURCES
array.cc
base64.cc
charset.cc
charset-def.cc
checksum.cc
errors.cc
hash.cc
lf_alloc-pin.cc
lf_dynarray.cc
lf_hash.cc
list.cc
mf_arr_appstr.cc
mf_cache.cc
mf_dirname.cc
mf_fn_ext.cc
mf_format.cc
mf_getdate.cc
mf_iocache2.cc
mf_iocache.cc
mf_keycache.cc
mf_keycaches.cc
mf_loadpath.cc
mf_pack.cc
mf_path.cc
mf_qsort2.cc
mf_qsort.cc
mf_radix.cc
mf_same.cc
mf_soundex.cc
mf_tempfile.cc
mf_unixpath.cc
mf_wcomp.cc
mulalloc.cc
my_access.cc
my_alloc.cc
my_bit.cc
my_bitmap.cc
my_chmod.cc
my_chsize.cc
my_compare.cc
my_compress.cc
my_copy.cc
my_create.cc
my_delete.cc
my_div.cc
my_error.cc
my_file.cc
my_fopen.cc
my_fstream.cc
my_gethwaddr.cc
my_getsystime.cc
my_getwd.cc
my_init.cc
my_lib.cc
my_lock.cc
my_malloc.cc
my_memmem.cc
my_mess.cc
my_mkdir.cc
my_mmap.cc
my_once.cc
my_open.cc
my_pread.cc
my_rdtsc.cc
my_read.cc
my_redel.cc
my_rename.cc
my_seek.cc
my_static.cc
my_symlink2.cc
my_symlink.cc
my_sync.cc
my_syslog.cc
my_thread.cc
my_thr_init.cc
my_timespec.cc
my_write.cc
psi_noop.cc
ptr_cmp.cc
queues.cc
stacktrace.cc
string.cc
thr_cond.cc
thr_lock.cc
thr_mutex.cc
thr_rwlock.cc
tree.cc
typelib.cc
)

IF (WIN32)
LIST(APPEND MYSYS_SOURCES
my_conio.c
my_windac.c
my_winerr.c
my_winfile.c
win_timers.c
my_conio.cc
my_windac.cc
my_winerr.cc
my_winfile.cc
win_timers.cc
)
ENDIF()

IF(HAVE_POSIX_TIMERS)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} posix_timers.c)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} posix_timers.cc)
ENDIF()

IF(HAVE_KQUEUE_TIMERS)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} kqueue_timers.c)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} kqueue_timers.cc)
ENDIF()

IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND CMAKE_C_COMPILER_ID MATCHES "SunPro")
# Inline assembly template for rdtsc
SET_SOURCE_FILES_PROPERTIES(my_rdtsc.c
SET_SOURCE_FILES_PROPERTIES(my_rdtsc.cc
PROPERTIES COMPILE_FLAGS "${CMAKE_CURRENT_SOURCE_DIR}/my_timer_cycles.il")
ENDIF()

IF(HAVE_LINUX_LARGE_PAGES)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} my_largepage.c)
SET(MYSYS_SOURCES ${MYSYS_SOURCES} my_largepage.cc)
ENDIF()

ADD_CONVENIENCE_LIBRARY(mysys ${MYSYS_SOURCES})
Expand All @@ -74,15 +144,15 @@ IF(CMAKE_USE_PTHREADS_INIT AND CMAKE_C_FLAGS MATCHES "-fsanitize=")
TARGET_LINK_LIBRARIES(mysys pthread)
ENDIF()

ADD_EXECUTABLE(thr_lock thr_lock.c)
ADD_EXECUTABLE(thr_lock thr_lock.cc)
TARGET_LINK_LIBRARIES(thr_lock mysys)
SET_TARGET_PROPERTIES(thr_lock PROPERTIES COMPILE_FLAGS "-DMAIN")
ADD_EXECUTABLE(queues queues.c)
ADD_EXECUTABLE(queues queues.cc)
TARGET_LINK_LIBRARIES(queues mysys)
SET_TARGET_PROPERTIES(queues PROPERTIES COMPILE_FLAGS "-DMAIN")
ADD_TEST(queues_test queues)

ADD_EXECUTABLE(base64_test base64.c)
ADD_EXECUTABLE(base64_test base64.cc)
SET_TARGET_PROPERTIES(base64_test PROPERTIES COMPILE_FLAGS "-DMAIN")
TARGET_LINK_LIBRARIES(base64_test mysys)

Expand Down
Loading

0 comments on commit c7a720b

Please sign in to comment.