From fec1ab31707e68c2396b186e0e4928632402d649 Mon Sep 17 00:00:00 2001 From: Matthias Kramm Date: Tue, 26 May 2009 20:06:59 -0700 Subject: [PATCH] small bugfixes and memory optimizations --- lib/gfxpoly/convert.c | 25 ++++++++++--------------- lib/gfxpoly/poly.c | 41 ++++++++++++++++------------------------- lib/gfxpoly/poly.h | 1 - lib/gfxpoly/renderpoly.c | 18 +++++++++--------- lib/gfxpoly/test.c | 16 +++++++++------- lib/q.c | 12 ++++-------- 6 files changed, 48 insertions(+), 65 deletions(-) diff --git a/lib/gfxpoly/convert.c b/lib/gfxpoly/convert.c index 7cf2329..1b35c8a 100644 --- a/lib/gfxpoly/convert.c +++ b/lib/gfxpoly/convert.c @@ -118,7 +118,6 @@ static void convert_file(const char*filename, polywriter_t*w, double gridsize) typedef struct _compactpoly { gfxpoly_t*poly; point_t last; - int strokes_size; point_t*points; int num_points; int points_size; @@ -130,13 +129,10 @@ void finish_segment(compactpoly_t*data) { if(data->num_points <= 1) return; - if(data->poly->num_strokes == data->strokes_size) { - data->strokes_size <<= 1; - assert(data->strokes_size > data->poly->num_strokes); - data->poly->strokes = rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->strokes_size); - } point_t*p = malloc(sizeof(point_t)*data->num_points); - gfxpolystroke_t*s = &data->poly->strokes[data->poly->num_strokes]; + gfxpolystroke_t*s = rfx_calloc(sizeof(gfxpolystroke_t)); + s->next = data->poly->strokes; + data->poly->strokes = s; s->num_points = data->num_points; s->dir = data->dir; s->points = p; @@ -156,7 +152,6 @@ void finish_segment(compactpoly_t*data) assert(p[t].y<=p[t+1].y); } #endif - data->poly->num_strokes++; } static void compactmoveto(polywriter_t*w, int32_t x, int32_t y) { @@ -210,7 +205,6 @@ static void*compactfinish(polywriter_t*w) { compactpoly_t*data = (compactpoly_t*)w->internal; finish_segment(data); - data->poly->strokes = (gfxpolystroke_t*)rfx_realloc(data->poly->strokes, sizeof(gfxpolystroke_t)*data->poly->num_strokes); //qsort(data->poly->strokes, data->poly->num_strokes, sizeof(gfxpolystroke_t), compare_stroke); free(data->points); gfxpoly_t*poly = data->poly; @@ -227,13 +221,12 @@ void gfxpolywriter_init(polywriter_t*w) data->poly = rfx_calloc(sizeof(gfxpoly_t)); data->poly->gridsize = 1.0; data->last.x = data->last.y = 0; - data->strokes_size = 16; data->num_points = 0; data->points_size = 16; data->new = 1; data->dir = DIR_UNKNOWN; data->points = (point_t*)rfx_alloc(sizeof(point_t)*data->points_size); - data->poly->strokes = (gfxpolystroke_t*)rfx_alloc(sizeof(gfxpolystroke_t)*data->strokes_size); + data->poly->strokes = 0; } gfxpoly_t* gfxpoly_from_gfxline(gfxline_t*line, double gridsize) @@ -255,11 +248,13 @@ gfxpoly_t* gfxpoly_from_file(const char*filename, double gridsize) void gfxpoly_destroy(gfxpoly_t*poly) { int t; - for(t=0;tnum_strokes;t++) { - free(poly->strokes[t].points); - poly->strokes[t].points = 0; + gfxpolystroke_t*stroke = poly->strokes; + while(stroke) { + gfxpolystroke_t*next = stroke->next; + free(stroke->points); + free(stroke); + stroke = next; } - free(poly->strokes); free(poly); } diff --git a/lib/gfxpoly/poly.c b/lib/gfxpoly/poly.c index 1c9e739..d2c5295 100644 --- a/lib/gfxpoly/poly.c +++ b/lib/gfxpoly/poly.c @@ -22,8 +22,8 @@ void gfxpoly_fail(char*expr, char*file, int line, const char*function) void*md5 = init_md5(); int s,t; - for(s=0;snum_strokes;s++) { - gfxpolystroke_t*stroke = ¤t_polygon->strokes[s]; + gfxpolystroke_t*stroke = current_polygon->strokes; + for(;stroke;stroke=stroke->next) { for(t=0;tnum_points;t++) { update_md5(md5, (unsigned char*)&stroke->points[t].x, sizeof(stroke->points[t].x)); update_md5(md5, (unsigned char*)&stroke->points[t].y, sizeof(stroke->points[t].y)); @@ -142,8 +142,8 @@ int gfxpoly_size(gfxpoly_t*poly) { int s,t; int edges = 0; - for(t=0;tnum_strokes;t++) { - gfxpolystroke_t*stroke = &poly->strokes[t]; + gfxpolystroke_t*stroke = poly->strokes; + for(;stroke;stroke=stroke->next) { edges += stroke->num_points-1; } return edges; @@ -153,8 +153,8 @@ char gfxpoly_check(gfxpoly_t*poly) { dict_t*d = dict_new2(&point_type); int s,t; - for(t=0;tnum_strokes;t++) { - gfxpolystroke_t*stroke = &poly->strokes[t]; + gfxpolystroke_t*stroke = poly->strokes; + for(;stroke;stroke=stroke->next) { for(s=0;snum_points;s++) { point_t p = stroke->points[s]; int num = (s>=1 && snum_points-1)?2:1; // mid points are two points (start+end) @@ -185,8 +185,8 @@ void gfxpoly_dump(gfxpoly_t*poly) int s,t; double g = poly->gridsize; fprintf(stderr, "polyon %08x (gridsize: %f)\n", poly, poly->gridsize); - for(t=0;tnum_strokes;t++) { - gfxpolystroke_t*stroke = &poly->strokes[t]; + gfxpolystroke_t*stroke = poly->strokes; + for(;stroke;stroke=stroke->next) { for(s=0;snum_points-1;s++) { point_t a = stroke->points[s]; point_t b = stroke->points[s+1]; @@ -202,8 +202,8 @@ void gfxpoly_save(gfxpoly_t*poly, const char*filename) fprintf(fi, "%% gridsize %f\n", poly->gridsize); fprintf(fi, "%% begin\n"); int s,t; - for(t=0;tnum_strokes;t++) { - gfxpolystroke_t*stroke = &poly->strokes[t]; + gfxpolystroke_t*stroke = poly->strokes; + for(;stroke;stroke=stroke->next) { for(s=0;snum_points-1;s++) { point_t a = stroke->points[s]; point_t b = stroke->points[s+1]; @@ -351,8 +351,8 @@ static void advance_stroke(heap_t*queue, gfxpolystroke_t*stroke, int polygon_nr, static void gfxpoly_enqueue(gfxpoly_t*p, heap_t*queue, int polygon_nr) { int t; - for(t=0;tnum_strokes;t++) { - gfxpolystroke_t*stroke = &p->strokes[t]; + gfxpolystroke_t*stroke = p->strokes; + for(;stroke;stroke=stroke->next) { assert(stroke->num_points > 1); #ifdef CHECKS @@ -1042,9 +1042,6 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c actlist_t* actlist = actlist_new(); event_t*e = heap_chopmax(queue); - int newstrokes_size = 4; - int num_newstrokes = 0; - gfxpolystroke_t*newstrokes = malloc(sizeof(gfxpolystroke_t)*newstrokes_size); while(e) { int32_t y = e->p.y; int32_t x = 0; @@ -1063,11 +1060,10 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c #endif assert(xp.x); - if(num_newstrokes == newstrokes_size) { - newstrokes_size = (newstrokes_size)<<1; - newstrokes = rfx_realloc(newstrokes, sizeof(gfxpolystroke_t)*newstrokes_size); - } - gfxpolystroke_t*stroke = &newstrokes[num_newstrokes++]; + gfxpolystroke_t*stroke = rfx_calloc(sizeof(gfxpolystroke_t)); + stroke->next = poly->strokes; + poly->strokes = stroke; + stroke->num_points = 2; stroke->points = malloc(sizeof(point_t)*2); stroke->dir = DIR_UP; // FIXME @@ -1137,11 +1133,6 @@ static void add_horizontals(gfxpoly_t*poly, windrule_t*windrule, windcontext_t*c assert(!fill); // check that polygon is not bleeding } - poly->strokes = rfx_realloc(poly->strokes, sizeof(gfxpolystroke_t)*(num_newstrokes+poly->num_strokes)); - memcpy(&poly->strokes[poly->num_strokes], newstrokes, sizeof(gfxpolystroke_t)*num_newstrokes); - poly->num_strokes += num_newstrokes; - free(newstrokes); - actlist_destroy(actlist); heap_destroy(queue); } diff --git a/lib/gfxpoly/poly.h b/lib/gfxpoly/poly.h index 5d50209..417c6c1 100644 --- a/lib/gfxpoly/poly.h +++ b/lib/gfxpoly/poly.h @@ -52,7 +52,6 @@ typedef struct _gfxpolystroke { } gfxpolystroke_t; typedef struct _gfxpoly { double gridsize; - int num_strokes; gfxpolystroke_t*strokes; } gfxpoly_t; diff --git a/lib/gfxpoly/renderpoly.c b/lib/gfxpoly/renderpoly.c index 2da9c63..9c9f11f 100644 --- a/lib/gfxpoly/renderpoly.c +++ b/lib/gfxpoly/renderpoly.c @@ -140,8 +140,8 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi int polygon_nr = 0; int s,t; - for(s=0;snum_strokes;s++) { - gfxpolystroke_t*stroke = &polygon->strokes[s]; + gfxpolystroke_t*stroke = polygon->strokes; + for(;stroke;stroke=stroke->next) { for(t=0;tnum_points-1;t++) { point_t a = stroke->points[t]; point_t b = stroke->points[t+1]; @@ -228,15 +228,15 @@ intbbox_t intbbox_from_polygon(gfxpoly_t*polygon, double zoom) double g = zoom*polygon->gridsize; - if(polygon->num_strokes && polygon->strokes[0].num_points) { - b.xmin = polygon->strokes[0].points[0].x*g; - b.ymin = polygon->strokes[0].points[0].y*g; - b.xmax = polygon->strokes[0].points[0].x*g; - b.ymax = polygon->strokes[0].points[0].y*g; + if(polygon->strokes && polygon->strokes->num_points) { + b.xmin = polygon->strokes->points[0].x*g; + b.ymin = polygon->strokes->points[0].y*g; + b.xmax = polygon->strokes->points[0].x*g; + b.ymax = polygon->strokes->points[0].y*g; } int s,t; - for(s=0;snum_strokes;s++) { - gfxpolystroke_t*stroke = &polygon->strokes[s]; + gfxpolystroke_t*stroke = polygon->strokes; + for(;stroke;stroke=stroke->next) { for(t=0;tnum_points;t++) { point_t p = stroke->points[t]; int x1 = floor(p.x); diff --git a/lib/gfxpoly/test.c b/lib/gfxpoly/test.c index a04f779..a6a5ab5 100644 --- a/lib/gfxpoly/test.c +++ b/lib/gfxpoly/test.c @@ -126,7 +126,8 @@ gfxline_t* make_circles(int n) int y = c%200;; c = crc32_add_byte(c, t^0x55); int r = c%100; - b = gfxline_append(b, gfxline_makecircle(x,y,r,r)); + gfxline_t*c = gfxline_makecircle(x,y,r,r); + b = gfxline_append(b, c); //b = gfxline_append(b, gfxline_makerectangle(10,10,100,100)); } return b; @@ -183,8 +184,8 @@ int test1(int argn, char*argv[]) //gfxline_transform(b, &matrix); gfxline_dump(b, stderr, ""); - gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05); + gfxline_free(box1); gfxline_free(box2); gfxline_free(box3); @@ -287,6 +288,7 @@ void test3(int argn, char*argv[]) gfxline_transform(l, &m); gfxpoly_t*poly1 = gfxpoly_from_gfxline(l, 0.05); + gfxpoly_t*poly2 = gfxpoly_process(poly1, rule, &onepolygon); tag = swf_InsertTag(tag, ST_DEFINESHAPE); @@ -303,11 +305,11 @@ void test3(int argn, char*argv[]) #define FILL #ifdef FILL - swf_ShapeSetAll(tag,s,0,0,0,fs,0); + swf_ShapeSetAll(tag,s,UNDEFINED_COORD,UNDEFINED_COORD,0,fs,0); int i,j; - for(i=0;inum_strokes;i++) { - gfxpolystroke_t*stroke = &poly2->strokes[i]; + gfxpolystroke_t*stroke = poly2->strokes; + for(;stroke;stroke=stroke->next) { for(j=0;jnum_points-1;j++) { point_t a = stroke->points[j]; point_t b = stroke->points[j+1]; @@ -359,8 +361,8 @@ void test3(int argn, char*argv[]) void rotate90(gfxpoly_t*poly) { int i,j; - for(i=0;inum_strokes;i++) { - gfxpolystroke_t*stroke = &poly->strokes[i]; + gfxpolystroke_t*stroke = poly->strokes; + for(;stroke;stroke=stroke->next) { for(j=0;jnum_points;j++) { point_t a = stroke->points[j]; stroke->points[j].x = a.y; diff --git a/lib/q.c b/lib/q.c index 1d50ca3..1d27b42 100644 --- a/lib/q.c +++ b/lib/q.c @@ -644,14 +644,12 @@ char* string_escape(string_t*str) unsigned int crc32_add_byte(unsigned int checksum, unsigned char b) { - if(!crc32) - crc32_init(); + crc32_init(); return checksum>>8 ^ crc32[(b^checksum)&0xff]; } unsigned int crc32_add_string(unsigned int checksum, const char*s) { - if(!crc32) - crc32_init(); + crc32_init(); if(!s) return checksum; while(*s) { @@ -665,8 +663,7 @@ unsigned int string_hash(const string_t*str) { int t; unsigned int checksum = 0; - if(!crc32) - crc32_init(); + crc32_init(); for(t=0;tlen;t++) { checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff]; } @@ -676,8 +673,7 @@ unsigned int string_hash2(const char*str) { unsigned int checksum = 0; const char*p = str; - if(!crc32) - crc32_init(); + crc32_init(); while(*p) { checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff]; p++; -- 1.7.10.4