Current File : //usr/share/doc/dbus/api/dbus-memory_8c_source.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>D-Bus: dbus-memory.c Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">D-Bus
   &#160;<span id="projectnumber">1.8.20</span>
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">dbus-memory.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* -*- mode: C; c-file-style: &quot;gnu&quot;; indent-tabs-mode: nil; -*- */</span>
<a name="l00002"></a>00002 <span class="comment">/* dbus-memory.c  D-Bus memory handling</span>
<a name="l00003"></a>00003 <span class="comment"> *</span>
<a name="l00004"></a>00004 <span class="comment"> * Copyright (C) 2002, 2003  Red Hat Inc.</span>
<a name="l00005"></a>00005 <span class="comment"> *</span>
<a name="l00006"></a>00006 <span class="comment"> * Licensed under the Academic Free License version 2.1</span>
<a name="l00007"></a>00007 <span class="comment"> * </span>
<a name="l00008"></a>00008 <span class="comment"> * This program is free software; you can redistribute it and/or modify</span>
<a name="l00009"></a>00009 <span class="comment"> * it under the terms of the GNU General Public License as published by</span>
<a name="l00010"></a>00010 <span class="comment"> * the Free Software Foundation; either version 2 of the License, or</span>
<a name="l00011"></a>00011 <span class="comment"> * (at your option) any later version.</span>
<a name="l00012"></a>00012 <span class="comment"> *</span>
<a name="l00013"></a>00013 <span class="comment"> * This program is distributed in the hope that it will be useful,</span>
<a name="l00014"></a>00014 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<a name="l00015"></a>00015 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<a name="l00016"></a>00016 <span class="comment"> * GNU General Public License for more details.</span>
<a name="l00017"></a>00017 <span class="comment"> * </span>
<a name="l00018"></a>00018 <span class="comment"> * You should have received a copy of the GNU General Public License</span>
<a name="l00019"></a>00019 <span class="comment"> * along with this program; if not, write to the Free Software</span>
<a name="l00020"></a>00020 <span class="comment"> * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>
<a name="l00021"></a>00021 <span class="comment"> *</span>
<a name="l00022"></a>00022 <span class="comment"> */</span>
<a name="l00023"></a>00023 
<a name="l00024"></a>00024 <span class="preprocessor">#include &lt;config.h&gt;</span>
<a name="l00025"></a>00025 <span class="preprocessor">#include &quot;dbus-memory.h&quot;</span>
<a name="l00026"></a>00026 <span class="preprocessor">#include &quot;dbus-internals.h&quot;</span>
<a name="l00027"></a>00027 <span class="preprocessor">#include &quot;dbus-sysdeps.h&quot;</span>
<a name="l00028"></a>00028 <span class="preprocessor">#include &quot;dbus-list.h&quot;</span>
<a name="l00029"></a>00029 <span class="preprocessor">#include &quot;dbus-threads.h&quot;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &lt;stdlib.h&gt;</span>
<a name="l00031"></a>00031  <span class="comment">/* end of public API docs */</span>
<a name="l00093"></a>00093 
<a name="l00100"></a>00100 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00101"></a>00101 <span class="preprocessor"></span><span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> debug_initialized = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00102"></a>00102 <span class="keyword">static</span> <span class="keywordtype">int</span> fail_nth = -1;
<a name="l00103"></a>00103 <span class="keyword">static</span> <span class="keywordtype">size_t</span> fail_size = 0;
<a name="l00104"></a>00104 <span class="keyword">static</span> <span class="keywordtype">int</span> fail_alloc_counter = _DBUS_INT_MAX;
<a name="l00105"></a>00105 <span class="keyword">static</span> <span class="keywordtype">int</span> n_failures_per_failure = 1;
<a name="l00106"></a>00106 <span class="keyword">static</span> <span class="keywordtype">int</span> n_failures_this_failure = 0;
<a name="l00107"></a>00107 <span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> guards = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00108"></a>00108 <span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> disable_mem_pools = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00109"></a>00109 <span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> backtrace_on_fail_alloc = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00110"></a>00110 <span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> malloc_cannot_fail = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00111"></a>00111 <span class="keyword">static</span> <a class="code" href="structDBusAtomic.html" title="An atomic integer safe to increment or decrement from multiple threads.">DBusAtomic</a> n_blocks_outstanding = {0};
<a name="l00112"></a>00112 
<a name="l00114"></a>00114 <span class="preprocessor">#define GUARD_VALUE 0xdeadbeef</span>
<a name="l00115"></a>00115 <span class="preprocessor"></span>
<a name="l00116"></a>00116 <span class="preprocessor">#define GUARD_INFO_SIZE 8</span>
<a name="l00117"></a>00117 <span class="preprocessor"></span>
<a name="l00118"></a>00118 <span class="preprocessor">#define GUARD_START_PAD 16</span>
<a name="l00119"></a>00119 <span class="preprocessor"></span>
<a name="l00120"></a>00120 <span class="preprocessor">#define GUARD_END_PAD 16</span>
<a name="l00121"></a>00121 <span class="preprocessor"></span>
<a name="l00122"></a>00122 <span class="preprocessor">#define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)</span>
<a name="l00123"></a>00123 <span class="preprocessor"></span>
<a name="l00124"></a>00124 <span class="preprocessor">#define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)</span>
<a name="l00125"></a>00125 <span class="preprocessor"></span>
<a name="l00126"></a>00126 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00127"></a>00127 _dbus_initialize_malloc_debug (<span class="keywordtype">void</span>)
<a name="l00128"></a>00128 {
<a name="l00129"></a>00129   <span class="keywordflow">if</span> (!debug_initialized)
<a name="l00130"></a>00130     {
<a name="l00131"></a>00131       debug_initialized = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00132"></a>00132       
<a name="l00133"></a>00133       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_FAIL_NTH&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00134"></a>00134         {
<a name="l00135"></a>00135           fail_nth = atoi (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_FAIL_NTH&quot;</span>));
<a name="l00136"></a>00136           fail_alloc_counter = fail_nth;
<a name="l00137"></a>00137           _dbus_verbose (<span class="stringliteral">&quot;Will fail dbus_malloc every %d times\n&quot;</span>, fail_nth);
<a name="l00138"></a>00138         }
<a name="l00139"></a>00139       
<a name="l00140"></a>00140       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_FAIL_GREATER_THAN&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00141"></a>00141         {
<a name="l00142"></a>00142           fail_size = atoi (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_FAIL_GREATER_THAN&quot;</span>));
<a name="l00143"></a>00143           _dbus_verbose (<span class="stringliteral">&quot;Will fail mallocs over %ld bytes\n&quot;</span>,
<a name="l00144"></a>00144                          (<span class="keywordtype">long</span>) fail_size);
<a name="l00145"></a>00145         }
<a name="l00146"></a>00146 
<a name="l00147"></a>00147       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_GUARDS&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00148"></a>00148         {
<a name="l00149"></a>00149           guards = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00150"></a>00150           _dbus_verbose (<span class="stringliteral">&quot;Will use dbus_malloc guards\n&quot;</span>);
<a name="l00151"></a>00151         }
<a name="l00152"></a>00152 
<a name="l00153"></a>00153       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_DISABLE_MEM_POOLS&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00154"></a>00154         {
<a name="l00155"></a>00155           disable_mem_pools = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00156"></a>00156           _dbus_verbose (<span class="stringliteral">&quot;Will disable memory pools\n&quot;</span>);
<a name="l00157"></a>00157         }
<a name="l00158"></a>00158 
<a name="l00159"></a>00159       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_BACKTRACES&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00160"></a>00160         {
<a name="l00161"></a>00161           backtrace_on_fail_alloc = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00162"></a>00162           _dbus_verbose (<span class="stringliteral">&quot;Will backtrace on failing a dbus_malloc\n&quot;</span>);
<a name="l00163"></a>00163         }
<a name="l00164"></a>00164 
<a name="l00165"></a>00165       <span class="keywordflow">if</span> (<a class="code" href="group__DBusSysdeps.html#gab846071a4de54cb7e518654f9375313a" title="Wrapper for getenv().">_dbus_getenv</a> (<span class="stringliteral">&quot;DBUS_MALLOC_CANNOT_FAIL&quot;</span>) != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00166"></a>00166         {
<a name="l00167"></a>00167           malloc_cannot_fail = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00168"></a>00168           _dbus_verbose (<span class="stringliteral">&quot;Will abort if system malloc() and friends fail\n&quot;</span>);
<a name="l00169"></a>00169         }
<a name="l00170"></a>00170     }
<a name="l00171"></a>00171 }
<a name="l00172"></a>00172 
<a name="l00178"></a>00178 <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a>
<a name="l00179"></a>00179 _dbus_disable_mem_pools (<span class="keywordtype">void</span>)
<a name="l00180"></a>00180 {
<a name="l00181"></a>00181   _dbus_initialize_malloc_debug ();
<a name="l00182"></a>00182   <span class="keywordflow">return</span> disable_mem_pools;
<a name="l00183"></a>00183 }
<a name="l00184"></a>00184 
<a name="l00193"></a>00193 <span class="keywordtype">void</span>
<a name="l00194"></a>00194 _dbus_set_fail_alloc_counter (<span class="keywordtype">int</span> until_next_fail)
<a name="l00195"></a>00195 {
<a name="l00196"></a>00196   _dbus_initialize_malloc_debug ();
<a name="l00197"></a>00197 
<a name="l00198"></a>00198   fail_alloc_counter = until_next_fail;
<a name="l00199"></a>00199 
<a name="l00200"></a>00200 <span class="preprocessor">#if 0</span>
<a name="l00201"></a>00201 <span class="preprocessor"></span>  _dbus_verbose (<span class="stringliteral">&quot;Set fail alloc counter = %d\n&quot;</span>, fail_alloc_counter);
<a name="l00202"></a>00202 <span class="preprocessor">#endif</span>
<a name="l00203"></a>00203 <span class="preprocessor"></span>}
<a name="l00204"></a>00204 
<a name="l00211"></a>00211 <span class="keywordtype">int</span>
<a name="l00212"></a>00212 _dbus_get_fail_alloc_counter (<span class="keywordtype">void</span>)
<a name="l00213"></a>00213 {
<a name="l00214"></a>00214   _dbus_initialize_malloc_debug ();
<a name="l00215"></a>00215 
<a name="l00216"></a>00216   <span class="keywordflow">return</span> fail_alloc_counter;
<a name="l00217"></a>00217 }
<a name="l00218"></a>00218 
<a name="l00225"></a>00225 <span class="keywordtype">void</span>
<a name="l00226"></a>00226 _dbus_set_fail_alloc_failures (<span class="keywordtype">int</span> failures_per_failure)
<a name="l00227"></a>00227 {
<a name="l00228"></a>00228   n_failures_per_failure = failures_per_failure;
<a name="l00229"></a>00229 }
<a name="l00230"></a>00230 
<a name="l00237"></a>00237 <span class="keywordtype">int</span>
<a name="l00238"></a>00238 _dbus_get_fail_alloc_failures (<span class="keywordtype">void</span>)
<a name="l00239"></a>00239 {
<a name="l00240"></a>00240   <span class="keywordflow">return</span> n_failures_per_failure;
<a name="l00241"></a>00241 }
<a name="l00242"></a>00242 
<a name="l00243"></a>00243 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00244"></a>00244 <span class="preprocessor"></span>
<a name="l00252"></a>00252 <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a>
<a name="l00253"></a>00253 _dbus_decrement_fail_alloc_counter (<span class="keywordtype">void</span>)
<a name="l00254"></a>00254 {
<a name="l00255"></a>00255   _dbus_initialize_malloc_debug ();
<a name="l00256"></a>00256 <span class="preprocessor">#ifdef DBUS_WIN_FIXME</span>
<a name="l00257"></a>00257 <span class="preprocessor"></span>  {
<a name="l00258"></a>00258     <span class="keyword">static</span> <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> called = 0;
<a name="l00259"></a>00259 
<a name="l00260"></a>00260     <span class="keywordflow">if</span> (!called)
<a name="l00261"></a>00261       {
<a name="l00262"></a>00262         _dbus_verbose(<span class="stringliteral">&quot;TODO: memory allocation testing errors disabled for now\n&quot;</span>);
<a name="l00263"></a>00263         called = 1;
<a name="l00264"></a>00264       }
<a name="l00265"></a>00265     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00266"></a>00266   }
<a name="l00267"></a>00267 <span class="preprocessor">#endif</span>
<a name="l00268"></a>00268 <span class="preprocessor"></span>
<a name="l00269"></a>00269   <span class="keywordflow">if</span> (fail_alloc_counter &lt;= 0)
<a name="l00270"></a>00270     {
<a name="l00271"></a>00271       <span class="keywordflow">if</span> (backtrace_on_fail_alloc)
<a name="l00272"></a>00272         <a class="code" href="group__DBusSysdeps.html#ga8646ab2ef727fcc862c7904113c88a8d" title="On GNU libc systems, print a crude backtrace to stderr.">_dbus_print_backtrace</a> ();
<a name="l00273"></a>00273 
<a name="l00274"></a>00274       _dbus_verbose (<span class="stringliteral">&quot;failure %d\n&quot;</span>, n_failures_this_failure);
<a name="l00275"></a>00275       
<a name="l00276"></a>00276       n_failures_this_failure += 1;
<a name="l00277"></a>00277       <span class="keywordflow">if</span> (n_failures_this_failure &gt;= n_failures_per_failure)
<a name="l00278"></a>00278         {
<a name="l00279"></a>00279           <span class="keywordflow">if</span> (fail_nth &gt;= 0)
<a name="l00280"></a>00280             fail_alloc_counter = fail_nth;
<a name="l00281"></a>00281           <span class="keywordflow">else</span>
<a name="l00282"></a>00282             fail_alloc_counter = _DBUS_INT_MAX;
<a name="l00283"></a>00283 
<a name="l00284"></a>00284           n_failures_this_failure = 0;
<a name="l00285"></a>00285 
<a name="l00286"></a>00286           _dbus_verbose (<span class="stringliteral">&quot;reset fail alloc counter to %d\n&quot;</span>, fail_alloc_counter);
<a name="l00287"></a>00287         }
<a name="l00288"></a>00288       
<a name="l00289"></a>00289       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00290"></a>00290     }
<a name="l00291"></a>00291   <span class="keywordflow">else</span>
<a name="l00292"></a>00292     {
<a name="l00293"></a>00293       fail_alloc_counter -= 1;
<a name="l00294"></a>00294       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00295"></a>00295     }
<a name="l00296"></a>00296 }
<a name="l00297"></a>00297 <span class="preprocessor">#endif </span><span class="comment">/* DBUS_ENABLE_EMBEDDED_TESTS */</span>
<a name="l00298"></a>00298 
<a name="l00304"></a>00304 <span class="keywordtype">int</span>
<a name="l00305"></a>00305 _dbus_get_malloc_blocks_outstanding (<span class="keywordtype">void</span>)
<a name="l00306"></a>00306 {
<a name="l00307"></a>00307   <span class="keywordflow">return</span> <a class="code" href="group__DBusSysdeps.html#ga626c810359ba0e362367ce2ec6691aca" title="Atomically get the value of an integer.">_dbus_atomic_get</a> (&amp;n_blocks_outstanding);
<a name="l00308"></a>00308 }
<a name="l00309"></a>00309 
<a name="l00313"></a>00313 <span class="keyword">typedef</span> <span class="keyword">enum</span>
<a name="l00314"></a>00314 {
<a name="l00315"></a>00315   SOURCE_UNKNOWN,
<a name="l00316"></a>00316   SOURCE_MALLOC,
<a name="l00317"></a>00317   SOURCE_REALLOC,
<a name="l00318"></a>00318   SOURCE_MALLOC_ZERO,
<a name="l00319"></a>00319   SOURCE_REALLOC_NULL
<a name="l00320"></a>00320 } BlockSource;
<a name="l00321"></a>00321 
<a name="l00322"></a>00322 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>*
<a name="l00323"></a>00323 source_string (BlockSource source)
<a name="l00324"></a>00324 {
<a name="l00325"></a>00325   <span class="keywordflow">switch</span> (source)
<a name="l00326"></a>00326     {
<a name="l00327"></a>00327     <span class="keywordflow">case</span> SOURCE_UNKNOWN:
<a name="l00328"></a>00328       <span class="keywordflow">return</span> <span class="stringliteral">&quot;unknown&quot;</span>;
<a name="l00329"></a>00329     <span class="keywordflow">case</span> SOURCE_MALLOC:
<a name="l00330"></a>00330       <span class="keywordflow">return</span> <span class="stringliteral">&quot;malloc&quot;</span>;
<a name="l00331"></a>00331     <span class="keywordflow">case</span> SOURCE_REALLOC:
<a name="l00332"></a>00332       <span class="keywordflow">return</span> <span class="stringliteral">&quot;realloc&quot;</span>;
<a name="l00333"></a>00333     <span class="keywordflow">case</span> SOURCE_MALLOC_ZERO:
<a name="l00334"></a>00334       <span class="keywordflow">return</span> <span class="stringliteral">&quot;malloc0&quot;</span>;
<a name="l00335"></a>00335     <span class="keywordflow">case</span> SOURCE_REALLOC_NULL:
<a name="l00336"></a>00336       <span class="keywordflow">return</span> <span class="stringliteral">&quot;realloc(NULL)&quot;</span>;
<a name="l00337"></a>00337     }
<a name="l00338"></a>00338   _dbus_assert_not_reached (<span class="stringliteral">&quot;Invalid malloc block source ID&quot;</span>);
<a name="l00339"></a>00339   <span class="keywordflow">return</span> <span class="stringliteral">&quot;invalid!&quot;</span>;
<a name="l00340"></a>00340 }
<a name="l00341"></a>00341 
<a name="l00342"></a>00342 <span class="keyword">static</span> <span class="keywordtype">void</span>
<a name="l00343"></a>00343 check_guards (<span class="keywordtype">void</span>       *free_block,
<a name="l00344"></a>00344               <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> overwrite)
<a name="l00345"></a>00345 {
<a name="l00346"></a>00346   <span class="keywordflow">if</span> (free_block != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00347"></a>00347     {
<a name="l00348"></a>00348       <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *block = ((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)free_block) - GUARD_START_OFFSET;
<a name="l00349"></a>00349       <span class="keywordtype">size_t</span> requested_bytes = *(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*)block;
<a name="l00350"></a>00350       BlockSource source = *(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*)(block + 4);
<a name="l00351"></a>00351       <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;
<a name="l00352"></a>00352       <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> failed;
<a name="l00353"></a>00353 
<a name="l00354"></a>00354       failed = <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00355"></a>00355 
<a name="l00356"></a>00356 <span class="preprocessor">#if 0</span>
<a name="l00357"></a>00357 <span class="preprocessor"></span>      _dbus_verbose (<span class="stringliteral">&quot;Checking %d bytes request from source %s\n&quot;</span>,
<a name="l00358"></a>00358                      requested_bytes, source_string (source));
<a name="l00359"></a>00359 <span class="preprocessor">#endif</span>
<a name="l00360"></a>00360 <span class="preprocessor"></span>      
<a name="l00361"></a>00361       i = GUARD_INFO_SIZE;
<a name="l00362"></a>00362       <span class="keywordflow">while</span> (i &lt; GUARD_START_OFFSET)
<a name="l00363"></a>00363         {
<a name="l00364"></a>00364           <a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a> value = *(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*) &amp;block[i];
<a name="l00365"></a>00365           <span class="keywordflow">if</span> (value != GUARD_VALUE)
<a name="l00366"></a>00366             {
<a name="l00367"></a>00367               <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;Block of %lu bytes from %s had start guard value 0x%ux at %d expected 0x%x\n&quot;</span>,
<a name="l00368"></a>00368                           (<span class="keywordtype">long</span>) requested_bytes, source_string (source),
<a name="l00369"></a>00369                           value, i, GUARD_VALUE);
<a name="l00370"></a>00370               failed = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00371"></a>00371             }
<a name="l00372"></a>00372           
<a name="l00373"></a>00373           i += 4;
<a name="l00374"></a>00374         }
<a name="l00375"></a>00375 
<a name="l00376"></a>00376       i = GUARD_START_OFFSET + requested_bytes;
<a name="l00377"></a>00377       <span class="keywordflow">while</span> (i &lt; (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
<a name="l00378"></a>00378         {
<a name="l00379"></a>00379           <a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a> value = *(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*) &amp;block[i];
<a name="l00380"></a>00380           <span class="keywordflow">if</span> (value != GUARD_VALUE)
<a name="l00381"></a>00381             {
<a name="l00382"></a>00382               <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;Block of %lu bytes from %s had end guard value 0x%ux at %d expected 0x%x\n&quot;</span>,
<a name="l00383"></a>00383                           (<span class="keywordtype">long</span>) requested_bytes, source_string (source),
<a name="l00384"></a>00384                           value, i, GUARD_VALUE);
<a name="l00385"></a>00385               failed = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00386"></a>00386             }
<a name="l00387"></a>00387           
<a name="l00388"></a>00388           i += 4;
<a name="l00389"></a>00389         }
<a name="l00390"></a>00390 
<a name="l00391"></a>00391       <span class="comment">/* set memory to anything but nul bytes */</span>
<a name="l00392"></a>00392       <span class="keywordflow">if</span> (overwrite)
<a name="l00393"></a>00393         memset (free_block, <span class="charliteral">&#39;g&#39;</span>, requested_bytes);
<a name="l00394"></a>00394       
<a name="l00395"></a>00395       <span class="keywordflow">if</span> (failed)
<a name="l00396"></a>00396         _dbus_assert_not_reached (<span class="stringliteral">&quot;guard value corruption&quot;</span>);
<a name="l00397"></a>00397     }
<a name="l00398"></a>00398 }
<a name="l00399"></a>00399 
<a name="l00400"></a>00400 <span class="keyword">static</span> <span class="keywordtype">void</span>*
<a name="l00401"></a>00401 set_guards (<span class="keywordtype">void</span>       *real_block,
<a name="l00402"></a>00402             <span class="keywordtype">size_t</span>      requested_bytes,
<a name="l00403"></a>00403             BlockSource source)
<a name="l00404"></a>00404 {
<a name="l00405"></a>00405   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *block = real_block;
<a name="l00406"></a>00406   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;
<a name="l00407"></a>00407   
<a name="l00408"></a>00408   <span class="keywordflow">if</span> (block == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00409"></a>00409     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00410"></a>00410 
<a name="l00411"></a>00411   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
<a name="l00412"></a>00412   
<a name="l00413"></a>00413   *((<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*)block) = requested_bytes;
<a name="l00414"></a>00414   *((<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*)(block + 4)) = source;
<a name="l00415"></a>00415 
<a name="l00416"></a>00416   i = GUARD_INFO_SIZE;
<a name="l00417"></a>00417   <span class="keywordflow">while</span> (i &lt; GUARD_START_OFFSET)
<a name="l00418"></a>00418     {
<a name="l00419"></a>00419       (*(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*) &amp;block[i]) = GUARD_VALUE;
<a name="l00420"></a>00420       
<a name="l00421"></a>00421       i += 4;
<a name="l00422"></a>00422     }
<a name="l00423"></a>00423 
<a name="l00424"></a>00424   i = GUARD_START_OFFSET + requested_bytes;
<a name="l00425"></a>00425   <span class="keywordflow">while</span> (i &lt; (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
<a name="l00426"></a>00426     {
<a name="l00427"></a>00427       (*(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*) &amp;block[i]) = GUARD_VALUE;
<a name="l00428"></a>00428       
<a name="l00429"></a>00429       i += 4;
<a name="l00430"></a>00430     }
<a name="l00431"></a>00431   
<a name="l00432"></a>00432   check_guards (block + GUARD_START_OFFSET, <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>);
<a name="l00433"></a>00433   
<a name="l00434"></a>00434   <span class="keywordflow">return</span> block + GUARD_START_OFFSET;
<a name="l00435"></a>00435 }
<a name="l00436"></a>00436 
<a name="l00437"></a>00437 <span class="preprocessor">#endif</span>
<a name="l00438"></a>00438 <span class="preprocessor"></span> <span class="comment">/* End of internals docs */</span>
<a name="l00440"></a>00440 
<a name="l00441"></a>00441 
<a name="l00460"></a>00460 <span class="keywordtype">void</span>*
<a name="l00461"></a><a class="code" href="group__DBusMemory.html#gaf6e588659067a854c3cca7ebe8ae5084">00461</a> <a class="code" href="group__DBusMemory.html#gaf6e588659067a854c3cca7ebe8ae5084" title="Allocates the given number of bytes, as with standard malloc().">dbus_malloc</a> (<span class="keywordtype">size_t</span> bytes)
<a name="l00462"></a>00462 {
<a name="l00463"></a>00463 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00464"></a>00464 <span class="preprocessor"></span>  _dbus_initialize_malloc_debug ();
<a name="l00465"></a>00465   
<a name="l00466"></a>00466   <span class="keywordflow">if</span> (_dbus_decrement_fail_alloc_counter ())
<a name="l00467"></a>00467     {
<a name="l00468"></a>00468       _dbus_verbose (<span class="stringliteral">&quot; FAILING malloc of %ld bytes\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00469"></a>00469       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00470"></a>00470     }
<a name="l00471"></a>00471 <span class="preprocessor">#endif</span>
<a name="l00472"></a>00472 <span class="preprocessor"></span>
<a name="l00473"></a>00473   <span class="keywordflow">if</span> (bytes == 0) <span class="comment">/* some system mallocs handle this, some don&#39;t */</span>
<a name="l00474"></a>00474     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00475"></a>00475 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00476"></a>00476 <span class="preprocessor"></span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
<a name="l00477"></a>00477     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00478"></a>00478   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (guards)
<a name="l00479"></a>00479     {
<a name="l00480"></a>00480       <span class="keywordtype">void</span> *block;
<a name="l00481"></a>00481 
<a name="l00482"></a>00482       block = malloc (bytes + GUARD_EXTRA_SIZE);
<a name="l00483"></a>00483       <span class="keywordflow">if</span> (block)
<a name="l00484"></a>00484         {
<a name="l00485"></a>00485           <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00486"></a>00486         }
<a name="l00487"></a>00487       <span class="keywordflow">else</span> <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00488"></a>00488         {
<a name="l00489"></a>00489           <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: malloc (%ld + %ld)\n&quot;</span>,
<a name="l00490"></a>00490               (<span class="keywordtype">long</span>) bytes, (<span class="keywordtype">long</span>) GUARD_EXTRA_SIZE);
<a name="l00491"></a>00491           <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00492"></a>00492         }
<a name="l00493"></a>00493       
<a name="l00494"></a>00494       <span class="keywordflow">return</span> set_guards (block, bytes, SOURCE_MALLOC);
<a name="l00495"></a>00495     }
<a name="l00496"></a>00496 <span class="preprocessor">#endif</span>
<a name="l00497"></a>00497 <span class="preprocessor"></span>  <span class="keywordflow">else</span>
<a name="l00498"></a>00498     {
<a name="l00499"></a>00499       <span class="keywordtype">void</span> *mem;
<a name="l00500"></a>00500       mem = malloc (bytes);
<a name="l00501"></a>00501 
<a name="l00502"></a>00502 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00503"></a>00503 <span class="preprocessor"></span>      <span class="keywordflow">if</span> (mem)
<a name="l00504"></a>00504         {
<a name="l00505"></a>00505           <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00506"></a>00506         }
<a name="l00507"></a>00507       <span class="keywordflow">else</span> <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00508"></a>00508         {
<a name="l00509"></a>00509           <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: malloc (%ld)\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00510"></a>00510           <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00511"></a>00511         }
<a name="l00512"></a>00512 <span class="preprocessor">#endif</span>
<a name="l00513"></a>00513 <span class="preprocessor"></span>
<a name="l00514"></a>00514       <span class="keywordflow">return</span> mem;
<a name="l00515"></a>00515     }
<a name="l00516"></a>00516 }
<a name="l00517"></a>00517 
<a name="l00530"></a>00530 <span class="keywordtype">void</span>*
<a name="l00531"></a><a class="code" href="group__DBusMemory.html#gaa02722b030a091f6c14c4cb11a593623">00531</a> <a class="code" href="group__DBusMemory.html#gaa02722b030a091f6c14c4cb11a593623" title="Allocates the given number of bytes, as with standard malloc(), but all bytes are initialized to zero...">dbus_malloc0</a> (<span class="keywordtype">size_t</span> bytes)
<a name="l00532"></a>00532 {
<a name="l00533"></a>00533 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00534"></a>00534 <span class="preprocessor"></span>  _dbus_initialize_malloc_debug ();
<a name="l00535"></a>00535   
<a name="l00536"></a>00536   <span class="keywordflow">if</span> (_dbus_decrement_fail_alloc_counter ())
<a name="l00537"></a>00537     {
<a name="l00538"></a>00538       _dbus_verbose (<span class="stringliteral">&quot; FAILING malloc0 of %ld bytes\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00539"></a>00539       
<a name="l00540"></a>00540       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00541"></a>00541     }
<a name="l00542"></a>00542 <span class="preprocessor">#endif</span>
<a name="l00543"></a>00543 <span class="preprocessor"></span>  
<a name="l00544"></a>00544   <span class="keywordflow">if</span> (bytes == 0)
<a name="l00545"></a>00545     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00546"></a>00546 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00547"></a>00547 <span class="preprocessor"></span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
<a name="l00548"></a>00548     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00549"></a>00549   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (guards)
<a name="l00550"></a>00550     {
<a name="l00551"></a>00551       <span class="keywordtype">void</span> *block;
<a name="l00552"></a>00552 
<a name="l00553"></a>00553       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
<a name="l00554"></a>00554 
<a name="l00555"></a>00555       <span class="keywordflow">if</span> (block)
<a name="l00556"></a>00556         {
<a name="l00557"></a>00557           <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00558"></a>00558         }
<a name="l00559"></a>00559       <span class="keywordflow">else</span> <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00560"></a>00560         {
<a name="l00561"></a>00561           <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: calloc (%ld + %ld, 1)\n&quot;</span>,
<a name="l00562"></a>00562               (<span class="keywordtype">long</span>) bytes, (<span class="keywordtype">long</span>) GUARD_EXTRA_SIZE);
<a name="l00563"></a>00563           <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00564"></a>00564         }
<a name="l00565"></a>00565 
<a name="l00566"></a>00566       <span class="keywordflow">return</span> set_guards (block, bytes, SOURCE_MALLOC_ZERO);
<a name="l00567"></a>00567     }
<a name="l00568"></a>00568 <span class="preprocessor">#endif</span>
<a name="l00569"></a>00569 <span class="preprocessor"></span>  <span class="keywordflow">else</span>
<a name="l00570"></a>00570     {
<a name="l00571"></a>00571       <span class="keywordtype">void</span> *mem;
<a name="l00572"></a>00572       mem = calloc (bytes, 1);
<a name="l00573"></a>00573 
<a name="l00574"></a>00574 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00575"></a>00575 <span class="preprocessor"></span>      <span class="keywordflow">if</span> (mem)
<a name="l00576"></a>00576         {
<a name="l00577"></a>00577           <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00578"></a>00578         }
<a name="l00579"></a>00579       <span class="keywordflow">else</span> <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00580"></a>00580         {
<a name="l00581"></a>00581           <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: calloc (%ld)\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00582"></a>00582           <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00583"></a>00583         }
<a name="l00584"></a>00584 <span class="preprocessor">#endif</span>
<a name="l00585"></a>00585 <span class="preprocessor"></span>
<a name="l00586"></a>00586       <span class="keywordflow">return</span> mem;
<a name="l00587"></a>00587     }
<a name="l00588"></a>00588 }
<a name="l00589"></a>00589 
<a name="l00600"></a>00600 <span class="keywordtype">void</span>*
<a name="l00601"></a><a class="code" href="group__DBusMemory.html#ga2d505f2960ccf343042d363170b27f9c">00601</a> <a class="code" href="group__DBusMemory.html#ga2d505f2960ccf343042d363170b27f9c" title="Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_realloc</a> (<span class="keywordtype">void</span>  *memory,
<a name="l00602"></a>00602               <span class="keywordtype">size_t</span> bytes)
<a name="l00603"></a>00603 {
<a name="l00604"></a>00604 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00605"></a>00605 <span class="preprocessor"></span>  _dbus_initialize_malloc_debug ();
<a name="l00606"></a>00606   
<a name="l00607"></a>00607   <span class="keywordflow">if</span> (_dbus_decrement_fail_alloc_counter ())
<a name="l00608"></a>00608     {
<a name="l00609"></a>00609       _dbus_verbose (<span class="stringliteral">&quot; FAILING realloc of %ld bytes\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00610"></a>00610       
<a name="l00611"></a>00611       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00612"></a>00612     }
<a name="l00613"></a>00613 <span class="preprocessor">#endif</span>
<a name="l00614"></a>00614 <span class="preprocessor"></span>  
<a name="l00615"></a>00615   <span class="keywordflow">if</span> (bytes == 0) <span class="comment">/* guarantee this is safe */</span>
<a name="l00616"></a>00616     {
<a name="l00617"></a>00617       <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (memory);
<a name="l00618"></a>00618       <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00619"></a>00619     }
<a name="l00620"></a>00620 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00621"></a>00621 <span class="preprocessor"></span>  <span class="keywordflow">else</span> <span class="keywordflow">if</span> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
<a name="l00622"></a>00622     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00623"></a>00623   <span class="keywordflow">else</span> <span class="keywordflow">if</span> (guards)
<a name="l00624"></a>00624     {
<a name="l00625"></a>00625       <span class="keywordflow">if</span> (memory)
<a name="l00626"></a>00626         {
<a name="l00627"></a>00627           <span class="keywordtype">size_t</span> old_bytes;
<a name="l00628"></a>00628           <span class="keywordtype">void</span> *block;
<a name="l00629"></a>00629           
<a name="l00630"></a>00630           check_guards (memory, <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>);
<a name="l00631"></a>00631           
<a name="l00632"></a>00632           block = realloc (((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)memory) - GUARD_START_OFFSET,
<a name="l00633"></a>00633                            bytes + GUARD_EXTRA_SIZE);
<a name="l00634"></a>00634 
<a name="l00635"></a>00635           <span class="keywordflow">if</span> (block == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00636"></a>00636             {
<a name="l00637"></a>00637               <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00638"></a>00638                 {
<a name="l00639"></a>00639                   <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: realloc (%p, %ld + %ld)\n&quot;</span>,
<a name="l00640"></a>00640                       memory, (<span class="keywordtype">long</span>) bytes, (<span class="keywordtype">long</span>) GUARD_EXTRA_SIZE);
<a name="l00641"></a>00641                   <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00642"></a>00642                 }
<a name="l00643"></a>00643 
<a name="l00644"></a>00644               <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00645"></a>00645             }
<a name="l00646"></a>00646 
<a name="l00647"></a>00647           old_bytes = *(<a class="code" href="group__DBusTypes.html#gaf513803b030613a669cc7ef199f90a8b" title="A 32-bit unsigned integer on all platforms.">dbus_uint32_t</a>*)block;
<a name="l00648"></a>00648           <span class="keywordflow">if</span> (bytes &gt;= old_bytes)
<a name="l00649"></a>00649             <span class="comment">/* old guards shouldn&#39;t have moved */</span>
<a name="l00650"></a>00650             check_guards (((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)block) + GUARD_START_OFFSET, <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>);
<a name="l00651"></a>00651           
<a name="l00652"></a>00652           <span class="keywordflow">return</span> set_guards (block, bytes, SOURCE_REALLOC);
<a name="l00653"></a>00653         }
<a name="l00654"></a>00654       <span class="keywordflow">else</span>
<a name="l00655"></a>00655         {
<a name="l00656"></a>00656           <span class="keywordtype">void</span> *block;
<a name="l00657"></a>00657           
<a name="l00658"></a>00658           block = malloc (bytes + GUARD_EXTRA_SIZE);
<a name="l00659"></a>00659 
<a name="l00660"></a>00660           <span class="keywordflow">if</span> (block)
<a name="l00661"></a>00661             {
<a name="l00662"></a>00662               <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00663"></a>00663             }
<a name="l00664"></a>00664           <span class="keywordflow">else</span> <span class="keywordflow">if</span> (malloc_cannot_fail)
<a name="l00665"></a>00665             {
<a name="l00666"></a>00666               <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: malloc (%ld + %ld)\n&quot;</span>,
<a name="l00667"></a>00667                   (<span class="keywordtype">long</span>) bytes, (<span class="keywordtype">long</span>) GUARD_EXTRA_SIZE);
<a name="l00668"></a>00668               <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00669"></a>00669             }
<a name="l00670"></a>00670 
<a name="l00671"></a>00671           <span class="keywordflow">return</span> set_guards (block, bytes, SOURCE_REALLOC_NULL);   
<a name="l00672"></a>00672         }
<a name="l00673"></a>00673     }
<a name="l00674"></a>00674 <span class="preprocessor">#endif</span>
<a name="l00675"></a>00675 <span class="preprocessor"></span>  <span class="keywordflow">else</span>
<a name="l00676"></a>00676     {
<a name="l00677"></a>00677       <span class="keywordtype">void</span> *mem;
<a name="l00678"></a>00678       mem = realloc (memory, bytes);
<a name="l00679"></a>00679 
<a name="l00680"></a>00680 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00681"></a>00681 <span class="preprocessor"></span>      <span class="keywordflow">if</span> (mem == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a> &amp;&amp; malloc_cannot_fail)
<a name="l00682"></a>00682         {
<a name="l00683"></a>00683           <a class="code" href="group__DBusInternalsUtils.html#gabea2c16d6d3fe7c4eb5b9496fc877f84" title="Prints a warning message to stderr.">_dbus_warn</a> (<span class="stringliteral">&quot;out of memory: malloc (%ld)\n&quot;</span>, (<span class="keywordtype">long</span>) bytes);
<a name="l00684"></a>00684           <a class="code" href="group__DBusSysdeps.html#ga83c51771d3c4e612522bd5ad72e6ad7a" title="Aborts the program with SIGABRT (dumping core).">_dbus_abort</a> ();
<a name="l00685"></a>00685         }
<a name="l00686"></a>00686 
<a name="l00687"></a>00687       <span class="keywordflow">if</span> (memory == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a> &amp;&amp; mem != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00688"></a>00688             <a class="code" href="group__DBusSysdeps.html#gae74c3fcf12acaeccdb152ef907de951c" title="Atomically increments an integer.">_dbus_atomic_inc</a> (&amp;n_blocks_outstanding);
<a name="l00689"></a>00689 <span class="preprocessor">#endif</span>
<a name="l00690"></a>00690 <span class="preprocessor"></span>      <span class="keywordflow">return</span> mem;
<a name="l00691"></a>00691     }
<a name="l00692"></a>00692 }
<a name="l00693"></a>00693 
<a name="l00700"></a>00700 <span class="keywordtype">void</span>
<a name="l00701"></a><a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a">00701</a> <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (<span class="keywordtype">void</span>  *memory)
<a name="l00702"></a>00702 {
<a name="l00703"></a>00703 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00704"></a>00704 <span class="preprocessor"></span>  <span class="keywordflow">if</span> (guards)
<a name="l00705"></a>00705     {
<a name="l00706"></a>00706       check_guards (memory, <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>);
<a name="l00707"></a>00707       <span class="keywordflow">if</span> (memory)
<a name="l00708"></a>00708         {
<a name="l00709"></a>00709 <span class="preprocessor">#ifdef DBUS_DISABLE_ASSERT</span>
<a name="l00710"></a>00710 <span class="preprocessor"></span>          <a class="code" href="group__DBusSysdeps.html#ga3c50a069f795dceeb9172fe2b517bbdc" title="Atomically decrement an integer.">_dbus_atomic_dec</a> (&amp;n_blocks_outstanding);
<a name="l00711"></a>00711 <span class="preprocessor">#else</span>
<a name="l00712"></a>00712 <span class="preprocessor"></span>          <a class="code" href="group__DBusTypes.html#gab18afa60e9b17b6e322735bda3d863d8" title="A 32-bit signed integer on all platforms.">dbus_int32_t</a> old_value;
<a name="l00713"></a>00713 
<a name="l00714"></a>00714           old_value = <a class="code" href="group__DBusSysdeps.html#ga3c50a069f795dceeb9172fe2b517bbdc" title="Atomically decrement an integer.">_dbus_atomic_dec</a> (&amp;n_blocks_outstanding);
<a name="l00715"></a>00715           _dbus_assert (old_value &gt;= 1);
<a name="l00716"></a>00716 <span class="preprocessor">#endif</span>
<a name="l00717"></a>00717 <span class="preprocessor"></span>
<a name="l00718"></a>00718           free (((<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span>*)memory) - GUARD_START_OFFSET);
<a name="l00719"></a>00719         }
<a name="l00720"></a>00720       
<a name="l00721"></a>00721       <span class="keywordflow">return</span>;
<a name="l00722"></a>00722     }
<a name="l00723"></a>00723 <span class="preprocessor">#endif</span>
<a name="l00724"></a>00724 <span class="preprocessor"></span>    
<a name="l00725"></a>00725   <span class="keywordflow">if</span> (memory) <span class="comment">/* we guarantee it&#39;s safe to free (NULL) */</span>
<a name="l00726"></a>00726     {
<a name="l00727"></a>00727 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00728"></a>00728 <span class="preprocessor"></span><span class="preprocessor">#ifdef DBUS_DISABLE_ASSERT</span>
<a name="l00729"></a>00729 <span class="preprocessor"></span>      <a class="code" href="group__DBusSysdeps.html#ga3c50a069f795dceeb9172fe2b517bbdc" title="Atomically decrement an integer.">_dbus_atomic_dec</a> (&amp;n_blocks_outstanding);
<a name="l00730"></a>00730 <span class="preprocessor">#else</span>
<a name="l00731"></a>00731 <span class="preprocessor"></span>      <a class="code" href="group__DBusTypes.html#gab18afa60e9b17b6e322735bda3d863d8" title="A 32-bit signed integer on all platforms.">dbus_int32_t</a> old_value;
<a name="l00732"></a>00732 
<a name="l00733"></a>00733       old_value = <a class="code" href="group__DBusSysdeps.html#ga3c50a069f795dceeb9172fe2b517bbdc" title="Atomically decrement an integer.">_dbus_atomic_dec</a> (&amp;n_blocks_outstanding);
<a name="l00734"></a>00734       _dbus_assert (old_value &gt;= 1);
<a name="l00735"></a>00735 <span class="preprocessor">#endif</span>
<a name="l00736"></a>00736 <span class="preprocessor"></span><span class="preprocessor">#endif</span>
<a name="l00737"></a>00737 <span class="preprocessor"></span>
<a name="l00738"></a>00738       free (memory);
<a name="l00739"></a>00739     }
<a name="l00740"></a>00740 }
<a name="l00741"></a>00741 
<a name="l00748"></a>00748 <span class="keywordtype">void</span>
<a name="l00749"></a><a class="code" href="group__DBusMemory.html#gac200b2dbc8b3f6ecac4d42426fb97b40">00749</a> <a class="code" href="group__DBusMemory.html#gac200b2dbc8b3f6ecac4d42426fb97b40" title="Frees a NULL-terminated array of strings.">dbus_free_string_array</a> (<span class="keywordtype">char</span> **str_array)
<a name="l00750"></a>00750 {
<a name="l00751"></a>00751   <span class="keywordflow">if</span> (str_array)
<a name="l00752"></a>00752     {
<a name="l00753"></a>00753       <span class="keywordtype">int</span> i;
<a name="l00754"></a>00754 
<a name="l00755"></a>00755       i = 0;
<a name="l00756"></a>00756       <span class="keywordflow">while</span> (str_array[i])
<a name="l00757"></a>00757         {
<a name="l00758"></a>00758           <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (str_array[i]);
<a name="l00759"></a>00759           i++;
<a name="l00760"></a>00760         }
<a name="l00761"></a>00761 
<a name="l00762"></a>00762       <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (str_array);
<a name="l00763"></a>00763     }
<a name="l00764"></a>00764 }
<a name="l00765"></a>00765  <span class="comment">/* End of public API docs block */</span>
<a name="l00767"></a>00767 
<a name="l00768"></a>00768 
<a name="l00781"></a><a class="code" href="group__DBusMemoryInternals.html#ga7c224d82013e2bdc181c1d85dcb6f295">00781</a> <span class="keywordtype">int</span> <a class="code" href="group__DBusMemoryInternals.html#ga7c224d82013e2bdc181c1d85dcb6f295" title="_dbus_current_generation is used to track each time that dbus_shutdown() is called, so we can reinit things after it&#39;s been called.">_dbus_current_generation</a> = 1;
<a name="l00782"></a>00782 
<a name="l00786"></a><a class="code" href="group__DBusMemoryInternals.html#ga719c469fe616ee7148e826e6ebf3c3e4">00786</a> <span class="keyword">typedef</span> <span class="keyword">struct </span><a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a> <a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a>;
<a name="l00787"></a>00787 
<a name="l00791"></a><a class="code" href="structShutdownClosure.html">00791</a> <span class="keyword">struct </span><a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a>
<a name="l00792"></a>00792 {
<a name="l00793"></a><a class="code" href="structShutdownClosure.html#a2152ea3eed35addda2f33eee5fbcdd26">00793</a>   <a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a> *<a class="code" href="structShutdownClosure.html#a2152ea3eed35addda2f33eee5fbcdd26" title="Next ShutdownClosure.">next</a>;     
<a name="l00794"></a><a class="code" href="structShutdownClosure.html#a8d8d4ad2e646f8643a8044de02cf823f">00794</a>   DBusShutdownFunction <a class="code" href="structShutdownClosure.html#a8d8d4ad2e646f8643a8044de02cf823f" title="Function to call.">func</a>; 
<a name="l00795"></a><a class="code" href="structShutdownClosure.html#ab6f70dacffc0aa2376a6c26e1fb17518">00795</a>   <span class="keywordtype">void</span> *<a class="code" href="structShutdownClosure.html#ab6f70dacffc0aa2376a6c26e1fb17518" title="Data for function.">data</a>;                
<a name="l00796"></a>00796 };
<a name="l00797"></a>00797 
<a name="l00798"></a>00798 <span class="comment">/* Protected by _DBUS_LOCK (shutdown_funcs) */</span>
<a name="l00799"></a>00799 <span class="keyword">static</span> <a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a> *registered_globals = <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>;
<a name="l00800"></a>00800 
<a name="l00809"></a>00809 <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a>
<a name="l00810"></a><a class="code" href="group__DBusMemoryInternals.html#ga8dc6d5dd0a0e7901ab793da7403ee734">00810</a> <a class="code" href="group__DBusMemoryInternals.html#ga8dc6d5dd0a0e7901ab793da7403ee734" title="Register a cleanup function to be called exactly once the next time dbus_shutdown() is called...">_dbus_register_shutdown_func</a> (DBusShutdownFunction  func,
<a name="l00811"></a>00811                               <span class="keywordtype">void</span>                 *data)
<a name="l00812"></a>00812 {
<a name="l00813"></a>00813   <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> ok;
<a name="l00814"></a>00814 
<a name="l00815"></a>00815   <span class="keywordflow">if</span> (!_DBUS_LOCK (shutdown_funcs))
<a name="l00816"></a>00816     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00817"></a>00817 
<a name="l00818"></a>00818   ok = _dbus_register_shutdown_func_unlocked (func, data);
<a name="l00819"></a>00819   _DBUS_UNLOCK (shutdown_funcs);
<a name="l00820"></a>00820   <span class="keywordflow">return</span> ok;
<a name="l00821"></a>00821 }
<a name="l00822"></a>00822 
<a name="l00823"></a>00823 <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a>
<a name="l00824"></a>00824 _dbus_register_shutdown_func_unlocked (DBusShutdownFunction  func,
<a name="l00825"></a>00825                                        <span class="keywordtype">void</span>                 *data)
<a name="l00826"></a>00826 {
<a name="l00827"></a>00827   <a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a> *c;
<a name="l00828"></a>00828 
<a name="l00829"></a>00829   c = <a class="code" href="group__DBusMemory.html#ga54ccb556e7964112a825a7f46c156ca0" title="Safe macro for using dbus_malloc().">dbus_new</a> (<a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a>, 1);
<a name="l00830"></a>00830 
<a name="l00831"></a>00831   <span class="keywordflow">if</span> (c == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00832"></a>00832     <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa93f0eb578d23995850d61f7d61c55c1" title="Expands to &quot;0&quot;.">FALSE</a>;
<a name="l00833"></a>00833 
<a name="l00834"></a>00834   c-&gt;<a class="code" href="structShutdownClosure.html#a8d8d4ad2e646f8643a8044de02cf823f" title="Function to call.">func</a> = func;
<a name="l00835"></a>00835   c-&gt;<a class="code" href="structShutdownClosure.html#ab6f70dacffc0aa2376a6c26e1fb17518" title="Data for function.">data</a> = data;
<a name="l00836"></a>00836 
<a name="l00837"></a>00837   c-&gt;<a class="code" href="structShutdownClosure.html#a2152ea3eed35addda2f33eee5fbcdd26" title="Next ShutdownClosure.">next</a> = registered_globals;
<a name="l00838"></a>00838   registered_globals = c;
<a name="l00839"></a>00839 
<a name="l00840"></a>00840   <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00841"></a>00841 }
<a name="l00842"></a>00842  <span class="comment">/* End of private API docs block */</span>
<a name="l00844"></a>00844 
<a name="l00845"></a>00845 
<a name="l00889"></a>00889 <span class="keywordtype">void</span>
<a name="l00890"></a><a class="code" href="group__DBusMemory.html#ga01912903e39428872920d861ef565bac">00890</a> <a class="code" href="group__DBusMemory.html#ga01912903e39428872920d861ef565bac" title="Frees all memory allocated internally by libdbus and reverses the effects of dbus_threads_init().">dbus_shutdown</a> (<span class="keywordtype">void</span>)
<a name="l00891"></a>00891 {
<a name="l00892"></a>00892   <span class="keywordflow">while</span> (registered_globals != <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00893"></a>00893     {
<a name="l00894"></a>00894       <a class="code" href="structShutdownClosure.html" title="This struct represents a function to be called on shutdown.">ShutdownClosure</a> *c;
<a name="l00895"></a>00895 
<a name="l00896"></a>00896       c = registered_globals;
<a name="l00897"></a>00897       registered_globals = c-&gt;<a class="code" href="structShutdownClosure.html#a2152ea3eed35addda2f33eee5fbcdd26" title="Next ShutdownClosure.">next</a>;
<a name="l00898"></a>00898       
<a name="l00899"></a>00899       (* c-&gt;<a class="code" href="structShutdownClosure.html#a8d8d4ad2e646f8643a8044de02cf823f" title="Function to call.">func</a>) (c-&gt;<a class="code" href="structShutdownClosure.html#ab6f70dacffc0aa2376a6c26e1fb17518" title="Data for function.">data</a>);
<a name="l00900"></a>00900       
<a name="l00901"></a>00901       <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (c);
<a name="l00902"></a>00902     }
<a name="l00903"></a>00903 
<a name="l00904"></a>00904   <span class="comment">/* We wrap this in the thread-initialization lock because</span>
<a name="l00905"></a>00905 <span class="comment">   * dbus_threads_init() uses the current generation to tell whether</span>
<a name="l00906"></a>00906 <span class="comment">   * we&#39;re initialized, so we need to make sure that un-initializing</span>
<a name="l00907"></a>00907 <span class="comment">   * propagates into all threads. */</span>
<a name="l00908"></a>00908   <a class="code" href="group__DBusSysdeps.html#ga65d32daf6d876dd39b7410f3fa35b591" title="Lock a static mutex used to protect _dbus_threads_init_platform_specific().">_dbus_threads_lock_platform_specific</a> ();
<a name="l00909"></a>00909   _dbus_current_generation += 1;
<a name="l00910"></a>00910   <a class="code" href="group__DBusSysdeps.html#ga99c22b80d17ef18fa0a11f3a2a415403" title="Undo _dbus_threads_lock_platform_specific().">_dbus_threads_unlock_platform_specific</a> ();
<a name="l00911"></a>00911 }
<a name="l00912"></a>00912  
<a name="l00915"></a>00915 <span class="preprocessor">#ifdef DBUS_ENABLE_EMBEDDED_TESTS</span>
<a name="l00916"></a>00916 <span class="preprocessor"></span><span class="preprocessor">#include &quot;dbus-test.h&quot;</span>
<a name="l00917"></a>00917 
<a name="l00923"></a>00923 <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a>
<a name="l00924"></a>00924 _dbus_memory_test (<span class="keywordtype">void</span>)
<a name="l00925"></a>00925 {
<a name="l00926"></a>00926   <a class="code" href="group__DBusTypes.html#ga39c9cb0f3a2a8ad6f55cc4855d035349" title="A boolean, valid values are TRUE and FALSE.">dbus_bool_t</a> old_guards;
<a name="l00927"></a>00927   <span class="keywordtype">void</span> *p;
<a name="l00928"></a>00928   <span class="keywordtype">size_t</span> size;
<a name="l00929"></a>00929 
<a name="l00930"></a>00930   old_guards = guards;
<a name="l00931"></a>00931   guards = <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00932"></a>00932   p = <a class="code" href="group__DBusMemory.html#gaf6e588659067a854c3cca7ebe8ae5084" title="Allocates the given number of bytes, as with standard malloc().">dbus_malloc</a> (4);
<a name="l00933"></a>00933   <span class="keywordflow">if</span> (p == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00934"></a>00934     _dbus_assert_not_reached (<span class="stringliteral">&quot;no memory&quot;</span>);
<a name="l00935"></a>00935   <span class="keywordflow">for</span> (size = 4; size &lt; 256; size += 4)
<a name="l00936"></a>00936     {
<a name="l00937"></a>00937       p = <a class="code" href="group__DBusMemory.html#ga2d505f2960ccf343042d363170b27f9c" title="Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_realloc</a> (p, size);
<a name="l00938"></a>00938       <span class="keywordflow">if</span> (p == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00939"></a>00939         _dbus_assert_not_reached (<span class="stringliteral">&quot;no memory&quot;</span>);
<a name="l00940"></a>00940     }
<a name="l00941"></a>00941   <span class="keywordflow">for</span> (size = 256; size != 0; size -= 4)
<a name="l00942"></a>00942     {
<a name="l00943"></a>00943       p = <a class="code" href="group__DBusMemory.html#ga2d505f2960ccf343042d363170b27f9c" title="Resizes a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_realloc</a> (p, size);
<a name="l00944"></a>00944       <span class="keywordflow">if</span> (p == <a class="code" href="group__DBusMacros.html#ga070d2ce7b6bb7e5c05602aa8c308d0c4" title="A null pointer, defined appropriately for C or C++.">NULL</a>)
<a name="l00945"></a>00945         _dbus_assert_not_reached (<span class="stringliteral">&quot;no memory&quot;</span>);
<a name="l00946"></a>00946     }
<a name="l00947"></a>00947   <a class="code" href="group__DBusMemory.html#ga34e666b19b015035a9a31e53da84b39a" title="Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().">dbus_free</a> (p);
<a name="l00948"></a>00948   guards = old_guards;
<a name="l00949"></a>00949   <span class="keywordflow">return</span> <a class="code" href="group__DBusMacros.html#gaa8cecfc5c5c054d2875c03e77b7be15d" title="Expands to &quot;1&quot;.">TRUE</a>;
<a name="l00950"></a>00950 }
<a name="l00951"></a>00951 
<a name="l00952"></a>00952 <span class="preprocessor">#endif</span>
</pre></div></div><!-- contents -->


<hr class="footer"/><address class="footer"><small>
Generated on Thu Mar 3 2016 17:50:30 for D-Bus by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>