From a695142b33199de25327709d4983592d96e20414 Mon Sep 17 00:00:00 2001
From: Marcus Müller <mmueller@gnuradio.org>
Date: Sun, 20 Jun 2021 18:44:37 +0200
Subject: blocks: zero-output, zero-copy Head mode
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If you don't want to test a stream with an exact number of input, but
just want your flow graph to terminate after a given number of items,
having a head block without an output enables you to do that without
copying the data from Head's in- to its output.

Signed-off-by: Marcus Müller <mmueller@gnuradio.org>
---
 gr-blocks/lib/head_impl.cc | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

(limited to 'gr-blocks/lib/head_impl.cc')

diff --git a/gr-blocks/lib/head_impl.cc b/gr-blocks/lib/head_impl.cc
index 0ee22682d0..567a12d1df 100644
--- a/gr-blocks/lib/head_impl.cc
+++ b/gr-blocks/lib/head_impl.cc
@@ -1,6 +1,7 @@
 /* -*- c++ -*- */
 /*
  * Copyright 2004,2009,2013 Free Software Foundation, Inc.
+ * Copyright 2021 Marcus Müller
  *
  * This file is part of GNU Radio
  *
@@ -27,7 +28,7 @@ head::sptr head::make(size_t sizeof_stream_item, uint64_t nitems)
 head_impl::head_impl(size_t sizeof_stream_item, uint64_t nitems)
     : sync_block("head",
                  io_signature::make(1, 1, sizeof_stream_item),
-                 io_signature::make(1, 1, sizeof_stream_item)),
+                 io_signature::make(0, 1, sizeof_stream_item)),
       d_nitems(nitems),
       d_ncopied_items(0)
 {
@@ -47,7 +48,12 @@ int head_impl::work(int noutput_items,
     if (n == 0)
         return 0;
 
-    memcpy(output_items[0], input_items[0], n * input_signature()->sizeof_stream_item(0));
+    // can have zero or one output port, if zero, don't copy
+    if (!output_items.empty()) {
+        memcpy(output_items[0],
+               input_items[0],
+               n * input_signature()->sizeof_stream_item(0));
+    }
     d_ncopied_items += n;
 
     return n;
-- 
cgit v1.2.3