Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'jed/malloc-zero' into next

* jed/malloc-zero:
  PetscMalloc: allow ptr=malloc(0) and free(ptr)
  • Loading branch information...
commit 30fbb492a451a20a8beb5ed990668bda23515f98 2 parents 78855ac + 49d7da5
@jedbrown jedbrown authored
Showing with 9 additions and 12 deletions.
  1. +8 −6 include/petscsys.h
  2. +1 −6 src/sys/memory/mtr.c
View
14 include/petscsys.h
@@ -516,17 +516,17 @@ PETSC_EXTERN PetscErrorCode PetscCommDestroy(MPI_Comm*);
Level: beginner
- Notes: Memory is always allocated at least double aligned
+ Notes:
+ Memory is always allocated at least double aligned
- If you request memory of zero size it will allocate no space and assign the pointer to 0; PetscFree() will
- properly handle not freeing the null pointer.
+ It is safe to allocate size 0 and pass the resulting pointer (which may or may not be NULL) to PetscFree().
.seealso: PetscFree(), PetscNew()
Concepts: memory allocation
M*/
-#define PetscMalloc(a,b) ((a != 0) ? (*PetscTrMalloc)((a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,(void**)(b)) : (*(b) = 0,0) )
+#define PetscMalloc(a,b) ((*PetscTrMalloc)((a),__LINE__,PETSC_FUNCTION_NAME,__FILE__,(void**)(b)))
/*MC
PetscAddrAlign - Rounds up an address to PETSC_MEMALIGN alignment
@@ -1072,14 +1072,16 @@ M*/
Level: beginner
- Notes: Memory must have been obtained with PetscNew() or PetscMalloc()
+ Notes:
+ Memory must have been obtained with PetscNew() or PetscMalloc().
+ It is safe to call PetscFree() on a NULL pointer.
.seealso: PetscNew(), PetscMalloc(), PetscFreeVoid()
Concepts: memory allocation
M*/
-#define PetscFree(a) ((a) && ((*PetscTrFree)((void*)(a),__LINE__,PETSC_FUNCTION_NAME,__FILE__) || ((a) = 0,0)))
+#define PetscFree(a) ((*PetscTrFree)((void*)(a),__LINE__,PETSC_FUNCTION_NAME,__FILE__) || ((a) = 0,0))
/*MC
PetscFreeVoid - Frees memory
View
7 src/sys/memory/mtr.c
@@ -176,8 +176,6 @@ PetscErrorCode PetscTrMallocDefault(size_t a,int lineno,const char function[],c
PetscErrorCode ierr;
PetscFunctionBegin;
- if (!a) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to malloc zero size array");
-
if (TRdebugLevel) {
ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr);
}
@@ -257,10 +255,7 @@ PetscErrorCode PetscTrFreeDefault(void *aa,int line,const char function[],const
PetscFunctionBegin;
/* Do not try to handle empty blocks */
- if (!a) {
- (*PetscErrorPrintf)("PetscTrFreeDefault called from %s() line %d in %s\n",function,line,file);
- SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Trying to free null block: Free called from %s() line %d in %s\n",function,line,file);
- }
+ if (!a) PetscFunctionReturn(0);
if (TRdebugLevel) {
ierr = PetscMallocValidate(line,function,file);CHKERRQ(ierr);
Please sign in to comment.
Something went wrong with that request. Please try again.