Skip to content

Commit

Permalink
simplify and unify my_safe_alloca usage
Browse files Browse the repository at this point in the history
  • Loading branch information
vuvova committed Sep 4, 2015
1 parent b6776b3 commit 4569a89
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 34 deletions.
15 changes: 8 additions & 7 deletions include/my_sys.h
Expand Up @@ -205,16 +205,17 @@ extern void my_large_free(uchar *ptr);
#endif /* GNUC */
#define my_alloca(SZ) alloca((size_t) (SZ))
#define my_afree(PTR) ((void)0)
#define my_safe_alloca(size, max_alloca_sz) ((size <= max_alloca_sz) ? \
my_alloca(size) : \
my_malloc(size, MYF(0)))
#define my_safe_afree(ptr, size, max_alloca_sz) if (size > max_alloca_sz) \
my_free(ptr)
#define MAX_ALLOCA_SZ 4096
#define my_safe_alloca(size) (((size) <= MAX_ALLOCA_SZ) ? \
my_alloca(size) : \
my_malloc((size), MYF(MY_THREAD_SPECIFIC|MY_WME)))
#define my_safe_afree(ptr, size) \
do { if ((size) > MAX_ALLOCA_SZ) my_free(ptr); } while(0)
#else
#define my_alloca(SZ) my_malloc(SZ,MYF(MY_FAE))
#define my_afree(PTR) my_free(PTR)
#define my_safe_alloca(size, max_alloca_sz) my_alloca(size)
#define my_safe_afree(ptr, size, max_alloca_sz) my_afree(ptr)
#define my_safe_alloca(size) my_alloca(size)
#define my_safe_afree(ptr, size) my_afree(ptr)
#endif /* HAVE_ALLOCA */

#ifndef errno /* did we already get it? */
Expand Down
7 changes: 3 additions & 4 deletions sql/sql_insert.cc
Expand Up @@ -1670,8 +1670,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)

if (!key)
{
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
MAX_KEY_LENGTH)))
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length)))
{
error=ENOMEM;
goto err;
Expand Down Expand Up @@ -1897,7 +1896,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)

ok_or_after_trg_err:
if (key)
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
my_safe_afree(key,table->s->max_unique_length);
if (!table->file->has_transactions())
thd->transaction.stmt.modified_non_trans_table= TRUE;
DBUG_RETURN(trg_error);
Expand All @@ -1909,7 +1908,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
before_trg_err:
table->file->restore_auto_increment(prev_insert_id);
if (key)
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
my_safe_afree(key, table->s->max_unique_length);
table->column_bitmaps_set(save_read_set, save_write_set);
DBUG_RETURN(1);
}
Expand Down
6 changes: 2 additions & 4 deletions storage/maria/ma_blockrec.c
Expand Up @@ -5187,8 +5187,7 @@ my_bool _ma_cmp_block_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
Don't allocate more than 16K on the stack to ensure we don't get
stack overflow.
*/
if (!(old_record= my_safe_alloca(info->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK)))
if (!(old_record= my_safe_alloca(info->s->base.reclength)))
DBUG_RETURN(1);

/* Don't let the compare destroy blobs that may be in use */
Expand All @@ -5210,8 +5209,7 @@ my_bool _ma_cmp_block_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
info->rec_buff_size= org_rec_buff_size;
}
DBUG_PRINT("exit", ("result: %d", error));
my_safe_afree(old_record, info->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(old_record, info->s->base.reclength);
DBUG_RETURN(error != 0);
}

