diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2021-08-23 23:49:17 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2021-08-24 00:48:46 -0700 |
commit | 6150dcd14773517d5c0fff0fee7bc6fe6f75e086 (patch) | |
tree | 086cb81303863adaca76364c703bce4dab66953e /src | |
parent | 34e7db0b48b952141b8fd392ff03dfc57124fc3d (diff) |
thunk_h: bump submodule for new lifecycle model
Udpate thunk usage througout to explicitly control thunk instance
lifecycles from calleees according to new model.
This enables discarding a bunch of the per-object dispatch
thunks, eliminates some thunk leaks, and I think generally makes
the code more expressive and clear about what's going on.
Keep in mind this is all experimental and and I'm not spending a
whole lot of time on this, it's mostly a toy and exploring some
different programming styles I'd never really consider for
production/real work.
Though it actually has some interesting properties, and produces
some surprisingly succinct and readable listings at times once
you have the cumbersome building blocks in place. Especially for
non-daemon programs where you can basically either log+ignore
errors or treat them as fatal, I think this programming style
might actually have its place.
Diffstat (limited to 'src')
-rw-r--r-- | src/journals.c | 59 | ||||
-rw-r--r-- | src/machid.c | 2 | ||||
-rw-r--r-- | src/readfile.c | 2 | ||||
-rw-r--r-- | src/report-entry-arrays.c | 33 | ||||
-rw-r--r-- | src/verify-hashed-objects.c | 24 |
5 files changed, 35 insertions, 85 deletions
diff --git a/src/journals.c b/src/journals.c index 05c14f0..f2df53e 100644 --- a/src/journals.c +++ b/src/journals.c @@ -91,7 +91,7 @@ THUNK_DEFINE_STATIC(opened_journal, iou_t *, iou, iou_op_t *, op, journals_t *, free(fds); - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } return 0; @@ -248,7 +248,7 @@ THUNK_DEFINE_STATIC(got_iter_object_header, iou_t *, iou, iou_op_t *, op, journa iter_object_header->size = le64toh(iter_object_header->size); - return thunk_dispatch_keep(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -270,12 +270,6 @@ THUNK_DEFINE_STATIC(got_iter_object_header, iou_t *, iou, iou_op_t *, op, journa * *iter_object_header, and do whatever is appropriate upon reaching the end of * the journal. If journal_iter_next_object() recurs after reaching this * point, it will restart iterating from the first object of the journal. - * - * Currently closures before the end of journal are dispatched w/the - * non-freeing variant thunk_dispatch_keep(). Only the last dispatch - * w/(*iter_offset == 0) is dispatched with the freeing thunk_dispatch(). This - * feels clunky, but it works for now. I might extend thunk.h to let closures - * control wether their dispatch should free or not via the return value. TODO */ THUNK_DEFINE(journal_iter_next_object, iou_t *, iou, journal_t **, journal, Header *, header, uint64_t *, iter_offset, ObjectHeader *, iter_object_header, thunk_t *, closure) { @@ -322,15 +316,15 @@ THUNK_DEFINE_STATIC(journal_iter_objects_dispatch, iou_t *, iou, journal_t **, j { int r; - if (!(*iter_offset)) - return thunk_dispatch(closure); - - r = thunk_dispatch_keep(closure); + r = thunk_dispatch(closure); if (r < 0) return r; - return journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( - journal_iter_objects_dispatch(iou, journal, header, iter_offset, iter_object_header, closure))); + if (!(*iter_offset)) + return 0; + + return thunk_end(journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( + journal_iter_objects_dispatch(iou, journal, header, iter_offset, iter_object_header, closure)))); } @@ -404,7 +398,7 @@ THUNK_DEFINE_STATIC(got_hash_table_iter_object_header, iou_t *, iou, iou_op_t *, } } - return thunk_dispatch_keep(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -426,13 +420,6 @@ THUNK_DEFINE_STATIC(got_hash_table_iter_object_header, iou_t *, iou, iou_op_t *, * the hash table. If journal_hash_table_iter_next_object() recurs after * reaching this point, it will restart iterating from the first object of the * table. - * - * Currently closures before the end of hash_table are dispatched w/the - * non-freeing variant thunk_dispatch_keep(). Only the last dispatch - * w/(*iter_offset == 0) is dispatched with the freeing thunk_dispatch(). - * This feels clunky, but it works for now. I might extend thunk.h to let - * closures control wether their dispatch should free or not via the return - * value. TODO */ THUNK_DEFINE(journal_hash_table_iter_next_object, iou_t *, iou, journal_t **, journal, HashItem **, hash_table, uint64_t *, hash_table_size, uint64_t *, iter_bucket, uint64_t *, iter_offset, HashedObjectHeader *, iter_object_header, size_t, iter_object_size, thunk_t *, closure) { @@ -464,7 +451,7 @@ THUNK_DEFINE(journal_hash_table_iter_next_object, iou_t *, iou, journal_t **, jo (*iter_bucket)++; if (*iter_bucket >= nbuckets) { *iter_offset = 0; - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } (*iter_offset) = (*hash_table)[*iter_bucket].head_hash_offset; } while (!(*iter_offset)); @@ -487,15 +474,15 @@ THUNK_DEFINE_STATIC(journal_hash_table_for_each_dispatch, iou_t *, iou, journal_ { int r; - if (!*iter_offset) - return thunk_dispatch(closure); - - r = thunk_dispatch_keep(closure); + r = thunk_dispatch(closure); if (r < 0) return r; - return journal_hash_table_iter_next_object(iou, journal, hash_table, hash_table_size, iter_bucket, iter_offset, iter_object_header, iter_object_size, THUNK( - journal_hash_table_for_each_dispatch(iou, journal, hash_table, hash_table_size, iter_bucket, iter_offset, iter_object_header, iter_object_size, closure))); + if (!*iter_offset) + return 0; + + return thunk_end(journal_hash_table_iter_next_object(iou, journal, hash_table, hash_table_size, iter_bucket, iter_offset, iter_object_header, iter_object_size, THUNK( + journal_hash_table_for_each_dispatch(iou, journal, hash_table, hash_table_size, iter_bucket, iter_offset, iter_object_header, iter_object_size, closure)))); } @@ -543,7 +530,7 @@ THUNK_DEFINE_STATIC(got_hashtable, iou_t *, iou, iou_op_t *, op, HashItem *, tab *res_hash_table = table; - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -618,7 +605,7 @@ THUNK_DEFINE_STATIC(got_header, iou_t *, iou, iou_op_t *, op, journal_t *, journ header->field_hash_chain_depth = le64toh(header->field_hash_chain_depth); /* TODO: validation/sanity checks? */ - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -662,7 +649,7 @@ THUNK_DEFINE_STATIC(got_object_header, iou_t *, iou, iou_op_t *, op, ObjectHeade object_header->size = le64toh(object_header->size); /* TODO: validation/sanity checks? */ - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -765,7 +752,7 @@ THUNK_DEFINE_STATIC(got_object, iou_t *, iou, iou_op_t *, op, uint64_t, size, Ob assert(0); } - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -852,11 +839,9 @@ THUNK_DEFINE(journals_for_each, journals_t **, journals, journal_t **, journal_i (*journal_iter) = &j->journals[i]; - r = thunk_dispatch_keep(closure); - if (r < 0) { - thunk_free(closure); + r = thunk_dispatch(closure); + if (r < 0) return r; - } } thunk_free(closure); diff --git a/src/machid.c b/src/machid.c index adc3ca0..5edd24a 100644 --- a/src/machid.c +++ b/src/machid.c @@ -51,7 +51,7 @@ THUNK_DEFINE_STATIC(have_machid, iou_t *, iou, char *, buf, size_t *, size, char if (!*res_ptr) return -ENOMEM; - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } diff --git a/src/readfile.c b/src/readfile.c index 0726dd7..76f7976 100644 --- a/src/readfile.c +++ b/src/readfile.c @@ -45,7 +45,7 @@ THUNK_DEFINE_STATIC(read_done, iou_t *, iou, iou_op_t *, op, int, fd, char *, bu *size = op->result; - return thunk_dispatch(closure); + return thunk_end(thunk_dispatch(closure)); } diff --git a/src/report-entry-arrays.c b/src/report-entry-arrays.c index 98f54e1..73aea34 100644 --- a/src/report-entry-arrays.c +++ b/src/report-entry-arrays.c @@ -125,24 +125,7 @@ THUNK_DEFINE_STATIC(per_entry_array_payload, iou_t *, iou, iou_op_t *, op, uint6 free(payload_buf); - return thunk_dispatch(closure); -} - - -/* this is derived from journal_iter_objects_dispatch(), and frankly the need for a separate dispatch - * thunk is pretty much entirely because thunk.h doesn't have a more streamlined means of controlling - * thunk instance life-cycles. If the return value could control freeing in thunk_dispatch(), I don't - * think this kruft would exist at all. But in the interest of just making things work for now, leave - * it be and do this junk TODO FIXME - * XXX also, if this manual dispatch sticks around, journals.[ch] should prolly just export this variant - * for the manual iter cases... - */ -THUNK_DEFINE_STATIC(per_object_dispatch, uint64_t *, iter_offset, thunk_t *, closure) -{ - if (!(*iter_offset)) - return thunk_dispatch(closure); - - return thunk_dispatch_keep(closure); + return thunk_end(thunk_dispatch(closure)); } @@ -235,8 +218,7 @@ THUNK_DEFINE_STATIC(per_object, thunk_t *, self, uint64_t *, iter_offset, Object /* skip non-entry-array objects */ if (iter_object_header->type != OBJECT_ENTRY_ARRAY) - return journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( - per_object_dispatch(iter_offset, self))); + return thunk_mid(journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, self)); stats->count++; @@ -260,12 +242,10 @@ THUNK_DEFINE_STATIC(per_object, thunk_t *, self, uint64_t *, iter_offset, Object op->sqe->flags = IOSQE_FIXED_FILE; op_queue(iou, op, THUNK( per_entry_array_payload(iou, op, payload_size, buf, stats, THUNK( - journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( - per_object_dispatch(iter_offset, self))))))); + journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, self))))); } - - return 0; + return 1; } @@ -288,9 +268,8 @@ THUNK_DEFINE_STATIC(per_journal, iou_t *, iou, journal_t **, journal_iter) foo->journal = *journal_iter; return journal_get_header(iou, &foo->journal, &foo->header, THUNK( - journal_iter_next_object(iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, THUNK( - per_object_dispatch(&foo->iter_offset, THUNK_INIT( - per_object(closure, closure, &foo->iter_offset, &foo->iter_object_header, iou, &foo->journal, &foo->header, &foo->stats))))))); + journal_iter_next_object(iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, THUNK_INIT( + per_object(closure, closure, &foo->iter_offset, &foo->iter_object_header, iou, &foo->journal, &foo->header, &foo->stats))))); } diff --git a/src/verify-hashed-objects.c b/src/verify-hashed-objects.c index 8d79228..f5c2c74 100644 --- a/src/verify-hashed-objects.c +++ b/src/verify-hashed-objects.c @@ -174,17 +174,6 @@ THUNK_DEFINE_STATIC(per_hashed_object, journal_t *, journal, Header *, header, O return thunk_dispatch(closure); } -/* XXX TODO: this should prolly move into journals.[ch] now that it's - * in both here and report-entry-arrays.c - */ -THUNK_DEFINE_STATIC(per_object_dispatch, uint64_t *, iter_offset, thunk_t *, closure) -{ - if (!(*iter_offset)) - return thunk_dispatch(closure); - - return thunk_dispatch_keep(closure); -} - THUNK_DEFINE_STATIC(per_object, thunk_t *, self, iou_t *, iou, journal_t **, journal, Header *, header, uint64_t *, iter_offset, ObjectHeader *, iter_object_header, Object **, iter_object, void **, decompressed) { @@ -201,8 +190,7 @@ THUNK_DEFINE_STATIC(per_object, thunk_t *, self, iou_t *, iou, journal_t **, jou /* skip non-hashed objects */ if (iter_object_header->type != OBJECT_FIELD && iter_object_header->type != OBJECT_DATA) - return journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( - per_object_dispatch(iter_offset, self))); + return thunk_mid(journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, self)); if (malloc_usable_size(*iter_object) < iter_object_header->size) { free(*iter_object); @@ -212,10 +200,9 @@ THUNK_DEFINE_STATIC(per_object, thunk_t *, self, iou_t *, iou, journal_t **, jou return -ENOMEM; } - return journal_get_object(iou, journal, iter_offset, &iter_object_header->size, iter_object, THUNK( + return thunk_mid(journal_get_object(iou, journal, iter_offset, &iter_object_header->size, iter_object, THUNK( per_hashed_object(*journal, header, iter_object, decompressed, THUNK( - journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, THUNK( - per_object_dispatch(iter_offset, self))))))); + journal_iter_next_object(iou, journal, header, iter_offset, iter_object_header, self)))))); } @@ -240,9 +227,8 @@ THUNK_DEFINE_STATIC(per_journal, iou_t *, iou, journal_t **, journal_iter) foo->iter_object = foo->decompressed = NULL; return journal_get_header(iou, &foo->journal, &foo->header, THUNK( - journal_iter_next_object(iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, THUNK( - per_object_dispatch(&foo->iter_offset, THUNK_INIT( - per_object(closure, closure, iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, &foo->iter_object, &foo->decompressed))))))); + journal_iter_next_object(iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, THUNK_INIT( + per_object(closure, closure, iou, &foo->journal, &foo->header, &foo->iter_offset, &foo->iter_object_header, &foo->iter_object, &foo->decompressed))))); } |