<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.11" -->
<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/"
	>

<channel>
	<title>AdaCore - The GNAT Pro Company</title>
	<link>http://www.adacore.com</link>
	<description>AdaCore technology and news</description>
	<pubDate>Wed, 02 Jul 2008 10:27:14 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.11</generator>
	<language>en</language>
			<item>
		<title>Gem #41: Accessibility Checks (Part II: Ada2005)</title>
		<link>http://www.adacore.com/2008/06/30/gem-41/</link>
		<comments>http://www.adacore.com/2008/06/30/gem-41/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 10:31:00 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/06/30/gem-41/</guid>
		<description><![CDATA[Ada Gem #41 &#8212; Ada 2005 brings a number of improvements concerning access types, aimed at simplifying the programmer's task and adding flexibility to the language. But with greater power comes greater responsibility, so accessibility checks have also been extended to prevent these new features from creating dangling pointers.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>Ada 2005 allows the use of anonymous access types in a more general manner, adding considerable power to the object-oriented programming features of the language. The accessibility rules have been correspondingly augmented to ensure safety by preventing the possibility of dangling references. The new rules have been designed with programming flexibility in mind, as well as to allow the compiler to enforce checks statically.</p>

<p>The accessibility levels in the new contexts for anonymous access types are generally determined by the scope where they are declared. This   makes it possible to perform compile-time accessibility checks.</p>


<p>Another rule that allows for static accessibility checks relates to derived types: a type derivation does not create new accessibility level for the derived type, but just takes that of the parent type:</p>

<pre>

     <b>procedure</b> Example_1 <b>is</b>
        <b>type</b> Node <b>is</b> <b>record</b>
           N : <b>access</b> Integer;
        <b>end</b> <b>record</b>;
        List : Node

        <b>procedure</b> P <b>is</b>
           <b>type</b> Other_Node <b>is</b> <b>new</b> Node;
        <b>begin</b>
           <b>declare</b>
              L : <b>aliased</b> Integer := 1;
              Data : Other_Node := Other_Node&#8217;(N =&gt; L&#8217;<b>Access</b>);
              <EM>&#45;&#45;  L&#8217;Access is illegal!</EM>
           <b>begin</b>
              List := Node (Data);
           <b>end;</b>
        <b>end</b> P;

     <b>begin</b>
        P;
     <b>end</b> Example_1;</EM>
</pre>


<p>In the above example, we don&#8217;t need to worry about expensive run-time checks on assignment or return of an object of type <tt>Other_Node</tt>; we know it has the same accessibility level as type <tt>Node</tt>, making the <tt>Access</tt> attribute illegal. If this were not prevented, after returning from <tt>P</tt>, <tt>List.N</tt> would be a dangling reference.</p>



<p>Ada 2005 also allows functions to return objects of anonymous access types. In this case, the accessibility level of the object is statically determined by the scope of the function declaration. Consider the following example:</p>


<pre>

     <b>procedure</b> Example_2 <b>is</b>
        <b>type</b> Rec <b>is</b> <b>record</b>
           V : <b>access</b> Integer;
           &#8230;
        <b>end</b> <b>record</b>;

        Global : <b>aliased</b> Integer := 1;

        <b>function</b> F1 (X : Boolean) <b>return</b> Rec <b>is</b>
           Local : <b>aliased</b> Integer := 2;

           <EM>&#45;&#45;  Nested function returns anonymous access values</EM>
           <EM>&#45;&#45;  with different nesting depths</EM>

           <b>function</b> F2 (Y : Boolean) <b>return</b> Access Integer <b>is</b>
           <b>begin</b>
              <b>if</b> Y <b>then</b>
                 <b>return</b> Global&#8217;Access;
              <b>else</b>
                 <b>return</b> Local&#8217;Access;
              <b>end</b> </b>if</b>;
           <b>end</b> F2;

        <b>begin</b>
           <b>return</b> (V =&gt; F2 (X), &#8230;); <EM>&#45;&#45; Illegal</EM>
        <b>end</b> F1;

        Data : Rec;
     <b>begin</b>
        Data := F1 (True);
     <b>end</b> Example_2;
</pre>



<p>In this example, applying the aforementioned rule, the compiler statically determines that
this accessibility level is the scope where <tt>F2</tt> is declared, which
is deeper than the accessibility level of <tt>Rec</tt>. So even though the
call <tt>F1 (True)</tt> would provide a valid value for <tt>V</tt>, the code is
illegal. The accessibility restriction is conservative, to keep the rules simple, and so that the compiler
is not required to perform data flow analysis to determine legality (not to mention that in general the
legality would be undecidable).</p>



<p>The new rules also take into account discriminants of an anonymous access type (which are technically referred to as access discriminants). In Ada 2005, access discriminants are now permitted for nonlimited types. Consequently, it&#8217;s necessary to disallow defaults for access discriminants of nonlimited types. Thus, the following declaration is illegal:</p>


<pre>

     Default : <b>aliased</b> Integer := &#8230;
     <b>type</b> Rec (D : <b>access</b> Integer := Default&#8217;<b>Access</b>) <b>is</b> <b>record</b>
        &#8230;
</pre>



<p>This restriction is needed to prevent the discriminant from creating a dangling reference due to an
assignment of the record object; it ensures that the object and the discriminant are bound together for their
lifetime.</p>

  

<p>Special care must be taken when types with access discriminants are used with allocators and return statements. The accessibility rules
require the compiler to perform static checks when new objects containing access discriminatns are created or returned. Consider the following example:</p>

  
<pre>

    <b>procedure</b> Example_3 <b>is</b>
       <b>type</b> Node (D : <b>access</b> Integer) <b>is</b> <b>record</b>
          V : Integer;
       <b>end</b> <b>record</b>;
       <b>type</b> Ptr <b>is</b> <b>access</b> <b>all</b> Node;

       Global_Value : <b>aliased</b> Integer := 1;
       Other_Data   : Integer := 2;
   
       <b>procedure</b> P <b>is</b>
          Local : <b>aliased</b> Integer := 3;
          R1 : Ptr;
          R2 : Ptr;
       <b>begin</b>
          R1 := <b>new</b> Node&#8217;(D =&gt; Global_Value&#8217;<b>Access</b>, V =&gt; Other_Data);
          <EM>&#45;&#45;  This is legal</EM>

          R2 := <b>new</b> Node&#8217;(D =&gt; Local_Value&#8217;<b>Access</b>, V =&gt; Other_Data);
          <EM>&#45;&#45;  This is illegal</EM>
       <b>end P;</b>
    <b>begin</b>
       <b>null</b>;
    <b>end</b> Example_3;
</pre>

<p>The allocator for <tt>R1</tt> is legal, since the accessibility level of
<tt>Global&#8217;Access</tt> is the same as the accessibility level of <tt>D</tt>. However the
allocator for <tt>R2</tt> is illegal, because the accessibility level of
<tt>Local&#8217;Access</tt> is deeper than the accessibility level of <tt>D</tt>, and assigning
<tt>R2</tt> to an object outside <tt>P</tt> could lead to a dangling reference.</p>

<p>In summary, these rules forbid the creation of an object in a storage pool that contains an access discriminant pointing to some area of memory, be it a part of the stack or some other storage pool, with a
shorter lifetime, thus preventing the discriminant from pointing to a
nonexistent object.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/06/30/gem-41/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #40: Safe and Secure Software : Chapter 5, Safe Object Oriented Programming</title>
		<link>http://www.adacore.com/2008/06/16/gem-40/</link>
		<comments>http://www.adacore.com/2008/06/16/gem-40/#comments</comments>
		<pubDate>Mon, 16 Jun 2008 10:00:20 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/06/16/gem-40/</guid>
		<description><![CDATA[This week's gem is the fifth chapter of John Barnes' new booklet:</p> 

<p>Safe and Secure Software: An Introduction to Ada 2005.</p>

<p>
Over the coming months, we will be publishing all thirteen chapters of the booklet. In the attachment at the bottom of Gem #30 you can access the contents and bibliography for the entire booklet.

We hope you will enjoy the read!</p>]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>OOP took programming by storm about twenty years ago. Its supreme merit is said to be its flexibility. But flexibility is somewhat like freedom discussed in the Introduction – the wrong kind of flexibility can be an opportunity that permits dangerous errors to intrude.</p>


<p>The key idea of OOP is that the objects dominate the programming and subprograms (methods) that manipulate objects are properties of objects. The other, older, view sometimes called Function-Oriented (or structured) programming, is that programming is primarily about functional decomposition and that it is the subprograms that dominate program organization, and that objects are merely passive things being manipulated by them.</p>


<p>Both views have their place and fanatical devotion to just a strict object view is often inappropriate.</p>


<p>Ada strikes an excellent balance and enables either approach to be taken according to the needs of the application. Indeed Ada has incorporated the idea of objects right from its inception in 1980 through the concept of packages which encapsulate types and the operations upon them, and tasks that encapsulate independent activities.</p>

<h3>Read Chapter 5 in full</h3>

<p>Note: All chapters of this booklet will, in time, be available on the <a href="/home/ada_answers/ada_2005">Ada 2005 home page</a>.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/06/16/gem-40/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #39: Efficient Stream I/O for Array Types</title>
		<link>http://www.adacore.com/2008/06/09/gem-39/</link>
		<comments>http://www.adacore.com/2008/06/09/gem-39/#comments</comments>
		<pubDate>Mon, 09 Jun 2008 10:00:32 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/06/09/gem-39/</guid>
		<description><![CDATA[Ada Gem #39 &#8212; Reading and writing values from/to streams is easy with Ada’s “stream attributes” but for some array types the default attribute implementations could be made more efficient.  In this Gem we show how the user can define these more efficient implementations.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>



<p>Ada has the notion of “streams” that are much like those of other languages: sequences of elements comprising values of arbitrary, possibly different, types.  Placing a value into a stream is easy using the language-defined “stream attributes”.  The programmer simply calls the type-specific attribute routine and specifies the stream and the value.  For example, to place an Integer value V into a stream S, one could write the following:</p>



<pre>
Integer’Write (S, V);
</pre>



<p>Strictly speaking, S is not a stream but, rather, an access value designating a stream.  The Integer’Write routine will convert the value of V into an array of “stream elements” – essentially an array of storage elements – and then put them into the stream designated by S. Actually, placing the bytes into the stream is accomplished by dynamically dispatching to a procedure specific to the stream representation.</p>



<p>Although this discussion is couched in terms of placing values into streams, you should understand that reading values from streams is very similar to writing them and that the same efficiency issue and solution apply.</p>



<p>For composite types, such as array or record types, each component value is individually written to the stream using the approach described above.  Consider an array type “A” specifying Integer as the component type.  The default version of A’Write will call Integer’Write for each component.  Thus, each Integer value is converted to the array of storage elements and written to the stream.  This component-driven behavior is necessary because programmers can define their own versions of the stream attributes, and naturally will expect them to be called even when the types in question are used as component types within enclosing array or record types.</p>



<p>But suppose the array type is structurally just a sequence of contiguous bytes, and the component type does not have a user-defined stream attribute defined.  In that case, calling the component-specific attribute for each array component is unnecessary and inefficient.</p>



<p>For example, suppose you are working with Military-Standard 1553B for communicating application values between remote devices.  Ultimately, Mil-Std-1553B sends and receives 32-word buffers, where each word is an unsigned 16-bit value.  Suppose as well that you want to write and read these buffers to and from streams.  We can override the stream attributes so that a whole buffer value is written directly to the stream instead of writing it one buffer component at a time.
</p>


<p>The buffer type could be declared as follows:</p>



<pre>

   <b>type</b> Buffer <b>is</b> <b>array</b> (1..32) <b>of</b> Interfaces.Unsigned_16;
</pre>



<p>We can then override the stream attributes for type Buffer.</p>

<p>First we declare the routines:</p>

<pre>

   <b>procedure</b> Read_Buffer
      (Stream : <b>not</b> <b>null</b> <b>access</b> Ada.Streams.Root_Stream_Type&#8217;Class;
       Item   : <b>out</b> Buffer);

   <b>procedure</b> Write_Buffer
      (Stream : <b>not</b> <b>null</b> <b>access</b> Ada.Streams.Root_Stream_Type&#8217;Class;
       Item   : <b>in</b> Buffer);
</pre>



<p>All such stream attributes have the same formal parameter types, i.e., an access parameter designating the class-wide root stream type defined by the language, and the type to be written to, or read from, that stream.</p>



<p>We then “tie” the routines to the stream attributes for type Buffer, thereby overriding the default versions:</p>



<pre>

   <b>for</b> Buffer&#8217;Read  <b>use</b> Read_Buffer;
   <b>for</b> Buffer&#8217;Write <b>use</b> Write_Buffer;
</pre>



<p>The language-defined root stream type and array element type are declared in package Ada.Streams:</p>



<pre>

<b>package</b> Ada.Streams <b>is</b>

   <b>type</b> Root_Stream_Type <b>is</b> <b>abstract</b> <b>tagged</b> <b>limited</b> <b>private</b>;

   <b>type</b> Stream_Element <b>is</b> <b>mod</b> 2 ** Standard&#8217;Storage_Unit;

   <b>type</b> Stream_Element_Offset <b>is</b> <b>range</b>
     -(2 ** (Standard&#8217;Address_Size - 1)) ..
     +(2 ** (Standard&#8217;Address_Size - 1)) - 1;

   &#8230;

   <b>type</b> Stream_Element_Array <b>is</b>
      <b>array</b> (Stream_Element_Offset <b>range</b> &lt;&gt;) <b>of</b> <b>aliased</b> Stream_Element;

   <b>procedure</b> Read
     (Stream : <b>in</b> <b>out</b> Root_Stream_Type;
      Item   : <b>out</b> Stream_Element_Array;
      Last   : <b>out</b> Stream_Element_Offset)
   <b>is</b> <b>abstract</b>;

   <b>procedure</b> Write
     (Stream : <b>in</b> <b>out</b> Root_Stream_Type;
      Item   : Stream_Element_Array)
   <b>is</b> <b>abstract</b>;

   &#8230;
<b>end</b> Ada.Streams;
</pre>



<p>The user-defined Read_Buffer and Write_Buffer routines will call these stream-oriented Read and Write procedures (via dynamic dispatching) once for the entire Buffer array value, instead of calling them once per array component.  Both routines are very similar, so we will omit the body of of Read_Buffer for the sake of brevity and show just the implementation of Write_Buffer:</p>



<pre>

   <b>procedure</b> Write_Buffer
      (Stream : <b>not</b> <b>null</b> <b>access</b> Ada.Streams.Root_Stream_Type&#8217;Class;
       Item   : <b>in</b> Buffer)
   <b>is</b>
      Item_Size : <b>constant</b> Stream_Element_Offset :=
                     Buffer&#8217;Object_Size / Stream_Element&#8217;Size;

      <b>type</b> SEA_Pointer <b>is</b>
         <b>access</b> <b>all</b> Stream_Element_Array (1 .. Item_Size);

      <b>function</b> As_SEA_Pointer <b>is</b>
         <b>new</b> Ada.Unchecked_Conversion (System.Address, SEA_Pointer);
   <b>begin</b>
      Ada.Streams.Write (Stream.<b>all</b>, As_SEA_Pointer (Item&#8217;Address).<b>all</b>);
   <b>end</b> Write_Buffer;
</pre>



<p>In the above, we cannot simply convert the value of Item, of array type Buffer, to a value of type Stream_Element_Array, so we work with pointers instead.  We define an access type designating a Stream_Element_Array that is the exact size, in terms of Stream_Elements, of the incoming Buffer value.  Note the use of the Buffer’Object_Size attribute in that computation.  That attribute gives us the size of objects of the type Buffer, a wise approach since in general the size of a type may not equal the size of objects of that type.  We can then use unchecked conversion to convert the address of the formal parameter Item to this access type.  Dereferencing that converted access value (via .all) gives us a value of type Stream_Element_Array that we can pass to the call to Ada.Streams.Write.</p>



<p>Thus we avoid processing each component of type Buffer, instead writing the entire Buffer value at once.  That’s a much more efficient approach.  As we said earlier, reading values from streams is analogous to writing values to them and only differs in obvious, minor ways.  That is true for using the default stream attributes as well as in the implementation of Read_Buffer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/06/09/gem-39/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #38: Safe and Secure Software : Chapter 4, Safe Architecture</title>
		<link>http://www.adacore.com/2008/06/02/gem-38/</link>
		<comments>http://www.adacore.com/2008/06/02/gem-38/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 10:00:25 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/06/02/gem-38/</guid>
		<description><![CDATA[This week's gem is the fourth chapter of John Barnes' new booklet:</p> 

<p>Safe and Secure Software: An Introduction to Ada 2005.</p>

<p>
Over the coming months, we will be publishing all thirteen chapters of the booklet. In the attachment at the bottom of Gem #30 you can access the contents and bibliography for the entire booklet.

We hope you will enjoy the read!</p>]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>When speaking of buildings, a good architecture is one whose design gives the required strength in a natural and unobtrusive manner and thereby provides a safe environment for the people within. An elegant example is the Pantheon in Rome whose spherical shape has enormous strength and provides an uncluttered space. Many ancient cathedrals are not so successful, and need buttresses tacked on the outside to prop up the walls. In 1624, Sir Henry Wooton summed the matter up in his book, The Elements of Architecture, by saying &#8220;Well building hath three conditions – commoditie, firmenes &#038; delight&#8221;. In modern terms, it should work, be strong and be beautiful as well.</p>



<p>A good architecture in a program should similarly provide unobtrusive safety for the detailed workings of the inner parts within a clean framework. It should permit interaction where appropriate and prevent unrelated activities from accidentally interfering with each other. And a good language should enable the writing of programs with a good architecture.</p>



<p>There is perhaps an analogy with the architecture of office spaces. An arrangement where everyone has an individual office can inhibit communication and the flow of ideas. On the other hand, an open plan office often causes problems because noise and other distractions interfere with productivity.</p>



<p>The structure of an Ada program is based primarily around the concept of a package, which groups related entities together and provides a natural framework for hiding implementation details from its clients.</p>

<h3>Read Chapter 4 in full</h3>

<p>Note: All chapters of this booklet will, in time, be available on the <a href="/home/ada_answers/ada_2005">Ada 2005 home page</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/06/02/gem-38/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #37: Bounded Buffer package in GNAT Hierarchy (Part 2)</title>
		<link>http://www.adacore.com/2008/05/26/gem-37/</link>
		<comments>http://www.adacore.com/2008/05/26/gem-37/#comments</comments>
		<pubDate>Mon, 26 May 2008 10:00:39 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/05/26/gem-37/</guid>
		<description><![CDATA[Ada Gem #37 &#8212; Part 1 of this Gem briefly
introduced bounded buffers, protected types, and the declaration
of the generic package GNAT.Bounded_Buffers, exporting protected
type Bounded_Buffer.  In Part 2 of the Gem we examine the private
part of Bounded_Buffer and the implementations of the protected
entries and functions.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>

<p>To recap Part 1, protected types add an efficient building block
to the tasking facilities of Ada.  Mutual exclusion is
automatically provided and condition synchronization is directly
expressed via entry barriers, all without the expense of task
switching.  As such, protected types are a natural implementation
approach for a circular bounded buffer used in a concurrent
programming context.  The complete declaration of the generic
package and encapsulated protected type are shown below,
including now the private part.  (We have omitted the comments in
the code since we cover them in the description.)</p>

<pre>

<b>with</b> System;

<b>generic</b>
   <b>type</b> Element <b>is</b> <b>private</b>;
<b>package</b> GNAT.Bounded_Buffers <b>is</b>
   <b>pragma</b> Pure;

   <b>type</b> Content <b>is</b> <b>array</b> (Positive <b>range</b> &lt;&gt;) <b>of</b> Element;

   Default_Ceiling : <b>constant</b> System.Priority := System.Default_Priority;

   <b>protected</b> <b>type</b> Bounded_Buffer
      (Capacity : Positive;
       Ceiling : System.Priority)
   <b>is</b>
      <b>pragma</b> Priority (Ceiling);

      <b>entry</b> Insert (Item : Element);
      <b>entry</b> Remove (Item : <b>out</b> Element);

      <b>function</b> Empty  <b>return</b> Boolean;
      <b>function</b> Full   <b>return</b> Boolean;
      <b>function</b> Extent <b>return</b> Natural;

   <b>private</b>
      Values   : Content (1 .. Capacity);
      Next_In  : Positive := 1;
      Next_Out : Positive := 1;
      Count    : Natural  := 0;
   <b>end</b> Bounded_Buffer;

<b>end</b> GNAT.Bounded_Buffers;
</pre>


<p>The private part of type Bounded_Buffer contains the encapsulated
data that can only be accessed via the exported routines.  The
abstraction is that of a bounded buffer, and it uses the component
Values, of the array type Content, to hold the buffer
values.  The discriminant Capacity determines the actual upper bound on the
array component, which is how different Bounded_Buffer objects
can have different capacities.  As a circular buffer, two array
indexes are required, one to indicate where the next inserted
value will be placed, and one to indicate where the next removed
value will come from.  These indexes are components Next_In and
Next_Out, respectively.  Finally, each Bounded_Buffer object
contains a count of the number of elements currently held by the
buffer.  This counter is used to provide the condition
synchronization required to prevent inserting items into a full
buffer or removing items from an empty buffer.  There are other
ways to determine whether the buffer is full or empty but this
counter-based approach is clear and is also useful for the
function Extent.</p>


<p>The generic package body contains only the body of the protected
type, and likewise, the protected body only contains the bodies
of the exported entries and functions, so we omit the lines for
the declarations of the package and protected type bodies and
focus instead on the routines themselves.</p>



<p>The first routine in the protected body is that of the entry
Insert.  Note the barrier “Count /= Capacity” providing the
condition synchronization for the state “not full”.  Callers to
Insert are kept waiting until the barrier is True.  As is the
case for any protected entry or protected procedure, callers
automatically execute with mutually exclusive access to the
encapsulated data.</p>

<pre>

      <b>entry</b> Insert (Item : Element) <b>when</b> Count /= Capacity <b>is</b>
      <b>begin</b>
         Values (Next_In) := Item;
         Next_In := (Next_In <b>mod</b> Capacity) + 1;
         Count := Count + 1;
      <b>end</b> Insert;
</pre>


<p>The body of Insert is straightforward.  Note that we have the
index Next_In wrap around the allowed index values based on the
capacity.  We could not use a modular type for either index
because that would require a static value for the modulus, but we
only have the Capacity discriminant to work with.</p>



<p>The body for entry Remove is similar to that of Insert, with
expected differences.</p>

<pre>

      <b>entry</b> Remove (Item : <b>out</b> Element) <b>when</b> Count &gt; 0 <b>is</b>
      <b>begin</b>
         Item := Values (Next_Out);
         Next_Out := (Next_Out <b>mod</b> Capacity) + 1;
         Count := Count - 1;
      <b>end</b> Remove;
</pre>


<p>An important point for each entry is the handling of the Count
component.  When either entry completes, the other entry is
examined to see if the barrier has become true.  If so, a waiting
caller (if any) is allowed to execute and the process repeats
itself.  Thus, a very simple expression of the condition is
provided by the barriers and these are automatically evaluated
whenever they could possibly change.</p>



<p>The function bodies are even simpler than the entry bodies.  It
is worth repeating a point made in Part 1 of this gem, namely
that the state of a protected object can change immediately after
a call to such a function returns.</p>

<pre>

      <b>function</b> Empty <b>return</b> Boolean <b>is</b>
      <b>begin</b>
         <b>return</b> Count = 0;
      <b>end</b> Empty;

      <b>function</b> Full <b>return</b> Boolean <b>is</b>
      <b>begin</b>
         <b>return</b> Count = Capacity;
      <b>end</b> Full;

      <b>function</b> Extent <b>return</b> Natural <b>is</b>
      <b>begin</b>
         <b>return</b> Count;
      <b>end</b> Extent;
</pre>


<p>Clients can query the capacity of any given Bounded_Buffer object
by simply reading the Capacity discriminant.  The discriminant
cannot be changed, so the effect is much the same as a function.</p>



<p>As Niklaus Wirth once wrote (citing Hoare), once you get the data
structures right, the code just writes itself.  Admittedly, he
didn’t use those exact words, but I think you will agree that the
data structures used in type Bounded_Buffer, when combined with
the semantics of protected types, make the bodies of the routines
trivial to write.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/05/26/gem-37/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #36: Safe and Secure Software : Chapter 3, Safe Pointers</title>
		<link>http://www.adacore.com/2008/05/19/gem-36/</link>
		<comments>http://www.adacore.com/2008/05/19/gem-36/#comments</comments>
		<pubDate>Mon, 19 May 2008 10:00:41 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/05/19/gem-36/</guid>
		<description><![CDATA[This week's gem is the third chapter of John Barnes' new booklet:</p> 

<p>Safe and Secure Software: An Introduction to Ada 2005.</p>

<p>
Over the coming months, we will be publishing all thirteen chapters of the booklet. In the attachment at the bottom of Gem #30 you can access the contents and bibliography for the entire booklet.

We hope you will enjoy the read!</p>]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>Primitive man made a huge leap forward with the discovery of fire. Not only did this allow him to keep warm and cook and thereby expand into more challenging environments but it also enabled the creation of metal tools and thus the bootstrap to an industrial society. But fire is dangerous when misused and can cause tremendous havoc; observe that society has special standing organizations just to deal with fires that are out of control.</p>


<p>Software similarly made a big leap forward in its capabilities when the notion of pointers or references was introduced. But playing with pointers is like playing with fire. Pointers can bring enormous benefits but if misused can bring immediate disaster such as a blue screen, or allow a rampaging program to destroy data, or create the loophole through which a virus can invade.</p>


<p>High integrity software typically limits drastically the use of pointers. The access types of Ada have the semantics of pointers but in addition carry numerous safeguards on their use, which makes them safe in the most demanding safety-critical programs.
</p>

<h3>Read Chapter 3 in full</h3>

<p>Note: All chapters of this booklet will, in time, be available on the <a href="/home/ada_answers/ada_2005">Ada 2005 home page</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/05/19/gem-36/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Spring newsletter available</title>
		<link>http://www.adacore.com/2008/05/13/spring-newsletter-available-2/</link>
		<comments>http://www.adacore.com/2008/05/13/spring-newsletter-available-2/#comments</comments>
		<pubDate>Tue, 13 May 2008 10:36:24 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/05/13/spring-newsletter-available-2/</guid>
		<description><![CDATA[The latest edition of the GNAT Pro Insider newsletter has been published and is available for download at:



www.adacore.com/category/press-center/newsletters



This issue features:


	New Release of GNAT Programming Studio
	Contract Award for Coverage Analysis Project

	Current Releases

	In the Pipeline

	Academia Corner

	Interview with Emmanuel Briot

	Webinar Schedule
	
Technology Corner:Pragmas Precondition and Postcondition

	Conferences/Events




]]></description>
			<content:encoded><![CDATA[<p>The latest edition of the GNAT Pro Insider newsletter has been published and is available for download at:<br/>
<br/>


<a href="http://www.adacore.com/category/press-center/newsletters/">www.adacore.com/category/press-center/newsletters</a>
</p>


<p>This issue features:<br/>


	<li>New Release of GNAT Programming Studio</li>
	<li>Contract Award for Coverage Analysis Project</li>

	<li>Current Releases</li>

	<li>In the Pipeline</li>

	<li>Academia Corner</li>

	<li>Interview with Emmanuel Briot</li>

	<li>Webinar Schedule</li>
	<li>
Technology Corner:Pragmas Precondition and Postcondition</li>

	<li>Conferences/Events</li>
</p>



]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/05/13/spring-newsletter-available-2/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #35: bounded buffer package in GNAT hierarchy (Part 1)</title>
		<link>http://www.adacore.com/2008/05/12/gem-35/</link>
		<comments>http://www.adacore.com/2008/05/12/gem-35/#comments</comments>
		<pubDate>Mon, 12 May 2008 10:00:57 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/05/12/gem-35/</guid>
		<description><![CDATA[Ada Gem #35 &#8212;  Ada 95 introduced “protected types” as
a fundamental building block for efficient concurrent programming
and interrupt handling.  In this Gem we examine the use of
protected types in the implementation of the classic asynchronous
bounded buffer abstraction provided by the GNAT hierarchy of library units.  This
Gem assumes the reader is somewhat familiar with protected types
and will, therefore, explain some, but by no means all, of their
semantics.]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>The bounded buffer is a classic concurrent programming component
exhibiting asynchronous task interactions.  The concept is that
of a buffer of a fixed size that is accessed by multiple tasks, some inserting items and some removing them, concurrently and
asynchronously.  Hence the buffer implementation must be
protected against race conditions in which the tasks access the
implementation in an interleaved manner and thereby corrupt the
representation.  In addition to this “mutually exclusive access”,
the buffer also requires “condition synchronization”, in which
callers are kept waiting until the requested buffer has the
necessary state.  For example, a task cannot remove an item from
a buffer when the buffer is empty.  Likewise, an item cannot be
put into a buffer when the buffer is full.</p>



<p>Prior to Ada 95, programmers wanting to write portable code had
to use the rendezvous to achieve mutual exclusion, with guards to
implement the condition synchronization, because no other
synchronization mechanism was provided by the language.  Although
the extended rendezvous has a number of advantages and was a step
forward in language design, it has significant overhead when
compared to lower-level mechanisms such as semaphores, and is a
synchronous mechanism as well.  (Ada 80 had a built-in
“Semaphore” task type, intended to be implemented efficiently and
used as the name suggests, but mixing the higher-level rendezvous
with the much lower-level semaphore abstraction was considered
poor language design.) In addition, the rendezvous is only
available between tasks, meaning that the buffer would have to be
implemented as a task too, like the accessing threads.  As a
result, inserting and removing items would involve expensive task
switching, which is the primary source of the comparative
inefficiency.</p>



<p>The protected type construct added in Ada 95 addresses this issue
directly.  Protected types provide efficient mutually exclusive
access to encapsulated data, with direct expression of condition
synchronization when required.  Protected types do not define
threads of control, so their use does not involve task switching,
and although they do more than simple semaphores, their overhead
is comparable.</p>


The GNAT hierarchy of packages includes the generic package
GNAT.Bounded_Buffers, providing just the sort of abstraction we
have in mind, parameterized for general use.  The implementation
of the bounded buffer will be that of an array, and we will do
assignments of the values held within any given buffer, so the
generic formal type representing the values is declared as
private, but not limited private or indefinite:</p>


<pre>

<b>generic</b>
   <b>type</b> Element <b>is</b> <b>private</b>;
<b>package</b> GNAT.Bounded_Buffers <b>is</b>
</pre>


<p>Given this generic formal profile, users can instantiate the
generic as required.  For example, given an appropriate generic
actual parameter type named “Job”, we could instantiate it as
follows:</p>

<pre>

   <b>package</b> Jobs <b>is</b> <b>new</b> GNAT.Bounded_Buffers (Element =&gt; Job);
</pre>


<p>The package declaration contains a pragma Pure so
that the generic can be used during library unit elaboration
without a potential access-before-elaboration problem.  That
effect is achieved because Pure units are preelaborated, in
addition to other semantics.</p>



<p>Next the package declares the array type used internally in the
representation of the bounded buffer type:</p>

<pre>

   <b>type</b> Content <b>is</b> <b>array</b> (Positive <b>range</b> &lt;&gt;) <b>of</b> Element;
</pre>


<p>The array type must be declared outside the protected type,
rather than inside in the private part as a hidden implementation
artifact.  This is an unfortunate holdover from the fact that
protected types were originally named “protected records”, with
record type semantics: record types cannot declare such things as
other types!  This limitation was known during the Ada 2005 revision
but other revision aspects were more important, so this
undesirable restriction remains.</p>



<p>The next declaration in the package is a constant value of type
System.Priority:</p>

<pre>

   Default_Ceiling : <b>constant</b> System.Priority := System.Default_Priority;
</pre>


<p>In a real-time application using the Real-Time Systems Annex,
protected types are given a “ceiling” priority.  The constant
declared here is a default for that purpose so that applications
not using that Annex can ignore this aspect.</p>



<p>Finally the package declares the protected type itself, with two
discriminants:</p>

<pre>

   <b>protected</b> <b>type</b> Bounded_Buffer
      (Capacity : Positive;
       Ceiling  : System.Priority)
   <b>is</b>
      <b>pragma</b> Priority (Ceiling);
</pre>


<p>The first discriminant is the capacity of the instance object,
that is, the maximum number of values it can contain.  This value
will be used in the declaration of a hidden array object of type
Content.  With this approach, different objects of the one buffer
type can have different capacities.  The second discriminant
represents the ceiling priority value, used in the pragma
Priority.  This is where the Default_Ceiling constant would be
used in non-real-time applications.  Note that we cannot use the
Default_Ceiling constant as a default discriminant value because
the language does not allow some discriminants to have defaults
unless all have defaults.</p>



<p>Continuing with our “Jobs” example instantiation, declaration of
a bounded buffer specifies these discriminant values:</p>

<pre>

   Buffer : Jobs.Bounded_Buffer (Capacity =&gt; 20,
                                 Ceiling =&gt; Jobs.Default_Ceiling);
</pre>


<p>In this example we have arbitrarily set the capacity of Buffer to
20.  Note that the Bounded_Buffer type is provided directly as a
protected type, rather than as a limited private type completed
with a protected type.  With this approach, clients have full
flexibility to do all that protected types allow, such as timed
and conditional calls.
</p>


<p>Next the protected type declares the visible operations.  The two
primary operations are Insert and Remove, defined as entries for
the sake of the barriers that specify the required condition
synchronization.  (Only protected entries can have barriers,
unlike protected procedures and functions.) The barriers express the “not full”
and “not empty” conditions and keep their callers waiting until
those conditions hold.</p>

<pre>

      <b>entry</b> Insert (Item : Element);
      <b>entry</b> Remove (Item : <b>out</b> Element);
</pre>


<p>Then three functions are declared.  The names “Empty” and “Full”
describe the purpose of the first two functions.  The third,
“Extent”, returns the number of elements currently held in the
buffer.  It is worth noting that the state of a buffer to which
these functions may be applied can change immediately after the
call returns.</p>

<pre>

      <b>function</b> Empty <b>return</b> Boolean;
      <b>function</b> Full <b>return</b> Boolean;
      <b>function</b> Extent <b>return</b> Natural;
</pre>


<p>In part two of this Gem we will explore the private part of the
protected type, the package body, and the body of the protected
type. </p>

<h3>Related Source Code</h3>

<p>Ada Gems example files are distributed by AdaCore and may be used or modified for any purpose without restrictions.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/05/12/gem-35/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Gem #34: Safe and Secure Software : Chapter 2, Safe Typing</title>
		<link>http://www.adacore.com/2008/05/05/gem-34/</link>
		<comments>http://www.adacore.com/2008/05/05/gem-34/#comments</comments>
		<pubDate>Mon, 05 May 2008 10:00:22 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<category>Devt log - Gem of the Week</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/05/05/gem-34/</guid>
		<description><![CDATA[This week's gem is the second chapter of John Barnes' new booklet:</p> 

<p>Safe and Secure Software: An Introduction to Ada 2005.</p>

<p>
Over the coming months, we will be publishing all thirteen chapters of the booklet. In the attachment at the bottom of Gem #30 you can access the contents and bibliography for the entire booklet.

We hope you will enjoy the read!</p>]]></description>
			<content:encoded><![CDATA[<h3>Let&#8217;s get started&#8230;</h3>


<p>Safe typing is not about preventing heavy-handed use of the keyboard, although it can detect errors made by typos!</p>


<p>Safe typing is about designing the type structure of the language in order to prevent many common semantic errors. It is often known as strong typing.</p>


<p>Early languages such as Fortran and Algol treated all data as numeric types. Of course, at the end of the day, everything is indeed held in the computer as a numeric of some form, usually as an integer or floating point value and usually encoded using a binary representation. Later languages, starting with Pascal, began to recognize that there was merit in taking a more abstract view of the objects being manipulated. Even if they were ultimately integers, there was much benefit to be gained by treating colors as colors and not as integers by using enumeration types (just called scalar types in Pascal).</p>


<p>Ada take this idea much further as we shall see, but other languages still treat scalar types as just raw numeric types, and miss the critical idea of abstraction, which is to distinguish semantic intent from machine representation. The Ada approach provides more opportunities for detecting programming errors.
</p>

<h3>Read Chapter 2 in full</h3>


<p>Note: All chapters of this booklet will, in time, be available on the <a href="/home/ada_answers/ada_2005">Ada 2005 home page</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/05/05/gem-34/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Designing Safe and Secure Systems</title>
		<link>http://www.adacore.com/2008/04/29/designing-safe-and-secure-systems/</link>
		<comments>http://www.adacore.com/2008/04/29/designing-safe-and-secure-systems/#comments</comments>
		<pubDate>Tue, 29 Apr 2008 11:41:16 +0000</pubDate>
		<dc:creator>AdaCore</dc:creator>
		
		<category>Development Log</category>

		<category>Ada / Ada 2005</category>

		<guid isPermaLink="false">http://www2.adacore.com/2008/04/29/designing-safe-and-secure-systems/</guid>
		<description><![CDATA[Ben Brosgol&#8217;s tutorial at SSTC 2008, entitled &#8220;Safety and Security: An                                  
Analysis of Certification Issues and Technologies for High-Integrity    [...]]]></description>
			<content:encoded><![CDATA[<p>Ben Brosgol&#8217;s tutorial at SSTC 2008, entitled &#8220;Safety and Security: An                                  
Analysis of Certification Issues and Technologies for High-Integrity                                    
Software&#8221;.</p>
                                                                                                        
<p>Today&#8217;s interconnected critical systems must be both safe and secure; software developers and decision makers need to understand the operative certification standards and their implications on technology choice and                                 system development.  This presentation first summarizes the DO-178B avionics safety standard and the Common Criteria / Common Evaluation Methodology security standard.  It identifies the requirements that these                               standards impose on programming language technology and development tools, and explains how safety and security considerations are similar and how they differ.  It describes how modern programming language features such as                             
Object-Oriented Programming affect safety and security certification, and assesses several current language family approaches &#8212; C / C++, Ada / SPARK, and Java &#8212; against safety and security requirements. </p>]]></content:encoded>
			<wfw:commentRss>http://www.adacore.com/2008/04/29/designing-safe-and-secure-systems/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
