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"
36 .\"--------------------------------------------------------------------------
38 growbuf \- extend buffers efficiently
40 .\"--------------------------------------------------------------------------
44 .B "#include <mLib/growbuf.h>"
46 .BI "GROWBUF_SIZE(size_t " sz ", size_t " want ", " \c
47 .BI "size_t " init ", size_t " granule ");"
49 .ds mT \fBGROWBUF_EXTEND(
50 .BI "\*(mTarena *" a ", " type " *" buf ", size_t " sz ", size_t " want ","
51 .BI "\h'\w'\*(mT'u'size_t " init ", size_t " granule ");"
52 .ds mT \fBGROWBUF_REPLACE(
53 .BI "\*(mTarena *" a ", " type " *" buf ", size_t " sz ", size_t " want ","
54 .BI "\h'\w'\*(mT'u'size_t " init ", size_t " granule ");"
57 .\"--------------------------------------------------------------------------
62 header file defines macros useful for dynamically resizing buffers.
64 The situation envisaged is a buffer, at address
70 but it has become necessary to store
72 objects in the buffer.
75 is zero, then the buffer pointer
78 otherwise, it is assumed to have been allocated from the arena
82 is a suggested minimal nonzero size, again counting objects of size
89 so that it is at least as large as
95 nothing especially terrible will happen if this is not the case,
96 but unnecessary work will take place,
99 will be left nonzero if it was previously zero,
103 arranges to increase buffer sizes in a geometric progression
104 so as to minimize reallocation and copying overheads \(en
105 the number of reallocations will be
106 at most logarithmic in the final size
107 and each byte is copied an (amortized) constant number of times.
111 macro will additionally resize the buffer,
112 preserving its contents,
115 to point to its new location;
120 then nothing happens.
123 macro is similar, except that it
125 the existing buffer contents if the buffer needs to be adjusted.
127 .\"--------------------------------------------------------------------------
133 .\"--------------------------------------------------------------------------
136 Mark Wooding, <mdw@distorted.org.uk>
138 .\"----- That's all, folks --------------------------------------------------