Use alloca for stack based memory allocation

This commit is contained in:
Marius Greuel 2022-01-07 17:23:50 +01:00
parent d05c2db3fb
commit fe6f08d48f
5 changed files with 14 additions and 42 deletions

View File

@ -334,7 +334,11 @@ if(MSVC)
add_compile_definitions(_CRT_NONSTDC_NO_WARNINGS=1) add_compile_definitions(_CRT_NONSTDC_NO_WARNINGS=1)
add_compile_definitions(_WINSOCK_DEPRECATED_NO_WARNINGS=1) add_compile_definitions(_WINSOCK_DEPRECATED_NO_WARNINGS=1)
add_compile_options(/W3) add_compile_options(/W3)
add_compile_options(/wd4018) # warning C4018: signed/unsigned mismatch
add_compile_options(/wd4244) # warning C4244: conversion from '...' to '...', possible loss of data
add_compile_options(/wd4267) # warning C4267: conversion from '...' to '...', possible loss of data
add_compile_options(/wd5105) # warning C5105: macro expansion producing 'xxx' has undefined behavior add_compile_options(/wd5105) # warning C5105: macro expansion producing 'xxx' has undefined behavior
add_compile_options(/wd6255) # warning C6255: _alloca indicates failure by raising a stack overflow exception. Consider using _malloca instead
set(EXTRA_WINDOWS_SOURCES ${EXTRA_WINDOWS_SOURCES} set(EXTRA_WINDOWS_SOURCES ${EXTRA_WINDOWS_SOURCES}
"${PROJECT_SOURCE_DIR}/msvc/getopt.c" "${PROJECT_SOURCE_DIR}/msvc/getopt.c"

View File

@ -356,15 +356,8 @@ static int avrftdi_transmit_bb(PROGRAMMER * pgm, unsigned char mode, const unsig
blocksize = MAX(1,(max_size-7)/((8*2*6)+(8*1*2))); blocksize = MAX(1,(max_size-7)/((8*2*6)+(8*1*2)));
//avrdude_message(MSG_INFO, "blocksize %d \n",blocksize); //avrdude_message(MSG_INFO, "blocksize %d \n",blocksize);
size_t send_buffer_size = (8 * 2 * 6) * blocksize + (8 * 1 * 2) * blocksize + 7; unsigned char* send_buffer = alloca((8 * 2 * 6) * blocksize + (8 * 1 * 2) * blocksize + 7);
size_t recv_buffer_size = 2 * 16 * blocksize; unsigned char* recv_buffer = alloca(2 * 16 * blocksize);
#ifdef _MSC_VER
unsigned char* send_buffer = _alloca(send_buffer_size);
unsigned char* recv_buffer = _alloca(recv_buffer_size);
#else
unsigned char send_buffer[send_buffer_size];
unsigned char recv_buffer[recv_buffer_size];
#endif
while(remaining) while(remaining)
{ {
@ -966,11 +959,7 @@ static int avrftdi_eeprom_read(PROGRAMMER *pgm, AVRPART *p, AVRMEM *m,
{ {
unsigned char cmd[4]; unsigned char cmd[4];
unsigned int add; unsigned int add;
#ifdef _MSC_VER unsigned char* buffer = alloca(len);
unsigned char* buffer = _alloca(len);
#else
unsigned char buffer[len];
#endif
unsigned char* bufptr = buffer; unsigned char* bufptr = buffer;
memset(buffer, 0, len); memset(buffer, 0, len);
@ -998,19 +987,14 @@ static int avrftdi_flash_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int word; unsigned int word;
unsigned int poll_index; unsigned int poll_index;
unsigned int buf_size;
unsigned char poll_byte; unsigned char poll_byte;
unsigned char *buffer = &m->buf[addr]; unsigned char *buffer = &m->buf[addr];
unsigned int alloc_size = 4 * len + 4; unsigned int buf_size = 4 * len + 4;
#ifdef _MSC_VER unsigned char* buf = alloca(buf_size);
unsigned char* buf = _alloca(alloc_size);
#else
unsigned char buf[alloc_size];
#endif
unsigned char* bufptr = buf; unsigned char* bufptr = buf;
memset(buf, 0, alloc_size); memset(buf, 0, buf_size);
/* pre-check opcodes */ /* pre-check opcodes */
if (m->op[AVR_OP_LOADPAGE_LO] == NULL) { if (m->op[AVR_OP_LOADPAGE_LO] == NULL) {
@ -1127,13 +1111,8 @@ static int avrftdi_flash_read(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int address = addr/2; unsigned int address = addr/2;
unsigned int buf_size = 4 * len + 4; unsigned int buf_size = 4 * len + 4;
#ifdef _MSC_VER unsigned char* o_buf = alloca(buf_size);
unsigned char* o_buf = _alloca(buf_size); unsigned char* i_buf = alloca(buf_size);
unsigned char* i_buf = _alloca(buf_size);
#else
unsigned char o_buf[buf_size];
unsigned char i_buf[buf_size];
#endif
unsigned int index; unsigned int index;
memset(o_buf, 0, buf_size); memset(o_buf, 0, buf_size);

View File

@ -41,9 +41,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#if defined(WIN32NATIVE)
# include <malloc.h> /* for alloca() */
#endif
#include "avrdude.h" #include "avrdude.h"
#include "libavrdude.h" #include "libavrdude.h"

View File

@ -22,11 +22,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <io.h> #include <io.h>
#include <intrin.h> #include <intrin.h>
#include <malloc.h>
#pragma warning(disable : 4018) // warning C4018: signed/unsigned mismatch
#pragma warning(disable : 4244) // warning C4244: conversion from '...' to '...', possible loss of data
#pragma warning(disable : 4267) // warning C4267: conversion from '...' to '...', possible loss of data
#pragma warning(disable : 5105) // warning C5105: macro expansion producing 'defined' has undefined behavior
#pragma comment(lib, "hid.lib") #pragma comment(lib, "hid.lib")
#pragma comment(lib, "ws2_32.lib") #pragma comment(lib, "ws2_32.lib")

View File

@ -760,11 +760,7 @@ static int stk500_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
unsigned int page_size, unsigned int page_size,
unsigned int addr, unsigned int n_bytes) unsigned int addr, unsigned int n_bytes)
{ {
#ifdef _MSC_VER unsigned char* buf = alloca(page_size + 16);
unsigned char* buf = _alloca(page_size + 16);
#else
unsigned char buf[page_size + 16];
#endif
int memtype; int memtype;
int a_div; int a_div;
int block_size; int block_size;