<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>AdaCore - The GNAT Pro Company &#187; Ada / Ada 2005</title>
	<atom:link href="http://www.adacore.com/category/developers-center/technologies/ada/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.adacore.com</link>
	<description>AdaCore technology and news</description>
	<lastBuildDate>Sun, 14 Mar 2010 08:49:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Gem #81: GNAT Semaphores</title>
		<link>http://www.adacore.com/2010/03/08/gem-81/</link>
		<comments>http://www.adacore.com/2010/03/08/gem-81/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 11:00:42 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3689</guid>
		<description><![CDATA[Ada Gem #81 &#8212; A previous Gem (#70, "The Scope Lock Idiom") discussed the occasional necessity of using low-level synchronization mechanisms instead of the higher-level protected object construct.  The code in that Gem referenced the facilities of the Semaphores package located in the GNAT hierarchy.  In this Gem, we examine the abstractions provided by that package, focusing especially on the design choices. ]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3><br/>


<p>A number of semaphore definitions exist, although the general concept remains as introduced by Dijkstra in 1968.  We assume the reader has some familiarity with semaphores and their semantics, and so we will not cover them here.  Many books are available for those requiring additional information. See especially <em>Principles of Concurrent and Distributed Programming</em> by M. Ben-Ari.</p>



<p>The GNAT.Semaphores package defines two semaphore abstractions, both in terms of protected types.  We use protected types, rather than defining abstract data types based on operating-system facilities, for the sake of portability.  A simple wrapper around the facilities of an operating system or RTOS would be reasonable, but the resulting interface (and implementation, of course) would not have the portability required of the general-purpose GNAT hierarchy.</p>



<p>The first abstraction defines a &#8220;counting&#8221; semaphore, in which an integer count is maintained by the operations, such that the acquire operation only executes (and thus returns, allowing the caller to continue) when the count is greater than zero.  Counting semaphores are convenient for expressing condition synchronization in terms of the availability of some number of resources.  For example, with a circular bounded buffer, a semaphore&#8217;s count can represent the number of available buffer slots.  Callers must wait for the buffer to be non-full when attempting to insert a new item, and the blocking call to acquire the semaphore will achieve that effect directly.</p>



<p>The declaration for the counting semaphore is as follows.  Note that we have removed the in-line comments for the sake of brevity, but will cover their content.</p>

<pre>
   <b>protected</b> <b>type</b> Counting_Semaphore
      (Initial_Value : Natural;
      Ceiling : System.Priority)
   <b>is</b>
      <b>pragma</b> Priority (Ceiling);

      <b>entry</b> Seize;
      <b>procedure</b> Release;

   <b>private</b>
      Count : Natural := Initial_Value;
   <b>end</b> Counting_Semaphore;
</pre>


<p>The second type implements &#8220;binary&#8221; semaphores.  This kind of semaphore is less flexible than the counting semaphore, in that no notion of a count is maintained.  The abstraction is simply that of a flag indicating whether or not the semaphore is available.  (If the value of a counting semaphore varied between zero and one, the effect would be the same.)</p>

<pre>
   <b>protected</b> <b>type</b> Binary_Semaphore
     (Initially_Available : Boolean;
      Ceiling : System.Priority)
   <b>is</b>
      <b>pragma</b> Priority (Ceiling);

      <b>entry</b> Seize;
      <b>procedure</b> Release;

   <b>private</b>
      Available : Boolean := Initially_Available;
   <b>end</b> Binary_Semaphore;
</pre>



<p>In both cases, the provided operations consist of an entry &#8220;Seize&#8221; to acquire the semaphore with mutually exclusive access, and a protected procedure &#8220;Release&#8221; to release it.  The Seize operation must be an entry for the sake of the barrier expressing the required condition synchronization.  Releasing the semaphore has no such requirement, so it need not be an entry.</p>



<p>Both types are visibly defined as protected types so that users can make conditional and timed calls when appropriate.  This capability addresses one of the portability problems with semaphores.  Although the basic &#8220;acquire&#8221; and &#8220;release&#8221; operations will always be provided (by whatever name), there is no &#8220;standard&#8221; interface for other forms of interaction.  Language-defined constructs provide some of those kinds of interactions, and do so portably.</p>



<p>Both types require discriminants.  The counting semaphore type uses a discriminant to specify the initial nonnegative value of the count.  The binary semaphore type uses a Boolean discriminant to specify whether the semaphore should be initially available.  For example, a binary semaphore would be initially available when used to express mutual exclusion, but might not be initially available when used to express condition synchronization.</p>



<p>In addition, both types use another discriminant to specify the ceiling priority for objects of the type. The discriminant is then passed as the argument to pragma Priority.  If the Real-Time Systems Annex is in force this part is essential; otherwise it has no effect.  Note that a default value cannot be provided for the Ceiling discriminant because that would require a default for the other discriminant, too.  The best we can do is to define a convenient value to be used in declarations of individual objects when the Real-Time Annex is not in force, so the following constant is provided in GNAT.Semaphores:</p>

<pre>
   Default_Ceiling : <b>constant</b> System.Priority := System.Default_Priority;
</pre>


<p>However, subtypes can be used to enhance convenience, readability and robustness.  The earlier Gem (#70) provided an example:</p>

<pre>
   <b>subtype</b> Mutual_Exclusion <b>is</b> Binary_Semaphore
     (Initially_Available =&gt; True,
      Ceiling             =&gt; Default_Ceiling);
</pre>


<p>The subtype ensures that corresponding objects are initially available, but also conveniently supplies the Ceiling discriminant value (assuming the Real-Time Annex is not in force) and gives the reader an indication of the intended use.</p>



<p>As you can see, the declarations of protected types can be very expressive.  The discriminants are the part you might not have considered using, although that is by no means an unusual approach. The protected bodies of both types are trivial and will not be shown here.  You can see them in the GNAT hierarchy, along with the implementations of all the other GNAT hierarchy packages.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/03/08/gem-81/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gem #80: Speedy Shift and Rotate in SPARK</title>
		<link>http://www.adacore.com/2010/02/25/gem-80/</link>
		<comments>http://www.adacore.com/2010/02/25/gem-80/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 13:50:16 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3502</guid>
		<description><![CDATA[Ada Gem #80 &#8212; This Gem covers a topic that I recently encountered
while working on a crypto algorithm in SPARK: how
to use Ada's predefined shift and rotate functions
with modular types from a SPARK program.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3><br/>

<p><strong>Introduction</strong></p>


<p>Ada 95 brought us the predefined package Interfaces, with its standard
definitions for types like Unsigned_32 and so on.
It also defines various functions to do common
shift and rotate operations on those types, such as:</p>

<pre>
   <b>function</b> Shift_Left
     (Value  : Unsigned_32;
      Amount : Natural) <b>return</b> Unsigned_32;
</pre>


<p>In GNAT Pro, these function declarations are
also followed by a curious-looking pragma:</p>

<pre>
   <b>pragma</b> Import (Intrinsic, Shift_Left);
</pre>


<p>The Ada 95 RM (6.3.1) says:</p>



<p>   &#8216;The intrinsic calling convention represents subprograms that
    are &#8220;built in&#8221; to the compiler.&#8217;</p>



<p>This basically means that the code generator &#8220;knows&#8221; how
to implement these functions. In the case of shifts and rotates,
this means that a call to one of these functions results in
very few (possibly just one) machine instructions &#8212; giving the
performance you&#8217;d expect from such a simple operation.</p>



<p>Shifts and rotates are useful in a wide variety of applications, 
but are endemic in cryptographic algorithms, where they
appear all over the place.</p>



<strong><p>So how can we get at them in SPARK?</p></strong>


<p>Unfortunately, the standard specification of package Interfaces
is not legal SPARK, since the various shift and rotate functions
are overloaded for each of the modular types, and overloading
within a scope is not allowed.  Darn&#8230;</p>



<p>To get round this, we first introduce a &#8220;shadow&#8221; specification
of Interfaces that supplies the legal SPARK bits that
we&#8217;re interested in &#8212; specifically the type declarations,
thus:</p>

<pre>
   <b>package</b> Interfaces <b>is</b>
      <b>type</b> Unsigned_8  <b>is</b> <b>mod</b> 2**8;
      <b>type</b> Unsigned_16 <b>is</b> <b>mod</b> 2**16;
      <b>type</b> Unsigned_32 <b>is</b> <b>mod</b> 2**32;
      <b>type</b> Unsigned_64 <b>is</b> <b>mod</b> 2**64;
   <b>end</b> Interfaces;
</pre>


<p>This is legal SPARK, and goes in a file called
interfaces.shs.  We then use the Examiner&#8217;s index-file
mechanism to make sure that the Examiner sees
this version of the package specification.</p>



<p>To get at the shift and rotate functions, we need
to introduce a new package that &#8220;de-overloads&#8221;
the names.  Let&#8217;s call this package &#8220;SR&#8221; &#8212; this is
also a &#8220;shadow&#8221;, so it goes in sr.shs or a similar file.
It looks like this:</p>

<pre>
   <b>with</b> Interfaces;
   <EM>&#45;&#45;# inherit Interfaces;</EM>
   <b>package</b> SR <b>is</b>

      <b>function</b> Rotate_Left_16
        (Value  : Interfaces.Unsigned_16;
         Amount : Natural) <b>return</b> Interfaces.Unsigned_16;

      <b>function</b> Rotate_Left_32
        (Value  : Interfaces.Unsigned_32;
         Amount : Natural) <b>return</b> Interfaces.Unsigned_32;

      <EM>&#45;&#45; ...and so on for all required functions...</EM>
   <b>end</b> SR;
</pre>


<p>Note how we removed the overloading of the function names.</p>



<p>OK&#8230; so how do we glue package SR to the
predefined intrinsic functions in package Interfaces?</p>



<p>It&#8217;s tempting to just implement the body of SR (in Ada, not SPARK)
to &#8220;call through&#8221; to the appropriate function in Interfaces,
but this might involve the overhead of a full-blown
function call/return sequence, losing all that juicy performance
that the intrinsic functions give us.  We could try using
pragma Inline to get rid of that overhead, but, as car
salesmen say, &#8220;your mileage may vary&#8221; with that idea.</p>



<p>Fortunately, there is a way out that preserves the
efficiency of the intrinsic version.  Remember that the
specification of SR above is a shadow?  We supply
the following slightly different version to the
compiler in sr.ads:</p>

<pre>
   <b>with</b> Interfaces;
   <b>package</b> SR <b>is</b>

      <b>function</b> Rotate_Left_16
        (Value  : Interfaces.Unsigned_16;
         Amount : Natural) <b>return</b> Interfaces.Unsigned_16 <b>renames</b> Interfaces.Rotate_Left;

      <b>function</b> Rotate_Left_32
        (Value  : Interfaces.Unsigned_32;
         Amount : Natural) <b>return</b> Interfaces.Unsigned_32 <b>renames</b> Interfaces.Rotate_Left;

      <EM>&#45;&#45; ...and so on</EM>
   <b>end</b> SR;
</pre>


<p>The renaming here ensures that our SPARK-friendly &#8220;un-overloaded&#8221; functions
are synonymous with the intrinsic &#8220;built in&#8221; functions, getting us
the best of both worlds &#8212; the type safety of SPARK, with the efficiency
of intrinsic machine code!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/25/gem-80/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Integrating static analysis with a compiler and database</title>
		<link>http://www.adacore.com/2010/02/25/integrating-static-analysis-with-a-compiler-and-database-2/</link>
		<comments>http://www.adacore.com/2010/02/25/integrating-static-analysis-with-a-compiler-and-database-2/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 08:42:56 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[codepeer]]></category>
		<category><![CDATA[static analysis]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3684</guid>
		<description><![CDATA[
In this article published in Embedded Computing Design, S. Tucker Taft looks at the advantages of integrating static analysis with a compiler and a database:



&#8220;Static analysis tools are becoming more integrated into the software development process. Saving data from the compiler, change history, and error information during the process instead of as a post-code step [...]]]></description>
			<content:encoded><![CDATA[
<p>In this article published in <a href="http://embedded-computing.com/" target="_blank">Embedded Computing Design</a>, S. Tucker Taft looks at the advantages of integrating static analysis with a compiler and a database:<br/>
<br/>


&#8220;Static analysis tools are becoming more integrated into the software development process. Saving data from the compiler, change history, and error information during the process instead of as a post-code step can make static analysis more productive.&#8221; </p>


<p>The full article can be found at:<br/>

<a href="http://embedded-computing.com/integrating-static-analysis-a-compiler-database">http://embedded-computing.com/integrating-static-analysis-a-compiler-database</a></p>

]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/25/integrating-static-analysis-with-a-compiler-and-database-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open-DO Conference &#8211; spaces still left</title>
		<link>http://www.adacore.com/2010/02/18/open-do-conference-2/</link>
		<comments>http://www.adacore.com/2010/02/18/open-do-conference-2/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 10:03:03 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3670</guid>
		<description><![CDATA[AdaCore is the organizer of the Open-DO conference &#8220;Combining Formality with Agility for Critical Software Development&#8221;. This event brings together experts from the two fields and asks the question &#8220;Can Formality and Agility be combined?&#8221; to develop software that has to reach the highest levels of safety and security. Talks will examine the theoretical consolidation [...]]]></description>
			<content:encoded><![CDATA[<p>AdaCore is the organizer of the Open-DO conference &#8220;Combining Formality with Agility for Critical Software Development&#8221;. This event brings together experts from the two fields and asks the question &#8220;Can Formality and Agility be combined?&#8221; to develop software that has to reach the highest levels of safety and security. Talks will examine the theoretical consolidation between these software development principles and industrial case studies will show how they are used in practice.

</p>


For more information and to sign-up (free of charge) for the event, please visit:<br/>
<br/>
 <a href="http://www.open-do.org/conference-2010/">www.open-do.org</a>.
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/18/open-do-conference-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>February Find The Bug Challenge</title>
		<link>http://www.adacore.com/2010/02/16/february-find-the-bug-challenge/</link>
		<comments>http://www.adacore.com/2010/02/16/february-find-the-bug-challenge/#comments</comments>
		<pubDate>Tue, 16 Feb 2010 08:08:57 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3641</guid>
		<description><![CDATA[
In order to demonstrate the scope of CodePeer’s code analysis capabilities we thought we’d have a little fun and each month post a simple piece of code that contains a few tricky bugs, so that you can measure your bug-finding capacities to those of CodePeer.



This Month’s Challenge


This code defines a function Cruise.Control which computes a [...]]]></description>
			<content:encoded><![CDATA[
<p>In order to demonstrate the scope of <a href="http://www.adacore.com/home/products/codepeer/">CodePeer</a>’s code analysis capabilities we thought we’d have a little fun and each month post a simple piece of code that contains a few tricky bugs, so that you can measure your bug-finding capacities to those of CodePeer.</p>



<p>This Month’s Challenge<br/>


This code defines a function Cruise.Control which computes a mode for automatically controlling a car speed, given the information provided by sensors on the position and speed of surrounding vehicles. The mode distinguishes various levels of breaking and speeding.</p>


<p>To see CodePeer in action please <a href="http://www.adacore.com/home/products/codepeer/toolset/findthebug/">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/16/february-find-the-bug-challenge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gem #79: Where did my memory go? (Part 3)</title>
		<link>http://www.adacore.com/2010/02/08/gem-79/</link>
		<comments>http://www.adacore.com/2010/02/08/gem-79/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 11:00:57 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3462</guid>
		<description><![CDATA[Ada Gem #79 &#8212; A number of tools and libraries exist to monitor memory usage,
detect memory leaks and more generally solve issues with memory management.
The Gems in this three-part series offer an overview of these issues and explain how you can benefit from these tools in your own development.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3><br/>


<p><strong>Part III: External Tools</strong></p>



<p>The previous two Gems on memory monitoring explained the use of
GNAT.Debug_Pools and GNATCOLL.Memory to monitor memory access and
detect memory leaks.</p>



<p>Some very powerful external tools exist on some systems that
can be used to achieve similar goals. Very often, these tools will
simply replace the system calls malloc and free, although in some
cases they will in fact emulate a virtual machine in which your
application is run.</p>



<p>One common kind of tool, available on most systems, indicates how much
memory your application is using (such as the Process Manager on Windows
or &#8220;top&#8221; on Unix systems). This is however a very limited tool, since memory
that is properly freed by your application might in fact not be given
back to the system (for performance reasons, malloc keeps it and
reuses it for the next allocation). So it is very hard to discover
memory leaks that way, and of course even if you can see that there is a
leak, you have no way of knowing where it is in your code.</p>



<p>One very useful Linux application is valgrind. This is a virtual
machine, that you start with various tools. One of them is a memory
checker, which can detect invalid memory accesses, double deallocation,
use of uninitialized variables, and memory leaks. You do not need to do
anything special when compiling your application and you can simply
run it as follows:</p>


<pre>
valgrind <EM>&#45;&#45;tool=memcheck your_app app_arguments</EM>
</pre>



<p>If you also want to detect memory leaks, start your application with:</p>


<pre>
valgrind <EM>&#45;&#45;tool=memcheck --leak-check=full --leak-resolution=med your_app</EM>
</pre>



<p>Compared to the techniques we discussed in the previous Gems, valgrind
is much slower (since all code runs in a virtual machine), but more
accurate. For instance, when it reports memory leaks, it will only
report those chunks of memory that are no longer referenced anywhere
in your application. For instance, if you have a global constant
initialized once by calling &#8220;new &#8230;&#8221;, the Ada packages would
report it as a leak, whereas valgrind by default knows it is still
referenced and will not bother you with it (although, of course,
you have an option to see it, namely &#8211;show-reachable). Since it is
often very hard to free such memory (you have to do it at finalization
time), and generally not worth it since the memory will be reclaimed
by the system anyway, you generally want to ignore those chunks.</p>



<p>Likewise, if you have an allocated object that itself contains
accesses to dynamically allocated memory, valgrind will by default
only report the root object as a leak, since the other chunks are
accessible from the first. When you fix the leak for the first,
it is likely that you will at the same time fix the other leaks as
well.</p>



<p>Other sources of memory leaks, much harder to detect, are those that
occur in the graphical part of your application. On most systems,
graphical rendering is a client-server process, and the memory is
allocated on the server, not on the client. Therefore, tools like
valgrind or the GNAT packages would not be able to report it as a leak
(for instance if you have allocated a big pixmap but never freed it). On
Windows these are called &#8220;GID&#8221;, and are visible in the Process
Manager, so that you can actually monitor whether your application
seems to have such leaks. Once again, though, this provides no detail as to
the origin of the leak.</p>



<p>On Unix, you can use the &#8220;xrestop&#8221; application, which can tell you the
number of windows, cursors, graphic context, etc. that you have
allocated.</p>
 


<p>Many such tools like this exist, both commercial and free. Let us know if
you routinely use such tools, as they could be useful to other readers of
this Gem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/08/gem-79/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ada and SPARK for Education and Research</title>
		<link>http://www.adacore.com/2010/02/08/ada-and-spark-for-education-and-research-2/</link>
		<comments>http://www.adacore.com/2010/02/08/ada-and-spark-for-education-and-research-2/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 09:51:31 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3560</guid>
		<description><![CDATA[AdaCore will be participating in this event targeting the academic community taking place in Belgium on Feb 23, 2010.  Robert Dewar will be giving a presentation entitled &#8220;What&#8217;s New in the World of Ada&#8221; and Rod Chapman will be giving a presentation entitled &#8220;SPARK &#8211; The Libre Language and Toolset for High-Assurance Software&#8221;. 

For [...]]]></description>
			<content:encoded><![CDATA[<p>AdaCore will be participating in this event targeting the academic community taking place in Belgium on Feb 23, 2010.<br/> <br/> Robert Dewar will be giving a presentation entitled &#8220;What&#8217;s New in the World of Ada&#8221; and Rod Chapman will be giving a presentation entitled &#8220;SPARK &#8211; The Libre Language and Toolset for High-Assurance Software&#8221;. </p>

For more information, please visit the <a href="http://distrinet.cs.kuleuven.be/events/AdaEvent/">event website</a>.]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/02/08/ada-and-spark-for-education-and-research-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Gem #78: Where did my memory go? (Part 2)</title>
		<link>http://www.adacore.com/2010/01/25/gem-78/</link>
		<comments>http://www.adacore.com/2010/01/25/gem-78/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 11:00:58 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3368</guid>
		<description><![CDATA[Ada Gem #78 &#8212; A number of tools and libraries exist to monitor memory usage,
detect memory leaks and more generally solve issues with memory management.
The Gems in this three-part series offer an overview of these issues and explain how you can benefit from these tools in your own development.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3><br/>


<p>Unless your coding standard forbids any dynamic allocation, memory
management is a constant concern during  system development. You might want to limit the amount of memory that your application requires, or you
might have memory leaks (allocation chunks that are never returned
to the system). The latter is a critical concern for long-running
applications.</p>



<p><strong>Part II: System.Memory</strong></p>


<p>In the previous Gem we discussed the use of GNAT.Debug_Pools to detect
memory problems. Another approach that is somewhat easier to use, because
it doesn&#8217;t require changes to the application, is to override
the System.Memory package. In GNAT and its run-time, all actual memory 
allocations are done via this package, which among other things
does the low-level system
calls to malloc() and free().</p>

<p>If you create your own
version of System.Memory, you will in fact short-circuit all memory
allocation and deallocation, and replace it with your own.
To do so, copy the file s-memory.adb to
one of your source directories, modify it as appropriate, and compile
your application, passing the &#8220;-a&#8221; switch to gnatmake (gprbuild
currently does not have an equivalent switch, although using project
files should work as expected). This ensures that GNAT will recompile
the library with your modified System.</p>



<p>Using this package does not provide the same safety as the debug
pools, since it does not check that dereferences are valid, and so
your code could still be accessing invalid memory. On the other hand,
the use of System.Memory is much less intrusive in your code. System.Memory
is best viewed as a performance analysis tool
rather than a debugging tool, although it will allow you to monitor
your code for memory leaks.</p>



<p>The GNATCOLL library (a recent addition to the GNAT technology, and
part of the latest customer and public releases) provides such an
implementation in the form of the GNATCOLL.Memory package. This
package is not a direct replacement for System.Memory, but only a
minimal amount of work is needed to make use of it, by creating
a version of s-memory.adb file that contains the following:</p>


<pre>
<b>with</b> GNATCOLL.Memory;
<b>package</b> <b>body</b> System.Memory <b>is</b>

   <b>package</b> M <b>renames</b> GNATCOLL.Memory;

   <b>function</b> Alloc (Size : size_t) <b>return</b> System.Address <b>is</b>
   <b>begin</b>
      <b>return</b> M.Alloc (M.size_t (Size));
   <b>end</b> Alloc;

   <b>procedure</b> Free (Ptr : System.Address) <b>renames</b> M.Free;

   <b>function</b> Realloc
      (Ptr  : System.Address;
       Size : size_t)
      <b>return</b> System.Address
   <b>is</b>
   <b>begin</b>
      <b>return</b> M.Realloc (Ptr, M.size_t (Size));
   <b>end</b> Realloc;

<b>end</b> System.Memory;
</pre>



<p>You then need to modify your code so that it properly initializes
GNATCOLL.Memory, which is done via a call like the following:</p>


<pre>
   GNATCOLL.Memory.Configure (Activate_Monitor =&gt; True);
</pre>



<p>The monitoring provided by this GNATCOLL package is not enabled by
default, to limit overhead on a running program. In your application,
monitoring could be activated through a command-line switch, or by
means of a specific environment variable. (This is all fully under your
control, though, so you&#8217;ll have to do the actual call to Getenv and
then to Configure.)</p>



<p>You can then instrument your code in one or more places to dump the
memory usage at that point. This is done through a call such as the following:</p>


<pre>
   GNATCOLL.Memory.Dump (Size =&gt; 3, Report =&gt; Memory_Usage);
</pre>



<p>Such a call will print on the console three backtraces for the code
that allocated the most memory (among the currently allocated memory).
Variants exist to dump the backtraces that executed the greatest
number of allocations (as opposed to the largest allocation size),
or the total amount of memory, even if that memory has since been released.</p>



<p>Such a dump includes a backtrace with addresses, which you can convert 
to a symbolic backtrace by using the external tool addr2line as follows:</p>

<pre>
    addr2line -e <executable> <backtrace>
</pre>


<p>This library has very light overhead (in particular when
not activated) so that you can distribute your application with
support for GNATCOLL.Memory built in, and then investigate any
issues that arise in production code. In fact, our own GPS IDE now includes
this support. Activation is controlled by an external variable,
and dumping the state of memory can be done through a Python command
at any point in time without the need to recompile GPS. (Note that GNATCOLL
also provides an interface to Python.)</p>



<p>Another feature of GNATCOLL.Memory is the capability of resetting all
counters to zero. For example, let&#8217;s assume we want to investigate
memory leaks while opening and closing editors in GPS. If we look at
the places that allocate memory, the biggest allocations that are displayed in
the console do not concern the editor itself, but rather memory
allocated when GPS started (if you are curious, this is generally
memory that is related to the cross-reference database). Therefore, we would
do the following: start GPS, reset GNATCOLL.Memory counters to 0, open
and close an editor, and dump memory usage. At that point, if Dump
prints any information on the console, we know that this is memory that has
been allocated since the call to Reset, and that wasn&#8217;t freed when the
editor was closed, and therefore is most likely a memory leak.</p>



<p>The appropriate use of Reset and Dump therefore allows the monitoring of
memory usage in specific parts of the code.</p>



<p>A separate tool called gnatmem is also distributed with GNAT. When you link your
application with the -lgmem switch, it will transparently instrument
all calls to the standard malloc and free (from the Ada code),
in a fashion similar
to GNATCOLL.Memory. On program exit, a disk file is created that
you can then analyze using gnatmem, to highlight the sources of
memory leaks in your application. This tool requires no change to your
code at all, but, on the other hand, does not provide a way to monitor
specific sections of your code like GNATCOLL.Memory does.</p>



<p>Gnatmem provides a number of command-line switches to control the
display of information. For instance, the switch &#8220;-m 0&#8243; lets you view
all places in your code that ever allocated memory, even if that
memory was properly deallocated afterwards. When one such place is
doing millions of allocations, it might sometimes be more efficient
to use a custom Storage_Pool and avoid the system call to malloc, by
reusing memory. The &#8220;-s&#8221; switch allows you to sort the output in
various ways.</p>



<p>In Part III of this series we will take a look at various commercially available
system tools for monitoring and analyzing memory usage.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/01/25/gem-78/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Gem #77: Where did my memory go? (Part 1)</title>
		<link>http://www.adacore.com/2010/01/11/gem-77/</link>
		<comments>http://www.adacore.com/2010/01/11/gem-77/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 11:00:51 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>
		<category><![CDATA[Devt log - Gem of the Week]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3305</guid>
		<description><![CDATA[Ada Gem #77 &#8212; A number of tools and libraries exist to monitor memory usage,
detect memory leaks, and more generally solve issues with memory management.
This Gem, and others to follow, offer an overview of these issues and explain how you can benefit from these tools in your own development.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3><br/>


<p>Unless your coding standard forbids any dynamic allocation, memory
management is a constant concern during  system development. You might want to limit the amount of memory that your application requires, or you
might have memory leaks (allocation chunks that are never returned
to the system). The latter is a critical concern for long-running
applications.
</p>


<p><strong>Part I: Storage Pools</strong></p>


<p>The standard Ada memory management mechanism is the storage pool, as defined in package System.Storage_Pools. A storage pool is a tagged type that allows you to override the standard &#8220;new&#8221; operator and the associated Unchecked_Deallocation procedure. A given pool can be associated with one or more access types. The GNAT run-time comes
with a number of predefined storage pools, and you can also create
your own. One basic implementation, for instance, would be to instrument the
pool operations to print a trace to the console every time memory is allocated or freed, and then post-process those traces with an external tool afterward.</p>



<p>This is of course a little tedious, so GNAT provides the package GNAT.Debug_Pools
as a much more advanced storage pool implementation that can, at any time during
program execution, display the currently allocated memory (along with a backtrace of the code at the point of allocation). It will also detect invalid memory references (for instance, attempting to dereference a pointer to already deallocated memory). The implementation is efficient andl imposes only a very small overhead on your application.</p>


<p>Here is a short example demonstrating the use of debug pools:</p>


<pre>
<b>with</b> GNAT.Debug_Pools;

<b>package</b> My_Package <b>is</b>
   Pool : GNAT.Debug_Pools.Debug_Pool;

   <b>type</b> Integer_Access <b>is</b> <b>access</b> Integer;
   <b>for</b> Integer_Access'Storage_Pool <b>use</b> Pool;
<b>end</b> My_Package;

<b>with</b> My_Package;
<b>with</b> Ada.Unchecked_Deallocation;
<b>procedure</b> Main <b>is</b>
   <b>procedure</b> Unchecked_Free <b>is</b>
      <b>new</b> Ada.Unchecked_Deallocation (Integer, Integer_Access);
   Ptr : Integer_Access;
<b>begin</b>
   Ptr := <b>new</b> Integer;
   Ptr.<b>all</b> := 1;
   Unchecked_Free (Ptr);
   Ptr.<b>all</b> := 2;  <EM>&#45;&#45;  raises exception</EM>
<b>end</b> Main;
</pre>



<p>The variable My_Package.Pool should be shared as much as possible among all your
access types. It&#8217;s not necessary to create one per access type.</p>



<p>As noted in the main procedure, the last reference to Ptr is
invalid, and will result in an exception raised from the debug pool (rather
than some erroneous behavior depending on the system).</p>



<p>GNAT.Debug_Pools provides various subprograms to analyze current
memory usage, in particular the total amount of memory currently
allocated, as well as which part of the code did the allocations. The
backtraces are also useful when analyzing double-deallocation
scenarios, since a debug pool shows both where the memory was
allocated and by what piece of code it was first deallocated.</p>



<p>However, GNAT&#8217;s debug pools are rather heavy to put in place in existing
code, since you need to add a &#8220;for Type_Name&#8217;Storage_Pool use Pool&#8221;
to every access type, and there is no mechanism to define a single
default storage pool for all types. GNAT.Debug_Pools can also give false warnings when dereferencing a pointer to aliased data on the stack (which was never allocated via a &#8220;new&#8221; operator, but was accessed via an &#8216;Access attribute).</p>

<p>In the next Gem we will discuss an alternative approach to controlling and instrumenting dynamic allocation and deallocation, by overriding the low-level memory management support itself.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2010/01/11/gem-77/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Open-DO conference 2010</title>
		<link>http://www.adacore.com/2009/12/16/open-do-conference-2010/</link>
		<comments>http://www.adacore.com/2009/12/16/open-do-conference-2010/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 14:02:50 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
				<category><![CDATA[Ada / Ada 2005]]></category>
		<category><![CDATA[Development Log]]></category>

		<guid isPermaLink="false">http://www2.adacore.com/?p=3287</guid>
		<description><![CDATA[Combining Formality with Agility for Critical Software Development


Event Date and Location: March 11, 2010 &#8211; Paris, France



Agile methods promote a disciplined project management process that encourages frequent inspection and adaptation, a leadership philosophy that encourages teamwork, self-organization and accountability, a set of engineering best practices that allow for rapid delivery of high-quality software, and a [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Combining Formality with Agility for Critical Software Development
</strong><br/>

<strong>Event Date and Location: March 11, 2010 &#8211; Paris, France</strong></p>



<p>Agile methods promote a disciplined project management process that encourages frequent inspection and adaptation, a leadership philosophy that encourages teamwork, self-organization and accountability, a set of engineering best practices that allow for rapid delivery of high-quality software, and a business approach that aligns development with customer needs and company goals. Formal Methods Formal methods and tools promise to find tough corner-case bugs and provide exhaustive proofs of software properties, based on mathematical techniques that are used both for the specification of properties and their verification. Although formal methods and tools don&#8217;t necessarily require PhDs in formal verification, they definitely require some expertise: someone has to know the tool well, understand how to write good formal properties, and know what to do when proofs don&#8217;t complete.
</p>


<p>This conference brings together experts from the two fields and asks the question &#8220;Can Formality and Agility be combined?&#8221; to develop software that has to reach the highest levels of safety and security. Talks will examine the theoretical consolidation between these software development principles and industrial case studies will show how they are used in practice.
</p>


<p>Speakers include Paul Boca, Jim Woodcock, Herve Delseny, Peter Gardner, Rod Chapman, Cyrille Comar.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2009/12/16/open-do-conference-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
