Skip to content

Commit

Permalink
Merge branch 'jed/malloc-zero' into next
Browse files Browse the repository at this point in the history
* jed/malloc-zero:
  PetscMalloc: allow ptr=malloc(0) and free(ptr)
  • Loading branch information
jedbrown committed Jan 30, 2014
2 parents 78855ac + 49d7da5 commit 30fbb49
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
14 changes: 8 additions & 6 deletions include/petscsys.h
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 1 addition & 6 deletions src/sys/memory/mtr.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 30fbb49

Please sign in to comment.