3 .\" Manual for buffer extension
5 .\" (c) 2024 Straylight/Edgeware
8 .\"----- Licensing notice ---------------------------------------------------
10 .\" This file is part of the mLib utilities library.
12 .\" mLib is free software: you can redistribute it and/or modify it under
13 .\" the terms of the GNU Library General Public License as published by
14 .\" the Free Software Foundation; either version 2 of the License, or (at
15 .\" your option) any later version.
17 .\" mLib is distributed in the hope that it will be useful, but WITHOUT
18 .\" ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 .\" FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 .\" License for more details.
22 .\" You should have received a copy of the GNU Library General Public
23 .\" License along with mLib. If not, write to the Free Software
24 .\" Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
27 .\"--------------------------------------------------------------------------
28 .so ../defs.man \" @@@PRE@@@
30 .\"--------------------------------------------------------------------------
31 .TH growbuf 3mLib "9 March 2024" "Straylight/Edgeware" "mLib utilities library"
38 .\"--------------------------------------------------------------------------
40 growbuf \- extend buffers efficiently
42 .\"--------------------------------------------------------------------------
46 .B "#include <mLib/growbuf.h>"
48 .ta \w'\fBvoid GROWBUF_SIZE('u
49 .BI "void GROWBUF_SIZE(" type ", " type " &" sz ", " type " " want ,
50 .BI " " type " " init ", " type " " granule ");"
52 .ta \w'\fBvoid GROWBUF_EXTEND('u
53 .BI "\fBvoid GROWBUF_EXTEND(" type ", arena *" a ", " type " *&" buf ,
54 .BI " " type " &" sz ", " type " " want ,
55 .BI " " type " " init ", " type " " granule ");"
56 .ta \w'\fBvoid GROWBUF_REPLACE('u
57 .BI "\fBvoid GROWBUF_REPLACE(" type ", arena *" a ", " type " *&" buf ,
58 .BI " " type " &" sz ", " type " " want ,
59 .BI " " type " " init ", " type " " granule ");"
61 .ta \w'\fBvoid GROWBUF_RENEWV('u
62 .BI "\fBvoid GROWBUF_RENEWV(" type ", arena *" a ", " type " *&" buf ,
63 .BI " " type " &" sz ", " type " " want ", " type " " init ");"
64 .ta \w'\fBvoid GROWBUF_REPLACEV('u
65 .BI "\fBvoid GROWBUF_REPLACEV(" type ", arena *" a ", " type " *&" buf ,
66 .BI " " type " &" sz ", " type " " want ", " type " " init ");"
69 .\"--------------------------------------------------------------------------
74 header file defines macros useful for dynamically resizing buffers.
76 The situation envisaged is a buffer, at address
82 but it has become necessary to store
84 objects in the buffer.
87 is zero, then the buffer pointer
90 otherwise, it is assumed to have been allocated from the arena
94 is a suggested minimal nonzero size, again counting objects of size
101 so that it is at least as large as
106 .IR sz "\ <\ " want ;
107 nothing especially terrible will happen if this is not the case,
108 but unnecessary work will take place,
111 will be left nonzero if it was previously zero,
115 arranges to increase buffer sizes in a geometric progression
116 so as to minimize reallocation and copying overheads \(en
117 the number of reallocations will be
118 at most logarithmic in the final size
119 and each byte is copied an (amortized) constant number of times.
123 macro will additionally resize the buffer,
124 preserving its contents,
127 to point to its new location;
132 then nothing happens.
135 macro is similar, except that it
137 the existing buffer contents if the buffer needs to be adjusted.
143 except that it implicitly uses
149 macro is similarly like
150 .BR GROWBUF_REPLACE .
152 .\"--------------------------------------------------------------------------
158 .\"--------------------------------------------------------------------------
161 Mark Wooding, <mdw@distorted.org.uk>
163 .\"----- That's all, folks --------------------------------------------------