summaryrefslogtreecommitdiffstats
path: root/lib/vio_fifo.c
blob: 685f33ec05639ccd11c75df19818bcaeac32451f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
/* VTY I/O FIFO
 * Copyright (C) 2010 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your
 * option) any later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stddef.h>
#include <string.h>

#include "vio_fifo.h"
#include "network.h"
#include "list_util.h"

#include "memory.h"
#include "zassert.h"

/*==============================================================================
 * VTY I/O FIFO manages an arbitrary length byte-wise FIFO buffer.
 *
 * The FIFO is arranged as lumps of some given size.  Lumps are allocated
 * as and when necessary, and released once emptied.
 *
 * The last lump is never released.  So, it may be that only one lump is
 * ever needed.
 *
 * When releasing lumps, keeps one lump "spare", to be reused as necessary.
 * This is used in ... TODO <<<<  And is released...
 *
 *------------------------------------------------------------------------------
 * Implementation notes:
 *
 * The FIFO is initialised with all pointers NULL -- so with no lumps at all.
 *
 * Once a lump has been allocated there is always one lump in the FIFO.
 *
 * The following are expected to be true:
 *
 *   * put_ptr == get_ptr    => FIFO empty
 *
 *   * put_ptr == tail->end  -- at all times (NULL when no lumps)
 *
 *     put_ptr >= tail->data )  otherwise something is broken
 *     put_ptr <= tail->end  )
 *
 *   * get_ptr == head->end  -- when there is more than one lump
 *     get_ptr <= put_ptr    -- when there is only one lump
 *
 *     get_ptr >= head->data )  otherwise something is broken
 *     get_ptr <= head->end  )
 *
 *   * put_ptr == put_end    => tail lump is full
 *     put_ptr <  put_end    => space exists in the tail lump
 *     put_ptr >  put_end    => broken
 *
 *   * get_ptr == get_end    => head lump is empty
 *                              BUT if there is only one lump, make sure that
 *                              get_end == put_ptr.
 *     get_ptr <  get_end    => data exists in the head lump
 *     get_ptr >  get_end    => broken
 *
 * Note that:
 *
 *   * when the get_ptr reaches the put_ptr the pointers are reset to the
 *     start of the one and only lump.
 *
 *     Everywhere that the get_ptr is moved, must check for meeting the
 *     put_ptr and reset pointers.  At the same time, when reaches the end of
 *     a lump, gets rid of it.
 *
 *   * when advancing the put_ptr does not check for advancing the get_end.
 *
 *     The one exception to this, is that when the put_ptr advances to a new
 *     block, if there was one lump, sets the get_end to the end of that block.
 *
 *     Everywhere that the get_end is used, must check for there being one
 *     lump and the possibility that put_ptr has changed.
 */

/*==============================================================================
 * Initialisation, allocation and freeing of FIFO and lumps thereof.
 */

/* Return default size, or given size rounded up to 16 byte boundary    */
static size_t
vio_fifo_size(size_t size)
{
  if (size == 0)
    return 4096 ;
  else
    return ((size + 16 - 1) / 16) * 16 ;
} ;

/*==============================================================================
 * Initialise VTY I/O FIFO -- allocating if required.
 */
extern vio_fifo
vio_fifo_init_new(vio_fifo vf, size_t size)
{
  if (vf == NULL)
    vf = XCALLOC(MTYPE_VIO_FIFO, sizeof(vio_fifo_t)) ;
  else
    memset(vf, 0, sizeof(vio_fifo_t)) ;

  /* Zeroising the the vio_fifo_t has set:
   *
   *    lump      -- base pair, both pointers NULL => list is empty
   *
   *    put_ptr   -- NULL ) no lump to put anything into
   *    put_end   -- NULL ) put_ptr == put_end   => no room in current lump
   *
   *    get_ptr   -- NULL ) no lump to get anything from
   *    get_end   -- NULL ) get_ptr -- get_end => nothing left in current lump
   *
   *    rdr_lump  -- NULL ) no rdr_lump
   *    rdr_ptr   -- NULL
   *
   *    spare     -- NULL  no spare lump
   *
   * ALSO  put_ptr == get_ptr => FIFO is empty !
   */

  vf->size = vio_fifo_size(size) ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  return vf ;
}

