comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Load an object from a file
Date: Fri, 03 Apr 2009 16:07:57 +0300
Date: 2009-04-03T16:07:57+03:00	[thread overview]
Message-ID: <49d60a26$0$22051$4f793bc4@news.tdc.fi> (raw)
In-Reply-To: <49d5fa88$0$2862$ba620e4c@news.skynet.be>

Olivier Scalbert wrote:
> Hello everybody !
> 
> In my Ada self-study context, I was asking myself how can I create and 
> fill objects or records from a file.
> As an example, I have tried to represent a java class file format 
> structure and fill it with a .class java file.
> 
> The ClassFile structure is something like:
> 
>     ClassFile {
>         u4 magic;
>         u2 minor_version;
>         u2 major_version;
>         u2 constant_pool_count;
>         cp_info constant_pool[constant_pool_count-1];
>         u2 access_flags;
>         u2 this_class;
>         u2 super_class;
>         u2 interfaces_count;
>         u2 interfaces[interfaces_count];
>         u2 fields_count;
>         field_info fields[fields_count];
>         u2 methods_count;
>         method_info methods[methods_count];
>         u2 attributes_count;
>         attribute_info attributes[attributes_count];
>     }
> 
> 
> JVM Specs can be found there:
> http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html
> 
> I have no problem to represent and to fill from file, the first four 
> fields.
> But I do not know what is the best (Ada) way of representing the array 
> of info constant_pool as the size is only known at run time.(= 
> constant_pool_cout).

You could use suitable container structures, for example 
Ada.Containers.Vectors. That would hide the storage management from 
your problem space.

There are two more basic methods:

1. Make the size components discriminants of the record, not
    ordinary components:

    type Constant_Pool_T is array (Positive range <>) of cp_info;
    ... etc types for the other arrays.

    type Class_File_T (
       Constant_Pool_Count : u2;
       Interfaces_Count    : u2;
       Fields_Count        : u2;
       Attributes_Count    : u2)
    is record
       ...
       Constant_Pool : Constant_Pool_T (1 .. Constant_Pool_Count);
       ...
       Attributes : Attributes_T (1 .. Attributes_Count);
    end record;

    In this method, you must know the sizes of the arrays
    before you can create the Class_File_T object. This could
    be hard in your case -- I assume you must read the components
    from a file in the order given in the record type, so you
    would have to read all constant-pool items before reading
    interfaces_count, for example.

    In this method it is more convenient to index the
    arrays starting from 1, not 0, because you cannot do
    arithmetic on the discriminants to define the array
    bounds; the "-1" is forbidden here:

    type Class_File_T ( ... ) is record
       ...
       Constant_Pool :
          Constant_Pool_T (0 .. Constant_Pool_Count - 1);
       ...
    end record;

2. Use heap allocation and access types:

    type Constant_Pool_T is array Natural range <>) of cp_info;
    type Constant_Pool_Ref is access Constant_Pool_T;

    type Class_File_T is record
       ...
       Constant_Pool : Constant_Pool_Ref;
       ...
       Attributes : Attributes_Ref;
    end record;

    In this method, you can create the Class_File_T object
    before you know the array sizes. The components Constant_Pool
    etc. will initially be null. When you know the size, you
    can create arrays of the right size on the heap:

       CF.Constant_Pool :=
          new Constant_Pool_T (0 .. constant_pool_count - 1);

HTH,

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  reply	other threads:[~2009-04-03 13:07 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-03 12:01 Load an object from a file Olivier Scalbert
2009-04-03 13:07 ` Niklas Holsti [this message]
2009-04-03 13:37 ` Ludovic Brenta
2009-04-03 15:19   ` Olivier Scalbert
2009-04-03 16:08     ` Georg Bauhaus
2009-04-03 16:22       ` Ludovic Brenta
2009-04-03 16:41         ` Olivier Scalbert
2009-04-03 16:46       ` Adam Beneschan
2009-04-03 20:22         ` Ludovic Brenta
2009-04-09 20:32   ` Olivier Scalbert
2009-04-09 21:22     ` Ludovic Brenta
2009-04-09 22:22       ` Olivier Scalbert
2009-04-19 13:08   ` Olivier Scalbert
2009-04-19 19:52     ` Ludovic Brenta
2009-04-19 20:27     ` Gautier
2009-04-03 13:41 ` Thomas Løcke
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox