<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css" style="display:none;"> P {margin-top:0;margin-bottom:0;} </style>
</head>
<body dir="ltr">
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
</div>
<div>
<div id="appendonsend"></div>
<div style="font-family:Calibri,Helvetica,sans-serif; font-size:12pt; color:rgb(0,0,0)">
<br>
</div>
<hr tabindex="-1" style="display:inline-block; width:98%">
<blockquote style="border-left:3px solid rgb(200,200,200); border-top-color:rgb(200,200,200); border-right-color:rgb(200,200,200); border-bottom-color:rgb(200,200,200); padding-left:1ex; margin-left:0.8ex; color:rgb(102,102,102)">
<div id="divRplyFwdMsg" dir="ltr"><font face="Calibri, sans-serif" color="#000000" style="font-size:11pt"><b>From:</b> Timur Kristóf <timur.kristof@gmail.com><br>
<b>Sent:</b> Monday, May 11, 2020 18:06<br>
<b>To:</b> Jose Fonseca <jfonseca@vmware.com>; ML mesa-dev <mesa-dev@lists.freedesktop.org><br>
<b>Cc:</b> erik.faye-lund@collabora.com <erik.faye-lund@collabora.com><br>
<b>Subject:</b> Re: [Mesa-dev] RFC: Memory allocation on Mesa</font>
<div> </div>
</div>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText">On Mon, 2020-05-11 at 16:13 +0000, Jose Fonseca wrote:<br>
> Some might retort: why not just play some tricks with the linker, and<br>
> intercept all malloc/free calls, without actually having to modify<br>
> any source code?<br>
> <br>
> Yes, that's indeed technically feasible.  And is precisely the sort<br>
> of trick I was planing to resort to satisfy VMware needs without<br>
> having to further modify the source code.  But for these tricks to<br>
> work, it is absolutely imperative that one statically links C++<br>
> library and STL.  The problem being that if one doesn't then there's<br>
> an imbalance: the malloc/free/new/delete calls done in inline code on<br>
> C++ headers will be intercepted, where as malloc/free/new/delete<br>
> calls  done in code from the shared object which is not inlined will<br>
> not, causing havoc.<br>
<br>
Wouldn't you face the same issue if you chose to wrap all calls to<br>
malloc and free in mesa, instead of relying on the linker? Any<br>
dynamically linked or 3rd party library, including the C++ standard<br>
library, will have no way of knowing about our wrapped malloc and free.<br>
<br>
Timur<br>
</div>
</span></font></div>
</blockquote>
<div class="BodyFragment"><font size="2"><span style="font-size:11pt">
<div class="PlainText"><br>
</div>
<div class="PlainText">Indeed 3rd part libraries aren't tracked either way.   But I wasn't talking about 3rd party libraries, but rather Mesa itself.</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">Mesa is mostly written in C but some C++ code (ever more in fact.)  My point is that even if we ignore 3rd party libraries, if one takes the linker approach without statically linking, mesa new/delete calls would go unbalanced and the
 consequence would be crashes.</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">With explicit malloc/free, the consequent at most is untracked mallocs/frees, but there should never be any unbalanced mallocs frees, hence no crashes.</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">To be clear, there are two kinds of problems here:</div>
<div class="PlainText">1. allocate memory with one allocator and free with another -- this is catastrophic and will lead to a segfault</div>
<div class="PlainText">2. not intercept every single malloc/free pair -- this is not ideal -- but is inescapable to some extent .  One always need some memory reservation to plain old malloc/free, but the less the better.</div>
<div class="PlainText">
<div><font size="2"><span style="font-size:11pt">
<div class="PlainText"></div>
</span></font></div>
</div>
<div class="PlainText"><br>
</div>
<div class="PlainText">Jose</div>
</span></font></div>
</div>
</body>
</html>