Skip to content

Commit

Permalink
WL#5825: Using C++ Standard Library with MySQL code
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Mats Kindahl committed Jun 1, 2011
2 parents 9b12c8e + 4631180 commit bc0d87c
Show file tree
Hide file tree
Showing 119 changed files with 555 additions and 322 deletions.
2 changes: 1 addition & 1 deletion BUILD/SETUP.sh
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ if test -z "$CC" ; then
fi

if test -z "$CXX" ; then
CXX=gcc
CXX=g++
fi

# If ccache (a compiler cache which reduces build time)
Expand Down
17 changes: 11 additions & 6 deletions client/mysql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include <signal.h>
#include <violite.h>

#include <algorithm>

using std::min;
using std::max;

#if defined(USE_LIBEDIT_INTERFACE) && defined(HAVE_LOCALE_H)
#include <locale.h>
#endif
Expand Down Expand Up @@ -3354,9 +3359,9 @@ print_table_data(MYSQL_RES *result)
{
uint length= column_names ? field->name_length : 0;
if (quick)
length=max(length,field->length);
length= max<size_t>(length, field->length);
else
length=max(length,field->max_length);
length= max<size_t>(length, field->max_length);
if (length < 4 && !IS_NOT_NULL(field->flags))
length=4; // Room for "NULL"
field->max_length=length;
Expand All @@ -3376,8 +3381,8 @@ print_table_data(MYSQL_RES *result)
field->name,
field->name + name_length);
uint display_length= field->max_length + name_length - numcells;
tee_fprintf(PAGER, " %-*s |",(int) min(display_length,
MAX_COLUMN_LENGTH),
tee_fprintf(PAGER, " %-*s |",
min<int>(display_length, MAX_COLUMN_LENGTH),
field->name);
num_flag[off]= IS_NUM(field->type);
}
Expand Down Expand Up @@ -3466,9 +3471,9 @@ static int get_field_disp_length(MYSQL_FIELD *field)
uint length= column_names ? field->name_length : 0;

if (quick)
length= max(length, field->length);
length= max<uint>(length, field->length);
else
length= max(length, field->max_length);
length= max<uint>(length, field->max_length);

if (length < 4 && !IS_NOT_NULL(field->flags))
length= 4; /* Room for "NULL" */
Expand Down
2 changes: 1 addition & 1 deletion client/mysql_upgrade.c
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ static int extract_variable_from_show(DYNAMIC_STRING* ds, char* value)
if ((value_end= strchr(value_start, '\n')) == NULL)
return 1; /* Unexpected result */

strncpy(value, value_start, min(FN_REFLEN, value_end-value_start));
strncpy(value, value_start, MY_MIN(FN_REFLEN, value_end - value_start));
return 0;
}

Expand Down
9 changes: 7 additions & 2 deletions client/mysqlbinlog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
#include "sql_common.h"
#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE

#define BIN_LOG_HEADER_SIZE 4
#include <algorithm>

using std::min;
using std::max;

#define BIN_LOG_HEADER_SIZE 4U
#define PROBE_HEADER_LEN (EVENT_LEN_OFFSET+4)


