summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2023-07-27 15:24:42 -0700
committerMatthew Sotoudeh <matthew@masot.net>2023-07-27 15:24:42 -0700
commit6f645bed54bd328d8a4075aad498892b0164c713 (patch)
treee9081d416b666159ef2a384699c4eb1e7a84c0a8
parent2b8985608d33abaae7b201a008e292cbbe2167ef (diff)
support arrays of pointers in refcounting
-rwxr-xr-xpython/examples/refcounting/dietpass6
-rw-r--r--python/examples/refcounting/test.c48
2 files changed, 33 insertions, 21 deletions
diff --git a/python/examples/refcounting/dietpass b/python/examples/refcounting/dietpass
index 9a7b0e8..9d6a77f 100755
--- a/python/examples/refcounting/dietpass
+++ b/python/examples/refcounting/dietpass
@@ -67,8 +67,10 @@ def ptr_exprs(name, ty):
elif isinstance(ty, (BasicType, FunctionType)):
return []
elif isinstance(ty, ArrayType):
- # TODO
- return []
+ result = []
+ for i in range(int(ty.length)):
+ result.extend(ptr_exprs(f"(({name})[{i}])", ty.base))
+ return result
else:
print(ty.classify(), file=sys.stderr)
raise NotImplementedError
diff --git a/python/examples/refcounting/test.c b/python/examples/refcounting/test.c
index 212bb8b..cd53e8b 100644
--- a/python/examples/refcounting/test.c
+++ b/python/examples/refcounting/test.c
@@ -2,33 +2,43 @@
#include <stdio.h>
int *foo(void) {
- int *ptr = 0;
- for (int i = 0; i < 5; i++) {
+ int *ptr = 0;
+ for (int i = 0; i < 5; i++)
+ ptr = calloc(1, sizeof(*ptr));
ptr = calloc(1, sizeof(*ptr));
- }
- ptr = calloc(1, sizeof(*ptr));
- ptr = calloc(1, sizeof(*ptr));
- *ptr = 5;
- return ptr;
+ ptr = calloc(1, sizeof(*ptr));
+ *ptr = 5;
+ return ptr;
}
struct bar {
- int x;
- int *y;
+ int x;
+ int *y;
};
int *bar(void) {
- struct bar bar;
- bar.x = 5;
- bar.y = calloc(1, sizeof(int));
- *bar.y = bar.x;
- return bar.y;
+ struct bar bar;
+ bar.x = 5;
+ bar.y = calloc(1, sizeof(int));
+ *bar.y = bar.x;
+ return bar.y;
+}
+
+int *arrays(void) {
+ int *foo[3];
+ foo[0] = calloc(1, sizeof(int));
+ foo[1] = calloc(1, sizeof(int));
+ foo[2] = calloc(1, sizeof(int));
+ *(foo[1]) = 123;
+ return foo[1];
}
int main(void) {
- int *x = foo();
- printf("Result of foo(): %d\n", *x);
- x = bar();
- printf("Result of bar(): %d\n", *x);
- return 0;
+ int *x = foo();
+ printf("Result of foo(): %d\n", *x);
+ x = bar();
+ printf("Result of bar(): %d\n", *x);
+ x = arrays();
+ printf("Result of arrays(): %d\n", *x);
+ return 0;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback