# Types of malloc in ATS

**URL:** https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389
**Category:** Google Group
**Created:** [November 18, 2014, 5:10pm UTC](https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389 "2014-11-18T17:10:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)
#### Post date: [November 18, 2014, 5:10pm UTC](https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389/1 "2014-11-18T17:10:30Z")

</div>

Memory allocated by malloc\_linear can be freed or leaked  
(that, turning into something non-linear).

So even if there is no malloc\_nonlinear in a program, it does  
not imply that the program cannot cause leaks.

For instance, calling functions like list\_of\_list\_vt (for turning  
a linear list into a non-linear one) leaks memory.

The truth is that there is really no malloc\_nonlinear.  
For instance, we studied this issue in the following paper:

[http://www.cs.bu.edu/~hwxi/academic/papers/ictac10.pdfOn](http://www.cs.bu.edu/~hwxi/academic/papers/ictac10.pdfOn) Sunday, November 16, 2014 10:48:22 PM UTC-5, Barry Schwartz wrote:

> I have been thinking about the role of what I will call ‘malloc’ in  
> ATS.
> 
> While having ‘malloc\_nonlinear’ and ‘malloc\_linear’ would be indeed  
> quite ad hoc, I think what amounts to the same thing by different  
> names is less so. Certainly to me (as a Schemer, albeit not an  
> addicted one) it makes at least as much sense as having to have  
> special notations for tail calls (‘and’, ‘fnx’).
> 
> My point is that calling an allocator with the intention of having  
> that call ‘bracketed’ by a free is different from calling an allocator  
> without the intention of having that call ‘bracketed’ by a free. There  
> is a philosophical difference. So _perhaps_ it deserves a hint or at  
> least a ‘philosophical distiction’ by having slightly different names  
> (at the #define level) for the two types of call; not  
> nonlinear-vs-linear, which is an ad hoc distinction, but ‘free is  
> expected’ versus ‘free is not expected’.
> 
> OTOH I’m still not convinced that ‘boolean’ should be a separate type  
> (in general) from modulo-2 arithmetic, so I don’t know. 🙂

---

<div class="post-metadata">

### Author: ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)
#### Post date: [November 18, 2014, 9:40pm UTC](https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389/2 "2014-11-18T21:40:03Z")

</div>

For what it is worth, the compiler now adds the following  
comment before the code that creates a nonlinear contructor:

/\*  
lincon==0  
\*/

For a linear constructor, the comment reads:

/\*  
lincon==1  
\*/On Tuesday, November 18, 2014 3:45:12 PM UTC-5, Barry Schwartz wrote:

> gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> skribis:
> 
> > Memory allocated by malloc\_linear can be freed or leaked  
> > (that, turning into something non-linear).
> > 
> > So even if there is no malloc\_nonlinear in a program, it does  
> > not imply that the program cannot cause leaks.
> 
> That is why I wrote in terms of general intentions. Similar to using  
> macro wrappers that do nothing but which document some mental  
> distinction, and perhaps could be used at some future date to give  
> hints to the machine. Or like the GCC attribute for ‘maybe unused’  
> that doesn’t mean the variable _is_ unused, but turns off warnings if  
> it happens to be. Or like in the Guile API, which has  
> scm\_gc\_protect\_object, scm\_gc\_unprotect\_object, and  
> scm\_permanent\_object, to control garbage collection; the last is  
> implemented as a wrapper around the first, and so actually the  
> ‘permanent’ object _could be_ ‘unprotected’; however, the  
> implementation _could_ be different, and in any case the call has a  
> vaguely different meaning to humans.
> 
> But, as I say, I don’t know. Drawing distinctions is fraught with  
> peril, just as glossing them over is.

---

<div class="post-metadata">

### Author: ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)
#### Post date: [November 18, 2014, 11:43pm UTC](https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389/3 "2014-11-18T23:43:35Z")

</div>

For the record, the comment now reads:

/\*  
#LINCONSTATUS==i  
\*/

where

i=0 implies linear constructor  
i=1 implies nonlinear constructor  
i=2 implies nonlinear record

So if you find #LINCONSTATUS==i for a positive integer i, then it is  
a strong indication of potential leak. Note that leaks are still possible  
even if you only see #LINCONSTATUS==0.On Tuesday, November 18, 2014 5:14:38 PM UTC-5, Barry Schwartz wrote:

> gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> skribis:
> 
> > For what it is worth, the compiler now adds the following  
> > comment before the code that creates a nonlinear contructor:
> > 
> > /\*  
> > lincon==0  
> > \*/
> > 
> > For a linear constructor, the comment reads:
> > 
> > /\*  
> > lincon==1  
> > \*/
> 
> Definitely safely harmless. 🙂 And easily grepped.

---

<div class="post-metadata">

### Author: ![Raoul\_Duke](https://avatars.discourse-cdn.com/v4/letter/r/bbe5ce/32.png) [@Raoul\_Duke](https://discourse.ats-lang.org/u/Raoul_Duke)
#### Post date: [November 18, 2014, 8:52pm UTC](https://discourse.ats-lang.org/t/types-of-malloc-in-ats/389/4 "2014-11-18T20:52:43Z")

</div>

choice quote.
