<p>George Joseph <strong>uploaded patch set #2</strong> to this change.</p><p><a href="https://gerrit.asterisk.org/10611">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">vector: Add options for alternate memory management<br><br>The vector implementation has been using the ast_calloc, ast_free,<br>and ast_malloc functions for memory management but there are<br>situations where the vector may be used to process error or<br>backtrace information where an error in the ast_ MM functions<br>could cause a recursive error situation.<br><br>* Added an ast_calloc_ptr function that, regardless of MALLOC_DEBUG,<br> always points to a real function and not a macro.<br><br>* Added an ast_std_strdup function that always calls the libc strdup<br> function regardless of MALLOC_DEBUG. Will be needed for a<br> follow-on commit.<br><br>* Added 3 virtual function pointers for calloc, free and element<br> cleanup to the vector structures. AST_VECTOR_INIT defaults these<br> to ast_calloc_ptr, ast_free_ptr and NULL respectively. This<br> preserves existing behavior.<br><br>* All places in vector.h that had called the ast_ functions<br> directly now use the virtual function pointers instead.<br><br>* A new macro AST_VECTOR_INIT_MM_FN() allows the caller to specify<br> the 3 new functions. The element cleanup function is optional.<br><br>* Two new macros AST_VECTOR_CLEANUP and AST_VECTOR_PTR_CLEANUP will<br> use the 3 virtual functions to...<br> 1. Cleanup all elements in the vector (provided the element<br> cleanup function was specified in AST_VECTOR_INIT_MM_FN)<br> 2. Use the free virtual function to free the internal "elems"<br> array.<br> 3. For AST_VECTOR_PTR_CLEANUP, use the free virtual function to<br> free the externally allocated vector structure itself.<br><br>* cli.c was using a calloc'd vector without having called<br> AST_VECTOR_INIT() which used to be effectively safe but was never<br> technically correct. It now calls AST_VECTOR_INIT.<br><br>Example where a function must return a vector that doesn't use the<br>ast_ memory management functions. Error checking omitted for<br>brevity.<br><br>struct ast_vector_string *get_somevector()<br>{<br> struct ast_vector_string *strings = ast_std_malloc(sizeof(*strings));<br> AST_VECTOR_INIT_MM_FN(strings, 0, ast_std_calloc, ast_std_free,<br> ast_std_free);<br> AST_VECTOR_APPEND(strings, ast_std_strdup(somestring1);<br> AST_VECTOR_APPEND(strings, ast_std_strdup(somestring2);<br> AST_VECTOR_APPEND(strings, ast_std_strdup(somestring3);<br> return strings;<br>}<br><br>In this case, the ast_std_calloc function will be used to allocate<br>the vector's elements array.<br><br>void myfunc()<br>{<br> int i;<br> struct ast_vector_string *strings = get_somevector();<br> for (i = 0; i < AST_VECTOR_SIZE(strings); i++) {<br> AST_LOG(LOG_ERROR, "%s\n", AST_VECTOR_GET(strings, i));<br> }<br> AST_VECTOR_PTR_CLEANUP(strings);<br>}<br><br>Notice that myfunc() doesn't have to worry about how either<br>the vector itself or the elements were allocated. It just<br>calls AST_VECTOR_PTR_CLEANUP() and that macro uses ast_std_free<br>to free the elements, the internal "elems" array, and the vector<br>structure itself.<br><br>Change-Id: I0d871dfed9ce215fd11aa03e7d6cfaf385b55597<br>---<br>M include/asterisk/astmm.h<br>M include/asterisk/utils.h<br>M include/asterisk/vector.h<br>M main/astmm.c<br>M main/cli.c<br>5 files changed, 124 insertions(+), 11 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/11/10611/2</pre><p>To view, visit <a href="https://gerrit.asterisk.org/10611">change 10611</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/10611"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 13 </div>
<div style="display:none"> Gerrit-MessageType: newpatchset </div>
<div style="display:none"> Gerrit-Change-Id: I0d871dfed9ce215fd11aa03e7d6cfaf385b55597 </div>
<div style="display:none"> Gerrit-Change-Number: 10611 </div>
<div style="display:none"> Gerrit-PatchSet: 2 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Jenkins2 (1000185) </div>