comp.lang.ada
 help / color / mirror / Atom feed
* Does mmap gives a right result?!
@ 2010-10-03 11:27 Francesco PIRANEO GIULIANO
  2010-10-03 17:04 ` Jeffrey Carter
                   ` (3 more replies)
  0 siblings, 4 replies; 12+ messages in thread
From: Francesco PIRANEO GIULIANO @ 2010-10-03 11:27 UTC (permalink / raw)


Hi all,

always trying to handle linux framebuffer with ada; big goal reached
yesterday accessing fixed and variable data structures importing and
executing ioctl functions.

Now I have to draw inside the framebuffer and I've imported mmap as
follows:

   function mmap(addr : in u32; length : in u32; prot : in mmap_prot;
flags : in mmap_flag; fd : in file_id; offset : in u32) return
Void_Ptr;
   pragma import( C, mmap, "mmap" );
   pragma import_function(mmap);

Void_Ptr is defined as follows:

   type void is mod System.Memory_Size;
   for void'Size use System.Word_Size;

   type Void_Ptr is access all Void;
   pragma Convention (C, Void_Ptr);

The lines above has been derived by:
http://en.wikibooks.org/wiki/Ada_Programming/Types/access#Where_is_void.2A.3F

Finally, the mmap has been called as follows:

      fbindex := mmap(0, fbhandler.fixinfo.smem_len,
fbhandler.PROT_READ + fbhandler.PROT_WRITE, fbhandler.MAP_FILE +
fbhandler.MAP_SHARED, fbid, 0);

NOTE: I defined PROT_READ and so on as constants in my code;

First question: I have to analyze if fbindex is /= -1 otherwise I have
to raise an exception:

      if integer(fbindex) = -1 then
	     raise BADMMAPRESULT;
      end if;

...seems to be the most logical comparision BUT the following error in
compile phase appears: "illegal operand for numeric conversion" -- Any
help is really apreciated here! :-)

When commenting out the above instructions and starting the compiled
application, everything goes right except, when I try to draw inside
the first location the program hungs with the following:

raised STORAGE_ERROR : stack overflow (or erroneous memory access)

I'm quite sure my mmap doesn't run properly. I'm about to makes all
framebuffer's interfacing functions in C then implement the remaining
in Ada but it will be a nice things if everything can be made with the
same language.

Any clues or opinion is welcome about.

Francesco



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 11:27 Does mmap gives a right result?! Francesco PIRANEO GIULIANO
@ 2010-10-03 17:04 ` Jeffrey Carter
  2010-10-03 17:49 ` Björn
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 12+ messages in thread
From: Jeffrey Carter @ 2010-10-03 17:04 UTC (permalink / raw)


On 10/03/2010 04:27 AM, Francesco PIRANEO GIULIANO wrote:
>
>        fbindex := mmap(0, fbhandler.fixinfo.smem_len,
> fbhandler.PROT_READ + fbhandler.PROT_WRITE, fbhandler.MAP_FILE +
> fbhandler.MAP_SHARED, fbid, 0);
>
>        if integer(fbindex) = -1 then
> 	     raise BADMMAPRESULT;
>        end if;
>
> ...seems to be the most logical comparision BUT the following error in
> compile phase appears: "illegal operand for numeric conversion" -- Any
> help is really apreciated here! :-)

Fbindex is an object of an access type. Access values are not numeric, and there 
is no conversion defined to numeric types for them.

Is mmap really defined as returning void*?

You could use Unchecked_Conversion to treat the internal representation of 
Fbindex as the representation of an Integer.

You could define Mmap to return Interfaces.C.Int.

> When commenting out the above instructions and starting the compiled
> application, everything goes right except, when I try to draw inside
> the first location the program hungs with the following:
>
> raised STORAGE_ERROR : stack overflow (or erroneous memory access)

I'm not familiar with mmap, but it appears you are trying to access memory 
address zero, which is protected.

