From a2de3cd7877a2168e9a3fc051711d4644cf3bd81 Mon Sep 17 00:00:00 2001 From: stefan schippers Date: Sat, 22 Apr 2023 00:21:53 +0200 Subject: [PATCH] unified copy_symbol() and copy_symbol1(), better new symbol allocation policy --- src/actions.c | 17 +++++++--- src/in_memory_undo.c | 78 -------------------------------------------- src/save.c | 11 +++++++ src/store.c | 10 ------ src/xinit.c | 20 ------------ 5 files changed, 23 insertions(+), 113 deletions(-) diff --git a/src/actions.c b/src/actions.c index dc8ec182..7cd58688 100644 --- a/src/actions.c +++ b/src/actions.c @@ -539,6 +539,16 @@ void remove_symbol(int j) } } my_free(_ALLOC_ID_, &xctx->sym[j].text); + + my_free(_ALLOC_ID_, &xctx->sym[j].line); + my_free(_ALLOC_ID_, &xctx->sym[j].rect); + my_free(_ALLOC_ID_, &xctx->sym[j].arc); + my_free(_ALLOC_ID_, &xctx->sym[j].poly); + my_free(_ALLOC_ID_, &xctx->sym[j].lines); + my_free(_ALLOC_ID_, &xctx->sym[j].polygons); + my_free(_ALLOC_ID_, &xctx->sym[j].arcs); + my_free(_ALLOC_ID_, &xctx->sym[j].rects); + xctx->sym[j].texts = 0; save = xctx->sym[j]; /* save cleared symbol slot */ @@ -1208,8 +1218,7 @@ const char *get_sym_name(int inst, int ext) return sym; } - -static void copy_symbol1(xSymbol *dest_sym, xSymbol *src_sym) +void copy_symbol(xSymbol *dest_sym, xSymbol *src_sym) { int c, j; @@ -1230,7 +1239,6 @@ static void copy_symbol1(xSymbol *dest_sym, xSymbol *src_sym) my_strdup2(_ALLOC_ID_, &dest_sym->templ, src_sym->templ); my_strdup2(_ALLOC_ID_, &dest_sym->prop_ptr, src_sym->prop_ptr); - /* dest_sym->line = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xLine *)); dest_sym->poly = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xPoly *)); dest_sym->arc = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xArc *)); @@ -1239,7 +1247,6 @@ static void copy_symbol1(xSymbol *dest_sym, xSymbol *src_sym) dest_sym->rects = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); dest_sym->arcs = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); dest_sym->polygons = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - */ dest_sym->text = my_calloc(_ALLOC_ID_, src_sym->texts, sizeof(xText)); memcpy(dest_sym->lines, src_sym->lines, sizeof(dest_sym->lines[0]) * cadlayers); @@ -1335,7 +1342,7 @@ void get_additional_symbols(int what) int_hash_lookup(&sym_table, sym, j, XINSERT); dbg(1, "get_additional_symbols(): adding symbol %s\n", sym); check_symbol_storage(); - copy_symbol1(&xctx->sym[j], xctx->inst[i].ptr + xctx->sym); + copy_symbol(&xctx->sym[j], xctx->inst[i].ptr + xctx->sym); xctx->sym[j].base_name = (xctx->inst[i].ptr + xctx->sym)->name; my_strdup(_ALLOC_ID_, &xctx->sym[j].name, sym); diff --git a/src/in_memory_undo.c b/src/in_memory_undo.c index 51fa881f..0f58e4aa 100644 --- a/src/in_memory_undo.c +++ b/src/in_memory_undo.c @@ -251,84 +251,6 @@ void mem_delete_undo(void) xctx->undo_initialized = 0; } -void copy_symbol(xSymbol *dest_sym, xSymbol *src_sym) -{ - int c, j; - *dest_sym = *src_sym; - dest_sym->name = NULL; - dest_sym->base_name = NULL; - dest_sym->prop_ptr = NULL; - dest_sym->type = NULL; - dest_sym->templ = NULL; - my_strdup2(_ALLOC_ID_, &dest_sym->name, src_sym->name); - my_strdup2(_ALLOC_ID_, &dest_sym->type, src_sym->type); - my_strdup2(_ALLOC_ID_, &dest_sym->templ, src_sym->templ); - my_strdup2(_ALLOC_ID_, &dest_sym->prop_ptr, src_sym->prop_ptr); - dest_sym->line = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xLine *)); - dest_sym->poly = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xPoly *)); - dest_sym->arc = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xArc *)); - dest_sym->rect = my_calloc(_ALLOC_ID_, cadlayers, sizeof(xRect *)); - dest_sym->lines = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - dest_sym->rects = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - dest_sym->arcs = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - dest_sym->polygons = my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - dest_sym->text = my_calloc(_ALLOC_ID_, src_sym->texts, sizeof(xText)); - - memcpy(dest_sym->lines, src_sym->lines, sizeof(dest_sym->lines[0]) * cadlayers); - memcpy(dest_sym->rects, src_sym->rects, sizeof(dest_sym->rects[0]) * cadlayers); - memcpy(dest_sym->arcs, src_sym->arcs, sizeof(dest_sym->arcs[0]) * cadlayers); - memcpy(dest_sym->polygons, src_sym->polygons, sizeof(dest_sym->polygons[0]) * cadlayers); - for(c = 0;cline[c] = my_calloc(_ALLOC_ID_, src_sym->lines[c], sizeof(xLine)); - for(j = 0; j < src_sym->lines[c]; ++j) { - dest_sym->line[c][j] = src_sym->line[c][j]; - dest_sym->line[c][j].prop_ptr = NULL; - my_strdup(_ALLOC_ID_, &dest_sym->line[c][j].prop_ptr, src_sym->line[c][j].prop_ptr); - } - /* symbol rects */ - dest_sym->rect[c] = my_calloc(_ALLOC_ID_, src_sym->rects[c], sizeof(xRect)); - for(j = 0; j < src_sym->rects[c]; ++j) { - dest_sym->rect[c][j] = src_sym->rect[c][j]; - dest_sym->rect[c][j].prop_ptr = NULL; - dest_sym->rect[c][j].extraptr = NULL; - my_strdup(_ALLOC_ID_, &dest_sym->rect[c][j].prop_ptr, src_sym->rect[c][j].prop_ptr); - } - /* symbol arcs */ - dest_sym->arc[c] = my_calloc(_ALLOC_ID_, src_sym->arcs[c], sizeof(xArc)); - for(j = 0; j < src_sym->arcs[c]; ++j) { - dest_sym->arc[c][j] = src_sym->arc[c][j]; - dest_sym->arc[c][j].prop_ptr = NULL; - my_strdup(_ALLOC_ID_, &dest_sym->arc[c][j].prop_ptr, src_sym->arc[c][j].prop_ptr); - } - /* symbol polygons */ - dest_sym->poly[c] = my_calloc(_ALLOC_ID_, src_sym->polygons[c], sizeof(xPoly)); - for(j = 0; j < src_sym->polygons[c]; ++j) { - int points = src_sym->poly[c][j].points; - dest_sym->poly[c][j] = src_sym->poly[c][j]; - dest_sym->poly[c][j].prop_ptr = NULL; - dest_sym->poly[c][j].x = my_malloc(_ALLOC_ID_, points * sizeof(double)); - dest_sym->poly[c][j].y = my_malloc(_ALLOC_ID_, points * sizeof(double)); - dest_sym->poly[c][j].selected_point = my_malloc(_ALLOC_ID_, points * sizeof(unsigned short)); - my_strdup(_ALLOC_ID_, &dest_sym->poly[c][j].prop_ptr, src_sym->poly[c][j].prop_ptr); - memcpy(dest_sym->poly[c][j].x, src_sym->poly[c][j].x, points * sizeof(double)); - memcpy(dest_sym->poly[c][j].y, src_sym->poly[c][j].y, points * sizeof(double)); - memcpy(dest_sym->poly[c][j].selected_point, src_sym->poly[c][j].selected_point, - points * sizeof(unsigned short)); - } - } - /* symbol texts */ - for(j = 0; j < src_sym->texts; ++j) { - dest_sym->text[j] = src_sym->text[j]; - dest_sym->text[j].prop_ptr = NULL; - dest_sym->text[j].txt_ptr = NULL; - dest_sym->text[j].font = NULL; - my_strdup(_ALLOC_ID_, &dest_sym->text[j].prop_ptr, src_sym->text[j].prop_ptr); - my_strdup(_ALLOC_ID_, &dest_sym->text[j].txt_ptr, src_sym->text[j].txt_ptr); - my_strdup(_ALLOC_ID_, &dest_sym->text[j].font, src_sym->text[j].font); - } -} - void mem_push_undo(void) { int slot, i, c; diff --git a/src/save.c b/src/save.c index c54d5657..284d3bd4 100644 --- a/src/save.c +++ b/src/save.c @@ -3191,6 +3191,17 @@ int load_sym_def(const char *name, FILE *embed_fd, int embedded) symbol[symbols].templ = NULL; symbol[symbols].base_name=NULL; symbol[symbols].name=NULL; + + symbol[symbols].line=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xLine *)); + symbol[symbols].poly=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xPoly *)); + symbol[symbols].arc=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xArc *)); + symbol[symbols].rect=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xRect *)); + symbol[symbols].lines=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); + symbol[symbols].rects=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); + symbol[symbols].arcs=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); + symbol[symbols].polygons=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); + + my_strdup2(_ALLOC_ID_, &symbol[symbols].name,name); /* read symbol from file */ while(1) diff --git a/src/store.c b/src/store.c index 82f7bf68..f02e3e1e 100644 --- a/src/store.c +++ b/src/store.c @@ -59,16 +59,6 @@ void check_symbol_storage(void) xctx->sch[xctx->currsch] ); xctx->maxs=(1 + xctx->symbols / ELEMDEF) * ELEMDEF; my_realloc(_ALLOC_ID_, &xctx->sym, sizeof(xSymbol)*xctx->maxs); - for(i=xctx->symbols;imaxs; ++i) { - xctx->sym[i].poly=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xPoly *)); - xctx->sym[i].arc=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xArc *)); - xctx->sym[i].line=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xLine *)); - xctx->sym[i].rect=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xRect *)); - xctx->sym[i].lines=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].rects=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].polygons=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].arcs=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - } } } diff --git a/src/xinit.c b/src/xinit.c index 3e05c554..27222495 100644 --- a/src/xinit.c +++ b/src/xinit.c @@ -335,16 +335,6 @@ static void free_xschem_data() my_free(_ALLOC_ID_, &xctx->poly[i]); my_free(_ALLOC_ID_, &xctx->arc[i]); } - for(i=0;imaxs; ++i) { - my_free(_ALLOC_ID_, &xctx->sym[i].line); - my_free(_ALLOC_ID_, &xctx->sym[i].rect); - my_free(_ALLOC_ID_, &xctx->sym[i].arc); - my_free(_ALLOC_ID_, &xctx->sym[i].poly); - my_free(_ALLOC_ID_, &xctx->sym[i].lines); - my_free(_ALLOC_ID_, &xctx->sym[i].polygons); - my_free(_ALLOC_ID_, &xctx->sym[i].arcs); - my_free(_ALLOC_ID_, &xctx->sym[i].rects); - } my_free(_ALLOC_ID_, &xctx->sym); my_free(_ALLOC_ID_, &xctx->rect); my_free(_ALLOC_ID_, &xctx->line); @@ -561,16 +551,6 @@ static void alloc_xschem_data(const char *top_path, const char *win_path) xctx->wire=my_calloc(_ALLOC_ID_, xctx->maxw,sizeof(xWire)); xctx->inst=my_calloc(_ALLOC_ID_, xctx->maxi , sizeof(xInstance) ); xctx->sym=my_calloc(_ALLOC_ID_, xctx->maxs , sizeof(xSymbol) ); - for(i=0;imaxs; ++i) { - xctx->sym[i].line=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xLine *)); - xctx->sym[i].poly=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xPoly *)); - xctx->sym[i].arc=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xArc *)); - xctx->sym[i].rect=my_calloc(_ALLOC_ID_, cadlayers, sizeof(xRect *)); - xctx->sym[i].lines=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].rects=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].arcs=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - xctx->sym[i].polygons=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); - } xctx->maxr=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); xctx->maxa=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int)); xctx->maxp=my_calloc(_ALLOC_ID_, cadlayers, sizeof(int));