From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00, GUARANTEED_100_PERCENT autolearn=no autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: "Alejandro R. Mosteo" Newsgroups: comp.lang.ada Subject: Re: Interfaces.C.Strings chars_ptr memory management strategy Date: Wed, 30 May 2018 15:10:23 +0200 Organization: A noiseless patient Spider Message-ID: References: <6ff31dc6-31e7-4755-bda0-1b53fa02f31f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Wed, 30 May 2018 13:10:24 -0000 (UTC) Injection-Info: h2725194.stratoserver.net; posting-host="2bc59c283ce1cb3e0dd940b941c9e8f9"; logging-data="17156"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Pm5pNJkqTx8aJn3p7LqxL" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 Cancel-Lock: sha1:ymTXKiF797o1T2S7HtV/NpSTpp4= In-Reply-To: <6ff31dc6-31e7-4755-bda0-1b53fa02f31f@googlegroups.com> Content-Language: en-US Xref: reader02.eternal-september.org comp.lang.ada:52781 Date: 2018-05-30T15:10:23+02:00 List-Id: On 26/05/2018 00:22, NiGHTS wrote: > I am creating a binding to a C library that requires me to repeat the same function but with a string parameter that changes on each call. I don't want to have to keep creating and destroying string memory for all of these function calls. I would like to create the memory for the string once, allocate enough space so it doesn't need to grow, and reuse that memory for the function call every time I need to pass a new string to it. I'm currently using this: https://github.com/mosteo/cstrings which is not what you want since it allocates on every instance (although on the stack). Still, it might give you some ideas. I'm still unsure if that's 100% guaranteed to be safe; for the experts out here, the question is, in a call to a C function like this: Call_To_C_Function (Function_That_Returns_A_Limited_Tagged_Type (...) .Subprogram_That_Returns_A_C_Pointer_To_Data_In_The_Tagged_Type); Is the in-place built limited tagged type guaranteed to live during the call to the C function? (In other words, is the pointer safe (as long as the C side does not make a copy, of course)? My suspicion is that once the subprogram returns the pointer, the limited type can be optimized away before the call to the C side. It's not what I'm seeing now, but I don't want to depend on an erroneous assumption. Alex. > > The trick here is that whatever strategy I use must be compatible with C, so for instance using a storage pool would not be directly compatible with the C binding. > > Here is just a quick and sloppy idea I had on how to tackle my problem. > > str : chars_ptr := New_String (" "); > ... > Update (Item => str, Offset => 0, Str => "Some Param"); > ... > Update (Item => str, Offset => 0, Str => "Some Other Param"); > ... > Free (str); > > I find the first line quite ugly. I'm sure there is an easier way to create a large empty string but I can't seem to come up with an elegant way to do it. > > As far as the Update commands, will it act like strcpy() in C? If so I'd guess that this is an efficient technique. > > Thanks for your help! >