-- 
Jeff Carter
"What lazy lout left these wires all over the lawn?"
Poppy
98



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 11:27 Does mmap gives a right result?! Francesco PIRANEO GIULIANO
  2010-10-03 17:04 ` Jeffrey Carter
@ 2010-10-03 17:49 ` Björn
  2010-10-03 19:22   ` Francesco PIRANEO GIULIANO
  2010-10-04  8:05 ` Ludovic Brenta
  2010-10-04  9:06 ` Jacob Sparre Andersen
  3 siblings, 1 reply; 12+ messages in thread
From: Björn @ 2010-10-03 17:49 UTC (permalink / raw)


> I'm quite sure my mmap doesn't run properly. I'm about to makes all
> framebuffer's interfacing functions in C then implement the remaining
> in Ada but it will be a nice things if everything can be made with the
> same language.
>
> Any clues or opinion is welcome about.

Did you look at the GNAT Component Collection from AdaCore?
http://libre.adacore.com/libre/tools/gnat-component-collection/

The mmap functions that it provides worked well for me.

Björn



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 17:49 ` Björn
@ 2010-10-03 19:22   ` Francesco PIRANEO GIULIANO
  2010-10-03 19:32     ` Shark8
  0 siblings, 1 reply; 12+ messages in thread
From: Francesco PIRANEO GIULIANO @ 2010-10-03 19:22 UTC (permalink / raw)


Bjorn,
Jeff,

Thank you for your answer.

I'm going to download and try GNAT component collection; before this
Jeff I think you have reason:

The original C code looks like:
fbuffer = mmap(NULL, fix.smem_len, PROT_READ | PROT_WRITE, MAP_FILE |
MAP_SHARED, fb_fd, 0);

and I translated it into:
fbindex := mmap(0, fbhandler.fixinfo.smem_len, fbhandler.PROT_READ +
fbhandler.PROT_WRITE, fbhandler.MAP_FILE + fbhandler.MAP_SHARED, fbid,
0);

...not the same! :-) Maybe if I'll try to change the "0" to "NULL" it
will works... maybe!

There is an equivalence of the C's NULL in Ada?

Thank you again.
Francesco



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 19:22   ` Francesco PIRANEO GIULIANO
@ 2010-10-03 19:32     ` Shark8
  2010-10-11  3:03       ` David Thompson
  0 siblings, 1 reply; 12+ messages in thread
From: Shark8 @ 2010-10-03 19:32 UTC (permalink / raw)


On Oct 3, 1:22 pm, Francesco PIRANEO GIULIANO
<liste.fpira...@gmail.com> wrote:
> Bjorn,
> Jeff,
>
> Thank you for your answer.
>
> I'm going to download and try GNAT component collection; before this
> Jeff I think you have reason:
>
> The original C code looks like:
> fbuffer = mmap(NULL, fix.smem_len, PROT_READ | PROT_WRITE, MAP_FILE |
> MAP_SHARED, fb_fd, 0);
>
> and I translated it into:
> fbindex := mmap(0, fbhandler.fixinfo.smem_len, fbhandler.PROT_READ +
> fbhandler.PROT_WRITE, fbhandler.MAP_FILE + fbhandler.MAP_SHARED, fbid,
> 0);
>
> ...not the same! :-) Maybe if I'll try to change the "0" to "NULL" it
> will works... maybe!
>
> There is an equivalence of the C's NULL in Ada?
>
> Thank you again.
> Francesco

Like too many things in C NULL is somewhat ill-defined; do you mean
the character-sized all-zero value used to terminate strings, or the
all-zero valued memory-address sized pointer value? By the context
here it is obvious that the later is what you're asking about but Ada
provides both: There is the Character NUL and the "null" keyword
which, in access-terms, is the null-pointer, the "null" keyword
resolves down to other meanings depending on the context: the null-
statement, a null-record, etc.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 11:27 Does mmap gives a right result?! Francesco PIRANEO GIULIANO
  2010-10-03 17:04 ` Jeffrey Carter
  2010-10-03 17:49 ` Björn
@ 2010-10-04  8:05 ` Ludovic Brenta
  2010-10-04 21:11   ` Francesco PIRANEO GIULIANO
  2010-10-04  9:06 ` Jacob Sparre Andersen
  3 siblings, 1 reply; 12+ messages in thread
From: Ludovic Brenta @ 2010-10-04  8:05 UTC (permalink / raw)


