string and gc

42 views
Skip to first unread message

Cyrille Duret

unread,
Aug 5, 2013, 4:00:01 AM8/5/13
to ats-lan...@googlegroups.com
hello,
I actually converting my data structures to linear ones to do manual memory management.
My question is how memory is handled for string variables ?
Is it similar to c++ (string memory is copied when passed by value as a function argument)

Should I use strptr1 instead for programs not using GC ?

thanks a lot ;-)

gmhwxi

unread,
Aug 5, 2013, 8:24:25 AM8/5/13
to ats-lan...@googlegroups.com
This is a very interesting topic. It depends what you are doing and what your goal is.

For something relatively small, using 'strptr1' for strings is fine.

For something more complicated that requires sharing of strings, one may want to use
ref-counted strings.

In ATS2, I added a type strobjref:

https://github.com/githwxi/ATS-Postiats/blob/master/libats/SATS/strobjref.sats

which is for reference-counted strings.

At the beginning of a project, I often do not know for sure what kind of strings I want.
So I often introduce an abstract type

absviewtype mystring (l:addr)

First I implement mystring based on strptr. Later, I may change the implementation,
making it based on strobjref or something else.

In practice, if string sharing is allowed. then memory leaks are often very difficult to prevent.
For instance, there are many leaks in graphviz (according to bug reports). ATS is very good
for stopping this kind of problems.

Cyrille Duret

unread,
Aug 5, 2013, 9:41:36 AM8/5/13
to ats-lan...@googlegroups.com
thanks ;-)

For sure having linear type to follow memory usage is very convenient but can be very tricky for highly shared resources

gmhwxi

unread,
Aug 5, 2013, 9:57:30 AM8/5/13
to ats-lan...@googlegroups.com
Usually, highly shared resources are handled by some form of reference counting.

Over the past few years, the most complicated
resource handling I encountered is the handling  kobjects in linux kernel. Often I see that complications in resource handling are primarily
added due to lack of linear types. For instance, in GTK, something called 'float-reference' was invented, which I had to get rid of when
doing an API for GTK in ATS.
Reply all
Reply to author
Forward
0 new messages