small bugfixes and memory optimizations
authorMatthias Kramm <kramm@matthias-kramms-macbook-pro.local>
Wed, 27 May 2009 03:06:59 +0000 (20:06 -0700)
committerMatthias Kramm <kramm@matthias-kramms-macbook-pro.local>
Wed, 27 May 2009 03:06:59 +0000 (20:06 -0700)
lib/gfxpoly/convert.c
lib/gfxpoly/poly.c
lib/gfxpoly/poly.h
lib/gfxpoly/renderpoly.c
lib/gfxpoly/test.c
lib/q.c

index 7cf2329..1b35c8a 100644 (file)
@@ -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;
 typedef struct _compactpoly {
     gfxpoly_t*poly;
     point_t last;
-    int strokes_size;
     point_t*points;
     int num_points;
     int points_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->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);
     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;
     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
        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)
 {
 }
 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);
 {
     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;
     //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->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->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)
 }
 
 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;
 void gfxpoly_destroy(gfxpoly_t*poly)
 {
     int t;
-    for(t=0;t<poly->num_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);
 }
 
     free(poly);
 }
 
index 1c9e739..d2c5295 100644 (file)
@@ -22,8 +22,8 @@ void gfxpoly_fail(char*expr, char*file, int line, const char*function)
     void*md5 = init_md5();
    
     int s,t;
     void*md5 = init_md5();
    
     int s,t;
-    for(s=0;s<current_polygon->num_strokes;s++) {
-       gfxpolystroke_t*stroke = &current_polygon->strokes[s];
+    gfxpolystroke_t*stroke = current_polygon->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(t=0;t<stroke->num_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));
        for(t=0;t<stroke->num_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;
 {
     int s,t;
     int edges = 0;
-    for(t=0;t<poly->num_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;
        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;
 {
     dict_t*d = dict_new2(&point_type);
     int s,t;
-    for(t=0;t<poly->num_strokes;t++) {
-       gfxpolystroke_t*stroke = &poly->strokes[t];
+    gfxpolystroke_t*stroke = poly->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(s=0;s<stroke->num_points;s++) {
            point_t p = stroke->points[s];
            int num = (s>=1 && s<stroke->num_points-1)?2:1; // mid points are two points (start+end)
        for(s=0;s<stroke->num_points;s++) {
            point_t p = stroke->points[s];
            int num = (s>=1 && s<stroke->num_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);
     int s,t;
     double g = poly->gridsize;
     fprintf(stderr, "polyon %08x (gridsize: %f)\n", poly, poly->gridsize);
-    for(t=0;t<poly->num_strokes;t++) {
-       gfxpolystroke_t*stroke = &poly->strokes[t];
+    gfxpolystroke_t*stroke = poly->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(s=0;s<stroke->num_points-1;s++) {
            point_t a = stroke->points[s];
            point_t b = stroke->points[s+1];
        for(s=0;s<stroke->num_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;
     fprintf(fi, "%% gridsize %f\n", poly->gridsize);
     fprintf(fi, "%% begin\n");
     int s,t;
-    for(t=0;t<poly->num_strokes;t++) {
-       gfxpolystroke_t*stroke = &poly->strokes[t];
+    gfxpolystroke_t*stroke = poly->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(s=0;s<stroke->num_points-1;s++) {
            point_t a = stroke->points[s];
            point_t b = stroke->points[s+1];
        for(s=0;s<stroke->num_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;
 static void gfxpoly_enqueue(gfxpoly_t*p, heap_t*queue, int polygon_nr)
 {
     int t;
-    for(t=0;t<p->num_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
        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);
     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;
     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(x<e->p.x);
 
 #endif
                assert(x<e->p.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
                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
     }
 
         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);
 }
     actlist_destroy(actlist);
     heap_destroy(queue);
 }
index 5d50209..417c6c1 100644 (file)
@@ -52,7 +52,6 @@ typedef struct _gfxpolystroke {
 } gfxpolystroke_t;
 typedef struct _gfxpoly {
     double gridsize;
 } gfxpolystroke_t;
 typedef struct _gfxpoly {
     double gridsize;
-    int num_strokes;
     gfxpolystroke_t*strokes;
 } gfxpoly_t;
 
     gfxpolystroke_t*strokes;
 } gfxpoly_t;
 
index 2da9c63..9c9f11f 100644 (file)
@@ -140,8 +140,8 @@ unsigned char* render_polygon(gfxpoly_t*polygon, intbbox_t*bbox, double zoom, wi
 
     int polygon_nr = 0;
     int s,t;
 
     int polygon_nr = 0;
     int s,t;
-    for(s=0;s<polygon->num_strokes;s++) {
-       gfxpolystroke_t*stroke = &polygon->strokes[s];
+    gfxpolystroke_t*stroke = polygon->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(t=0;t<stroke->num_points-1;t++) {
            point_t a = stroke->points[t];
            point_t b = stroke->points[t+1];
        for(t=0;t<stroke->num_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;
 
 
     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;
     }
     int s,t;
-    for(s=0;s<polygon->num_strokes;s++) {
-       gfxpolystroke_t*stroke = &polygon->strokes[s];
+    gfxpolystroke_t*stroke = polygon->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(t=0;t<stroke->num_points;t++) {
            point_t p = stroke->points[t];
            int x1 = floor(p.x);
        for(t=0;t<stroke->num_points;t++) {
            point_t p = stroke->points[t];
            int x1 = floor(p.x);
index a04f779..a6a5ab5 100644 (file)
@@ -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;
        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;
        //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, "");
     //gfxline_transform(b, &matrix);
 
     gfxline_dump(b, stderr, "");
-
     gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
     gfxpoly_t*poly = gfxpoly_from_gfxline(b, 0.05);
+    
     gfxline_free(box1);
     gfxline_free(box2);
     gfxline_free(box3);
     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);
         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);
         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
 
 #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;
 
        int i,j;
-       for(i=0;i<poly2->num_strokes;i++) {
-           gfxpolystroke_t*stroke = &poly2->strokes[i];
+       gfxpolystroke_t*stroke = poly2->strokes;
+       for(;stroke;stroke=stroke->next) {
            for(j=0;j<stroke->num_points-1;j++) {
                point_t a = stroke->points[j];
                point_t b = stroke->points[j+1];
            for(j=0;j<stroke->num_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;
 void rotate90(gfxpoly_t*poly)
 {
     int i,j;
-    for(i=0;i<poly->num_strokes;i++) {
-       gfxpolystroke_t*stroke = &poly->strokes[i];
+    gfxpolystroke_t*stroke = poly->strokes;
+    for(;stroke;stroke=stroke->next) {
        for(j=0;j<stroke->num_points;j++) {
            point_t a = stroke->points[j];
            stroke->points[j].x = a.y;
        for(j=0;j<stroke->num_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 (file)
--- 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) 
 {
 
 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)
 {
     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) {
     if(!s)
         return checksum;
     while(*s) {
@@ -665,8 +663,7 @@ unsigned int string_hash(const string_t*str)
 {
     int t;
     unsigned int checksum = 0;
 {
     int t;
     unsigned int checksum = 0;
-    if(!crc32)
-        crc32_init();
+    crc32_init();
     for(t=0;t<str->len;t++) {
         checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
     }
     for(t=0;t<str->len;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;
 {
     unsigned int checksum = 0;
     const char*p = str;
-    if(!crc32)
-        crc32_init();
+    crc32_init();
     while(*p) {
         checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
         p++;
     while(*p) {
         checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
         p++;