Expand Down Expand Up @@ -2074,7 +2079,7 @@ static Exit_status dump_local_log_entries(PRINT_EVENT_INFO *print_event_info,
my_off_t length,tmp;
for (length= start_position_mot ; length > 0 ; length-=tmp)
{
tmp=min(length,sizeof(buff));
tmp= min<size_t>(length, sizeof(buff));
if (my_b_read(file, buff, (uint) tmp))
{
error("Failed reading from file.");
Expand Down
4 changes: 2 additions & 2 deletions client/mysqldump.c
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
&err_ptr, &err_len);
if (err_len)
{
strmake(buff, err_ptr, min(sizeof(buff) - 1, err_len));
strmake(buff, err_ptr, MY_MIN(sizeof(buff) - 1, err_len));
fprintf(stderr, "Invalid mode to --compatible: %s\n", buff);
exit(1);
}
Expand Down Expand Up @@ -4668,7 +4668,7 @@ static ulong find_set(TYPELIB *lib, const char *x, uint length,

for (; pos != end && *pos != ','; pos++) ;
var_len= (uint) (pos - start);
strmake(buff, start, min(sizeof(buff) - 1, var_len));
strmake(buff, start, MY_MIN(sizeof(buff) - 1, var_len));
find= find_type(buff, lib, FIND_TYPE_BASIC);
if (!find)
{
Expand Down
9 changes: 7 additions & 2 deletions client/mysqltest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@

#include <welcome_copyright_notice.h> // ORACLE_WELCOME_COPYRIGHT_NOTICE

#include <algorithm>

using std::min;
using std::max;

#ifdef __WIN__
#include <crtdbg.h>
#define SIGNAL_FMT "exception 0x%x"
Expand Down Expand Up @@ -5920,9 +5925,9 @@ int read_line(char *buf, int size)
}
else if ((c == '{' &&
(!my_strnncoll_simple(charset_info, (const uchar*) "while", 5,
(uchar*) buf, min(5, p - buf), 0) ||
(uchar*) buf, min<my_ptrdiff_t>(5, p - buf), 0) ||
!my_strnncoll_simple(charset_info, (const uchar*) "if", 2,
(uchar*) buf, min(2, p - buf), 0))))
(uchar*) buf, min<my_ptrdiff_t>(2, p - buf), 0))))
{
/* Only if and while commands can be terminated by { */
*p++= c;
Expand Down
5 changes: 5 additions & 0 deletions client/sql_string.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

#include "sql_string.h"

#include <algorithm>

using std::min;
using std::max;

/*****************************************************************************
** String functions
*****************************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@

#cmakedefine HAVE_MBSTATE_T

#define MAX_INDEXES 64
#define MAX_INDEXES 64U

#cmakedefine QSORT_TYPE_IS_VOID 1
#cmakedefine RETQSORTTYPE @RETQSORTTYPE@
Expand Down
4 changes: 2 additions & 2 deletions dbug/dbug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ void _db_dump_(uint _line_, const char *keyword,
if (TRACING)
{
Indent(cs, cs->level + 1);
pos= min(max(cs->level-cs->stack->sub_level,0)*INDENT,80);
pos= MY_MIN(MY_MAX(cs->level-cs->stack->sub_level,0)*INDENT,80);
}
else
{
Expand Down Expand Up @@ -1743,7 +1743,7 @@ static void Indent(CODE_STATE *cs, int indent)
{
REGISTER int count;

indent= max(indent-1-cs->stack->sub_level,0)*INDENT;
indent= MY_MAX(indent-1-cs->stack->sub_level,0)*INDENT;
for (count= 0; count < indent ; count++)
{
if ((count % INDENT) == 0)
Expand Down
2 changes: 1 addition & 1 deletion include/m_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
(DBL_DIG + 2) significant digits + sign + "." + ("e-NNN" or
MAX_DECPT_FOR_F_FORMAT zeros for cases when |x|<1 and the 'f' format is used).
*/
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + max(5, MAX_DECPT_FOR_F_FORMAT)) \
#define MY_GCVT_MAX_FIELD_WIDTH (DBL_DIG + 4 + MY_MAX(5, MAX_DECPT_FOR_F_FORMAT)) \