/*------------------------------------------------------------------------------
 * Free contents of given FIFO, and free FIFO structure as well, if required.
 *
 * Does nothing if given a NULL pointer -- must already have been freed !
 *
 * If does not free the FIFO structure, resets it all empty.
 *
 * Frees *all* FIFO lumps.
 *
 * See also: vio_fifo_reset_keep(vio_fifo)
 *           vio_fifo_reset_free(vio_fifo)
 */
extern vio_fifo
vio_fifo_reset(vio_fifo vf, int free_structure)
{
  vio_fifo_lump lump ;

  if (vf == NULL)
    return NULL ;

  while (ddl_pop(&lump, vf->base, list) != NULL)
    XFREE(MTYPE_VIO_FIFO_LUMP, lump) ;

  if (vf->spare != NULL)
    XFREE(MTYPE_VIO_FIFO_LUMP, vf->spare) ;

  if (free_structure)
    XFREE(MTYPE_VIO_FIFO, vf) ;         /* sets vf = NULL       */
  else
    vio_fifo_init_new(vf, vf->size) ;

  return vf ;
} ;

/*------------------------------------------------------------------------------
 * The FIFO is empty, with one lump -- reset all pointers.
 */
inline static void
vio_fifo_ptr_reset(vio_fifo vf, vio_fifo_lump lump)
{
  if (vf->rdr_lump != NULL)
    {
      assert((lump == vf->rdr_lump) && (vf->rdr_ptr == vf->get_ptr)) ;
      vf->rdr_ptr = lump->data ;
    } ;

  /* Note that sets the lump->end to the true lump->end                 */
  vf->get_ptr = vf->get_end = vf->put_ptr = lump->data ;
  vf->put_end = lump->end = lump->data + lump->size ;
} ;

/*------------------------------------------------------------------------------
 * The FIFO is utterly empty, with ZERO lumps -- unset all pointers.
 */
inline static void
vio_fifo_ptr_unset(vio_fifo vf)
{
  assert((ddl_head(vf->base) == NULL) && (ddl_tail(vf->base) == NULL)) ;

  vf->one     = false ;

  vf->put_ptr = NULL ;
  vf->put_end = NULL ;
  vf->get_ptr = NULL ;
  vf->get_end = NULL ;

  vf->rdr_lump = NULL ;
  vf->rdr_ptr  = NULL ;
} ;

/*------------------------------------------------------------------------------
 * Clear out contents of FIFO -- will continue to use the FIFO.
 *
 * Keeps one FIFO lump.  (Frees everything else, including any spare.)
 */