Francesco PIRANEO GIULIANO wrote on comp.lang.ada:
>[...] I've imported mmap as follows:
>
>    function mmap(addr : in u32; length : in u32; prot : in mmap_prot;
> flags : in mmap_flag; fd : in file_id; offset : in u32) return
> Void_Ptr;
>    pragma import( C, mmap, "mmap" );
>    pragma import_function(mmap);
>
> Void_Ptr is defined as follows:
>
>    type void is mod System.Memory_Size;
>    for void'Size use System.Word_Size;
>
>    type Void_Ptr is access all Void;
>    pragma Convention (C, Void_Ptr);
>
> The lines above has been derived by:http://en.wikibooks.org/wiki/Ada_Programming/Types/access#Where_is_vo...
>
> Finally, the mmap has been called as follows:
>
>       fbindex := mmap(0, fbhandler.fixinfo.smem_len,
> fbhandler.PROT_READ + fbhandler.PROT_WRITE, fbhandler.MAP_FILE +
> fbhandler.MAP_SHARED, fbid, 0);
>
> NOTE: I defined PROT_READ and so on as constants in my code;
>
> First question: I have to analyze if fbindex is /= -1 otherwise I have
> to raise an exception:
>
>       if integer(fbindex) = -1 then
>              raise BADMMAPRESULT;
>       end if;
>
> ...seems to be the most logical comparision BUT the following error in
> compile phase appears: "illegal operand for numeric conversion" -- Any
> help is really apreciated here! :-)
>
> When commenting out the above instructions and starting the compiled
> application, everything goes right except, when I try to draw inside
> the first location the program hungs with the following:
>
> raised STORAGE_ERROR : stack overflow (or erroneous memory access)
>
> I'm quite sure my mmap doesn't run properly. I'm about to makes all
> framebuffer's interfacing functions in C then implement the remaining
> in Ada but it will be a nice things if everything can be made with the
> same language.
>
> Any clues or opinion is welcome about.

Since mmap returns (void*) -1 or a "valid" void pointer, I would
import it into Ada as a function returning
System.Storage_Elements.Integer_Address, which you can compare against
-1 and convert to a System.Address using
System.Storage_Elements.To_Address. For example:

function mmap (Hint : in System.Address; Length : in
System.Storage_Elements.Storage_Count;
  Prot : in mmap_prot; Flags : in mmap_flag; fd : in file_id; offset :
in u32)
  return System.Storage_Elements.Integer_Address;
pragma Import (C, mmap, "mmap");

But all this work (the definition of constants for the flags, the
import, the comparison with -1, the raising of an exception and the
conversion to System.Address) has already been done in Florist, see
the function POSIX.Memory_Mapping.Map_Memory. I suggest you simply
"aptitude install libflorist2009-dev", add "with "florist";" to your
project file and compile...

Once you have the address, you can either:

(1) declare the object contained in the file with an address
representation clause, or
(2) convert the address to an access value using an instance of
System.Address_To_Access_Conversions.

HTH

PS. Jacob has nice stories to tell about this function...

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 11:27 Does mmap gives a right result?! Francesco PIRANEO GIULIANO
                   ` (2 preceding siblings ...)
  2010-10-04  8:05 ` Ludovic Brenta
@ 2010-10-04  9:06 ` Jacob Sparre Andersen
  2010-10-04 21:08   ` Francesco PIRANEO GIULIANO
  3 siblings, 1 reply; 12+ messages in thread
From: Jacob Sparre Andersen @ 2010-10-04  9:06 UTC (permalink / raw)


Francesco PIRANEO GIULIANO wrote:

> Now I have to draw inside the framebuffer and I've imported mmap [...]

Is there any special reason that you don't use the POSIX Ada API?  (In
Debian it is known as "libflorist".)

Greetings,

Jacob
-- 
�A corollary of Murphy's law is that duplicate information
 eventually becomes different information.  Putting both in
 the same file may slow down the process, but it will not
 prevent it.�                                 -- Wes Groleau



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-04  9:06 ` Jacob Sparre Andersen
@ 2010-10-04 21:08   ` Francesco PIRANEO GIULIANO
  2010-10-05  8:19     ` Ludovic Brenta
  2010-10-05 13:17     ` Jacob Sparre Andersen
  0 siblings, 2 replies; 12+ messages in thread
From: Francesco PIRANEO GIULIANO @ 2010-10-04 21:08 UTC (permalink / raw)


> > Now I have to draw inside the framebuffer and I've imported mmap [...]
>
> Is there any special reason that you don't use the POSIX Ada API?  (In
> Debian it is known as "libflorist".)

Jacob,
simply I don't know them... :-( Can you indicate me some
documentation / examples about?

Thank you very much!
Francesco



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-04  8:05 ` Ludovic Brenta
@ 2010-10-04 21:11   ` Francesco PIRANEO GIULIANO
  0 siblings, 0 replies; 12+ messages in thread
From: Francesco PIRANEO GIULIANO @ 2010-10-04 21:11 UTC (permalink / raw)


> But all this work (the definition of constants for the flags, the
> import, the comparison with -1, the raising of an exception and the
> conversion to System.Address) has already been done in Florist, see
> the function POSIX.Memory_Mapping.Map_Memory. I suggest you simply
> "aptitude install libflorist2009-dev", add "with "florist";" to your
> project file and compile...
>
> Once you have the address, you can either:
>....
> PS. Jacob has nice stories to tell about this function...

