comp.lang.ada
 help / color / mirror / Atom feed
* Dynamically reallocated buffer
@ 2007-06-01  6:33 Maciej Sobczak
  2007-06-01  7:15 ` anon
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Maciej Sobczak @ 2007-06-01  6:33 UTC (permalink / raw)


I need a dynamically reallocated buffer of bytes, which I can extend
at run-time by appending new fragments to the whole buffer. The
purpose of the buffer is to pass it later to the subprogram that
writes it "en bloc" to some external device.

For those of you who know C++ I need something like:

vector<unsigned char> buffer;
// fill the buffer with push_back or insert at end
// ...
write_to_device(&buffer[0], buffer.size());

The problem is that Ada.Containers.Vectors does not provide the
necessary guarantees to be any useful in this context.

What are your suggestions?

--
Maciej Sobczak
http://www.msobczak.com/




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

* Re: Dynamically reallocated buffer
  2007-06-01  6:33 Dynamically reallocated buffer Maciej Sobczak
@ 2007-06-01  7:15 ` anon
  2007-06-01 13:04   ` Maciej Sobczak
  2007-06-01  7:30 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: anon @ 2007-06-01  7:15 UTC (permalink / raw)


In <1180679626.254857.59070@w5g2000hsg.googlegroups.com>,  Maciej Sobczak <see.my.homepage@gmail.com> writes:
>I need a dynamically reallocated buffer of bytes, which I can extend
>at run-time by appending new fragments to the whole buffer. The
>purpose of the buffer is to pass it later to the subprogram that
>writes it "en bloc" to some external device.
>
>For those of you who know C++ I need something like:
>
>vector<unsigned char> buffer;
>// fill the buffer with push_back or insert at end
>// ...
>write_to_device(&buffer[0], buffer.size());

Stay away from C++. Its the same as a girl on PMS.


>
>The problem is that Ada.Containers.Vectors does not provide the
>necessary guarantees to be any useful in this context.
>
>What are your suggestions?

Did this using Ada 83 for strings vectors for my first class.  Which 
shocked my prof because he said what I had in mine could not be 
done.  The code is at the ACM code server.  Using the code it can 
be modified to work for all Ada packages.





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

* Re: Dynamically reallocated buffer
  2007-06-01  6:33 Dynamically reallocated buffer Maciej Sobczak
  2007-06-01  7:15 ` anon
@ 2007-06-01  7:30 ` Dmitry A. Kazakov
  2007-06-01 13:08   ` Maciej Sobczak
  2007-06-01 11:32 ` Georg Bauhaus
  2007-06-01 14:49 ` Matthew Heaney
  3 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2007-06-01  7:30 UTC (permalink / raw)


On Thu, 31 May 2007 23:33:46 -0700, Maciej Sobczak wrote:

> I need a dynamically reallocated buffer of bytes, which I can extend
> at run-time by appending new fragments to the whole buffer. The
> purpose of the buffer is to pass it later to the subprogram that
> writes it "en bloc" to some external device.
> 
> For those of you who know C++ I need something like:
> 
> vector<unsigned char> buffer;
> // fill the buffer with push_back or insert at end
> // ...
> write_to_device(&buffer[0], buffer.size());
> 
> The problem is that Ada.Containers.Vectors does not provide the
> necessary guarantees to be any useful in this context.
> 
> What are your suggestions?

type Device_Buffer is array (Positiver range <>)
   of Interfaces.C.unsigned_char;

The maximal block size is usually known. If not, then device is
stream-oriented and blocks can be safely split into chucks of known size.
In that case Device_Buffer is a segmented buffer. In most cases you don't
need to realloc anything.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: Dynamically reallocated buffer
  2007-06-01  6:33 Dynamically reallocated buffer Maciej Sobczak
  2007-06-01  7:15 ` anon
  2007-06-01  7:30 ` Dmitry A. Kazakov
@ 2007-06-01 11:32 ` Georg Bauhaus
  2007-06-01 14:49 ` Matthew Heaney
  3 siblings, 0 replies; 10+ messages in thread
From: Georg Bauhaus @ 2007-06-01 11:32 UTC (permalink / raw)


Maciej Sobczak wrote:

> For those of you who know C++ I need something like:
> 
> vector<unsigned char> buffer;
> // fill the buffer with push_back or insert at end
> // ...
> write_to_device(&buffer[0], buffer.size());
> 


I'd use references to normal arrays and if I wanted to be sure that
a program can rely on realloc() I'd consider a simple storage pool
using this OS function. (GNAT has System.Memory with ready made
bindings on some platforms.)

Then just call new Flex_Buffer(as needed). Possibly behind some
Container like interface (or Controlled and Reserve_Capacity in
Initialize).



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

* Re: Dynamically reallocated buffer
  2007-06-01  7:15 ` anon
@ 2007-06-01 13:04   ` Maciej Sobczak
  0 siblings, 0 replies; 10+ messages in thread
From: Maciej Sobczak @ 2007-06-01 13:04 UTC (permalink / raw)


On 1 Cze, 09:15, a...@anon.org (anon) wrote:

> >vector<unsigned char> buffer;
> >// fill the buffer with push_back or insert at end
> >// ...
> >write_to_device(&buffer[0], buffer.size());
>
> Stay away from C++.

What is the problem with the code above?

> Its the same as a girl on PMS.

What is PMS?

--
Maciej Sobczak
http://www.msobczak.com/




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

* Re: Dynamically reallocated buffer
  2007-06-01  7:30 ` Dmitry A. Kazakov
@ 2007-06-01 13:08   ` Maciej Sobczak
  0 siblings, 0 replies; 10+ messages in thread
