summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesper Jensen <jesper@jnsn.dev>2026-02-11 20:02:04 +0100
committerJesper Jensen <jesper@jnsn.dev>2026-02-11 20:02:04 +0100
commit679907d1b2023396b5f353b3390ca0879fc15e50 (patch)
tree2688a6f42bcf2688c80a96a9bd2128982befcd64
parent254e05a47ad4e22fa20885dec62acc59ecc561d0 (diff)
Free some memory before it leaks
-rw-r--r--examples/stacked.c95
-rw-r--r--inc/cad/construction.h1
-rw-r--r--inc/cad/solve.h1
-rw-r--r--inc/cad/topology.h1
-rw-r--r--src/construction.c12
-rw-r--r--src/main.c7
-rw-r--r--src/solve.c10
-rw-r--r--src/topology.c7
-rw-r--r--test/construction.c8
-rw-r--r--test/solve.c3
10 files changed, 101 insertions, 44 deletions
diff --git a/examples/stacked.c b/examples/stacked.c
index c61a441..8f3d09b 100644
--- a/examples/stacked.c
+++ b/examples/stacked.c
@@ -1,35 +1,39 @@
#include "cad.h"
struct box {
- struct component corner[4];
- struct component side[4];
+ struct component bottom_left;
+ struct component bottom_right;
+ struct component top_right;
+ struct component top_left;
+
+ struct component bottom;
+ struct component right;
+ struct component top;
+ struct component left;
};
void a_box(struct box *box, struct topology *topo, struct constraints *constr) {
assert(box != NULL);
*box = (struct box){
- .corner = {
- {.type = COM_POINT},
- {.type = COM_POINT},
- {.type = COM_POINT},
- {.type = COM_POINT},
- },
- .side = {
- {.type = COM_LINE},
- {.type = COM_LINE},
- {.type = COM_LINE},
- {.type = COM_LINE},
- },
+ .bottom_left = {.type = COM_POINT},
+ .bottom_right = {.type = COM_POINT},
+ .top_right = {.type = COM_POINT},
+ .top_left = {.type = COM_POINT},
+
+ .bottom = {.type = COM_LINE},
+ .right = {.type = COM_LINE},
+ .top = {.type = COM_LINE},
+ .left = {.type = COM_LINE},
};
if(topo != NULL) {
add_fragment(topo, (struct topology_elem[]){
- TOPO_MOVETO(&box->corner[0]),
- TOPO_LINETO(&box->corner[1]),
- TOPO_LINETO(&box->corner[2]),
- TOPO_LINETO(&box->corner[3]),
- TOPO_LINETO(&box->corner[0]),
+ TOPO_MOVETO(&box->bottom_left),
+ TOPO_LINETO(&box->bottom_right),
+ TOPO_LINETO(&box->top_right),
+ TOPO_LINETO(&box->top_left),
+ TOPO_LINETO(&box->bottom_left),
TOPO_END(),
});
@@ -37,21 +41,21 @@ void a_box(struct box *box, struct topology *topo, struct constraints *constr) {
if(constr != NULL) {
add_constraint(constr, (struct constraint[]){
- POINT_ON_LINE(&box->corner[0], &box->side[0]),
- POINT_ON_LINE(&box->corner[1], &box->side[0]),
+ POINT_ON_LINE(&box->bottom_left, &box->bottom),
+ POINT_ON_LINE(&box->bottom_right, &box->bottom),
- POINT_ON_LINE(&box->corner[1], &box->side[1]),
- POINT_ON_LINE(&box->corner[2], &box->side[1]),
+ POINT_ON_LINE(&box->bottom_right, &box->right),
+ POINT_ON_LINE(&box->top_right, &box->right),
- POINT_ON_LINE(&box->corner[2], &box->side[2]),
- POINT_ON_LINE(&box->corner[3], &box->side[2]),
+ POINT_ON_LINE(&box->top_right, &box->top),
+ POINT_ON_LINE(&box->top_left, &box->top),
- POINT_ON_LINE(&box->corner[0], &box->side[3]),
- POINT_ON_LINE(&box->corner[3], &box->side[3]),
+ POINT_ON_LINE(&box->bottom_left, &box->left),
+ POINT_ON_LINE(&box->top_left, &box->left),
- LL_ANGLE(&box->side[3], &box->side[0], DEG(90)),
- LL_ANGLE(&box->side[1], &box->side[2], DEG(90)),
- LL_ANGLE(&box->side[2], &box->side[3], DEG(90)),
+ LL_ANGLE(&box->left, &box->bottom, DEG(90)),
+ LL_ANGLE(&box->right, &box->top, DEG(90)),
+ LL_ANGLE(&box->top, &box->left, DEG(90)),
CEND(),
});
}
@@ -76,6 +80,8 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can
draw_topology(cv, t);
draw_constraints(cv, c);
end_drawing(cv);
+
+ free_drawing(&drawing);
}
int main(int argc, char *argv[]) {
@@ -92,18 +98,18 @@ int main(int argc, char *argv[]) {
a_box(&box3, &topo, &constraints);
add_constraint(&constraints, (struct constraint[]){
- PP_DISTANCE(&box1.corner[0], &box1.corner[1], 10),
- PP_DISTANCE(&box1.corner[1], &box1.corner[2], 10),
-
- PP_SAME(&box2.corner[0], &box1.corner[3]),
- PP_DISTANCE(&box2.corner[0], &box2.corner[1], 10),
- PP_DISTANCE(&box2.corner[1], &box2.corner[2], 10),
- LL_ANGLE(&box2.side[0], &box1.side[0], DEG(-60)),
-
- PP_SAME(&box3.corner[0], &box2.corner[3]),
- PP_DISTANCE(&box3.corner[1], &box3.corner[2], 10),
- PP_DISTANCE(&box3.corner[0], &box3.corner[1], 7),
- LL_ANGLE(&box3.side[0], &box2.side[0], DEG(-20)),
+ PP_DISTANCE(&box1.bottom_left, &box1.bottom_right, 10),
+ PP_DISTANCE(&box1.bottom_right, &box1.top_right, 10),
+
+ PP_SAME(&box2.bottom_left, &box1.top_left),
+ PP_DISTANCE(&box2.bottom_left, &box2.bottom_right, 10),
+ PP_DISTANCE(&box2.bottom_right, &box2.top_right, 10),
+ LL_ANGLE(&box2.bottom, &box1.bottom, DEG(-60)),
+
+ PP_SAME(&box3.bottom_left, &box2.top_left),
+ PP_DISTANCE(&box3.bottom_right, &box3.top_right, 10),
+ PP_DISTANCE(&box3.bottom_left, &box3.bottom_right, 7),
+ LL_ANGLE(&box3.bottom, &box2.bottom, DEG(-20)),
CEND(),
});
@@ -111,6 +117,9 @@ int main(int argc, char *argv[]) {
.state = CANVAS_INIT,
.f = stdout,
};
-
+
draw_from_constraints(&constraints, &topo, &canvas);
+
+ free_constraints(&constraints);
+ free_topology(&topo);
}
diff --git a/inc/cad/construction.h b/inc/cad/construction.h
index e4b0428..ef36421 100644
--- a/inc/cad/construction.h
+++ b/inc/cad/construction.h
@@ -79,6 +79,7 @@ struct drawing {
struct element* insert_cmd(struct drawing *drawing, struct command cmd);
void place_points(struct drawing *drawing, double inputs[]);
+void free_drawing(struct drawing *drawing);
bool circle_line_intersect(struct circle circle, struct line line, uint8_t root, struct point *point);
void line_through_points(struct point p1, struct point p2, struct line* l);
diff --git a/inc/cad/solve.h b/inc/cad/solve.h
index d1afce5..a470aae 100644
--- a/inc/cad/solve.h
+++ b/inc/cad/solve.h
@@ -103,6 +103,7 @@ struct constraint {
void alias_point(struct constraints *c, struct component *alias, struct component *target);
void add_constraint(struct constraints *c, struct constraint *new);
+void free_constraints(struct constraints *c);
bool solve_constraints(struct constraints *constraints, struct drawing *drawing);
#define DEG(x) ((x) * M_PI / 180.0)
diff --git a/inc/cad/topology.h b/inc/cad/topology.h
index b3879b2..d0bf3a5 100644
--- a/inc/cad/topology.h
+++ b/inc/cad/topology.h
@@ -48,3 +48,4 @@ struct topology {
};
void add_fragment(struct topology *topo, struct topology_elem *new);
+void free_topology(struct topology *topo);
diff --git a/src/construction.c b/src/construction.c
index 090a36f..dc80359 100644
--- a/src/construction.c
+++ b/src/construction.c
@@ -197,4 +197,16 @@ void place_points(struct drawing *drawing, double inputs[]) {
}
}
+void free_drawing(struct drawing *drawing) {
+ struct command *c = drawing->root;
+ while (c) {
+ struct command *next = c->next;
+ free(c);
+ c = next;
+ }
+ drawing->root = NULL;
+ drawing->tail = NULL;
+ drawing->error = NULL;
+}
+
diff --git a/src/main.c b/src/main.c
index 5bf91b1..3781dcd 100644
--- a/src/main.c
+++ b/src/main.c
@@ -143,6 +143,8 @@ void draw_from_constraints(struct constraints *c, struct topology *t, struct can
draw_topology(cv, t);
draw_constraints(cv, c);
end_drawing(cv);
+
+ free_drawing(&drawing);
}
int main(int argc, char *argv[]) {
@@ -268,6 +270,9 @@ int main(int argc, char *argv[]) {
.state = CANVAS_INIT,
.f = stdout,
};
-
+
draw_from_constraints(&constraints, &topo, &canvas);
+
+ free_constraints(&constraints);
+ free_topology(&topo);
}
diff --git a/src/solve.c b/src/solve.c
index 3f8e081..630a0a6 100644
--- a/src/solve.c
+++ b/src/solve.c
@@ -40,6 +40,14 @@ void add_constraint(struct constraints *c, struct constraint *new) {
c->length += new_num;
}
+void free_constraints(struct constraints *c) {
+ free(c->elements);
+ c->elements = NULL;
+ c->length = 0;
+ c->capacity = 0;
+ c->aliases_num = 0;
+}
+
// @COMPL: We should do something better than this. I don't really know what.
struct frontier {
@@ -177,6 +185,7 @@ static bool find_angle(struct constraint *constraints, size_t constraints_num, s
if(frame < frames + SEARCH_DEPTH-1) {
path[frame - frames + 1].i = -1;
}
+ free(checked);
return true;
}
@@ -187,6 +196,7 @@ static bool find_angle(struct constraint *constraints, size_t constraints_num, s
frame->head = other;
}
+ free(checked);
return false;
}
diff --git a/src/topology.c b/src/topology.c
index e12057d..3422efb 100644
--- a/src/topology.c
+++ b/src/topology.c
@@ -38,3 +38,10 @@ void add_fragment(struct topology *topo, struct topology_elem *new) {
memcpy(topo->elements + topo->length, new, new_num * sizeof(struct topology_elem));
topo->length += new_num;
}
+
+void free_topology(struct topology *topo) {
+ free(topo->elements);
+ topo->elements = NULL;
+ topo->length = 0;
+ topo->capacity = 0;
+}
diff --git a/test/construction.c b/test/construction.c
index 2bb58e5..3dbbdfc 100644
--- a/test/construction.c
+++ b/test/construction.c
@@ -65,6 +65,7 @@ int main(int argc, char *argv[]) {
assert(drawing.error == NULL);
assert(p3->point.pos[0] == 0.5 && fabs(p3->point.pos[1] - 0.866025) < 0.001);
+ free_drawing(&drawing);
}
{
@@ -131,6 +132,7 @@ int main(int argc, char *argv[]) {
assert(drawing.error == NULL);
assert(p3->point.pos[0] == 0.5 && fabs(p3->point.pos[1] - (-0.866025)) < 0.001);
+ free_drawing(&drawing);
}
{
@@ -213,6 +215,7 @@ int main(int argc, char *argv[]) {
assert(drawing.error != NULL);
assert(&drawing.error->result == p3);
+ free_drawing(&drawing);
}
{
@@ -265,6 +268,7 @@ int main(int argc, char *argv[]) {
place_points(&drawing, (double[]){1.0, 2.0});
assert(drawing.error != NULL);
assert(&drawing.error->result == p);
+ free_drawing(&drawing);
}
{
@@ -347,6 +351,7 @@ int main(int argc, char *argv[]) {
assert(drawing.error == NULL);
assert(fabs(tangent_point->point.pos[0] - 1.0) < 0.001);
assert(fabs(tangent_point->point.pos[1] - 0.0) < 0.001);
+ free_drawing(&drawing);
}
{
@@ -428,6 +433,7 @@ int main(int argc, char *argv[]) {
place_points(&drawing, (double[]){1.0, 5.0, 1.0});
assert(drawing.error != NULL);
assert(&drawing.error->result == p);
+ free_drawing(&drawing);
}
{
@@ -473,6 +479,7 @@ int main(int argc, char *argv[]) {
assert(drawing.error == NULL);
assert(fabs(intersection->point.pos[0]) < 0.001);
assert(fabs(intersection->point.pos[1]) < 0.001);
+ free_drawing(&drawing);
}
{
@@ -548,5 +555,6 @@ int main(int argc, char *argv[]) {
assert(drawing.error == NULL);
assert(fabs(intersection->point.pos[0] - (-2.0)) < 0.001);
assert(fabs(intersection->point.pos[1] - (-1.0)) < 0.001);
+ free_drawing(&drawing);
}
}
diff --git a/test/solve.c b/test/solve.c
index ea767f4..b52d5f9 100644
--- a/test/solve.c
+++ b/test/solve.c
@@ -41,6 +41,9 @@ int main(int argc, char *argv[]) {
bool solved = solve_constraints(&constraints, &drawing);
assert(!solved);
+
+ free_drawing(&drawing);
+ free_constraints(&constraints);
}
return 0;