Actual source code: destroy.c

      1: #define PETSC_DLL
      2: /*
      3:      Provides utility routines for manulating any type of PETSc object.
      4: */
     5:  #include petsc.h

      7: PetscCookie PETSC_OBJECT_COOKIE = 0;

      9: struct _p_Object {
     10:   PETSCHEADER(int);
     11: };

     13: PetscErrorCode PetscObjectDestroy_Private(PetscObject obj)
     14: {
     15:   return 0;
     16: }

     20: /*@C
     21:    PetscObjectCreate - Creates a PetscObject

     23:    Collective on PetscObject

     25:    Input Parameter:
     26: .  comm - An MPI communicator

     28:    Output Parameter:
     29: .  obj - The object

     31:    Level: beginner

     33:     Concepts: destroying object
     34:     Concepts: freeing object
     35:     Concepts: deleting object

     37: @*/
     38: PetscErrorCode PETSC_DLLEXPORT PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
     39: {
     40:   PetscObject    o;

     46: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
     47:   PetscInitializePackage(PETSC_NULL);
     48: #endif
     49:   PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_Private,0);
     50:   /* records not yet defined in PetscObject
     51:   o->data        = 0;
     52:   o->setupcalled = 0;
     53:   */
     54:   *obj = o;
     55:   return(0);
     56: }

     60: /*@C
     61:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type.

     63:    Collective on PetscObject

     65:    Input Parameter:
     66: .  obj - any PETSc object, for example a Vec, Mat or KSP.
     67:          This must be cast with a (PetscObject), for example,
     68:          PetscObjectDestroy((PetscObject)mat);

     70:    Level: beginner

     72:     Concepts: destroying object
     73:     Concepts: freeing object
     74:     Concepts: deleting object

     76: @*/
     77: PetscErrorCode PETSC_DLLEXPORT PetscObjectDestroy(PetscObject obj)
     78: {

     84:   if (obj->bops->destroy) {
     85:     (*obj->bops->destroy)(obj);
     86:   } else {
     87:     SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
     88:   }
     89:   return(0);
     90: }

     94: /*@C
     95:    PetscObjectView - Views any PetscObject, regardless of the type.

     97:    Collective on PetscObject

     99:    Input Parameters:
    100: +  obj - any PETSc object, for example a Vec, Mat or KSP.
    101:          This must be cast with a (PetscObject), for example,
    102:          PetscObjectView((PetscObject)mat,viewer);
    103: -  viewer - any PETSc viewer

    105:    Level: intermediate

    107: @*/
    108: PetscErrorCode PETSC_DLLEXPORT PetscObjectView(PetscObject obj,PetscViewer viewer)
    109: {

    114:   if (!viewer) viewer = PETSC_VIEWER_STDOUT_(obj->comm);

    117:   if (obj->bops->view) {
    118:     (*obj->bops->view)(obj,viewer);
    119:   } else {
    120:     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
    121:   }
    122:   return(0);
    123: }

    127: /*@C
    128:    PetscTypeCompare - Determines whether a PETSc object is of a particular type.

    130:    Not Collective

    132:    Input Parameters:
    133: +  obj - any PETSc object, for example a Vec, Mat or KSP.
    134:          This must be cast with a (PetscObject), for example,
    135:          PetscObjectDestroy((PetscObject)mat);
    136: -  type_name - string containing a type name

    138:    Output Parameter:
    139: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
    140:
    141:    Level: intermediate

    143: .seealso: VecGetType(), KSPGetType(), PCGetType(), SNESGetType()

    145:    Concepts: comparing^object types
    146:    Concepts: types^comparing
    147:    Concepts: object type^comparing

    149: @*/
    150: PetscErrorCode PETSC_DLLEXPORT PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
    151: {

    155:   if (!obj) {
    156:     *same = PETSC_FALSE;
    157:   } else if (!type_name && !obj->type_name) {
    158:     *same = PETSC_TRUE;
    159:   } else if (!type_name || !obj->type_name) {
    160:     *same = PETSC_FALSE;
    161:   } else {
    165:     PetscStrcmp((char*)(obj->type_name),type_name,same);
    166:   }
    167:   return(0);
    168: }

    170: static int         PetscObjectRegisterDestroy_Count = 0;
    171: static PetscObject PetscObjectRegisterDestroy_Objects[256];

    175: /*@C
    176:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
    177:      PetscFinalize() is called.

    179:    Collective on PetscObject

    181:    Input Parameter:
    182: .  obj - any PETSc object, for example a Vec, Mat or KSP.
    183:          This must be cast with a (PetscObject), for example,
    184:          PetscObjectRegisterDestroy((PetscObject)mat);

    186:    Level: developer

    188:    Notes:
    189:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
    190:     when PETSc ends.

    192: .seealso: PetscObjectRegisterDestroyAll()
    193: @*/
    194: PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroy(PetscObject obj)
    195: {
    198:   PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
    199:   return(0);
    200: }

    204: /*@C
    205:    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
    206:      with PetscObjectRegisterDestroy(). Called by PetscFinalize()
    207:      PetscFinalize() is called.

    209:    Collective on individual PetscObjects

    211:    Level: developer

    213: .seealso: PetscObjectRegisterDestroy()
    214: @*/
    215: PetscErrorCode PETSC_DLLEXPORT PetscObjectRegisterDestroyAll(void)
    216: {
    218:   int i;

    221:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
    222:     PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);
    223:   }
    224:   return(0);
    225: }