From 3de545771826006015b92d2c6325d3ee9ff7baf1 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Thu, 18 Apr 2024 17:23:08 +0100 Subject: [PATCH] Utilise magic memory tree interface for butterfly.c --- src/avrdude.h | 1 + src/butterfly.c | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/avrdude.h b/src/avrdude.h index c679a654..6b777d30 100644 --- a/src/avrdude.h +++ b/src/avrdude.h @@ -44,6 +44,7 @@ extern const char *pgmid; // Programmer -c string #define mmt_strdup(s) cfg_strdup(__func__, s) #define mmt_malloc(n) cfg_malloc(__func__, n) #define mmt_realloc(p, n) cfg_realloc(__func__, p, n) +#define mmt_free(p) free(p) int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...); diff --git a/src/butterfly.c b/src/butterfly.c index 29ac22ef..11f6021b 100644 --- a/src/butterfly.c +++ b/src/butterfly.c @@ -82,18 +82,12 @@ struct pdata } while(0) -static void butterfly_setup(PROGRAMMER * pgm) -{ - if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) { - pmsg_error("out of memory allocating private data\n"); - exit(1); - } - memset(pgm->cookie, 0, sizeof(struct pdata)); +static void butterfly_setup(PROGRAMMER * pgm) { + pgm->cookie = mmt_malloc(sizeof(struct pdata)); } -static void butterfly_teardown(PROGRAMMER * pgm) -{ - free(pgm->cookie); +static void butterfly_teardown(PROGRAMMER * pgm) { + mmt_free(pgm->cookie); } static int butterfly_send(const PROGRAMMER *pgm, char *buf, size_t len) { @@ -603,8 +597,8 @@ static int butterfly_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const return -1; #endif - cmd = malloc(4+blocksize); - if (!cmd) return -1; + cmd = mmt_malloc(4+blocksize); + cmd[0] = 'B'; cmd[3] = isee? 'E': mem_is_flash(m)? 'F': 'U'; @@ -616,13 +610,16 @@ static int butterfly_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const cmd[1] = (blocksize >> 8) & 0xff; cmd[2] = blocksize & 0xff; - EI(butterfly_send(pgm, cmd, 4+blocksize)); - if (butterfly_vfy_cmd_sent(pgm, "write block") < 0) + if(butterfly_send(pgm, cmd, 4+blocksize) < 0 || + butterfly_vfy_cmd_sent(pgm, "write block") < 0) { + + mmt_free(cmd); return -1; + } addr += blocksize; } - free(cmd); + mmt_free(cmd); return n_bytes; }