Skip to content

Commit 4569a89

Browse files
committed
simplify and unify my_safe_alloca usage
1 parent b6776b3 commit 4569a89

File tree

5 files changed

+25
-34
lines changed

5 files changed

+25
-34
lines changed

include/my_sys.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,17 @@ extern void my_large_free(uchar *ptr);
205205
#endif /* GNUC */
206206
#define my_alloca(SZ) alloca((size_t) (SZ))
207207
#define my_afree(PTR) ((void)0)
208-
#define my_safe_alloca(size, max_alloca_sz) ((size <= max_alloca_sz) ? \
209-
my_alloca(size) : \
210-
my_malloc(size, MYF(0)))
211-
#define my_safe_afree(ptr, size, max_alloca_sz) if (size > max_alloca_sz) \
212-
my_free(ptr)
208+
#define MAX_ALLOCA_SZ 4096
209+
#define my_safe_alloca(size) (((size) <= MAX_ALLOCA_SZ) ? \
210+
my_alloca(size) : \
211+
my_malloc((size), MYF(MY_THREAD_SPECIFIC|MY_WME)))
212+
#define my_safe_afree(ptr, size) \
213+
do { if ((size) > MAX_ALLOCA_SZ) my_free(ptr); } while(0)
213214
#else
214215
#define my_alloca(SZ) my_malloc(SZ,MYF(MY_FAE))
215216
#define my_afree(PTR) my_free(PTR)
216-
#define my_safe_alloca(size, max_alloca_sz) my_alloca(size)
217-
#define my_safe_afree(ptr, size, max_alloca_sz) my_afree(ptr)
217+
#define my_safe_alloca(size) my_alloca(size)
218+
#define my_safe_afree(ptr, size) my_afree(ptr)
218219
#endif /* HAVE_ALLOCA */
219220

220221
#ifndef errno /* did we already get it? */

sql/sql_insert.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1670,8 +1670,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
16701670

16711671
if (!key)
16721672
{
1673-
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length,
1674-
MAX_KEY_LENGTH)))
1673+
if (!(key=(char*) my_safe_alloca(table->s->max_unique_length)))
16751674
{
16761675
error=ENOMEM;
16771676
goto err;
@@ -1897,7 +1896,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
18971896

18981897
ok_or_after_trg_err:
18991898
if (key)
1900-
my_safe_afree(key,table->s->max_unique_length,MAX_KEY_LENGTH);
1899+
my_safe_afree(key,table->s->max_unique_length);
19011900
if (!table->file->has_transactions())
19021901
thd->transaction.stmt.modified_non_trans_table= TRUE;
19031902
DBUG_RETURN(trg_error);
@@ -1909,7 +1908,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info)
19091908
before_trg_err:
19101909
table->file->restore_auto_increment(prev_insert_id);
19111910
if (key)
1912-
my_safe_afree(key, table->s->max_unique_length, MAX_KEY_LENGTH);
1911+
my_safe_afree(key, table->s->max_unique_length);
19131912
table->column_bitmaps_set(save_read_set, save_write_set);
19141913
DBUG_RETURN(1);
19151914
}

storage/maria/ma_blockrec.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5187,8 +5187,7 @@ my_bool _ma_cmp_block_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
51875187
Don't allocate more than 16K on the stack to ensure we don't get
51885188
stack overflow.
51895189
*/
5190-
if (!(old_record= my_safe_alloca(info->s->base.reclength,
5191-
MARIA_MAX_RECORD_ON_STACK)))
5190+
if (!(old_record= my_safe_alloca(info->s->base.reclength)))
51925191
DBUG_RETURN(1);
51935192

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

storage/maria/ma_dynrec.c

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ my_bool _ma_write_blob_record(MARIA_HA *info, const uchar *record)
250250
MARIA_DYN_DELETE_BLOCK_HEADER+1);
251251
reclength= (info->s->base.pack_reclength +
252252
_ma_calc_total_blob_length(info,record)+ extra);
253-
if (!(rec_buff=(uchar*) my_safe_alloca(reclength,
254-
MARIA_MAX_RECORD_ON_STACK)))
253+
if (!(rec_buff=(uchar*) my_safe_alloca(reclength)))
255254
{
256255
my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
257256
return(1);
@@ -265,7 +264,7 @@ my_bool _ma_write_blob_record(MARIA_HA *info, const uchar *record)
265264
error= write_dynamic_record(info,
266265
rec_buff+ALIGN_SIZE(MARIA_MAX_DYN_BLOCK_HEADER),
267266
reclength2);
268-
my_safe_afree(rec_buff, reclength, MARIA_MAX_RECORD_ON_STACK);
267+
my_safe_afree(rec_buff, reclength);
269268
return(error != 0);
270269
}
271270

