Sophie

Sophie

distrib > Mandriva > 2007.0 > x86_64 > media > main-release > by-pkgid > 926d2d1e3111287cee1b0a4fad4fb4f6 > files > 97

lib64dbus-1_3-devel-0.92-6mdv2007.0.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>dbus-memory.c Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="modules.html">Modules</a> &nbsp; <a class="qindex" href="annotated.html">Data Structures</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Data Fields</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; </center>
<hr><h1>dbus-memory.c</h1><div class="fragment"><pre>00001 <font class="comment">/* -*- mode: C; c-file-style: "gnu" -*- */</font>
00002 <font class="comment">/* dbus-memory.c  D-BUS memory handling</font>
00003 <font class="comment"> *</font>
00004 <font class="comment"> * Copyright (C) 2002, 2003  Red Hat Inc.</font>
00005 <font class="comment"> *</font>
00006 <font class="comment"> * Licensed under the Academic Free License version 2.0</font>
00007 <font class="comment"> * </font>
00008 <font class="comment"> * This program is free software; you can redistribute it and/or modify</font>
00009 <font class="comment"> * it under the terms of the GNU General Public License as published by</font>
00010 <font class="comment"> * the Free Software Foundation; either version 2 of the License, or</font>
00011 <font class="comment"> * (at your option) any later version.</font>
00012 <font class="comment"> *</font>
00013 <font class="comment"> * This program is distributed in the hope that it will be useful,</font>
00014 <font class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00015 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</font>
00016 <font class="comment"> * GNU General Public License for more details.</font>
00017 <font class="comment"> * </font>
00018 <font class="comment"> * You should have received a copy of the GNU General Public License</font>
00019 <font class="comment"> * along with this program; if not, write to the Free Software</font>
00020 <font class="comment"> * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</font>
00021 <font class="comment"> *</font>
00022 <font class="comment"> */</font>
00023 
00024 <font class="preprocessor">#include "dbus-memory.h"</font>
00025 <font class="preprocessor">#include "dbus-internals.h"</font>
00026 <font class="preprocessor">#include "dbus-sysdeps.h"</font>
00027 <font class="preprocessor">#include "dbus-list.h"</font>
00028 <font class="preprocessor">#include &lt;stdlib.h&gt;</font>
00029  <font class="comment">/* end of public API docs */</font>
00091 
00098 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00099 <font class="preprocessor"></font><font class="keyword">static</font> dbus_bool_t debug_initialized = FALSE;
00100 <font class="keyword">static</font> <font class="keywordtype">int</font> fail_nth = -1;
00101 <font class="keyword">static</font> size_t fail_size = 0;
00102 <font class="keyword">static</font> <font class="keywordtype">int</font> fail_alloc_counter = _DBUS_INT_MAX;
00103 <font class="keyword">static</font> <font class="keywordtype">int</font> n_failures_per_failure = 1;
00104 <font class="keyword">static</font> <font class="keywordtype">int</font> n_failures_this_failure = 0;
00105 <font class="keyword">static</font> dbus_bool_t guards = FALSE;
00106 <font class="keyword">static</font> dbus_bool_t disable_mem_pools = FALSE;
00107 <font class="keyword">static</font> dbus_bool_t backtrace_on_fail_alloc = FALSE;
00108 <font class="keyword">static</font> <font class="keywordtype">int</font> n_blocks_outstanding = 0;
00109 
<a name="l00111"></a><a class="code" href="group__DBusMemoryInternals.html#a26">00111</a> <font class="preprocessor">#define GUARD_VALUE 0xdeadbeef</font>
00112 <font class="preprocessor"></font>
<a name="l00113"></a><a class="code" href="group__DBusMemoryInternals.html#a27">00113</a> <font class="preprocessor">#define GUARD_INFO_SIZE 8</font>
00114 <font class="preprocessor"></font>
<a name="l00115"></a><a class="code" href="group__DBusMemoryInternals.html#a28">00115</a> <font class="preprocessor">#define GUARD_START_PAD 16</font>
00116 <font class="preprocessor"></font>
<a name="l00117"></a><a class="code" href="group__DBusMemoryInternals.html#a29">00117</a> <font class="preprocessor">#define GUARD_END_PAD 16</font>
00118 <font class="preprocessor"></font>
<a name="l00119"></a><a class="code" href="group__DBusMemoryInternals.html#a30">00119</a> <font class="preprocessor">#define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)</font>
00120 <font class="preprocessor"></font>
<a name="l00121"></a><a class="code" href="group__DBusMemoryInternals.html#a31">00121</a> <font class="preprocessor">#define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)</font>
00122 <font class="preprocessor"></font>
00123 <font class="keyword">static</font> <font class="keywordtype">void</font>
00124 _dbus_initialize_malloc_debug (<font class="keywordtype">void</font>)
00125 {
00126   <font class="keywordflow">if</font> (!debug_initialized)
00127     {
00128       debug_initialized = TRUE;
00129       
00130       <font class="keywordflow">if</font> (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_FAIL_NTH"</font>) != NULL)
00131         {
00132           fail_nth = atoi (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_FAIL_NTH"</font>));
00133           fail_alloc_counter = fail_nth;
00134           _dbus_verbose (<font class="stringliteral">"Will fail malloc every %d times\n"</font>, fail_nth);
00135         }
00136       
00137       <font class="keywordflow">if</font> (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_FAIL_GREATER_THAN"</font>) != NULL)
00138         {
00139           fail_size = atoi (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_FAIL_GREATER_THAN"</font>));
00140           _dbus_verbose (<font class="stringliteral">"Will fail mallocs over %d bytes\n"</font>,
00141                          fail_size);
00142         }
00143 
00144       <font class="keywordflow">if</font> (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_GUARDS"</font>) != NULL)
00145         {
00146           guards = TRUE;
00147           _dbus_verbose (<font class="stringliteral">"Will use malloc guards\n"</font>);
00148         }
00149 
00150       <font class="keywordflow">if</font> (_dbus_getenv (<font class="stringliteral">"DBUS_DISABLE_MEM_POOLS"</font>) != NULL)
00151         {
00152           disable_mem_pools = TRUE;
00153           _dbus_verbose (<font class="stringliteral">"Will disable memory pools\n"</font>);
00154         }
00155 
00156       <font class="keywordflow">if</font> (_dbus_getenv (<font class="stringliteral">"DBUS_MALLOC_BACKTRACES"</font>) != NULL)
00157         {
00158           backtrace_on_fail_alloc = TRUE;
00159           _dbus_verbose (<font class="stringliteral">"Will backtrace on failing a malloc\n"</font>);
00160         }
00161     }
00162 }
00163 
00169 dbus_bool_t
<a name="l00170"></a><a class="code" href="group__DBusMemoryInternals.html#a25">00170</a> _dbus_disable_mem_pools (<font class="keywordtype">void</font>)
00171 {
00172   _dbus_initialize_malloc_debug ();
00173   <font class="keywordflow">return</font> disable_mem_pools;
00174 }
00175 
00184 <font class="keywordtype">void</font>
<a name="l00185"></a><a class="code" href="group__DBusMemoryInternals.html#a26">00185</a> _dbus_set_fail_alloc_counter (<font class="keywordtype">int</font> until_next_fail)
00186 {
00187   _dbus_initialize_malloc_debug ();
00188 
00189   fail_alloc_counter = until_next_fail;
00190 
00191 <font class="preprocessor">#if 0</font>
00192 <font class="preprocessor"></font>  _dbus_verbose (<font class="stringliteral">"Set fail alloc counter = %d\n"</font>, fail_alloc_counter);
00193 <font class="preprocessor">#endif</font>
00194 <font class="preprocessor"></font>}
00195 
00202 <font class="keywordtype">int</font>
<a name="l00203"></a><a class="code" href="group__DBusMemoryInternals.html#a27">00203</a> _dbus_get_fail_alloc_counter (<font class="keywordtype">void</font>)
00204 {
00205   _dbus_initialize_malloc_debug ();
00206 
00207   <font class="keywordflow">return</font> fail_alloc_counter;
00208 }
00209 
00216 <font class="keywordtype">void</font>
<a name="l00217"></a><a class="code" href="group__DBusMemoryInternals.html#a28">00217</a> _dbus_set_fail_alloc_failures (<font class="keywordtype">int</font> failures_per_failure)
00218 {
00219   n_failures_per_failure = failures_per_failure;
00220 }
00221 
00228 <font class="keywordtype">int</font>
<a name="l00229"></a><a class="code" href="group__DBusMemoryInternals.html#a29">00229</a> _dbus_get_fail_alloc_failures (<font class="keywordtype">void</font>)
00230 {
00231   <font class="keywordflow">return</font> n_failures_per_failure;
00232 }
00233 
00242 dbus_bool_t
<a name="l00243"></a><a class="code" href="group__DBusMemoryInternals.html#a30">00243</a> _dbus_decrement_fail_alloc_counter (<font class="keywordtype">void</font>)
00244 {
00245   _dbus_initialize_malloc_debug ();
00246   
00247   <font class="keywordflow">if</font> (fail_alloc_counter &lt;= 0)
00248     {
00249       <font class="keywordflow">if</font> (backtrace_on_fail_alloc)
00250         _dbus_print_backtrace ();
00251 
00252       _dbus_verbose (<font class="stringliteral">"failure %d\n"</font>, n_failures_this_failure);
00253       
00254       n_failures_this_failure += 1;
00255       <font class="keywordflow">if</font> (n_failures_this_failure &gt;= n_failures_per_failure)
00256         {
00257           <font class="keywordflow">if</font> (fail_nth &gt;= 0)
00258             fail_alloc_counter = fail_nth;
00259           <font class="keywordflow">else</font>
00260             fail_alloc_counter = _DBUS_INT_MAX;
00261 
00262           n_failures_this_failure = 0;
00263 
00264           _dbus_verbose (<font class="stringliteral">"reset fail alloc counter to %d\n"</font>, fail_alloc_counter);
00265         }
00266       
00267       <font class="keywordflow">return</font> TRUE;
00268     }
00269   <font class="keywordflow">else</font>
00270     {
00271       fail_alloc_counter -= 1;
00272       <font class="keywordflow">return</font> FALSE;
00273     }
00274 }
00275 
00281 <font class="keywordtype">int</font>
<a name="l00282"></a><a class="code" href="group__DBusMemoryInternals.html#a31">00282</a> _dbus_get_malloc_blocks_outstanding (<font class="keywordtype">void</font>)
00283 {
00284   <font class="keywordflow">return</font> n_blocks_outstanding;
00285 }
00286 
<a name="l00290"></a><a class="code" href="group__DBusMemoryInternals.html#a32">00290</a> <font class="keyword">typedef</font> <font class="keyword">enum</font>
00291 {
00292   SOURCE_UNKNOWN,
00293   SOURCE_MALLOC,
00294   SOURCE_REALLOC,
00295   SOURCE_MALLOC_ZERO,
00296   SOURCE_REALLOC_NULL
00297 } BlockSource;
00298 
00299 <font class="keyword">static</font> <font class="keyword">const</font> <font class="keywordtype">char</font>*
00300 source_string (BlockSource source)
00301 {
00302   <font class="keywordflow">switch</font> (source)
00303     {
00304     <font class="keywordflow">case</font> SOURCE_UNKNOWN:
00305       <font class="keywordflow">return</font> <font class="stringliteral">"unknown"</font>;
00306     <font class="keywordflow">case</font> SOURCE_MALLOC:
00307       <font class="keywordflow">return</font> <font class="stringliteral">"malloc"</font>;
00308     <font class="keywordflow">case</font> SOURCE_REALLOC:
00309       <font class="keywordflow">return</font> <font class="stringliteral">"realloc"</font>;
00310     <font class="keywordflow">case</font> SOURCE_MALLOC_ZERO:
00311       <font class="keywordflow">return</font> <font class="stringliteral">"malloc0"</font>;
00312     <font class="keywordflow">case</font> SOURCE_REALLOC_NULL:
00313       <font class="keywordflow">return</font> <font class="stringliteral">"realloc(NULL)"</font>;
00314     }
00315   _dbus_assert_not_reached (<font class="stringliteral">"Invalid malloc block source ID"</font>);
00316   <font class="keywordflow">return</font> <font class="stringliteral">"invalid!"</font>;
00317 }
00318 
00319 <font class="keyword">static</font> <font class="keywordtype">void</font>
00320 check_guards (<font class="keywordtype">void</font> *free_block)
00321 {
00322   <font class="keywordflow">if</font> (free_block != NULL)
00323     {
00324       <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *block = ((<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*)free_block) - GUARD_START_OFFSET;
00325       size_t requested_bytes = *(dbus_uint32_t*)block;
00326       BlockSource source = *(dbus_uint32_t*)(block + 4);
00327       <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> i;
00328       dbus_bool_t failed;
00329 
00330       failed = FALSE;
00331 
00332 <font class="preprocessor">#if 0</font>
00333 <font class="preprocessor"></font>      _dbus_verbose (<font class="stringliteral">"Checking %d bytes request from source %s\n"</font>,
00334                      requested_bytes, source_string (source));
00335 <font class="preprocessor">#endif</font>
00336 <font class="preprocessor"></font>      
00337       i = GUARD_INFO_SIZE;
00338       <font class="keywordflow">while</font> (i &lt; GUARD_START_OFFSET)
00339         {
00340           dbus_uint32_t value = *(dbus_uint32_t*) &amp;block[i];
00341           <font class="keywordflow">if</font> (value != GUARD_VALUE)
00342             {
00343               _dbus_warn (<font class="stringliteral">"Block of %u bytes from %s had start guard value 0x%x at %d expected 0x%x\n"</font>,
00344                           requested_bytes, source_string (source),
00345                           value, i, GUARD_VALUE);
00346               failed = TRUE;
00347             }
00348           
00349           i += 4;
00350         }
00351 
00352       i = GUARD_START_OFFSET + requested_bytes;
00353       <font class="keywordflow">while</font> (i &lt; (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
00354         {
00355           dbus_uint32_t value = *(dbus_uint32_t*) &amp;block[i];
00356           <font class="keywordflow">if</font> (value != GUARD_VALUE)
00357             {
00358               _dbus_warn (<font class="stringliteral">"Block of %u bytes from %s had end guard value 0x%x at %d expected 0x%x\n"</font>,
00359                           requested_bytes, source_string (source),
00360                           value, i, GUARD_VALUE);
00361               failed = TRUE;
00362             }
00363           
00364           i += 4;
00365         }
00366 
00367       <font class="keywordflow">if</font> (failed)
00368         _dbus_assert_not_reached (<font class="stringliteral">"guard value corruption"</font>);
00369     }
00370 }
00371 
00372 <font class="keyword">static</font> <font class="keywordtype">void</font>*
00373 set_guards (<font class="keywordtype">void</font>       *real_block,
00374             size_t      requested_bytes,
00375             BlockSource source)
00376 {
00377   <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *block = real_block;
00378   <font class="keywordtype">unsigned</font> <font class="keywordtype">int</font> i;
00379   
00380   <font class="keywordflow">if</font> (block == NULL)
00381     <font class="keywordflow">return</font> NULL;
00382 
00383   _dbus_assert (GUARD_START_OFFSET + GUARD_END_PAD == GUARD_EXTRA_SIZE);
00384   
00385   *((dbus_uint32_t*)block) = requested_bytes;
00386   *((dbus_uint32_t*)(block + 4)) = source;
00387 
00388   i = GUARD_INFO_SIZE;
00389   <font class="keywordflow">while</font> (i &lt; GUARD_START_OFFSET)
00390     {
00391       (*(dbus_uint32_t*) &amp;block[i]) = GUARD_VALUE;
00392       
00393       i += 4;
00394     }
00395 
00396   i = GUARD_START_OFFSET + requested_bytes;
00397   <font class="keywordflow">while</font> (i &lt; (GUARD_START_OFFSET + requested_bytes + GUARD_END_PAD))
00398     {
00399       (*(dbus_uint32_t*) &amp;block[i]) = GUARD_VALUE;
00400       
00401       i += 4;
00402     }
00403   
00404   check_guards (block + GUARD_START_OFFSET);
00405   
00406   <font class="keywordflow">return</font> block + GUARD_START_OFFSET;
00407 }
00408 
00409 <font class="preprocessor">#endif</font>
00410 <font class="preprocessor"></font> <font class="comment">/* End of internals docs */</font>
00412 
00413 
00429 <font class="keywordtype">void</font>*
<a name="l00430"></a><a class="code" href="group__DBusMemory.html#a0">00430</a> dbus_malloc (size_t bytes)
00431 {
00432 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00433 <font class="preprocessor"></font>  _dbus_initialize_malloc_debug ();
00434   
00435   <font class="keywordflow">if</font> (_dbus_decrement_fail_alloc_counter ())
00436     {
00437       _dbus_verbose (<font class="stringliteral">" FAILING malloc of %d bytes\n"</font>, bytes);
00438       
00439       <font class="keywordflow">return</font> NULL;
00440     }
00441 <font class="preprocessor">#endif</font>
00442 <font class="preprocessor"></font>  
00443   <font class="keywordflow">if</font> (bytes == 0) <font class="comment">/* some system mallocs handle this, some don't */</font>
00444     <font class="keywordflow">return</font> NULL;
00445 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00446 <font class="preprocessor"></font>  <font class="keywordflow">else</font> <font class="keywordflow">if</font> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
00447     <font class="keywordflow">return</font> NULL;
00448   <font class="keywordflow">else</font> <font class="keywordflow">if</font> (guards)
00449     {
00450       <font class="keywordtype">void</font> *block;
00451 
00452       block = malloc (bytes + GUARD_EXTRA_SIZE);
00453       <font class="keywordflow">if</font> (block)
00454         n_blocks_outstanding += 1;
00455       
00456       <font class="keywordflow">return</font> set_guards (block, bytes, SOURCE_MALLOC);
00457     }
00458 <font class="preprocessor">#endif</font>
00459 <font class="preprocessor"></font>  <font class="keywordflow">else</font>
00460     {
00461       <font class="keywordtype">void</font> *mem;
00462       mem = malloc (bytes);
00463 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00464 <font class="preprocessor"></font>      <font class="keywordflow">if</font> (mem)
00465         n_blocks_outstanding += 1;
00466 <font class="preprocessor">#endif</font>
00467 <font class="preprocessor"></font>      <font class="keywordflow">return</font> mem;
00468     }
00469 }
00470 
00480 <font class="keywordtype">void</font>*
<a name="l00481"></a><a class="code" href="group__DBusMemory.html#a1">00481</a> dbus_malloc0 (size_t bytes)
00482 {
00483 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00484 <font class="preprocessor"></font>  _dbus_initialize_malloc_debug ();
00485   
00486   <font class="keywordflow">if</font> (_dbus_decrement_fail_alloc_counter ())
00487     {
00488       _dbus_verbose (<font class="stringliteral">" FAILING malloc0 of %d bytes\n"</font>, bytes);
00489       
00490       <font class="keywordflow">return</font> NULL;
00491     }
00492 <font class="preprocessor">#endif</font>
00493 <font class="preprocessor"></font>
00494   <font class="keywordflow">if</font> (bytes == 0)
00495     <font class="keywordflow">return</font> NULL;
00496 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00497 <font class="preprocessor"></font>  <font class="keywordflow">else</font> <font class="keywordflow">if</font> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
00498     <font class="keywordflow">return</font> NULL;
00499   <font class="keywordflow">else</font> <font class="keywordflow">if</font> (guards)
00500     {
00501       <font class="keywordtype">void</font> *block;
00502 
00503       block = calloc (bytes + GUARD_EXTRA_SIZE, 1);
00504       <font class="keywordflow">if</font> (block)
00505         n_blocks_outstanding += 1;
00506       <font class="keywordflow">return</font> set_guards (block, bytes, SOURCE_MALLOC_ZERO);
00507     }
00508 <font class="preprocessor">#endif</font>
00509 <font class="preprocessor"></font>  <font class="keywordflow">else</font>
00510     {
00511       <font class="keywordtype">void</font> *mem;
00512       mem = calloc (bytes, 1);
00513 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00514 <font class="preprocessor"></font>      <font class="keywordflow">if</font> (mem)
00515         n_blocks_outstanding += 1;
00516 <font class="preprocessor">#endif</font>
00517 <font class="preprocessor"></font>      <font class="keywordflow">return</font> mem;
00518     }
00519 }
00520 
00531 <font class="keywordtype">void</font>*
<a name="l00532"></a><a class="code" href="group__DBusMemory.html#a2">00532</a> dbus_realloc (<font class="keywordtype">void</font>  *memory,
00533               size_t bytes)
00534 {
00535 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00536 <font class="preprocessor"></font>  _dbus_initialize_malloc_debug ();
00537   
00538   <font class="keywordflow">if</font> (_dbus_decrement_fail_alloc_counter ())
00539     {
00540       _dbus_verbose (<font class="stringliteral">" FAILING realloc of %d bytes\n"</font>, bytes);
00541       
00542       <font class="keywordflow">return</font> NULL;
00543     }
00544 <font class="preprocessor">#endif</font>
00545 <font class="preprocessor"></font>  
00546   <font class="keywordflow">if</font> (bytes == 0) <font class="comment">/* guarantee this is safe */</font>
00547     {
00548       dbus_free (memory);
00549       <font class="keywordflow">return</font> NULL;
00550     }
00551 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00552 <font class="preprocessor"></font>  <font class="keywordflow">else</font> <font class="keywordflow">if</font> (fail_size != 0 &amp;&amp; bytes &gt; fail_size)
00553     <font class="keywordflow">return</font> NULL;
00554   <font class="keywordflow">else</font> <font class="keywordflow">if</font> (guards)
00555     {
00556       <font class="keywordflow">if</font> (memory)
00557         {
00558           size_t old_bytes;
00559           <font class="keywordtype">void</font> *block;
00560           
00561           check_guards (memory);
00562           
00563           block = realloc (((<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*)memory) - GUARD_START_OFFSET,
00564                            bytes + GUARD_EXTRA_SIZE);
00565 
00566           old_bytes = *(dbus_uint32_t*)block;
00567           <font class="keywordflow">if</font> (block &amp;&amp; bytes &gt;= old_bytes)
00568             <font class="comment">/* old guards shouldn't have moved */</font>
00569             check_guards (((<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*)block) + GUARD_START_OFFSET);
00570           
00571           <font class="keywordflow">return</font> set_guards (block, bytes, SOURCE_REALLOC);
00572         }
00573       <font class="keywordflow">else</font>
00574         {
00575           <font class="keywordtype">void</font> *block;
00576           
00577           block = malloc (bytes + GUARD_EXTRA_SIZE);
00578 
00579           <font class="keywordflow">if</font> (block)
00580             n_blocks_outstanding += 1;
00581           
00582           <font class="keywordflow">return</font> set_guards (block, bytes, SOURCE_REALLOC_NULL);   
00583         }
00584     }
00585 <font class="preprocessor">#endif</font>
00586 <font class="preprocessor"></font>  <font class="keywordflow">else</font>
00587     {
00588       <font class="keywordtype">void</font> *mem;
00589       mem = realloc (memory, bytes);
00590 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00591 <font class="preprocessor"></font>      <font class="keywordflow">if</font> (memory == NULL &amp;&amp; mem != NULL)
00592         n_blocks_outstanding += 1;
00593 <font class="preprocessor">#endif</font>
00594 <font class="preprocessor"></font>      <font class="keywordflow">return</font> mem;
00595     }
00596 }
00597 
00604 <font class="keywordtype">void</font>
<a name="l00605"></a><a class="code" href="group__DBusMemory.html#a3">00605</a> dbus_free (<font class="keywordtype">void</font>  *memory)
00606 {
00607 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00608 <font class="preprocessor"></font>  <font class="keywordflow">if</font> (guards)
00609     {
00610       check_guards (memory);
00611       <font class="keywordflow">if</font> (memory)
00612         {
00613           n_blocks_outstanding -= 1;
00614           
00615           _dbus_assert (n_blocks_outstanding &gt;= 0);
00616           
00617           free (((<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>*)memory) - GUARD_START_OFFSET);
00618         }
00619       
00620       <font class="keywordflow">return</font>;
00621     }
00622 <font class="preprocessor">#endif</font>
00623 <font class="preprocessor"></font>    
00624   <font class="keywordflow">if</font> (memory) <font class="comment">/* we guarantee it's safe to free (NULL) */</font>
00625     {
00626 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00627 <font class="preprocessor"></font>      n_blocks_outstanding -= 1;
00628       
00629       _dbus_assert (n_blocks_outstanding &gt;= 0);
00630 <font class="preprocessor">#endif</font>
00631 <font class="preprocessor"></font>
00632       free (memory);
00633     }
00634 }
00635 
00642 <font class="keywordtype">void</font>
<a name="l00643"></a><a class="code" href="group__DBusMemory.html#a4">00643</a> dbus_free_string_array (<font class="keywordtype">char</font> **str_array)
00644 {
00645   <font class="keywordflow">if</font> (str_array)
00646     {
00647       <font class="keywordtype">int</font> i;
00648 
00649       i = 0;
00650       <font class="keywordflow">while</font> (str_array[i])
00651         {
00652           dbus_free (str_array[i]);
00653           i++;
00654         }
00655 
00656       dbus_free (str_array);
00657     }
00658 }
00659  <font class="comment">/* End of public API docs block */</font>
00661 
00662 
<a name="l00675"></a><a class="code" href="group__DBusMemoryInternals.html#a10">00675</a> <font class="keywordtype">int</font> _dbus_current_generation = 1;
00676 
<a name="l00680"></a><a class="code" href="group__DBusMemoryInternals.html#a11">00680</a> <font class="keyword">typedef</font> <font class="keyword">struct </font><a class="code" href="structShutdownClosure.html">ShutdownClosure</a> ShutdownClosure;
00681 
<a name="l00685"></a><a class="code" href="structShutdownClosure.html">00685</a> <font class="keyword">struct </font>ShutdownClosure
00686 {
<a name="l00687"></a><a class="code" href="structShutdownClosure.html#m0">00687</a>   ShutdownClosure *<a class="code" href="structShutdownClosure.html#m0">next</a>;     
<a name="l00688"></a><a class="code" href="structShutdownClosure.html#m1">00688</a>   DBusShutdownFunction <a class="code" href="structShutdownClosure.html#m1">func</a>; 
<a name="l00689"></a><a class="code" href="structShutdownClosure.html#m2">00689</a>   <font class="keywordtype">void</font> *<a class="code" href="structShutdownClosure.html#m2">data</a>;                
00690 };
00691 
00692 _DBUS_DEFINE_GLOBAL_LOCK (shutdown_funcs);
00693 <font class="keyword">static</font> ShutdownClosure *registered_globals = NULL;
00694 
00703 dbus_bool_t
<a name="l00704"></a><a class="code" href="group__DBusMemoryInternals.html#a40">00704</a> _dbus_register_shutdown_func (DBusShutdownFunction  func,
00705                               <font class="keywordtype">void</font>                 *data)
00706 {
00707   ShutdownClosure *c;
00708 
00709   c = dbus_new (ShutdownClosure, 1);
00710 
00711   <font class="keywordflow">if</font> (c == NULL)
00712     <font class="keywordflow">return</font> FALSE;
00713 
00714   c-&gt;<a class="code" href="structShutdownClosure.html#m1">func</a> = func;
00715   c-&gt;<a class="code" href="structShutdownClosure.html#m2">data</a> = data;
00716 
00717   _DBUS_LOCK (shutdown_funcs);
00718   
00719   c-&gt;<a class="code" href="structShutdownClosure.html#m0">next</a> = registered_globals;
00720   registered_globals = c;
00721 
00722   _DBUS_UNLOCK (shutdown_funcs);
00723   
00724   <font class="keywordflow">return</font> TRUE;
00725 }
00726  <font class="comment">/* End of private API docs block */</font>
00728 
00729 
00748 <font class="keywordtype">void</font>
<a name="l00749"></a><a class="code" href="group__DBusMemory.html#a5">00749</a> dbus_shutdown (<font class="keywordtype">void</font>)
00750 {
00751   <font class="keywordflow">while</font> (registered_globals != NULL)
00752     {
00753       ShutdownClosure *c;
00754 
00755       c = registered_globals;
00756       registered_globals = c-&gt;<a class="code" href="structShutdownClosure.html#m0">next</a>;
00757       
00758       (* c-&gt;<a class="code" href="structShutdownClosure.html#m1">func</a>) (c-&gt;<a class="code" href="structShutdownClosure.html#m2">data</a>);
00759       
00760       dbus_free (c);
00761     }
00762 
00763   _dbus_current_generation += 1;
00764 }
00765  
00768 <font class="preprocessor">#ifdef DBUS_BUILD_TESTS</font>
00769 <font class="preprocessor"></font><font class="preprocessor">#include "dbus-test.h"</font>
00770 
00776 dbus_bool_t
<a name="l00777"></a><a class="code" href="group__DBusMemoryInternals.html#a25">00777</a> _dbus_memory_test (<font class="keywordtype">void</font>)
00778 {
00779   dbus_bool_t old_guards;
00780   <font class="keywordtype">void</font> *p;
00781   size_t size;
00782 
00783   old_guards = guards;
00784   guards = TRUE;
00785   p = dbus_malloc (4);
00786   <font class="keywordflow">if</font> (p == NULL)
00787     _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>);
00788   <font class="keywordflow">for</font> (size = 4; size &lt; 256; size += 4)
00789     {
00790       p = dbus_realloc (p, size);
00791       <font class="keywordflow">if</font> (p == NULL)
00792         _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>);
00793     }
00794   <font class="keywordflow">for</font> (size = 256; size != 0; size -= 4)
00795     {
00796       p = dbus_realloc (p, size);
00797       <font class="keywordflow">if</font> (p == NULL)
00798         _dbus_assert_not_reached (<font class="stringliteral">"no memory"</font>);
00799     }
00800   dbus_free (p);
00801   guards = old_guards;
00802   <font class="keywordflow">return</font> TRUE;
00803 }
00804 
00805 <font class="preprocessor">#endif</font>
</pre></div><hr><address align="right"><small>Generated on Wed Jun 9 05:01:25 2004 for D-BUS by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>