Expand Down
21 changes: 8 additions & 13 deletions storage/maria/ma_dynrec.c
Expand Up @@ -250,8 +250,7 @@ my_bool _ma_write_blob_record(MARIA_HA *info, const uchar *record)
MARIA_DYN_DELETE_BLOCK_HEADER+1);
reclength= (info->s->base.pack_reclength +
_ma_calc_total_blob_length(info,record)+ extra);
if (!(rec_buff=(uchar*) my_safe_alloca(reclength,
MARIA_MAX_RECORD_ON_STACK)))
if (!(rec_buff=(uchar*) my_safe_alloca(reclength)))
{
my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
return(1);
Expand All @@ -265,7 +264,7 @@ my_bool _ma_write_blob_record(MARIA_HA *info, const uchar *record)
error= write_dynamic_record(info,
rec_buff+ALIGN_SIZE(MARIA_MAX_DYN_BLOCK_HEADER),
reclength2);
my_safe_afree(rec_buff, reclength, MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(rec_buff, reclength);
return(error != 0);
}

Expand All @@ -289,8 +288,7 @@ my_bool _ma_update_blob_record(MARIA_HA *info, MARIA_RECORD_POS pos,
return 1;
}
#endif
if (!(rec_buff=(uchar*) my_safe_alloca(reclength,
MARIA_MAX_RECORD_ON_STACK)))
if (!(rec_buff=(uchar*) my_safe_alloca(reclength)))
{
my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
return(1);
Expand All @@ -300,7 +298,7 @@ my_bool _ma_update_blob_record(MARIA_HA *info, MARIA_RECORD_POS pos,
error=update_dynamic_record(info,pos,
rec_buff+ALIGN_SIZE(MARIA_MAX_DYN_BLOCK_HEADER),
reclength);
my_safe_afree(rec_buff, reclength, MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(rec_buff, reclength);
return(error != 0);
}

Expand Down Expand Up @@ -1555,8 +1553,7 @@ my_bool _ma_cmp_dynamic_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
my_bool error;
DBUG_ENTER("_ma_cmp_dynamic_unique");

if (!(old_record= my_safe_alloca(info->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK)))
if (!(old_record= my_safe_alloca(info->s->base.reclength)))
DBUG_RETURN(1);

/* Don't let the compare destroy blobs that may be in use */
Expand All @@ -1577,8 +1574,7 @@ my_bool _ma_cmp_dynamic_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
info->rec_buff= old_rec_buff;
info->rec_buff_size= old_rec_buff_size;
}
my_safe_afree(old_record, info->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(old_record, info->s->base.reclength);
DBUG_RETURN(error);
}

Expand Down Expand Up @@ -1613,8 +1609,7 @@ my_bool _ma_cmp_dynamic_record(register MARIA_HA *info,
{
buffer_length= (info->s->base.pack_reclength +
_ma_calc_total_blob_length(info,record));
if (!(buffer=(uchar*) my_safe_alloca(buffer_length,
MARIA_MAX_RECORD_ON_STACK)))
if (!(buffer=(uchar*) my_safe_alloca(buffer_length)))
DBUG_RETURN(1);
}
reclength= _ma_rec_pack(info,buffer,record);
Expand Down Expand Up @@ -1666,7 +1661,7 @@ my_bool _ma_cmp_dynamic_record(register MARIA_HA *info,
error= 0;
err:
if (buffer != info->rec_buff)
my_safe_afree(buffer, buffer_length, MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(buffer, buffer_length);
DBUG_PRINT("exit", ("result: %d", error));
DBUG_RETURN(error);
}
Expand Down
10 changes: 4 additions & 6 deletions storage/maria/maria_pack.c
Expand Up @@ -861,7 +861,7 @@ static int get_statistic(PACK_MRG_INFO *mrg,HUFF_COUNTS *huff_counts)

reclength= mrg->file[0]->s->base.reclength;
null_bytes= mrg->file[0]->s->base.null_bytes;
record=(uchar*) my_safe_alloca(reclength, MARIA_MAX_RECORD_ON_STACK);
record=(uchar*) my_safe_alloca(reclength);
end_count=huff_counts+mrg->file[0]->s->base.fields;
record_count=0; glob_crc=0;
max_blob_length=0;
Expand Down Expand Up @@ -1145,7 +1145,7 @@ static int get_statistic(PACK_MRG_INFO *mrg,HUFF_COUNTS *huff_counts)

mrg->records=record_count;
mrg->max_blob_length=max_blob_length;
my_safe_afree(record, reclength, MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(record, reclength);
DBUG_RETURN(error != HA_ERR_END_OF_FILE);
}

Expand Down Expand Up @@ -2415,8 +2415,7 @@ static int compress_maria_file(PACK_MRG_INFO *mrg, HUFF_COUNTS *huff_counts)
DBUG_ENTER("compress_maria_file");

/* Allocate a buffer for the records (excluding blobs). */
if (!(record=(uchar*) my_safe_alloca(isam_file->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK)))
if (!(record=(uchar*) my_safe_alloca(isam_file->s->base.reclength)))
return -1;

end_count=huff_counts+isam_file->s->base.fields;
Expand Down Expand Up @@ -2779,8 +2778,7 @@ static int compress_maria_file(PACK_MRG_INFO *mrg, HUFF_COUNTS *huff_counts)
if (verbose >= 2)
printf("wrote %s records.\n", llstr((longlong) record_count, llbuf));

my_safe_afree(record, isam_file->s->base.reclength,
MARIA_MAX_RECORD_ON_STACK);
my_safe_afree(record, isam_file->s->base.reclength);
mrg->ref_length=max_pack_length;
mrg->min_pack_length=max_record_length ? min_record_length : 0;
mrg->max_pack_length=max_record_length;
Expand Down

0 comments on commit 4569a89

Please sign in to comment.