Closed andychu closed 1 year ago
This is exposed by mycpp/examples/parse.py
This one is good because it has a test with a simple verification
data_
pointer that's not GC managed on a GC managed object can be problematic
data_
is just a heap-allocated Str
- Sweep -> free()
- AllocStr(2 * old_size_)
- memcpy()
@CoffeeTableEspresso
got rid of some leaks by explicitly calling reset()
, also needed to change mycpp/mylib.pyi
Is this right architecturally?
@CoffeeTableEspresso I think I figured out a principled solution in the shower (of course)
It's not valid under Cheney though (because you can't really realloc with a bump allocator, although I guess you can simulate it ?)
class BufWriter
should hold a new type class Buf
, which is like Str
but mutable
data_
or buf_
pointer should have a field mask ! Forgot this partBuf
is a full fledged GC object
calloc()
to ensure it's zero'd. Also to fix the quadratic problem it should be
realloc(10), realloc(20), realloc(40) realloc(80) -- doubling
not
realloc(10), realloc(20), realloc(30) realloc(40) realloc(50) ...
Basic knowledge that I've never really needed to use :)
Or actually an easier option is to make BufWriter
itself like Str
-- it uses the "flexible array" pattern like char buf_[1]
? And then placement new?
Although this could complicate generated code ... It couldn't use Alloc<BufWriter>
, it would have to use a custom NewBufWriter
Also we zero'd for Cheney and string NUL termination of C strings
But this is slightly harder with realloc()
, so maybe we can get away with just doing NUL termination and not zeroing the whole buffer
https://stackoverflow.com/questions/2141277/how-to-zero-out-new-memory-after-realloc
@CoffeeTableEspresso fixed this!
Because the internal
data_
pointer is malloc'd and we never call BufWriter::reset() which calls free()