Skip to content

Commit

Permalink
Merge pull request avrdudes#1795 from stefanrueger/cx
Browse files Browse the repository at this point in the history
Use magic memory tree interface throughout
  • Loading branch information
stefanrueger authored Apr 28, 2024
2 parents 5f14524 + 3f91c2e commit caf4746
Show file tree
Hide file tree
Showing 28 changed files with 279 additions and 360 deletions.
6 changes: 3 additions & 3 deletions src/avr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ int avr_write_mem(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, int
return LIBAVRDUDE_GENERAL_FAILURE;
}

uint8_t *spc = cfg_malloc(__func__, cm->page_size);
uint8_t *spc = mmt_malloc(cm->page_size);

// Set cwsize as rounded-up wsize
int cwsize = (wsize + pgsize-1)/pgsize*pgsize;
Expand Down Expand Up @@ -1158,7 +1158,7 @@ int avr_write_mem(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM *m, int
}

avr_free_mem(cm);
free(spc);
mmt_free(spc);

if (!failure) {
led_clr(pgm, LED_PGM);
Expand Down Expand Up @@ -1539,7 +1539,7 @@ int avr_get_mem_type(const char *str) {
return avr_mem_order[i].type;
if(!avr_mem_order[i].str) {
pmsg_warning("avr_mem_order[] does not know %s; add to array and recompile\n", str);
avr_mem_order[i].str = cfg_strdup(__func__, str);
avr_mem_order[i].str = mmt_strdup(str);
return avr_mem_order[i].type;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/avrcache.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ int avr_read_page_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM
led_clr(pgm, LED_ERR);
led_set(pgm, LED_PGM);

unsigned char *pagecopy = cfg_malloc(__func__, pgsize);
unsigned char *pagecopy = mmt_malloc(pgsize);
memcpy(pagecopy, mem->buf + base, pgsize);
if((rc = pgm->paged_load(pgm, p, mem, pgsize, base, pgsize)) >= 0)
memcpy(buf, mem->buf + base, pgsize);
Expand All @@ -175,7 +175,7 @@ int avr_read_page_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM
if(rc == LIBAVRDUDE_SUCCESS)
memcpy(buf, pagecopy, pgsize);
}
free(pagecopy);
mmt_free(pagecopy);

if(rc < 0)
led_set(pgm, LED_ERR);
Expand All @@ -200,12 +200,12 @@ int avr_write_page_default(const PROGRAMMER *pgm, const AVRPART *p, const AVRMEM
if(pgsize == 1)
return fallback_write_byte(pgm, p, mem, addr, *data);

unsigned char *pagecopy = cfg_malloc(__func__, pgsize);
unsigned char *pagecopy = mmt_malloc(pgsize);
memcpy(pagecopy, mem->buf + base, pgsize);
memcpy(mem->buf + base, data, pgsize);
rc = pgm->paged_write(pgm, p, mem, pgsize, base, pgsize);
memcpy(mem->buf + base, pagecopy, pgsize);
free(pagecopy);
mmt_free(pagecopy);

return rc;
}
Expand Down Expand Up @@ -271,9 +271,9 @@ static int initCache(AVR_Cache *cp, const PROGRAMMER *pgm, const AVRPART *p) {
cp->size = basemem->size;
cp->page_size = basemem->page_size;
cp->offset = basemem->offset;
cp->cont = cfg_malloc("initCache()", cp->size);
cp->copy = cfg_malloc("initCache()", cp->size);
cp->iscached = cfg_malloc("initCache()", cp->size/cp->page_size);
cp->cont = mmt_malloc(cp->size);
cp->copy = mmt_malloc( cp->size);
cp->iscached = mmt_malloc(cp->size/cp->page_size);

if((pgm->prog_modes & PM_SPM) && mem_is_in_flash(basemem)) { // Could be vector bootloader
// Caching the vector page hands over to the progammer that then can patch the reset vector
Expand Down Expand Up @@ -802,11 +802,11 @@ int avr_reset_cache(const PROGRAMMER *pgm, const AVRPART *p_unused) {
for(size_t i = 0; i < sizeof mems/sizeof*mems; i++) {
AVR_Cache *cp = mems[i];
if(cp->cont)
free(cp->cont);
mmt_free(cp->cont);
if(cp->copy)
free(cp->copy);
mmt_free(cp->copy);
if(cp->iscached)
free(cp->iscached);
mmt_free(cp->iscached);
memset(cp, 0, sizeof*cp);
}

Expand Down
30 changes: 9 additions & 21 deletions src/avrftdi_tpi.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ static void avrftdi_tpi_disable(const PROGRAMMER *);
static int avrftdi_tpi_program_enable(const PROGRAMMER *pgm, const AVRPART *p);

#ifdef notyet
static void
avrftdi_debug_frame(uint16_t frame)
{
static void avrftdi_debug_frame(uint16_t frame) {
static char bit_name[] = "IDLES01234567PSS";
//static char bit_name[] = "SSP76543210SELDI";
char line0[34], line1[34], line2[34];
Expand Down Expand Up @@ -106,8 +104,7 @@ void avrftdi_tpi_initpgm(PROGRAMMER *pgm) {

#define TPI_PARITY_MASK 0x2000

static inline int count1s(unsigned int x)
{
static inline int count1s(unsigned int x) {
#if defined(__GNUC__)
return __builtin_popcount(x);
#else
Expand All @@ -123,9 +120,7 @@ static inline int count1s(unsigned int x)
#endif
}

static uint16_t
tpi_byte2frame(uint8_t byte)
{
static uint16_t tpi_byte2frame(uint8_t byte) {
uint16_t frame = 0xc00f;
int parity = count1s(byte) & 1;

Expand All @@ -137,9 +132,7 @@ tpi_byte2frame(uint8_t byte)
return frame;
}

static int
tpi_frame2byte(uint16_t frame, uint8_t * byte)
{
static int tpi_frame2byte(uint16_t frame, uint8_t * byte) {
/* drop idle and start bit(s) */
*byte = (frame >> 5) & 0xff;

Expand All @@ -150,17 +143,15 @@ tpi_frame2byte(uint16_t frame, uint8_t * byte)
}

#ifdef notyet
static int
avrftdi_tpi_break(const PROGRAMMER *pgm) {
static int avrftdi_tpi_break(const PROGRAMMER *pgm) {
unsigned char buffer[] = { MPSSE_DO_WRITE | MPSSE_WRITE_NEG | MPSSE_LSB, 1, 0, 0, 0 };
E(ftdi_write_data(to_pdata(pgm)->ftdic, buffer, sizeof(buffer)) != sizeof(buffer), to_pdata(pgm)->ftdic);

return 0;
}
#endif /* notyet */

static int
avrftdi_tpi_write_byte(const PROGRAMMER *pgm, unsigned char byte) {
static int avrftdi_tpi_write_byte(const PROGRAMMER *pgm, unsigned char byte) {
uint16_t frame;

struct ftdi_context* ftdic = to_pdata(pgm)->ftdic;
Expand All @@ -185,8 +176,7 @@ avrftdi_tpi_write_byte(const PROGRAMMER *pgm, unsigned char byte) {
#define TPI_FRAME_SIZE 12
#define TPI_IDLE_BITS 2

static int
avrftdi_tpi_read_byte(const PROGRAMMER *pgm, unsigned char *byte) {
static int avrftdi_tpi_read_byte(const PROGRAMMER *pgm, unsigned char *byte) {
uint16_t frame;

/* use 2 guard bits, 2 default idle bits + 12 frame bits = 16 bits total */
Expand Down Expand Up @@ -229,8 +219,7 @@ avrftdi_tpi_read_byte(const PROGRAMMER *pgm, unsigned char *byte) {
return err;
}

static int
avrftdi_tpi_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {
static int avrftdi_tpi_program_enable(const PROGRAMMER *pgm, const AVRPART *p) {
return avr_tpi_program_enable(pgm, p, TPIPCR_GT_2b);
}

Expand All @@ -257,8 +246,7 @@ avrftdi_cmd_tpi(const PROGRAMMER *pgm, const unsigned char *cmd, int cmd_len,
return 0;
}

static void
avrftdi_tpi_disable(const PROGRAMMER *pgm) {
static void avrftdi_tpi_disable(const PROGRAMMER *pgm) {
unsigned char cmd[] = {TPI_OP_SSTCS(TPIPCR), 0};
pgm->cmd_tpi(pgm, cmd, sizeof(cmd), NULL, 0);

Expand Down
2 changes: 1 addition & 1 deletion src/avrpart.c
Original file line number Diff line number Diff line change
Expand Up @@ -989,7 +989,7 @@ void avr_free_part(AVRPART * d) {
d->mem = NULL;
ldestroy_cb(d->mem_alias, (void(*)(void *)) avr_free_memalias);
d->mem_alias = NULL;
ldestroy_cb(d->variants, cfg_free);
ldestroy_cb(d->variants, mmt_f_free);
d->variants = NULL;

/* do not free d->parent_id and d->config_file */
Expand Down
44 changes: 22 additions & 22 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ Component_t avr_comp[] = {

void cleanup_config(void)
{
ldestroy_cb(part_list, (void(*)(void*))avr_free_part);
ldestroy_cb(programmers, (void(*)(void*))pgm_free);
ldestroy_cb(string_list, (void(*)(void*))free_token);
ldestroy_cb(number_list, (void(*)(void*))free_token);
ldestroy_cb(part_list, (void(*)(void*)) avr_free_part);
ldestroy_cb(programmers, (void(*)(void*)) pgm_free);
ldestroy_cb(string_list, (void(*)(void*)) free_token);
ldestroy_cb(number_list, (void(*)(void*)) free_token);
}

int init_config(void)
Expand Down Expand Up @@ -217,7 +217,7 @@ char *cfg_strdup(const char *funcname, const char *s) {
}


void cfg_free(void *ptr) {
void mmt_f_free(void *ptr) {
mmt_free(ptr);
}

Expand Down Expand Up @@ -263,7 +263,7 @@ int yywarning(char * errmsg, ...)


TOKEN * new_token(int primary) {
TOKEN * tkn = (TOKEN *) cfg_malloc("new_token()", sizeof(TOKEN));
TOKEN * tkn = (TOKEN *) mmt_malloc(sizeof(TOKEN));
tkn->primary = primary;
return tkn;
}
Expand All @@ -275,12 +275,12 @@ void free_token(TOKEN * tkn)
switch (tkn->value.type) {
case V_STR:
if (tkn->value.string)
free(tkn->value.string);
mmt_free(tkn->value.string);
tkn->value.string = NULL;
break;
}

free(tkn);
mmt_free(tkn);
}
}

Expand Down Expand Up @@ -382,7 +382,7 @@ TOKEN *new_constant(const char *con) {
TOKEN *new_string(const char *text) {
struct token_t *tkn = new_token(TKN_STRING);
tkn->value.type = V_STR;
tkn->value.string = cfg_strdup("new_string()", text);
tkn->value.string = mmt_strdup(text);

#if DEBUG
msg_info("STRING(%s)\n", tkn->value.string);
Expand Down Expand Up @@ -451,7 +451,7 @@ int read_config(const char * file)
f = fopen(cfg_infile, "r");
if (f == NULL) {
pmsg_ext_error("cannot open config file %s: %s\n", cfg_infile, strerror(errno));
free(cfg_infile);
mmt_free(cfg_infile);
cfg_infile = NULL;
return -1;
}
Expand All @@ -469,7 +469,7 @@ int read_config(const char * file)
fclose(f);

if(cfg_infile) {
free(cfg_infile);
mmt_free(cfg_infile);
cfg_infile = NULL;
}

Expand Down Expand Up @@ -500,18 +500,18 @@ const char *cache_string(const char *p) {

h = strhash(p) % (sizeof hstrings/sizeof*hstrings);
if(!(hs=hstrings[h]))
hs = hstrings[h] = (char **) cfg_realloc("cache_string()", NULL, (16+1)*sizeof**hstrings);
hs = hstrings[h] = (char **) mmt_realloc(NULL, (16+1)*sizeof**hstrings);

for(k=0; hs[k]; k++)
if(*p == *hs[k] && str_eq(p, hs[k]))
return hs[k];

if(k && k%16 == 0)
hstrings[h] = (char **) cfg_realloc("cache_string()", hstrings[h], (k+16+1)*sizeof**hstrings);
hstrings[h] = (char **) mmt_realloc(hstrings[h], (k+16+1)*sizeof**hstrings);

hstrings[h][k+1]=NULL;
hstrings[h][k+1] = NULL;

return hstrings[h][k] = cfg_strdup("cache_string()", p);
return hstrings[h][k] = mmt_strdup(p);
}


Expand All @@ -537,9 +537,9 @@ COMMENT *locate_comment(const LISTID comments, const char *where, int rhs) {

static void addcomment(int rhs) {
if(lkw) {
COMMENT *node = cfg_malloc("addcomment()", sizeof(*node));
COMMENT *node = mmt_malloc(sizeof(*node));
node->rhs = rhs;
node->kw = cfg_strdup("addcomment()", lkw);
node->kw = mmt_strdup(lkw);
node->comms = cfg_comms;
cfg_comms = NULL;
if(!cfg_strctcomms)
Expand All @@ -562,7 +562,7 @@ LISTID cfg_get_prologue(void) {
void capture_comment_str(const char *com, int lineno) {
if(!cfg_comms)
cfg_comms = lcreat(NULL, 0);
ladd(cfg_comms, cfg_strdup("capture_comment_str()", com));
ladd(cfg_comms, mmt_strdup(com));

// Last keyword lineno is the same as this comment's
if(lkw && lkw_lineno == lineno)
Expand All @@ -583,8 +583,8 @@ void capture_lvalue_kw(const char *kw, int lineno) {
kw = "*"; // Show comment before programmer/part/memory

if(lkw)
free(lkw);
lkw = cfg_strdup("capture_lvalue_kw()", kw);
mmt_free(lkw);
lkw = mmt_strdup(kw);
lkw_lineno = lineno;
if(cfg_comms) // Accrued list of # one-line comments
addcomment(0); // Register comment to appear before lkw assignment
Expand Down Expand Up @@ -789,7 +789,7 @@ char *cfg_unescape(char *d, const char *s) {
return (char *) cfg_unescapeu((unsigned char *) d, (const unsigned char *) s);
}

// Return an escaped string that looks like a C-style input string incl quotes, memory is malloc'd
// Return an mmt_malloc'd escaped string that looks like a C-style input string incl quotes
char *cfg_escape(const char *s) {
char buf[50*1024], *d = buf;

Expand Down Expand Up @@ -833,7 +833,7 @@ char *cfg_escape(const char *s) {
*d++ = '"';
*d = 0;

return cfg_strdup("cfg_escape()", buf);
return mmt_strdup(buf);
}


Expand Down
Loading

0 comments on commit caf4746

Please sign in to comment.