[petsc-dev] Allocating arrays
Jed Brown
jedbrown at mcs.anl.gov
Tue Dec 3 13:54:37 CST 2013
Jed Brown <jedbrown at mcs.anl.gov> writes:
> Barry Smith <bsmith at mcs.anl.gov> writes:
>> PetscNew() is PetscCalloc1(1,&beasty); the original analogy with C++
>> was that it creates a struct and initializes it to zero entries. :-)
>
> But C++ new does not initialize structs. It does calls constructors for
> non-POD types.
>
>> I’d like to keep it.
>
> Okay, it becomes
>
> PetscNew(&beasty);
'jed/malloc-array' is now pushed to 'next'. Other people merging to
'next' may encounter compilation errors if they have been using the
original macros. The commit message contains a shell script that can be
used to convert almost all normal use cases. Running
./malloc-convert.sh src
should be idempotent. There are two places in include/ that the script
would update, but where I'd rather stick with the original.
Since this change is somewhat disruptive, I would like to merge it to
'master' as soon as possible. If you are significantly affected by this
change, go ahead and merge 'jed/malloc-array' into your branch, then run
the attached script.
commit 38506d2d16b2b06013b0dc5205b1648e494db573
Merge: 335e720 b00a911
Author: Jed Brown <jedbrown at mcs.anl.gov>
Date: Tue Dec 3 13:35:29 2013 -0600
Merge branch 'jed/malloc-array' into next
* jed/malloc-array:
Sys: drop explicit type arguments from PetscNew() and PetscNewLog()
Sys: add PetscCalloc[1-7]
Sys: add PetscMalloc1 macro, array allocation without redundant types
PetscMalloc[2-7]: remove type arguments, infer from pointer type
SNESComputeJacobianDefaultColor: fix uninitialized variable
Conflicts:
src/sys/objects/mpinit.c
cat > malloc-convert.sh <<EOF
git grep -l PetscMalloc[2-7] $1 | xargs perl -pi \
-e '
s at PetscMalloc2\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc2($1,$3,$4,$6)@;
s at PetscMalloc3\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc3($1,$3,$4,$6,$7,$9)@;
s at PetscMalloc4\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc4($1,$3,$4,$6,$7,$9,$10,$12)@;
s at PetscMalloc5\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc5($1,$3,$4,$6,$7,$9,$10,$12,$13,$15)@;
s at PetscMalloc6\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc6($1,$3,$4,$6,$7,$9,$10,$12,$13,$15,$16,$18)@;
s at PetscMalloc7\(([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+),([^,;()]+)\)@PetscMalloc7($1,$3,$4,$6,$7,$9,$10,$12,$13,$15,$16,$18,$19,$21)@;
'
git grep -l 'PetscMalloc(.*sizeof' -- $1 | xargs perl -pi -e 's at PetscMalloc\(([^,;]*[^,; ]) *\* *sizeof\([^,;()]+\), at PetscMalloc1($1,@'
git grep -l 'PetscNew\(Log\)\?(' -- $1 | xargs perl -pi -e '
s at PetscNew\([^,;()]+ *, *@PetscNew(@;
s at PetscNewLog\(([^,;()]+) *,[^,;()]+, *@PetscNewLog($1,@'
EOF
malloc-convert.sh src
I have also attached the script.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: malloc-convert.sh
Type: text/x-sh
Size: 1641 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20131203/b54bef9b/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20131203/b54bef9b/attachment.sig>
More information about the petsc-dev
mailing list