extern void
vio_fifo_clear(vio_fifo vf)
{
  vio_fifo_lump tail ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  assert(vf != NULL) ;

  tail = ddl_tail(vf->base) ;

  if (tail != NULL)
    {
      while (ddl_head(vf->base) != tail)
        {
          vio_fifo_lump lump ;
          ddl_pop(&lump, vf->base, list) ;
          XFREE(MTYPE_VIO_FIFO_LUMP, lump) ;
        } ;

      vf->rdr_lump  = NULL ;            /* clear rdr            */
      vf->rdr_ptr   = NULL ;

      vf->one = true ;
      vio_fifo_ptr_reset(vf, tail) ;
    }
  else
    vio_fifo_ptr_unset(vf) ;

  if (vf->spare != NULL)
    XFREE(MTYPE_VIO_FIFO_LUMP, vf->spare) ;   /* sets vf->spare = NULL  */

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*------------------------------------------------------------------------------
 * See how much room there is in the FIFO.
 *
 * If no lumps have been allocated, returns the size of the lump that would
 * allocate.
 *
 * Otherwise, returns the amount of space available *without* allocating any
 * further lumps.
 *
 * Returns: room available as described
 */
extern size_t
vio_fifo_room(vio_fifo vf)
{
  if (vf->put_ptr != NULL)
    return vf->put_end - vf->put_ptr ;
  else
    return vf->size ;
} ;

/*------------------------------------------------------------------------------
 * Allocate another lump for putting into.
 *
 * Call when (vf->put_ptr >= vf->put_end) -- asserts that they are equal.
 *
 * Set the put_ptr/put_end pointers to point at the new lump.
 *
 * If this is the first lump allocated, set the get_ptr/get_end pointers too.
 *
 * If have just filled the first lump on the list, update the get_end pointer
 * to reflect the fact that the out lump is now full.
 */
extern void
vio_fifo_lump_new(vio_fifo vf, size_t size)
{
  vio_fifo_lump lump ;
  int      first_alloc ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  passert(vf->put_ptr == vf->put_end) ; /* must be end of tail lump     */

  if (vf->one)
    vf->get_end = vf->put_ptr ;         /* update get_end               */

  lump = ddl_tail(vf->base) ;

  first_alloc = (lump == NULL) ;        /* extra initialisation needed  */

  if (first_alloc)
    assert(vf->put_ptr == NULL) ;       /* must all be NULL together    */
  else
    assert(vf->put_ptr == lump->end) ;  /* must be end of tail lump     */

  size = vio_fifo_size(size) ;

  if ((vf->spare != NULL) && (vf->spare->size >= size))
    {
      lump = vf->spare ;
      vf->spare = NULL ;
    }
  else
    {
      lump = XMALLOC(MTYPE_VIO_FIFO_LUMP,
                                       offsetof(vio_fifo_lump_t, data[size])) ;
      lump->size = size ;
    } ;

  lump->end = lump->data + lump->size ;

  ddl_append(vf->base, lump, list) ;

  vf->one     = first_alloc ;

  vf->put_ptr = lump->data ;
  vf->put_end = lump->end ;

  if (first_alloc)
    {
      vf->get_ptr = vf->put_ptr ;       /* get_ptr == put_ptr => empty  */
      vf->get_end = vf->put_ptr ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*------------------------------------------------------------------------------
 * Release lump, head or tail (or both) and update pointers.
 *
 * Note that this does release the lump if it is the only lump.
 *
 * Do nothing if nothing is yet allocated.
 *
 * If releasing the only lump in the FIFO, resets all pointers to NULL.
 *
 * If releasing the head lump:
 *
 *   * the lump MUST be finished with -- so vf->get_ptr must be at the end
 *
 *     If releasing the only lump, the FIFO MUST be empty.
 *
 *   * if the lump is the current vf->rdr_lump, the reader must be at the
 *     end too -- ie it must be the same as the vf->get_ptr !
 *
 * If releasing the tail lump:
 *
 *   * the lump MUST be empty
 *
 *     If releasing the only lump, the FIFO MUST be empty.
 *
 *   * if the lump is the current vf->rdr_lump, the reader must be at the
 *     end too -- ie it must be the same as the vf->get_ptr !
 */
static void
vio_fifo_lump_release(vio_fifo vf, vio_fifo_lump lump)
{
  vio_fifo_lump head ;
  vio_fifo_lump tail ;
  vio_fifo_lump free ;
  bool release_head ;
  bool release_tail ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  /* Prepare and check whether removing head or tail (or both)          */
  head = ddl_head(vf->base) ;
  tail = ddl_tail(vf->base) ;

  release_head = (lump == head) ;
  release_tail = (lump == tail) ;

  assert(release_head || release_tail) ;

  /* Unless nothing ever allocated -- release the lump.                 */
  free = lump ;                 /* expect to free the lump      */
  if (lump != NULL)
    {
      vio_fifo_lump keep ;

      /* Consistency checks                                     */
      if (release_head)
        {
          if (release_tail)
            assert(vf->get_ptr == vf->put_ptr) ;
          else
            assert(vf->get_ptr == lump->end) ;

          if (vf->rdr_lump == lump)
            assert(vf->rdr_ptr == vf->get_ptr) ;
        }
      else if (release_tail)
        {
          assert(vf->put_ptr == lump->data) ;

          if (vf->rdr_lump == lump)
            assert(vf->rdr_ptr == vf->put_ptr) ;
        } ;

      /* Remove lump from FIFO and decide whether to keep as spare, or
       * which of spare and this to free.
       */
      ddl_del(vf->base, lump, list) ;

      keep = vf->spare ;        /* expect to keep current spare */

      if ((keep == NULL) || (keep->size < lump->size))
        {
          keep = lump ;
          free = vf->spare ;
        } ;

      vf->spare = keep ;

      head = ddl_head(vf->base) ;   /* changed if released head */
      tail = ddl_tail(vf->base) ;   /* changed if released tail */
    } ;

  /* Now update pointers... depending on what was released and what have
   * left.
   */
  if (head == NULL)
    {
      /* Deal with FIFO that now has no lumps or had none to start with     */
      if (lump != NULL)
        assert(vf->one) ;

      vio_fifo_ptr_unset(vf) ;
    }
  else
    {
      /* Have at least one lump left -- so must have had at least two ! */
      assert(!vf->one) ;

      vf->one = (head == tail) ;        /* update       */

      if (release_head)
        {
          /* Released the head.
           *
           * Update the vf->get_ptr and the vf->get_end.
           */
          vf->get_ptr = head->data ;
          if (vf->one)
            vf->get_end = vf->put_ptr ;
          else
            vf->get_end = head->end ;

          /* Update vf->rdr_ptr and vf->rdr_lump.       */
         if (vf->rdr_lump == lump)
           {
             vf->rdr_lump = head ;
             vf->rdr_ptr  = head->data ;
           } ;
        }
      else
        {
          /* Released the tail.
           * Update the vf->put_ptr and vf->put_end
           */
          vf->put_ptr = vf->put_end = tail->end ;

         /* Update vf->rdr_ptr and vf->rdr_lump.        */
         if (vf->rdr_lump == lump)
           {
             vf->rdr_lump = tail ;
             vf->rdr_ptr  = tail->end ;
           } ;
        } ;
    } ;

  /* Finally, free any lump that is actually to be freed                */

  if (free != NULL)
    XFREE(MTYPE_VIO_FIFO_LUMP, free) ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*------------------------------------------------------------------------------
 * Re-allocate lump for putting into.
 *
 * Call when vf->put_ptr == start of last lump, and that lump is not big
 * enough !
 *
 * There must be at least one lump.
 *
 * Updates put_ptr/put_end pointers to point at the new lump.
 *
 * Updates get_ptr/get_end pointers if required.
 *
 * Updates rdr_ptr if required.
 */
static void
vio_fifo_lump_renew(vio_fifo vf, vio_fifo_lump lump, size_t size)
{
  bool    rdr_set ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  /* FIFO may not be completely empty.
   * This must be the last lump.
   * The last lump must be empty.
   */
  assert((lump != NULL) && (lump == ddl_tail(vf->base))) ;

  /* Remove the last, *empty* lump, and update all pointers to suit.    */
  rdr_set = (vf->rdr_lump == lump) ;

  vio_fifo_lump_release(vf, lump) ;

  /* Now allocate a new lump with the required size                     */
  vio_fifo_lump_new(vf, size) ;

  /* Restore the rdr_ptr, if required                                   */
  if (rdr_set)
    {
      vio_fifo_lump tail ;

      tail = ddl_tail(vf->base) ;

      vf->rdr_lump = tail ;
      vf->rdr_ptr  = tail->data ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*==============================================================================
 * Put data to the FIFO.
 */

/*------------------------------------------------------------------------------
 * Store 'n' bytes -- allocate new lump if current is exhausted.
 */
extern void
vio_fifo_put(vio_fifo vf, const char* src, size_t n)
{
  size_t take ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  while (n > 0)
    {
      if (vf->put_ptr >= vf->put_end)
        vio_fifo_lump_new(vf, vf->size) ; /* traps put_ptr > put_end    */

      take = (vf->put_end - vf->put_ptr) ;
      if (take > n)
        take = n ;

      memcpy(vf->put_ptr, src, take) ;
      vf->put_ptr += take ;

      src += take ;
      n   -= take ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*------------------------------------------------------------------------------
 * Formatted print to fifo -- cf printf()
 *
 * Returns: >= 0 -- number of bytes written
 *           < 0 -- failed (unlikely though that is)
 */
extern int
vio_fifo_printf(vio_fifo vf, const char* format, ...)
{
  va_list args;
  int      len ;

  va_start (args, format);
  len = vio_fifo_vprintf(vf, format, args);
  va_end (args);

  return len;
} ;

/*------------------------------------------------------------------------------
 * Formatted print to fifo -- cf vprintf()
 *
 * Returns: >= 0 -- number of bytes written
 *           < 0 -- failed (unlikely though that is)
 */
extern int
vio_fifo_vprintf(vio_fifo vf, const char *format, va_list args)
{
  va_list  ac ;
  int      len ;
  int      have ;
  size_t   size ;
  vio_fifo_lump lump ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  size = vf->size ;             /* standard allocation size             */
  while (1)
    {
      /* Find what space is left in the tail lump, and allocate a new,
       * empty lump if required.
       */
      if (vf->put_ptr >= vf->put_end)
        vio_fifo_lump_new(vf, size) ;   /* traps put_ptr > put_end      */

      have = vf->put_end - vf->put_ptr ;
      assert(have > 0) ;

      /* Note that vsnprintf() returns the length of what it would like to
       * have produced, if it had the space.  That length does not include
       * the trailing '\0'.
       */
      va_copy(ac, args) ;
      len = vsnprintf(vf->put_ptr, have, format, ac) ;
      va_end(ac) ;

      if (len < have)
        {
          if (len < 0)
            break ;             /* quit if failed       */

          vf->put_ptr += len ;
          break ;               /* done                 */
        } ;

      /* Not able to complete the operation in the current buffer.
       *
       * If the required space (len + 1) is greater than the standard
       * allocation, then need to increase the allocation for the next lump.
       *
       * If the current lump is empty, need to renew it with a fresh lump of
       * the now known required size.
       *
       * If the current lump is not empty, need to cut the end off and then
       * allocate a fresh lump (of the standard or now known required size).
       */
      if (len >= (int)size)
        size = len + 1 ;                /* need a non-standard size     */

      lump = ddl_tail(vf->base) ;

      if (vf->put_ptr == lump->data)
        /* Need to replace the last, empty, lump with another empty lump, but
         * big enough.
         */
        vio_fifo_lump_renew(vf, lump, size) ;
      else
        /* Need to cut this lump short, and allocate new lump at top of loop.
         */
        lump->end = vf->put_end = vf->put_ptr ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  return len ;
} ;

/*==============================================================================
 * Get data from the FIFO.
 */

static bool vio_fifo_get_next_lump(vio_fifo vf) ;

/*------------------------------------------------------------------------------
 * Get ready to read something out of the FIFO.
 *
 * Makes sure vf->get_end is up to date (if required) and if the FIFO is not
 * empty, makes sure vf->get_ptr points at the next byte to be read.
 *
 * Returns: true <=> there is something in the FIFO.
 */
static inline bool
vio_fifo_get_ready(vio_fifo vf)
{
  assert(vf->rdr_lump == NULL) ;

  if (vf->one)
    vf->get_end = vf->put_ptr ;         /* make sure have everything    */

  if (vf->get_ptr >= vf->get_end)
    if (!vio_fifo_get_next_lump(vf))
      return 0 ;                        /* quit now if nothing there    */

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  return 1 ;
} ;

/*------------------------------------------------------------------------------
 * Get upto 'n' bytes.
 *
 * Returns: number of bytes got -- may be zero.
 */
extern size_t
vio_fifo_get(vio_fifo vf, void* dst, size_t n)
{
  size_t have ;
  void*  dst_in ;

  if (!vio_fifo_get_ready(vf))
    return 0 ;                          /* quit now if nothing there    */

  dst_in = dst ;
  while (n > 0)
    {
      have = vf->get_end - vf->get_ptr ;

      if (have > n)
        have = n ;

      memcpy(dst, vf->get_ptr, have) ;
      vf->get_ptr += have ;
      dst = (char*)dst + have ;

      if (vf->get_ptr >= vf->get_end)   /* deal with exhausted lump     */
        if (!vio_fifo_get_next_lump(vf))
          break ;                       /* quit if nothing more to come */

      n -= have ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  return (char*)dst - (char*)dst_in ;
} ;

/*------------------------------------------------------------------------------
 * Get byte -- the long winded way.
 *
 * See the inline vio_fifo_get_byte().
 *
 * The version is used when the get_ptr is at or just before the end of the
 * current lump.  Looks after all the necessary pointer updates associated with
 * hitting end of lump, or hitting end of FIFO.
 *
 * Returns: 0x00..0xFF -- byte value      (as an int)
 *          -1         => FIFO is empty.
 */

extern int
vio_fifo_get_next_byte(vio_fifo vf)
{
  unsigned char u ;

  if (!vio_fifo_get_ready(vf))
    return -1 ;                         /* quit now if nothing there    */

  u = *vf->get_ptr++ ;

  /* As soon as reach the end want either to discard empty lump, or reset
   * the pointers.
   */
  if (vf->get_ptr >= vf->get_end)       /* deal with exhausted lump     */
    vio_fifo_get_next_lump(vf) ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  return u ;
} ;

/*------------------------------------------------------------------------------
 * Get pointer to a lump of bytes.
 *
 * Returns: address of next byte to get, *p_have = number of bytes available
 *      or: NULL => FIFO is empty,       *p_have = 0
 *
 * If the FIFO is not empty, will return pointer to at least one byte.
 *
 * Returns number of bytes to the end of the current lump.  There may be
 * further lumps beyond the current one.
 */
extern void*
vio_fifo_get_lump(vio_fifo vf, size_t* p_have)
{
  if (!vio_fifo_get_ready(vf))
    {
      *p_have = 0 ;
      return NULL ;
    } ;

  *p_have = (vf->get_end - vf->get_ptr) ;
  return vf->get_ptr ;
} ;

/*------------------------------------------------------------------------------
 * Advance FIFO to position reached.
 *
 * Having done vio_fifo_get_lump(), can take any number of bytes (up to the
 * number that "have"), then call this function to advance the pointers.
 *
 * The "here" argument must the the address returned by vio_fifo_get_lump()
 * plus the number of bytes taken.
 */
extern void
vio_fifo_got_upto(vio_fifo vf, void* here)
{
  vf->get_ptr = here ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  if (vf->get_ptr >= vf->get_end)
    vio_fifo_get_next_lump(vf) ;
} ;

/*------------------------------------------------------------------------------
 * Write contents of FIFO -- assuming non-blocking file
 *
 * Will write all of FIFO, or upto but excluding the last lump.
 *
 * Returns: > 0 => blocked
 *            0 => all gone (up to last lump if !all)
 *          < 0 => failed -- see errno
 *
 * Note: will work perfectly well for a non-blocking file -- which should
 *       never return EAGAIN/EWOULDBLOCK, so will return from here "all gone".
 */
extern int
vio_fifo_write_nb(vio_fifo vf, int fd, bool all)
{
  char*   src ;
  size_t  have ;
  int     done ;

  while ((src = vio_fifo_get_lump(vf, &have)) != NULL)
    {
      if (!all && vf->one)
        break ;                         /* don't write last lump        */

      done = write_nb(fd, src, have) ;

      if (done < 0)
        return -1 ;                     /* failed                       */

      vio_fifo_got_upto(vf, src + done) ;

      if (done < (int)have)
        return 1 ;                      /* blocked                      */
    } ;

  return 0 ;                            /* all gone                     */
} ;

/*------------------------------------------------------------------------------
 * Get the current rdr_end value.
 *
 * Unlike get_end, do not have a field for this, but find it each time.
 */
inline static char*
vio_fifo_rdr_end(vio_fifo vf)
{
  if (vf->rdr_lump == ddl_tail(vf->base))
    return vf->put_ptr ;
  else
    return vf->rdr_lump->end ;
} ;

/*------------------------------------------------------------------------------
 * Get the current rdr position -- sets it up if not currently set.
 *
 * Returns: address of next byte to get, *p_have = number of bytes available
 *      or: NULL => FIFO is empty,       *p_have = 0
 *
 * If the FIFO is not empty, will return pointer to at least one byte.
 *
 * Returns number of bytes to the end of the current lump.  There may be
 * further lumps beyond the current one.
 *
 * NB: unless returns FIFO is empty, it is a mistake to now do any "get"
 *     operation other than vio_fifo_step_rdr(), until do vio_fifo_sync_rdr()
 *     or vio_fifo_drop_rdr.
 */
extern void*
vio_fifo_get_rdr(vio_fifo vf, size_t* p_have)
{
  if (!vio_fifo_get_ready(vf))
    {
      *p_have = 0 ;
      return NULL ;
    } ;

  if (vf->rdr_lump == NULL)     /* set up new rdr if required   */
    {
      vf->rdr_lump = ddl_head(vf->base) ;
      vf->rdr_ptr  = vf->get_ptr ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  *p_have = vio_fifo_rdr_end(vf) - vf->rdr_ptr ;
  return vf->rdr_ptr ;
} ;

/*------------------------------------------------------------------------------
 * Step the rdr forward by the given number of bytes.
 *
 * Returns: address of next byte to get, *p_have = number of bytes available
 *      or: NULL => FIFO is empty,       *p_have = 0
 *
 * If the FIFO is not empty, will return pointer to at least one byte.
 *
 * Returns number of bytes to the end of the current lump.  There may be
 * further lumps beyond the current one.
 *
 * NB: this does not change the get pointers, so all the data being stepped
 *     over is preserved in the FIFO, until vio_fifo_sync_rdr().
 *
 * NB: the step may NOT exceed the last reported "have".
 */
extern void*
vio_fifo_step_rdr(vio_fifo vf, size_t* p_have, size_t step)
{
  char* rdr_end ;

  assert(vf->rdr_lump != NULL) ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  rdr_end = vio_fifo_rdr_end(vf) ;
  vf->rdr_ptr += step ;

  if (vf->rdr_ptr >= rdr_end)
    {
      assert(vf->rdr_ptr == rdr_end) ;

      if (vf->rdr_lump != ddl_tail(vf->base))
        {
          vf->rdr_lump = ddl_next(vf->rdr_lump, list) ;
          vf->rdr_ptr  = vf->rdr_lump->data ;

          rdr_end = vio_fifo_rdr_end(vf) ;
        } ;
    } ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  *p_have = (rdr_end - vf->rdr_ptr) ;
  return (*p_have > 0) ? vf->rdr_ptr : NULL ;
} ;

/*------------------------------------------------------------------------------
 * Move FIFO get position to the rdr position, if any.
 *
 * This clears the rdr position, and removes all data between the current and
 * new get positions from the FIFO.
 */
extern void
vio_fifo_sync_rdr(vio_fifo vf)
{
  vio_fifo_lump head ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;

  if (vf->rdr_lump == NULL)
    return ;

  while ((head = ddl_head(vf->base)) != vf->rdr_lump)
    {
      vf->get_ptr = vf->get_end ;       /* jump to end of lump  */
      vio_fifo_lump_release(vf, head) ;
    } ;

  vf->get_ptr  = vf->rdr_ptr ;          /* jump to rdr_ptr      */

  vf->rdr_lump = NULL ;                 /* clear the rdr        */
  vf->rdr_ptr  = NULL ;

  if (vf->one)
    {
      if (vf->put_ptr == vf->get_ptr)   /* reset pointers if FIFO empty  */
        vio_fifo_ptr_reset(vf, head) ;
      else
        vf->get_end = vf->put_ptr ;
    }
  else
    vf->get_end = head->end ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
} ;

/*------------------------------------------------------------------------------
 * Drop the rdr position (if any).
 *
 * This clears the rdr position leaving the get position and FIFO unchanged.
 */
extern void
vio_fifo_drop_rdr(vio_fifo vf)
{
  VIO_FIFO_DEBUG_VERIFY(vf) ;

  vf->rdr_lump = NULL ;
  vf->rdr_ptr  = NULL ;
} ;

/*------------------------------------------------------------------------------
 * Move on to next lump to get stuff from.
 *
 * Advance pointers etc. so that have at least one byte available, unless
 * the FIFO is entirely empty.
 *
 * This should be called if (vf->get_ptr >= vf->get_end) -- asserts that
 * these are equal !
 *
 * NB: when there is only one block, it may be that get_end is out of date,
 *     and should be advanced to the current put_ptr position.
 *
 *     That is done here, but may be worth updating get_end before testing
 *     against get_ptr.
 *
 * Returns: true <=> at least one byte in FIFO.
 *
 * NB: if finds that the FIFO is empty, resets the pointers to the start
 *     of the last lump -- does not release the last lump.
 */
static bool
vio_fifo_get_next_lump(vio_fifo vf)
{
  vio_fifo_lump head ;

  VIO_FIFO_DEBUG_VERIFY(vf) ;
  assert(vf->get_ptr == vf->get_end) ;

  head = ddl_head(vf->base) ;   /* current lump for get     */

  /* Deal with the simple case of one lump, first.
   *
   * To save work when putting data into the FIFO (particularly when putting
   * a byte at a time) does not keep the vf->get_end up to date (when there is
   * only one lump).
   *
   * If the FIFO is empty, reset pointers and return empty.
   */
  if (vf->one)
    {
      assert( (head != NULL) && (head == ddl_tail(vf->base)) ) ;

      if (vf->get_ptr < vf->put_ptr)
        {
          /* Had an out of date vf->get_end                     */
          vf->get_end = vf->put_ptr ;

          return true ;         /* FIFO not empty               */
        } ;

      assert(vf->get_ptr == vf->put_ptr) ;

      /* FIFO is empty -- reset pointers and exit               */
      vio_fifo_ptr_reset(vf, head) ;

      return false ;            /* FIFO empty                   */
    } ;

  /* Release the head and update pointers
   *
   * Deals with possibility that nothing has yet been allocated
   */
  vio_fifo_lump_release(vf, head) ;

  return (vf->get_ptr < vf->get_end) ;
} ;

/*==============================================================================
 * For debug purposes -- verify the state of the given FIFO
 */
Private void
vio_fifo_verify(vio_fifo vf)
{
  vio_fifo_lump head ;
  vio_fifo_lump lump ;
  vio_fifo_lump tail ;

  head = ddl_head(vf->base) ;
  tail = ddl_tail(vf->base) ;

  /* If nothing allocated, should all be NULL & !vf->one        */
  /* If something allocated, tail must not be NULL              */
  if (head == NULL)
    {
      if ( (tail != NULL)
          || (vf->put_ptr  != NULL)
          || (vf->put_end  != NULL)
          || (vf->get_ptr  != NULL)
          || (vf->rdr_lump != NULL)
          || (vf->rdr_ptr  != NULL)
          || (vf->one) )
        zabort("nothing allocated, but not all NULL") ;
      return ;
    }
  else
    {
      if (tail == NULL)
        zabort("head pointer not NULL, but tail pointer is") ;
    } ;

  /* Check that all the pointers are within respective lumps
   *
   * Know that put_end is always tail->end, but get_end need not be.
   */
  if ( (tail->data > vf->put_ptr)
      ||            (vf->put_ptr > vf->put_end)
      ||                          (vf->put_end != tail->end) )
    zabort("put pointers outside the tail lump") ;

  if ( (head->data > vf->get_ptr)
      ||            (vf->get_ptr > vf->get_end)
      ||                          (vf->get_end > head->end) )
    zabort("get pointers outside the head lump") ;

  /* If head == tail, should be vf->one, etc.                   */
  if (head == tail)
    {
      if (!vf->one)
        zabort("have one lump, but !vf->one") ;

      if (vf->get_end > vf->put_ptr)
        zabort("get_end is greater than put_ptr when vf->one") ;
    }
  else
    {
      if (vf->one)
        zabort("have two or more lumps, but vf->one is true") ;

      if (vf->get_end != head->end)
        zabort("get_end is not head->end when !vf->one") ;
    } ;

  /* If have an rdr_lump -- make sure everything else is valid  */
  if (vf->rdr_lump != NULL)
    {
      lump = head ;
      while (lump != vf->rdr_lump)
        {
          if (lump == tail)
            zabort("rdr_lump is not part of FIFO") ;
          lump = ddl_next(lump, list) ;
        } ;

      if ( (lump->data > vf->rdr_ptr)
          ||            (vf->rdr_ptr > lump->end) )
        zabort("rdr_ptr outside its lump") ;

      if ( (lump == head) && (vf->rdr_ptr < vf->get_ptr))
         zabort("rdr_ptr is less than get_ptr in first lump") ;

      if ( (lump == tail) && (vf->rdr_ptr > vf->put_ptr))
         zabort("rdr_ptr is greater than put_ptr in last lump") ;
    }
  else
    {
      if (vf->rdr_ptr != NULL)
        zabort("rdr_ptr not NULL when rdr_lump is") ;
    }
} ;