summaryrefslogtreecommitdiff
path: root/c/examples/refcounting/dietpass.c
diff options
context:
space:
mode:
Diffstat (limited to 'c/examples/refcounting/dietpass.c')
-rw-r--r--c/examples/refcounting/dietpass.c55
1 files changed, 13 insertions, 42 deletions
diff --git a/c/examples/refcounting/dietpass.c b/c/examples/refcounting/dietpass.c
index 33e2531..395eac2 100644
--- a/c/examples/refcounting/dietpass.c
+++ b/c/examples/refcounting/dietpass.c
@@ -3,10 +3,9 @@
#include <stdlib.h>
#include <string.h>
#include <libdietc.h>
+#include <string_helpers.c>
static int startswith(char *s, char *p);
-static struct instruction *instruction_before(struct instruction *i, char *line);
-static struct instruction *instruction_after(struct instruction *i, char *line);
static char **pointer_expressions(char *name, struct type *type);
static void rewrite(struct instruction *i, char *old, char *new);
@@ -41,22 +40,19 @@ int main(int argc, char **argv) {
sscanf(type_name, "Type_%ld", &type_id);
char **ptr_exprs = pointer_expressions(name, program.id2type[type_id]);
for (char **expr = ptr_exprs; *expr; expr++) {
- char *line = calloc(strlen(*expr) + 128, sizeof(char));
- sprintf(line, "\t%s = 0;", *expr);
- i = instruction_after(i, line);
+ char *line = sbuild("\t%s = 0;", *expr);
+ i = libdietc_insert_after(i, line);
}
// is this the return expression?
if (!strcmp(name, return_expr)) { // yes
for (char **expr = ptr_exprs; *expr; expr++) {
- char *line = calloc(strlen(*expr) + 128, sizeof(char));
- sprintf(line, "\trefcount_returning(%s);", *expr);
- instruction_before(return_instruction, line);
+ char *line = sbuild("\trefcount_returning(%s);", *expr);
+ libdietc_insert_before(return_instruction, line);
}
} else { // no
for (char **expr = ptr_exprs; *expr; expr++) {
- char *line = calloc(strlen(*expr) + 128, sizeof(char));
- sprintf(line, "\trefcount_write(0, %s);", *expr);
- instruction_after(before_return, line);
+ char *line = sbuild("\trefcount_write(0, %s);", *expr);
+ libdietc_insert_after(before_return, line);
}
}
} else if (strstr(i->line, " = ")) {
@@ -83,17 +79,14 @@ int main(int argc, char **argv) {
// *before* the instruction, need to insert backups
unsigned long backup_count = count;
for (char **e = ptr_exprs; *e; e++) {
- char *line = calloc(strlen(*e) + 128, sizeof(char));
- assert(strlen(*e));
- sprintf(line, "\tvoid *backup_%lu = (%s);", count++, *e);
- instruction_before(i, line);
+ char *line = sbuild("\tvoid *backup_%lu = (%s);", count++, *e);
+ libdietc_insert_before(i, line);
}
// *then* execute the instruction
// *then* call refcount_write
for (char **e = ptr_exprs; *e; e++) {
- char *line = calloc(strlen(*e) + 128, sizeof(char));
- sprintf(line, "\trefcount_write(%s, backup_%lu);", *e, backup_count++);
- i = instruction_after(i, line);
+ char *line = sbuild("\trefcount_write(%s, backup_%lu);", *e, backup_count++);
+ i = libdietc_insert_after(i, line);
}
}
}
@@ -107,26 +100,6 @@ static int startswith(char *s, char *p) {
return !strncmp(s, p, strlen(p));
}
-static struct instruction *instruction_before(struct instruction *i, char *line) {
- struct instruction *new = calloc(1, sizeof(*new));
- *(i->pprev) = new;
- new->pprev = i->pprev;
- new->line = line;
- new->next = i;
- i->pprev = &(new->next);
- return new;
-}
-
-static struct instruction *instruction_after(struct instruction *i, char *line) {
- struct instruction *new = calloc(1, sizeof(*new));
- new->line = line;
- new->next = i->next;
- i->next->pprev = &(new->next);
- new->pprev = &(i->next);
- i->next = new;
- return new;
-}
-
static char **empty_list() {
return calloc(1, sizeof(char *));
}
@@ -151,8 +124,7 @@ static char **pointer_expressions(char *name, struct type *type) {
case TYPE_ARRAY: {
char **list = empty_list();
for (unsigned long i = 0; i < type->length; i++) {
- char *line = calloc(strlen(name) + 128, sizeof(char));
- sprintf(line, "((%s)[%ld])", name, i);
+ char *line = sbuild("((%s)[%ld])", name, i);
list = append_list(list, line);
}
return list;
@@ -161,8 +133,7 @@ static char **pointer_expressions(char *name, struct type *type) {
case TYPE_UNION: {
char **list = empty_list();
for (struct type *m = type->members; m; m = m->next_member) {
- char *subname = calloc(strlen(name) + strlen(m->field_name) + 4, sizeof(char));
- sprintf(subname, "%s.%s", name, m->field_name);
+ char *subname = sbuild("%s.%s", name, m->field_name);
char **sublist = pointer_expressions(subname, m);
for (char **subline = sublist; *subline; subline++)
list = append_list(list, *subline);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback