diff --git a/src/jtagmkI.c b/src/jtagmkI.c index b02c9626..9f546cf8 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -97,32 +97,23 @@ static void jtagmkI_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp) static int jtagmkI_resync(const PROGRAMMER *pgm, int maxtries, int signon); -static void jtagmkI_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 jtagmkI_setup(PROGRAMMER *pgm) { + pgm->cookie = mmt_malloc(sizeof(struct pdata)); } -static void jtagmkI_teardown(PROGRAMMER * pgm) -{ - free(pgm->cookie); +static void jtagmkI_teardown(PROGRAMMER *pgm) { + mmt_free(pgm->cookie); + pgm->cookie = NULL; } -static void -u32_to_b3(unsigned char *b, unsigned long l) -{ +static void u32_to_b3(unsigned char *b, unsigned long l) { b[2] = l & 0xff; b[1] = (l >> 8) & 0xff; b[0] = (l >> 16) & 0xff; } -static void -u16_to_b2(unsigned char *b, unsigned short l) -{ +static void u16_to_b2(unsigned char *b, unsigned short l) { b[0] = l & 0xff; b[1] = (l >> 8) & 0xff; } @@ -186,23 +177,18 @@ static int jtagmkI_send(const PROGRAMMER *pgm, unsigned char *data, size_t len) msg_debug("\n"); pmsg_debug("jtagmkI_send(): sending %u bytes\n", (unsigned int) len); - if ((buf = malloc(len + 2)) == NULL) - { - pmsg_error("out of memory"); - exit(1); - } - + buf = mmt_malloc(len + 2); memcpy(buf, data, len); buf[len] = ' '; /* "CRC" */ buf[len + 1] = ' '; /* EOP */ if (serial_send(&pgm->fd, buf, len + 2) != 0) { pmsg_error("unable to send command to serial port\n"); - free(buf); + mmt_free(buf); return -1; } - free(buf); + mmt_free(buf); return 0; } @@ -402,7 +388,6 @@ static int jtagmkI_reset(const PROGRAMMER *pgm) { } static int jtagmkI_program_enable_dummy(const PROGRAMMER *pgm, const AVRPART *p) { - return 0; } @@ -458,8 +443,7 @@ static int jtagmkI_program_disable(const PROGRAMMER *pgm) { return 0; } -static unsigned char jtagmkI_get_baud(long baud) -{ +static unsigned char jtagmkI_get_baud(long baud) { for (size_t i = 0; i < sizeof baudtab / sizeof baudtab[0]; i++) if (baud == baudtab[i].baud) return baudtab[i].val; @@ -523,18 +507,11 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { jtagmkI_setparm(pgm, PARM_FLASH_PAGESIZE_HIGH, PDATA(pgm)->flash_pagesize >> 8); jtagmkI_setparm(pgm, PARM_EEPROM_PAGESIZE, PDATA(pgm)->eeprom_pagesize & 0xff); - free(PDATA(pgm)->flash_pagecache); - free(PDATA(pgm)->eeprom_pagecache); - if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) { - pmsg_error("out of memory\n"); - return -1; - } - if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) { - pmsg_error("out of memory\n"); - free(PDATA(pgm)->flash_pagecache); - return -1; - } - PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; + mmt_free(PDATA(pgm)->flash_pagecache); + mmt_free(PDATA(pgm)->eeprom_pagecache); + PDATA(pgm)->flash_pagecache = mmt_malloc(PDATA(pgm)->flash_pagesize); + PDATA(pgm)->eeprom_pagecache = mmt_malloc(PDATA(pgm)->eeprom_pagesize); + PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long) -1L; if (jtagmkI_reset(pgm) < 0) return -1; @@ -548,10 +525,9 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { static void jtagmkI_disable(const PROGRAMMER *pgm) { - - free(PDATA(pgm)->flash_pagecache); + mmt_free(PDATA(pgm)->flash_pagecache); PDATA(pgm)->flash_pagecache = NULL; - free(PDATA(pgm)->eeprom_pagecache); + mmt_free(PDATA(pgm)->eeprom_pagecache); PDATA(pgm)->eeprom_pagecache = NULL; (void)jtagmkI_program_disable(pgm); @@ -562,8 +538,7 @@ static void jtagmkI_enable(PROGRAMMER *pgm, const AVRPART *p) { } -static int jtagmkI_open(PROGRAMMER *pgm, const char *port) -{ +static int jtagmkI_open(PROGRAMMER *pgm, const char *port) { size_t i; pmsg_notice2("jtagmkI_open()\n"); @@ -601,8 +576,7 @@ static int jtagmkI_open(PROGRAMMER *pgm, const char *port) } -static void jtagmkI_close(PROGRAMMER * pgm) -{ +static void jtagmkI_close(PROGRAMMER *pgm) { unsigned char b; pmsg_notice2("jtagmkI_close()\n"); @@ -638,7 +612,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV { int block_size, send_size, tries; unsigned int maxaddr = addr + n_bytes; - unsigned char cmd[6], *datacmd; + unsigned char cmd[6], *datacmd = NULL; unsigned char resp[2]; int is_flash = 0; long otimeout = serial_recv_timeout; @@ -657,11 +631,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV return -1; } - if ((datacmd = malloc(page_size + 1)) == NULL) { - pmsg_error("out of memory\n"); - return -1; - } - + datacmd = mmt_malloc(page_size + 1); cmd[0] = CMD_WRITE_MEM; if (mem_is_flash(m)) { cmd[1] = MTYPE_FLASH_PAGE; @@ -682,6 +652,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV if (tries != 0 && jtagmkI_resync(pgm, 2000, 0) < 0) { pmsg_error("sync loss, retries exhausted\n"); + mmt_free(datacmd); return -1; } @@ -707,14 +678,17 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV /* First part, send the write command. */ jtagmkI_send(pgm, cmd, 6); - if (jtagmkI_recv(pgm, resp, 1) < 0) + if (jtagmkI_recv(pgm, resp, 1) < 0) { + mmt_free(datacmd); return -1; + } if (resp[0] != RESP_OK) { msg_notice2("\n"); pmsg_warning("timeout/error communicating with programmer (resp %c)\n", resp[0]); if (tries++ < MAXTRIES) goto again; serial_recv_timeout = otimeout; + mmt_free(datacmd); return -1; } else { msg_notice2("OK\n"); @@ -732,21 +706,24 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV /* Second, send the data command. */ jtagmkI_send(pgm, datacmd, send_size + 1); - if (jtagmkI_recv(pgm, resp, 2) < 0) + if (jtagmkI_recv(pgm, resp, 2) < 0) { + mmt_free(datacmd); return -1; + } if (resp[1] != RESP_OK) { msg_notice2("\n"); pmsg_warning("timeout/error communicating with programmer (resp %c)\n", resp[0]); if (tries++ < MAXTRIES) goto again; serial_recv_timeout = otimeout; + mmt_free(datacmd); return -1; } else { msg_notice2("OK\n"); } } - free(datacmd); + mmt_free(datacmd); serial_recv_timeout = otimeout; #undef MAXTRIES