Skip to content

Commit bc0d87c

Browse files
author
Mats Kindahl
committed
WL#5825: Using C++ Standard Library with MySQL code
This patch will make it possible to use the C++ Standard Library by removing the min/max macros from the code. The patch makes the following changes: - Define macros MY_MIN and MY_MAX instead of min and max. - Use MY_MIN/MY_MAX in C code - Use MY_MIN/MY_MAX in array declarations - Include <algorithm> to get min/max from std - Add using-declarations in source files - Use std:: prefix for min/max in header files - Turning some constant declarations unsigned - Removing an overloaded use of 'min' as both macro name and variable. - It enables C++ builds for all files
2 parents 9b12c8e + 4631180 commit bc0d87c

File tree

119 files changed

+555
-322
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+555
-322
lines changed

BUILD/SETUP.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ if test -z "$CC" ; then
215215
fi
216216

217217
if test -z "$CXX" ; then
218-
CXX=gcc
218+
CXX=g++
219219
fi
220220

221221
# If ccache (a compiler cache which reduces build time)

client/mysql.cc

+11-6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
#include <signal.h>
4040
#include <violite.h>
4141

42+
#include <algorithm>
43+
44+
using std::min;
45+
using std::max;
46+
4247
#if defined(USE_LIBEDIT_INTERFACE) && defined(HAVE_LOCALE_H)
4348
#include <locale.h>
4449
#endif
@@ -3354,9 +3359,9 @@ print_table_data(MYSQL_RES *result)
33543359
{
33553360
uint length= column_names ? field->name_length : 0;
33563361
if (quick)
3357-
length=max(length,field->length);
3362+
length= max<size_t>(length, field->length);
33583363
else
3359-
length=max(length,field->max_length);
3364+
length= max<size_t>(length, field->max_length);
33603365
if (length < 4 && !IS_NOT_NULL(field->flags))
33613366
length=4; // Room for "NULL"
33623367
field->max_length=length;
@@ -3376,8 +3381,8 @@ print_table_data(MYSQL_RES *result)
33763381
field->name,
33773382
field->name + name_length);
33783383
uint display_length= field->max_length + name_length - numcells;
3379-
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
3380-
MAX_COLUMN_LENGTH),
3384+
tee_fprintf(PAGER, " %-*s |",
3385+
min<int>(display_length, MAX_COLUMN_LENGTH),
33813386
field->name);
33823387
num_flag[off]= IS_NUM(field->type);
33833388
}
@@ -3466,9 +3471,9 @@ static int get_field_disp_length(MYSQL_FIELD *field)
34663471
uint length= column_names ? field->name_length : 0;
34673472

34683473
if (quick)
3469-
length= max(length, field->length);
3474+
length= max<uint>(length, field->length);
34703475
else
3471-
length= max(length, field->max_length);
3476+
length= max<uint>(length, field->max_length);
34723477

34733478
if (length < 4 && !IS_NOT_NULL(field->flags))
34743479
length= 4; /* Room for "NULL" */

client/mysql_upgrade.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ static int extract_variable_from_show(DYNAMIC_STRING* ds, char* value)
546546
if ((value_end= strchr(value_start, '\n')) == NULL)
547547
return 1; /* Unexpected result */
548548

549-
strncpy(value, value_start, min(FN_REFLEN, value_end-value_start));
549+
strncpy(value, value_start, MY_MIN(FN_REFLEN, value_end - value_start));
550550
return 0;
551551
}
552552

client/mysqlbinlog.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@
3939
#include "sql_common.h"
4040
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
4141

42-
#define BIN_LOG_HEADER_SIZE 4
42+
#include <algorithm>
43+
44+
using std::min;
45+
using std::max;
46+
47+
#define BIN_LOG_HEADER_SIZE 4U
4348
#define PROBE_HEADER_LEN (EVENT_LEN_OFFSET+4)
4449

4550

