comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve Adams" <s.adams_s_p_a_m_217@_s_p_a_m_ntlworld.csopam>
Subject: Re: C array to Ada pointer to unconstrained array without copyingmemory
Date: Thu, 9 Oct 2003 23:59:19 +0100
Date: 2003-10-09T23:59:19+01:00	[thread overview]
Message-ID: <pLlhb.1420$7b5.1241@newsfep1-gui.server.ntli.net> (raw)
In-Reply-To: mailman.51.1065630515.25614.comp.lang.ada@ada-france.org

Not sure, let me answer a different question :)

Fat pointer is, and forgive me for using C, but otherwise ada would need
type definitions for lots of things

typedef struct
{
    int *bounds;
    T *data;
} Fat_Pointer;

Then when you declare:

fp : Fat_Pointer := NULL;

it means that the pointers are actually null, the fp variable is the size of
the structure above, 2 32 bit values for ease.

when you do:

fp := new t(1..20);

then what happens in essence is:

allocate enough memory for 20*sizeof(T) + 2 * sizeof(int) (for bounds)

fp.bounds = 20*sizeof(T) + 2 * sizeof(int)
fp.bounds(1) := 1;
fp.bounds(2) := 20;
fp.data = (T*)(fp.bounds+2); -- O/S conversion, but basically move 2 int
sizes along in memory

When you deallocate you release the memory pointed to.

To take this to the C - Ada chat level, and using a thin pointer because of
'size applied:

void cf()
{
    int *data;
    data = malloc((N_ELEMENTS + 2)*sizeof(int);
    data[0] = 1;
    data[1] = N_ELEMENTS;
    data += 2;
    for (i =0; i < N_ELEMENTS; i++)
    {
        data[i] = i;
    }
    ada_func(data, data);
}

------
procedure ada_func(p_ia_1 : int_arr_ptr; p_ia_2 : in out int_arr) is
begin
    for i in p_ia_1'first .. p_ia_1'last loop
        text_io.put_line("p_ia_1 (" & integer'image(i) & "} = " &
integer'image(p_ia_1(i)) );
   end loop;
end ada_func;

I actually have code that does this and works (well something more useful
anway).

Objectada uses thin pointers by default. btw.

Freeing a fat pointer with unchecked_deallocation is a good question, I
would guess that it does free correctly. However its any ones guess outside
of Gnat whether the data is freed in one call or as two. If two I am in
trouble :)
However, every time i traced the use of these things the bounds were ALWAYS
immediately before the data in memory, suggesting a single allocation from
the heap.
If you use a thin pointer then I'd be pretty sure that you will always have
correct behavour as by definition the bounds must reside at the start of the
data in someway.


"Duncan Sands" <duncan.sands@math.u-psud.fr> wrote in message
news:mailman.51.1065630515.25614.comp.lang.ada@ada-france.org...
> Hi Steve, thanks for the interesting information.
>
> > For infomration the Fat pointer is a struct with two pointers, one to
the
> > bounds and one to the data. Surprise is that the layout of the bounds
and
> > data in memory is the same, so they are allocated in a single call, this
> > means thin and fat are the same at the basest level, you just have more
> > overhead for fats.
>
> Do you mean that the bounds are also stored directly before the data in
> memory?  If so, it should still be possible to have the two pointers point
> to quite different memory areas - after all, when you use an address
clause
> to say that the data is somewhere, the bounds will be on the stack and
thus
> not immediately before the data.  Does this mean that if you free a fat
> pointer using Unchecked_Deallocation, then only the memory pointed to by
> the bounds part is freed?  (Because the memory is allocated as one block,
> the bounds point to the start, the data a bit further along).
>
> Thanks again for your help - I'm glad someone understood my question.
>
> All the best,
>
> Duncan.





  reply	other threads:[~2003-10-09 22:59 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-07 14:43 C array to Ada pointer to unconstrained array without copying memory Duncan Sands
2003-10-07 18:30 ` tmoran
2003-10-07 19:24   ` Duncan Sands
2003-10-08  0:02   ` sk
     [not found]   ` <3F83540E.5000302@myob.com>
2003-10-08  9:18     ` Duncan Sands
2003-10-07 20:00 ` Jeffrey Carter
2003-10-07 20:39   ` Duncan Sands
2003-10-08  1:27     ` Jeffrey Carter
2003-10-07 20:53 ` Chad R. Meiners
2003-10-07 21:24   ` Duncan Sands
2003-10-07 22:44     ` C array to Ada pointer to unconstrained array without copyingmemory Chad R. Meiners
2003-10-07 22:52       ` Chad R. Meiners
2003-10-08  9:20         ` Duncan Sands
2003-10-08 16:16           ` C array to Ada pointer to unconstrained array withoutcopyingmemory Chad R. Meiners
2003-10-08 16:49             ` Duncan Sands
2003-10-08  2:14     ` C array to Ada pointer to unconstrained array without copying memory Robert I. Eachus
2003-10-08  9:27       ` Duncan Sands
2003-10-08 22:43         ` Robert I. Eachus
2003-10-09  9:31           ` Duncan Sands
2003-10-08 14:07 ` Steve Adams
2003-10-08 14:33   ` Preben Randhol
2003-10-09 23:04     ` Steve Adams
2003-10-09 23:11       ` Steve Adams
2003-10-08 16:30   ` Duncan Sands
2003-10-09 22:59     ` Steve Adams [this message]
2003-10-10  7:02       ` C array to Ada pointer to unconstrained array without copyingmemory Duncan Sands
2003-10-10 16:44         ` C array to Ada pointer to unconstrained array withoutcopyingmemory Steve Adams
2003-10-23 21:27   ` C array to Ada pointer to unconstrained array without copying memory Craig Carey
replies disabled

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