[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

auto_mem_buffer and a memory leak fix.

I finally got my auto_mem_buffer working.  I works very nicely now.
I still have to write documentation for it though along with a regression
test.  I've attached a patch against a clean xtl-1.3.pl.4 /include/xtl
directory only since it's only code code anyway.

A couple of explanations are needed though.  I basically took the code
for mem_buffer and modified it to manage the buffer memory.  That was the 
easiest way for me to be sure I kept the same interface as was expected
by everything else.  I had to split the require() method into separate 
methods for input and output since output is allowed to expand the size of
the buffer to satisfy the desired buffer space (hence the name of the
method desire()) while the require() function is now only used for input
(since we can't expand the buffer on input and the memory is required).

I've had great success with my own test program and with LyX using this
new buffer.  It's performance is not optimized although a couple of simple
changes can easily remove some code from the longest path in desire() --
at present the default constructor doesn't allocate memory at all.  When
the first request to output is made the buffer is created with 64bytes of
space. At least one line can go from this code if we allocate in the
default constructor or provide a auto_mem_buffer(size_t) constructor.
See the comments in the file for more ideas.

The memory leak is a one line fix.  Add "delete[] tmp;" in
input_simple(std::string).  I also experimented with ways of avoiding the
double copy but all resulted in a segfault due to std::string taking
ownership of the memory in the buffer rather than copying that memory. 

Josť if you have already started incorporating Angus's work I'll wait for
another official patch from you before resubmitting. You seem to be
swamped with patches at present.

Just so you know, I've incorporated this patch into the xtl dist in my
branch of the LyX codebase as I am already using auto_mem_buffer there.

Allan. (ARRae)

P.S.  Tested with egcs-1.1.2 and gcc-2.95.2 with both SGI-STL-3.2 and the
stl distributed with the respective compilers on i686 Linux 2.2.15pre17.

auto_mem_buffer and a memory-leak fix