2008/7/17 Peter W. Draper <[log in to unmask]>:
> On Thu, 17 Jul 2008, David Berry wrote:
>
>> 2008/7/16 Tim Jenness <[log in to unmask]>:
>>>
>>> If I call NDF_CLONE on an NDF that has previously mapped the data array,
>>> do
>>> I need to remap the data array for the new clone or is ndf_annul clever
>>> enough to not unmap the data array if there are remaining clones active?
>>> (so
>>> I can just copy the pointer of the mapped data array and associate it
>>> with
>>> the cloned identifier).
>>
>> I thought I knew the answer to this - that ndf_annul would only unmap
>> the array when no remaining cloned NDF identifiers have the data array
>> mapped. But in practice this seems not to be the case.
>>
>> In fortran, annulling an NDF identifier immediately causes CNF to
>> nullify the true C pointer associated with the corresponding fortran
>> integer, so CNF_PVAL returns NULL. This is perhaps to be expected.
>>
>> But in C, ndfMap returns the true C pointer rather than a CNF
>> identifier, so you would expect the returned pointer to remain valid.
>> But experiment shows that it doesn't. The following program suffers a
>> segvio in the second call to "aaa":
>>
>> #include "ndf.h"
>> #include <stdio.h>
>>
>> void aaa( int n, float *d, int *status );
>>
>> main(){
>> int status, indf, indf2, place, el;
>> void *ip;
>>
>> status = 0;
>>
>> ndfOpen( NULL, "fred", "Read", "Old", &indf, &place, &status );
>> ndfMap( indf, "Data", "_REAL", "Read", &ip, &el, &status );
>> aaa( el, (float *) ip, &status );
>>
>> ndfClone( indf, &indf2, &status );
>> ndfAnnul( &indf, &status );
>>
>> /* ndfMap( indf2, "Data", "_REAL", "Read", &ip, &el, &status );*/
>> aaa( el, (float *) ip, &status );
>>
>> ndfAnnul( &indf2, &status );
>> }
>> void aaa( int n, float *d, int *status ){
>> if( *status != 0 ) return;
>> printf("%g %g\n", d[ 0 ], d[ n - 1 ] );
>> }
>>
>> But if you uncomment the second call to ndfMap, it works.
>
> Which is exactly the expected behaviour, isn't it.
Errr, yes. What on earth was I talking about?
So, in answer to Tim's question, ndf_annul will unmap the data if none
of the remaining active identifiers for the NDF has the data array
mapped. So a call to ndf-map is needed for each identifier. But for
the second and subsequent identifiers, ndf_map will presumably be a
cheap operation since all it will do is set a flag indicating that the
array is mapped through the supplied identifier (it won't actually
need to map anything since the mapping has already been done via an
earlier identifier).
David
Calling NDF_CLONE gives
> you a new identifier, which has no active resources, like mapped data,
> associated with it. It's independent of the parent identifier, just like a
> section.
>
> So when you annul the parent identifier that fully releases the mapped data,
> since you've not mapped it through any other identifiers and the memory
> reference becomes invalid.
>
> Peter.
>
--
Note my change of e-mail address. Please send e-mail to
[log in to unmask] from now on.
|