extern char *llstr(longlong value,char *buff);
extern char *ullstr(longlong value,char *buff);
Expand Down
6 changes: 2 additions & 4 deletions include/my_global.h
Original file line number Diff line number Diff line change
Expand Up @@ -1378,10 +1378,8 @@ do { doubleget_union _tmp; \
#define MY_INT64_NUM_DECIMAL_DIGITS 21U

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

/*
Only Linux is known to need an explicit sync of the directory to make sure a
Expand Down
10 changes: 5 additions & 5 deletions libmysql/libmysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ void my_net_local_init(NET *net)
my_net_set_read_timeout(net, CLIENT_NET_READ_TIMEOUT);
my_net_set_write_timeout(net, CLIENT_NET_WRITE_TIMEOUT);
net->retry_count= 1;
net->max_packet_size= max(net_buffer_length, max_allowed_packet);
net->max_packet_size= MY_MAX(net_buffer_length, max_allowed_packet);
}

/*
Expand Down Expand Up @@ -3197,7 +3197,7 @@ static void fetch_string_with_conversion(MYSQL_BIND *param, char *value,
copy_length= end - start;
/* We've got some data beyond offset: copy up to buffer_length bytes */
if (param->buffer_length)
memcpy(buffer, start, min(copy_length, param->buffer_length));
memcpy(buffer, start, MY_MIN(copy_length, param->buffer_length));
}
else
copy_length= 0;
Expand Down Expand Up @@ -3423,7 +3423,7 @@ static void fetch_float_with_conversion(MYSQL_BIND *param, MYSQL_FIELD *field,
size_t len;
if (field->decimals >= NOT_FIXED_DEC)
len= my_gcvt(value, type,
(int) min(sizeof(buff)-1, param->buffer_length),
(int) MY_MIN(sizeof(buff)-1, param->buffer_length),
buff, NULL);
else
len= my_fcvt(value, (int) field->decimals, buff, NULL);
Expand Down Expand Up @@ -3733,7 +3733,7 @@ static void fetch_result_bin(MYSQL_BIND *param,
uchar **row)
{
ulong length= net_field_length(row);
ulong copy_length= min(length, param->buffer_length);
ulong copy_length= MY_MIN(length, param->buffer_length);
memcpy(param->buffer, (char *)*row, copy_length);
*param->length= length;
*param->error= copy_length < length;
Expand All @@ -3745,7 +3745,7 @@ static void fetch_result_str(MYSQL_BIND *param,
uchar **row)
{
ulong length= net_field_length(row);
ulong copy_length= min(length, param->buffer_length);
ulong copy_length= MY_MIN(length, param->buffer_length);
memcpy(param->buffer, (char *)*row, copy_length);
/* Add an end null if there is room in the buffer */
if (copy_length != param->buffer_length)
Expand Down
7 changes: 6 additions & 1 deletion libmysqld/lib_sql.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ C_MODE_START
#include "errmsg.h"
#include "embedded_priv.h"

#include <algorithm>

using std::min;
using std::max;

extern unsigned int mysql_server_last_errno;
extern char mysql_server_last_error[MYSQL_ERRMSG_SIZE];
static my_bool emb_read_query_result(MYSQL *mysql);
Expand Down Expand Up @@ -888,7 +893,7 @@ write_eof_packet(THD *thd, uint server_status, uint statement_warn_count)
is cleared between substatements, and mysqltest gets confused
*/
thd->cur_data->embedded_info->warning_count=
(thd->spcont ? 0 : min(statement_warn_count, 65535));
(thd->spcont ? 0 : min(statement_warn_count, 65535U));
return FALSE;
}

Expand Down
4 changes: 2 additions & 2 deletions mysys/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ my_bool init_dynamic_array2(DYNAMIC_ARRAY *array, uint element_size,
DBUG_ENTER("init_dynamic_array");
if (!alloc_increment)
{
alloc_increment=max((8192-MALLOC_OVERHEAD)/element_size,16);
alloc_increment=MY_MAX((8192-MALLOC_OVERHEAD)/element_size,16);
if (init_alloc > 8 && alloc_increment > init_alloc * 2)
alloc_increment=init_alloc*2;
}
Expand Down Expand Up @@ -340,7 +340,7 @@ void delete_dynamic_element(DYNAMIC_ARRAY *array, uint idx)

void freeze_size(DYNAMIC_ARRAY *array)
{
uint elements=max(array->elements,1);
uint elements=MY_MAX(array->elements,1);

/*
Do nothing if we are using a static buffer
Expand Down
2 changes: 1 addition & 1 deletion mysys/default.c
Original file line number Diff line number Diff line change
Expand Up @@ -901,7 +901,7 @@ static int search_default_file_with_ext(Process_option_func opt_handler,
for ( ; my_isspace(&my_charset_latin1,end[-1]) ; end--) ;
end[0]=0;

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

/* signal that a new group is found */
opt_handler(handler_ctx, curr_gr, NULL);
Expand Down
2 changes: 1 addition & 1 deletion mysys/lf_alloc-pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ struct st_harvester {
static int harvest_pins(LF_PINS *el, struct st_harvester *hv)
{
int i;
LF_PINS *el_end= el+min(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
LF_PINS *el_end= el + MY_MIN(hv->npins, LF_DYNARRAY_LEVEL_LENGTH);
for (; el < el_end; el++)
{
for (i= 0; i < LF_PINBOX_PINS; i++)
Expand Down
4 changes: 2 additions & 2 deletions mysys/lf_dynarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ void *_lf_dynarray_lvalue(LF_DYNARRAY *array, uint idx)
{
uchar *alloc, *data;
alloc= my_malloc(LF_DYNARRAY_LEVEL_LENGTH * array->size_of_element +
max(array->size_of_element, sizeof(void *)),
MYF(MY_WME|MY_ZEROFILL));
MY_MAX(array->size_of_element, sizeof(void *)),
MYF(MY_WME|MY_ZEROFILL));
if (unlikely(!alloc))
return(NULL);
/* reserve the space for free() address */
Expand Down
2 changes: 1 addition & 1 deletion mysys/mf_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ char * fn_format(char * to, const char *name, const char *dir,
tmp_length= strlength(startpos);
DBUG_PRINT("error",("dev: '%s' ext: '%s' length: %u",dev,ext,
(uint) length));
(void) strmake(to,startpos,min(tmp_length,FN_REFLEN-1));
(void) strmake(to, startpos, MY_MIN(tmp_length, FN_REFLEN-1));
}
else
{
Expand Down
8 changes: 4 additions & 4 deletions mysys/mf_iocache.c
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ static void copy_to_read_buffer(IO_CACHE *write_cache,
*/
while (write_length)
{
size_t copy_length= min(write_length, write_cache->buffer_length);
size_t copy_length= MY_MIN(write_length, write_cache->buffer_length);
int __attribute__((unused)) rc;

rc= lock_io_cache(write_cache, write_cache->pos_in_file);
Expand Down Expand Up @@ -1286,7 +1286,7 @@ int _my_b_seq_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
TODO: figure out if the assert below is needed or correct.
*/
DBUG_ASSERT(pos_in_file == info->end_of_file);
copy_len=min(Count, len_in_buff);
copy_len= MY_MIN(Count, len_in_buff);
memcpy(Buffer, info->append_read_pos, copy_len);
info->append_read_pos += copy_len;
Count -= copy_len;
Expand Down Expand Up @@ -1395,7 +1395,7 @@ int _my_b_async_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
}
#endif
/* Copy found bytes to buffer */
length=min(Count,read_length);
length= MY_MIN(Count, read_length);
memcpy(Buffer,info->read_pos,(size_t) length);
Buffer+=length;
Count-=length;
Expand Down Expand Up @@ -1429,7 +1429,7 @@ int _my_b_async_read(register IO_CACHE *info, uchar *Buffer, size_t Count)
if ((read_length=mysql_file_read(info->file,info->request_pos,
read_length, info->myflags)) == (size_t) -1)
return info->error= -1;
use_length=min(Count,read_length);
use_length= MY_MIN(Count, read_length);
memcpy(Buffer,info->request_pos,(size_t) use_length);
info->read_pos=info->request_pos+Count;
info->read_end=info->request_pos+read_length;
Expand Down
2 changes: 1 addition & 1 deletion mysys/my_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void *alloc_root(MEM_ROOT *mem_root, size_t length)
{ /* Time to alloc new block */
block_size= mem_root->block_size * (mem_root->block_num >> 2);
get_size= length+ALIGN_SIZE(sizeof(USED_MEM));
get_size= max(get_size, block_size);
get_size= MY_MAX(get_size, block_size);

if (!(next = (USED_MEM*) my_malloc(get_size,MYF(MY_WME | ME_FATALERROR))))
{
Expand Down
2 changes: 1 addition & 1 deletion mysys/my_bitmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ void bitmap_intersect(MY_BITMAP *map, const MY_BITMAP *map2)

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

end= to+min(len,len2);
end= to + MY_MIN(len, len2);
for (; to < end; to++, from++)
*to &= *from;

Expand Down
4 changes: 2 additions & 2 deletions mysys/my_compare.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ int ha_compare_text(const CHARSET_INFO *charset_info, uchar *a, uint a_length,
static int compare_bin(uchar *a, uint a_length, uchar *b, uint b_length,
my_bool part_key, my_bool skip_end_space)
{
uint length= min(a_length,b_length);
uint length= MY_MIN(a_length, b_length);
uchar *end= a+ length;
int flag;

Expand Down Expand Up @@ -164,7 +164,7 @@ int ha_key_cmp(register HA_KEYSEG *keyseg, register uchar *a,
continue; /* To next key part */
}
}
end= a+ min(keyseg->length,key_length);
end= a + MY_MIN(keyseg->length, key_length);
next_key_length=key_length-keyseg->length;

switch ((enum ha_base_keytype) keyseg->type) {
Expand Down
2 changes: 1 addition & 1 deletion mysys/my_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ int unpackfrm(uchar **unpack_data, size_t *unpack_len,

if (ver != 1)
DBUG_RETURN(1);
if (!(data= my_malloc(max(orglen, complen), MYF(MY_WME))))
if (!(data= my_malloc(MY_MAX(orglen, complen), MYF(MY_WME))))
DBUG_RETURN(2);
memcpy(data, pack_data + BLOB_HEADER, complen);

Expand Down
Loading

0 comments on commit bc0d87c

Please sign in to comment.