Boost logo

Boost :

From: Andrey Tarantsov (andreyvit_at_[hidden])
Date: 2005-07-24 21:15:50


Hello!

I've got a relatively simple queue-like container which can be
efficiently used as a buffer. It allocates memory in chunks, and
reuses these chunks without deallocating them. (Unused chunks are
deallocated based on a simple heuristic.) It's generic (with element
type and allocator as parameters), but is expected to be often
instantiated with 'unsigned char' (byte) element type.

The interface and implementation are not currently iterator-friendly,
because I did not need iterator behaviour. As the buffer is expected
to be used for storing multiple elements at a time from a continuous
storage (C array), it does reads and writes more efficiently than
by-element copying iterators can provide. But there is no problem
adding iterators, if there will be any interest in this container.

Here is the interface (which is short and hopefully more undestandable
than my description):

            /* (constructor)
             * Specifies buffer memory management details. Storage is
allocated in chunks
             * of [chunk_size] elements; this defaults to the count
which makes each chunk
             * about 10Kb in size (a wild guess). Adjust
[reuse_amount] to specify how
             * fast should the buffer deallocate unused chunks, in
1/128's of full buffer
             * fill-and-read cycle. A value of 128 (the default)
corresponds to the time
             * all old elements in the buffer are read. A value of
256 means that
             * unneeded chunks are deallocated after filling and
emptying the buffer
             * twice.
             */
            buffer (size_t chunk_size = default_chunk_size, unsigned
reuse_amount = default_reuse_amount);

            /*
             * Add the given elements to the end of buffer.
             */
            void write (const El *data, size_t count);
            /*
             * Read the given number of elements data from the start
of buffer, and remove
             * the read elements. If less then [count] elements are
available, will
             * read (and remove) all the available elements. Returns
the number of
             * elements read (and removed).
             */
            size_t read (El *data, size_t count);
            /*
             * Read the given number of elements starting with the
given position,
             * WITHOUT removing them from the buffer. Position starts
at zero, so
             * specifying zero [pos] results in the behaviour similar
to read(), but
             * without removing the elements.
             */
            size_t read_at (size_t pos, El *data, size_t count);

            size_t size () const;
            bool empty () const;

-- 
Andrey.

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk