+#ifdef DEBUG
+static unsigned int io_loop_level;
+static struct io_conn *free_later;
+static void io_loop_enter(void)
+{
+ io_loop_level++;
+}
+static void io_loop_exit(void)
+{
+ io_loop_level--;
+ if (io_loop_level == 0) {
+ /* Delayed free. */
+ while (free_later) {
+ struct io_conn *c = free_later;
+ free_later = c->finish_arg;
+ io_alloc.free(c);
+ }
+ }
+}
+static void free_conn(struct io_conn *conn)
+{
+ /* Only free on final exit: chain via finish. */
+ if (io_loop_level > 1) {
+ struct io_conn *c;
+ for (c = free_later; c; c = c->finish_arg)
+ assert(c != conn);
+ conn->finish_arg = free_later;
+ free_later = conn;
+ } else
+ io_alloc.free(conn);
+}
+#else
+static void io_loop_enter(void)
+{
+}
+static void io_loop_exit(void)
+{
+}
+static void free_conn(struct io_conn *conn)
+{
+ io_alloc.free(conn);
+}
+#endif