@@ -2074,7 +2079,7 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
20742079
my_off_t length,tmp;
20752080
for (length= start_position_mot ; length > 0 ; length-=tmp)
20762081
{
2077-
tmp=min(length,sizeof(buff));
2082+
tmp= min<size_t>(length, sizeof(buff));
20782083
if (my_b_read(file, buff, (uint) tmp))
20792084
{
20802085
error("Failed reading from file.");

client/mysqldump.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -845,7 +845,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
845845
&err_ptr, &err_len);
846846
if (err_len)
847847
{
848-
strmake(buff, err_ptr, min(sizeof(buff) - 1, err_len));
848+
strmake(buff, err_ptr, MY_MIN(sizeof(buff) - 1, err_len));
849849
fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
850850
exit(1);
851851
}
@@ -4668,7 +4668,7 @@ static ulong find_set(TYPELIB *lib, const char *x, uint length,
46684668

46694669
for (; pos != end && *pos != ','; pos++) ;
46704670
var_len= (uint) (pos - start);
4671-
strmake(buff, start, min(sizeof(buff) - 1, var_len));
4671+
strmake(buff, start, MY_MIN(sizeof(buff) - 1, var_len));
46724672
find= find_type(buff, lib, FIND_TYPE_BASIC);
46734673
if (!find)
46744674
{

client/mysqltest.cc

+7-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454

5555
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE
5656

57+
#include <algorithm>
58+
59+
using std::min;
60+
using std::max;
61+
5762
#ifdef __WIN__
5863
#include <crtdbg.h>
5964
#define SIGNAL_FMT "exception 0x%x"
@@ -5920,9 +5925,9 @@ int read_line(char *buf, int size)
59205925
}
59215926
else if ((c == '{' &&
59225927
(!my_strnncoll_simple(charset_info, (const uchar*) "while", 5,
5923-
(uchar*) buf, min(5, p - buf), 0) ||
5928+
(uchar*) buf, min<my_ptrdiff_t>(5, p - buf), 0) ||
59245929
!my_strnncoll_simple(charset_info, (const uchar*) "if", 2,
5925-
(uchar*) buf, min(2, p - buf), 0))))
5930+
(uchar*) buf, min<my_ptrdiff_t>(2, p - buf), 0))))
59265931
{
59275932
/* Only if and while commands can be terminated by { */
59285933
*p++= c;

client/sql_string.cc

+5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323

2424
#include "sql_string.h"
2525

26+
#include <algorithm>
27+
28+
using std::min;
29+
using std::max;
30+
2631
/*****************************************************************************
2732
** String functions
2833
*****************************************************************************/

config.h.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@
379379

380380
#cmakedefine HAVE_MBSTATE_T
381381

382-
#define MAX_INDEXES 64
382+
#define MAX_INDEXES 64U
383383

384384
#cmakedefine QSORT_TYPE_IS_VOID 1
385385
#cmakedefine RETQSORTTYPE @RETQSORTTYPE@

dbug/dbug.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1323,7 +1323,7 @@ void _db_dump_(uint _line_, const char *keyword,
13231323
if (TRACING)
13241324
{
13251325
Indent(cs, cs->level + 1);
1326-
pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80);
1326+
pos= MY_MIN(MY_MAX(cs->level-cs->stack->sub_level,0)*INDENT,80);
13271327
}
13281328
else
13291329
{
@@ -1743,7 +1743,7 @@ static void Indent(CODE_STATE *cs, int indent)
17431743
{
17441744
REGISTER int count;
17451745

1746-
indent= max(indent-1-cs->stack->sub_level,0)*INDENT;
1746+
indent= MY_MAX(indent-1-cs->stack->sub_level,0)*INDENT;
17471747
for (count= 0; count < indent ; count++)
17481748
{
17491749
if ((count % INDENT) == 0)

include/m_string.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
144144
(DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
145145
MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
146146
*/
147-
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT)) \
147+
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \
148148

149149
extern char *llstr(longlong value,char *buff);
150150
extern char *ullstr(longlong value,char *buff);

include/my_global.h

+2-4
Original file line numberDiff line numberDiff line change
@@ -1378,10 +1378,8 @@ do { doubleget_union _tmp; \
13781378
#define MY_INT64_NUM_DECIMAL_DIGITS 21U
13791379

13801380
/* Define some useful general macros (should be done after all headers). */
1381-
#if !defined(max)
1382-
#define max(a, b) ((a) > (b) ? (a) : (b))
1383-
#define min(a, b) ((a) < (b) ? (a) : (b))
1384-
#endif
1381+
#define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
1382+
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
13851383

13861384
/*
13871385
Only Linux is known to need an explicit sync of the directory to make sure a

libmysql/libmysql.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ void my_net_local_init(NET *net)
11091109
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
11101110
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
11111111
net->retry_count= 1;
1112-
net->max_packet_size= max(net_buffer_length, max_allowed_packet);
1112+
net->max_packet_size= MY_MAX(net_buffer_length, max_allowed_packet);
11131113
}
11141114

11151115
/*
@@ -3197,7 +3197,7 @@ static void fetch_string_with_conversion(MYSQL_BIND *param, char *value,
31973197
copy_length= end - start;
31983198
/* We've got some data beyond offset: copy up to buffer_length bytes */
31993199
if (param->buffer_length)
3200-
memcpy(buffer, start, min(copy_length, param->buffer_length));
3200+
memcpy(buffer, start, MY_MIN(copy_length, param->buffer_length));
32013201
}
32023202
else
32033203
copy_length= 0;
@@ -3423,7 +3423,7 @@ static void fetch_float_with_conversion(MYSQL_BIND *param, MYSQL_FIELD *field,
34233423
size_t len;
34243424
if (field->decimals >= NOT_FIXED_DEC)
34253425
len= my_gcvt(value, type,
3426-
(int) min(sizeof(buff)-1, param->buffer_length),
3426+
(int) MY_MIN(sizeof(buff)-1, param->buffer_length),
34273427
buff, NULL);
34283428
else
34293429
len= my_fcvt(value, (int) field->decimals, buff, NULL);
@@ -3733,7 +3733,7 @@ static void fetch_result_bin(MYSQL_BIND *param,
37333733
uchar **row)
37343734
{
37353735
ulong length= net_field_length(row);
3736-
ulong copy_length= min(length, param->buffer_length);
3736+
ulong copy_length= MY_MIN(length, param->buffer_length);
37373737
memcpy(param->buffer, (char *)*row, copy_length);
37383738
*param->length= length;
37393739
*param->error= copy_length < length;
@@ -3745,7 +3745,7 @@ static void fetch_result_str(MYSQL_BIND *param,
37453745
uchar **row)
37463746
{
37473747
ulong length= net_field_length(row);
3748-
ulong copy_length= min(length, param->buffer_length);
3748+
ulong copy_length= MY_MIN(length, param->buffer_length);
37493749
memcpy(param->buffer, (char *)*row, copy_length);
37503750
/* Add an end null if there is room in the buffer */
37513751
if (copy_length != param->buffer_length)

libmysqld/lib_sql.cc

+6-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ C_MODE_START
3737
#include "errmsg.h"
3838
#include "embedded_priv.h"
3939

40+
#include <algorithm>
41+
42+
using std::min;
43+
using std::max;
44+
4045
extern unsigned int mysql_server_last_errno;
4146
extern char mysql_server_last_error[MYSQL_ERRMSG_SIZE];
4247
static my_bool emb_read_query_result(MYSQL *mysql);
@@ -888,7 +893,7 @@ write_eof_packet(THD *thd, uint server_status, uint statement_warn_count)
888893
is cleared between substatements, and mysqltest gets confused
889894
*/
890895
thd->cur_data->embedded_info->warning_count=
891-
(thd->spcont ? 0 : min(statement_warn_count, 65535));
896+
(thd->spcont ? 0 : min(statement_warn_count, 65535U));
892897
return FALSE;
893898
}
894899

mysys/array.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
4747
DBUG_ENTER("init_dynamic_array");
4848
if (!alloc_increment)
4949
{
50-
alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16);
50+
alloc_increment=MY_MAX((8192-MALLOC_OVERHEAD)/element_size,16);
5151
if (init_alloc > 8 && alloc_increment > init_alloc * 2)
5252
alloc_increment=init_alloc*2;
5353
}
@@ -340,7 +340,7 @@ void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)
340340

341341
void freeze_size(DYNAMIC_ARRAY *array)
342342
{
343-
uint elements=max(array->elements,1);
343+
uint elements=MY_MAX(array->elements,1);
344344

345345
/*
346346
Do nothing if we are using a static buffer

mysys/default.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
901901
for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
902902
end[0]=0;
903903

904-
strmake(curr_gr, ptr, min((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
904+
strmake(curr_gr, ptr, MY_MIN((size_t) (end-ptr)+1, sizeof(curr_gr)-1));
905905

906906
/* signal that a new group is found */
907907
opt_handler(handler_ctx, curr_gr, NULL);

mysys/lf_alloc-pin.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ struct st_harvester {
284284
static int harvest_pins(LF_PINS *el, struct st_harvester *hv)
285285
{
286286
int i;
287-
LF_PINS *el_end= el+min(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
287+
LF_PINS *el_end= el + MY_MIN(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
288288
for (; el < el_end; el++)
289289
{
290290
for (i= 0; i < LF_PINBOX_PINS; i++)

mysys/lf_dynarray.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ void *_lf_dynarray_lvalue(LF_DYNARRAY *array, uint idx)
124124
{
125125
uchar *alloc, *data;
126126
alloc= my_malloc(LF_DYNARRAY_LEVEL_LENGTH * array->size_of_element +
127-
max(array->size_of_element, sizeof(void *)),
128-
MYF(MY_WME|MY_ZEROFILL));
127+
MY_MAX(array->size_of_element, sizeof(void *)),
128+
MYF(MY_WME|MY_ZEROFILL));
129129
if (unlikely(!alloc))
130130
return(NULL);
131131
/* reserve the space for free() address */

mysys/mf_format.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ char * fn_format(char * to, const char *name, const char *dir,
8585
tmp_length= strlength(startpos);
8686
DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %u",dev,ext,
8787
(uint) length));
88-
(void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
88+
(void) strmake(to, startpos, MY_MIN(tmp_length, FN_REFLEN-1));
8989
}
9090
else
9191
{

mysys/mf_iocache.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ static void copy_to_read_buffer(IO_CACHE *write_cache,
11281128
*/
11291129
while (write_length)
11301130
{
1131-
size_t copy_length= min(write_length, write_cache->buffer_length);
1131+
size_t copy_length= MY_MIN(write_length, write_cache->buffer_length);
11321132
int __attribute__((unused)) rc;
11331133

11341134
rc= lock_io_cache(write_cache, write_cache->pos_in_file);
@@ -1286,7 +1286,7 @@ int _my_b_seq_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
12861286
TODO: figure out if the assert below is needed or correct.
12871287
*/
12881288
DBUG_ASSERT(pos_in_file == info->end_of_file);
1289-
copy_len=min(Count, len_in_buff);
1289+
copy_len= MY_MIN(Count, len_in_buff);
12901290
memcpy(Buffer, info->append_read_pos, copy_len);
12911291
info->append_read_pos += copy_len;
12921292
Count -= copy_len;
@@ -1395,7 +1395,7 @@ int _my_b_async_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
13951395
}
13961396
#endif
13971397
/* Copy found bytes to buffer */
1398-
length=min(Count,read_length);
1398+
length= MY_MIN(Count, read_length);
13991399
memcpy(Buffer,info->read_pos,(size_t) length);
14001400
Buffer+=length;
14011401
Count-=length;
@@ -1429,7 +1429,7 @@ int _my_b_async_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
14291429
if ((read_length=mysql_file_read(info->file,info->request_pos,
14301430
read_length, info->myflags)) == (size_t) -1)
14311431
return info->error= -1;
1432-
use_length=min(Count,read_length);
1432+
use_length= MY_MIN(Count, read_length);
14331433
memcpy(Buffer,info->request_pos,(size_t) use_length);
14341434
info->read_pos=info->request_pos+Count;
14351435
info->read_end=info->request_pos+read_length;

mysys/my_alloc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ void *alloc_root(MEM_ROOT *mem_root, size_t length)
212212
{ /* Time to alloc new block */
213213
block_size= mem_root->block_size * (mem_root->block_num >> 2);
214214
get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
215-
get_size= max(get_size, block_size);
215+
get_size= MY_MAX(get_size, block_size);
216216

217217
if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
218218
{

mysys/my_bitmap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)
415415

416416
DBUG_ASSERT(map->bitmap && map2->bitmap);
417417

418-
end= to+min(len,len2);
418+
end= to + MY_MIN(len, len2);
419419
for (; to < end; to++, from++)
420420
*to &= *from;
421421

mysys/my_compare.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int ha_compare_text(const CHARSET_INFO *charset_info, uchar *a, uint a_length,
3636
static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
3737
my_bool part_key, my_bool skip_end_space)
3838
{
39-
uint length= min(a_length,b_length);
39+
uint length= MY_MIN(a_length, b_length);
4040
uchar *end= a+ length;
4141
int flag;
4242

@@ -164,7 +164,7 @@ int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
164164
continue; /* To next key part */
165165
}
166166
}
167-
end= a+ min(keyseg->length,key_length);
167+
end= a + MY_MIN(keyseg->length, key_length);
168168
next_key_length=key_length-keyseg->length;
169169

170170
switch ((enum ha_base_keytype) keyseg->type) {

mysys/my_compress.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ int unpackfrm(uchar **unpack_data, size_t *unpack_len,
244244

245245
if (ver != 1)
246246
DBUG_RETURN(1);
247-
if (!(data= my_malloc(max(orglen, complen), MYF(MY_WME))))
247+
if (!(data= my_malloc(MY_MAX(orglen, complen), MYF(MY_WME))))
248248
DBUG_RETURN(2);
249249
memcpy(data, pack_data + BLOB_HEADER, complen);
250250

0 commit comments

Comments
 (0)