Connecting Tech Pros Worldwide Help | Site Map

Expanding buffer in C

  #1  
Old November 16th, 2008, 01:15 AM
James Harris
Guest
 
Posts: n/a
Do you remember a previous discussion on this newsgroup as at

http://groups.google.com/group/comp....738c4f91113b4e

It concerned code to manage a buffer, expanding it as needed semi
automatically. I've incorporated suggestions people made and published
the code at

http://codewiki.wikispaces.com/xbuf.c

(For the impatient check the last two links in the table of contents
for the code. The rest is documentation.)

The code is intended to be fast. It's uses include reading an
arbitrary-length line from an input stream - a safe gets() replacement
if you like - but it is more flexible than line reading functions and
is intended to be useful in more cases than just line reading.

--NB. Code and documentation are on a wiki. If you see things that
should be changed feel free to change them.

Apart from the code itself what do you think of the concept of
publishing it on a wiki...?

James
  #2  
Old November 17th, 2008, 02:25 AM
Gene
Guest
 
Posts: n/a

re: Expanding buffer in C


On Nov 15, 8:12*pm, James Harris <james.harri...@googlemail.com>
wrote:
Quote:
Do you remember a previous discussion on this newsgroup as at
>
*http://groups.google.com/group/comp....hread/cb502ce8....
>
It concerned code to manage a buffer, expanding it as needed semi
automatically. I've incorporated suggestions people made and published
the code at
>
*http://codewiki.wikispaces.com/xbuf.c
>
(For the impatient check the last two links in the table of contents
for the code. The rest is documentation.)
>
The code is intended to be fast. It's uses include reading an
arbitrary-length line from an input stream - a safe gets() replacement
if you like - but it is more flexible than line reading functions and
is intended to be useful in more cases than just line reading.
>
--NB. Code and documentation are on a wiki. If you see things that
should be changed feel free to change them.
>
Apart from the code itself what do you think of the concept of
publishing it on a wiki...?
>
James
Nice presentation. You'll probably get less heap fragmentation for
programs that use lots of buffers if all of them are at set standard
sizes. So your new_size computation would become something like

new_size = current_size;
while (new_size < requested_size)
new_size = new_size * FACTOR;

NB. Your choice of default 3 / 2 for FACTOR is interesting. I've
been using this for many years because the standard 2 seemed
extravagant.
  #3  
Old November 21st, 2008, 12:15 AM
James Harris
Guest
 
Posts: n/a

re: Expanding buffer in C


On 18 Nov, 17:53, Gene <gene.ress...@gmail.comwrote:


....
Quote:
Quote:
Is there a better way to increase the buffer size? Especially as the
address space fills up should the increase factor be reduced, and how?
>
Quote:
Anyone been down this road?
>
Garbage collectors deal with this when they need to increase the pool
size when not much VM is left. There isn't a single best policy
because behavior depends heavily on the OS.
Agreed. The more I think about this the less I think there is any kind
of one-size-fits-all solution. One advantage of distributing as source
code is that people can vary the code to suit their situation. I've
kept the original code as it was but have documented some alternatives
as comments within the code.

http://codewiki.wikispaces.com/xbuf.c

The commented alternatives are purely illustrative and are untested. I
hope I've got the C syntax etc correct. Either way they should be
enough to illustrate the options to anyone using the code. If you,
dear reader, see an error feel free to fix it or let me know what I've
got wrong and I'll fix it.

James
  #4  
Old November 22nd, 2008, 02:15 AM
CBFalconer
Guest
 
Posts: n/a

re: Expanding buffer in C


James Harris wrote:
Quote:
Gene <gene.ress...@gmail.comwrote:
>
.... snip ...
Quote:
>
Quote:
>Garbage collectors deal with this when they need to increase the
>pool size when not much VM is left. There isn't a single best
>policy because behavior depends heavily on the OS.
>
Agreed. The more I think about this the less I think there is any
kind of one-size-fits-all solution. One advantage of distributing
as source code is that people can vary the code to suit their
situation. I've kept the original code as it was but have
documented some alternatives as comments within the code.
In general you should adjust the mechanism to fit the expected
use. As an example, my ggets routine expands the buffer in
constant increments (of 128), because its primary use is expected
to be interactive input, and those strings are not normally
unlimited. However my hashlib expands its table by a factor of two
each time, resulting in constant average overhead, and basically
eliminating consideration of table size. In each case there are
possible penalties.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
Closed Thread