From: Maciej Sobczak @ 2007-06-01 13:08 UTC (permalink / raw)


On 1 Cze, 09:30, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:

> type Device_Buffer is array (Positiver range <>)
>    of Interfaces.C.unsigned_char;

I might want to pack it.

> The maximal block size is usually known. If not, then device is
> stream-oriented and blocks can be safely split into chucks of known size.

Yes.

> In that case Device_Buffer is a segmented buffer. In most cases you don't
> need to realloc anything.

Which means - in the worst case I need a list (or some other
container) of Device_Buffers. Yes, that makes sense.

Thank you,

--
Maciej Sobczak
http://www.msobczak.com/




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

* Re: Dynamically reallocated buffer
  2007-06-01  6:33 Dynamically reallocated buffer Maciej Sobczak
                   ` (2 preceding siblings ...)
  2007-06-01 11:32 ` Georg Bauhaus
@ 2007-06-01 14:49 ` Matthew Heaney
  2007-06-02 12:38   ` Stephen Leake
  3 siblings, 1 reply; 10+ messages in thread
From: Matthew Heaney @ 2007-06-01 14:49 UTC (permalink / raw)


On Jun 1, 2:33 am, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
>
> The problem is that Ada.Containers.Vectors does not provide the
> necessary guarantees to be any useful in this context.

Right, that container does not guarantee that the underlying structure
is a contiguous array (as is the case in C++).  Think of it not as an
unbounded array (which would a physical view), but rather as a
container that provides random access (which is the logical view).

In your case you'll probably have to use an unbounded array directly.
If you want a vector-like thing then you can just grab the vector
source code and modify it as you see fit.




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

* Re: Dynamically reallocated buffer
  2007-06-01 14:49 ` Matthew Heaney
@ 2007-06-02 12:38   ` Stephen Leake
  2007-06-03  9:52     ` Georg Bauhaus
  0 siblings, 1 reply; 10+ messages in thread
From: Stephen Leake @ 2007-06-02 12:38 UTC (permalink / raw)


Matthew Heaney <mheaney@on2.com> writes:

> On Jun 1, 2:33 am, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
>>
>> The problem is that Ada.Containers.Vectors does not provide the
>> necessary guarantees to be any useful in this context.
>
> Right, that container does not guarantee that the underlying structure
> is a contiguous array (as is the case in C++).  Think of it not as an
> unbounded array (which would a physical view), but rather as a
> container that provides random access (which is the logical view).

SAL.Poly.Unbounded_Arrays. http://stephe-leake.org/ada/sal.html

-- 
-- Stephe



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

* Re: Dynamically reallocated buffer
  2007-06-02 12:38   ` Stephen Leake
@ 2007-06-03  9:52     ` Georg Bauhaus
  2007-06-05  0:25       ` Stephen Leake
  0 siblings, 1 reply; 10+ messages in thread
From: Georg Bauhaus @ 2007-06-03  9:52 UTC (permalink / raw)


Stephen Leake wrote:
> Matthew Heaney <mheaney@on2.com> writes:
> 
>> On Jun 1, 2:33 am, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
>>> The problem is that Ada.Containers.Vectors does not provide the
>>> necessary guarantees to be any useful in this context.
>> Right, that container does not guarantee that the underlying structure
>> is a contiguous array (as is the case in C++).  Think of it not as an
>> unbounded array (which would a physical view), but rather as a
>> container that provides random access (which is the logical view).
> 
> SAL.Poly.Unbounded_Arrays. http://stephe-leake.org/ada/sal.html

How would I pass the array storage of an Array_Type object
to a byte copying function? (As in 
write_to_device(&buffer[0], buffer.size());)





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

* Re: Dynamically reallocated buffer
  2007-06-03  9:52     ` Georg Bauhaus
@ 2007-06-05  0:25       ` Stephen Leake
  0 siblings, 0 replies; 10+ messages in thread
From: Stephen Leake @ 2007-06-05  0:25 UTC (permalink / raw)


Georg Bauhaus <bauhaus@futureapps.de> writes:

> Stephen Leake wrote:
>> Matthew Heaney <mheaney@on2.com> writes:
>>
>>> On Jun 1, 2:33 am, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
>>>> The problem is that Ada.Containers.Vectors does not provide the
>>>> necessary guarantees to be any useful in this context.
>>> Right, that container does not guarantee that the underlying structure
>>> is a contiguous array (as is the case in C++).  Think of it not as an
>>> unbounded array (which would a physical view), but rather as a
>>> container that provides random access (which is the logical view).
>> SAL.Poly.Unbounded_Arrays. http://stephe-leake.org/ada/sal.html
>
> How would I pass the array storage of an Array_Type object
> to a byte copying function? (As in write_to_device(&buffer[0],
> buffer.size());)

That operation is not provided. You need to derive from Array_Type and
provide an access function that returns the address of the current
actual storage.

-- 
-- Stephe



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

end of thread, other threads:[~2007-06-05  0:25 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-06-01  6:33 Dynamically reallocated buffer Maciej Sobczak
2007-06-01  7:15 ` anon
2007-06-01 13:04   ` Maciej Sobczak
2007-06-01  7:30 ` Dmitry A. Kazakov
2007-06-01 13:08   ` Maciej Sobczak
2007-06-01 11:32 ` Georg Bauhaus
2007-06-01 14:49 ` Matthew Heaney
2007-06-02 12:38   ` Stephen Leake
2007-06-03  9:52     ` Georg Bauhaus
2007-06-05  0:25       ` Stephen Leake

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