Ok... now I have some clues to think about! ;-)

Thank you very much!
Francesco



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-04 21:08   ` Francesco PIRANEO GIULIANO
@ 2010-10-05  8:19     ` Ludovic Brenta
  2010-10-05 13:17     ` Jacob Sparre Andersen
  1 sibling, 0 replies; 12+ messages in thread
From: Ludovic Brenta @ 2010-10-05  8:19 UTC (permalink / raw)


Francesco PIRANEO GIULIANO wrote on comp.lang.ada:
>>> Now I have to draw inside the framebuffer and I've imported mmap [...]
>>
>> Is there any special reason that you don't use the POSIX Ada API?  (In
>> Debian it is known as "libflorist".)
>
> Jacob,
> simply I don't know them... :-( Can you indicate me some
> documentation / examples about?

The best documentation about Florist is simply the sources (i.e. the
specs)
of Florist together with knowledge of the POSIX:

http://www.opengroup.org/onlinepubs/9699919799/

--
Ludovic Brenta.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-04 21:08   ` Francesco PIRANEO GIULIANO
  2010-10-05  8:19     ` Ludovic Brenta
@ 2010-10-05 13:17     ` Jacob Sparre Andersen
  1 sibling, 0 replies; 12+ messages in thread
From: Jacob Sparre Andersen @ 2010-10-05 13:17 UTC (permalink / raw)


Francesco PIRANEO GIULIANO wrote:

>> Is there any special reason that you don't use the POSIX Ada API?
>> (In Debian it is known as "libflorist".)
>
> Can you indicate me some documentation / examples about?

The source code and my lecture notes on the subject:

   http://edb.jacob-sparre.dk/Posix_in_Ada/

Jacob
-- 
Jacob Sparre Andersen Research & Innovation
Vesterbrogade 148K, 1. th.
1620 K�benhavn V
Danmark



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: Does mmap gives a right result?!
  2010-10-03 19:32     ` Shark8
@ 2010-10-11  3:03       ` David Thompson
  0 siblings, 0 replies; 12+ messages in thread
From: David Thompson @ 2010-10-11  3:03 UTC (permalink / raw)


On Sun, 3 Oct 2010 12:32:25 -0700 (PDT), Shark8
<onewingedshark@gmail.com> wrote:

> On Oct 3, 1:22�pm, Francesco PIRANEO GIULIANO
> <liste.fpira...@gmail.com> wrote:

> > There is an equivalence of the C's NULL in Ada?

> Like too many things in C NULL is somewhat ill-defined; do you mean
> the character-sized all-zero value used to terminate strings, or the
> all-zero valued memory-address sized pointer value? By the context
> here it is obvious that the later is what you're asking about but Ada
> provides both: There is the Character NUL and the "null" keyword
> which, in access-terms, is the null-pointer, the "null" keyword
> resolves down to other meanings depending on the context: the null-
> statement, a null-record, etc.

C is case-sensitive. 

It does use the term 'null' for both a character -- actually an
integer, since chars in C are just small integers; and a special
pointer value -- which traditionally is in fact zero, but the Standard
only requires that it acts like zero: a _constant_ zero in a pointer
context becomes a null pointer, and a (valid) pointer compared for
equality to zero is true iff the pointer is null, but the actual null
pointer representation need not be all zero bits, see 5 esp. 5.5 in
the comp.lang.c FAQ at usual places and www.c-faq.com .

But the macro named 'NULL' is only the pointer value. There is no
standard or even common name for the null character, which can be
written as a literal '\0' or just the integer literal 0.

<G?>



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-10-11  3:03 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-03 11:27 Does mmap gives a right result?! Francesco PIRANEO GIULIANO
2010-10-03 17:04 ` Jeffrey Carter
2010-10-03 17:49 ` Björn
2010-10-03 19:22   ` Francesco PIRANEO GIULIANO
2010-10-03 19:32     ` Shark8
2010-10-11  3:03       ` David Thompson
2010-10-04  8:05 ` Ludovic Brenta
2010-10-04 21:11   ` Francesco PIRANEO GIULIANO
2010-10-04  9:06 ` Jacob Sparre Andersen
2010-10-04 21:08   ` Francesco PIRANEO GIULIANO
2010-10-05  8:19     ` Ludovic Brenta
2010-10-05 13:17     ` Jacob Sparre Andersen

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