@@ -289,8 +288,7 @@ my_bool _ma_update_blob_record(MARIA_HA *info, MARIA_RECORD_POS pos,
289288
return 1;
290289
}
291290
#endif
292-
if (!(rec_buff=(uchar*) my_safe_alloca(reclength,
293-
MARIA_MAX_RECORD_ON_STACK)))
291+
if (!(rec_buff=(uchar*) my_safe_alloca(reclength)))
294292
{
295293
my_errno= HA_ERR_OUT_OF_MEM; /* purecov: inspected */
296294
return(1);
@@ -300,7 +298,7 @@ my_bool _ma_update_blob_record(MARIA_HA *info, MARIA_RECORD_POS pos,
300298
error=update_dynamic_record(info,pos,
301299
rec_buff+ALIGN_SIZE(MARIA_MAX_DYN_BLOCK_HEADER),
302300
reclength);
303-
my_safe_afree(rec_buff, reclength, MARIA_MAX_RECORD_ON_STACK);
301+
my_safe_afree(rec_buff, reclength);
304302
return(error != 0);
305303
}
306304

@@ -1555,8 +1553,7 @@ my_bool _ma_cmp_dynamic_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
15551553
my_bool error;
15561554
DBUG_ENTER("_ma_cmp_dynamic_unique");
15571555

1558-
if (!(old_record= my_safe_alloca(info->s->base.reclength,
1559-
MARIA_MAX_RECORD_ON_STACK)))
1556+
if (!(old_record= my_safe_alloca(info->s->base.reclength)))
15601557
DBUG_RETURN(1);
15611558

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

@@ -1613,8 +1609,7 @@ my_bool _ma_cmp_dynamic_record(register MARIA_HA *info,
16131609
{
16141610
buffer_length= (info->s->base.pack_reclength +
16151611
_ma_calc_total_blob_length(info,record));
1616-
if (!(buffer=(uchar*) my_safe_alloca(buffer_length,
1617-
MARIA_MAX_RECORD_ON_STACK)))
1612+
if (!(buffer=(uchar*) my_safe_alloca(buffer_length)))
16181613
DBUG_RETURN(1);
16191614
}
16201615
reclength= _ma_rec_pack(info,buffer,record);
@@ -1666,7 +1661,7 @@ my_bool _ma_cmp_dynamic_record(register MARIA_HA *info,
16661661
error= 0;
16671662
err:
16681663
if (buffer != info->rec_buff)
1669-
my_safe_afree(buffer, buffer_length, MARIA_MAX_RECORD_ON_STACK);
1664+
my_safe_afree(buffer, buffer_length);
16701665
DBUG_PRINT("exit", ("result: %d", error));
16711666
DBUG_RETURN(error);
16721667
}

storage/maria/maria_pack.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static int get_statistic(PACK_MRG_INFO *mrg,HUFF_COUNTS *huff_counts)
861861

862862
reclength= mrg->file[0]->s->base.reclength;
863863
null_bytes= mrg->file[0]->s->base.null_bytes;
864-
record=(uchar*) my_safe_alloca(reclength, MARIA_MAX_RECORD_ON_STACK);
864+
record=(uchar*) my_safe_alloca(reclength);
865865
end_count=huff_counts+mrg->file[0]->s->base.fields;
866866
record_count=0; glob_crc=0;
867867
max_blob_length=0;
@@ -1145,7 +1145,7 @@ static int get_statistic(PACK_MRG_INFO *mrg,HUFF_COUNTS *huff_counts)
11451145

11461146
mrg->records=record_count;
11471147
mrg->max_blob_length=max_blob_length;
1148-
my_safe_afree(record, reclength, MARIA_MAX_RECORD_ON_STACK);
1148+
my_safe_afree(record, reclength);
11491149
DBUG_RETURN(error != HA_ERR_END_OF_FILE);
11501150
}
11511151

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

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

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

2782-
my_safe_afree(record, isam_file->s->base.reclength,
2783-
MARIA_MAX_RECORD_ON_STACK);
2781+
my_safe_afree(record, isam_file->s->base.reclength);
27842782
mrg->ref_length=max_pack_length;
27852783
mrg->min_pack_length=max_record_length ? min_record_length : 0;
27862784
mrg->max_pack_length=max_record_length;

0 commit comments

Comments
 (0)