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: struct _p_Object {
  8:   PETSCHEADER(int);
  9: };

 11: PetscErrorCode PetscObjectDestroy_PetscObject(PetscObject obj)
 12: {
 16:   if (--obj->refct > 0) return(0);
 17:   PetscHeaderDestroy(obj);
 18:   return(0);
 19: }

 23: /*@C
 24:    PetscObjectCreate - Creates a PetscObject

 26:    Collective on PetscObject

 28:    Input Parameter:
 29: .  comm - An MPI communicator

 31:    Output Parameter:
 32: .  obj - The object

 34:    Level: developer

 36:    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject()
 37:           to make new object classes.

 39:     Concepts: destroying object
 40:     Concepts: freeing object
 41:     Concepts: deleting object

 43: @*/
 44: PetscErrorCode  PetscObjectCreate(MPI_Comm comm, PetscObject *obj)
 45: {
 46:   PetscObject    o;

 51: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 52:   PetscInitializePackage(PETSC_NULL);
 53: #endif
 54:   PetscHeaderCreate(o,_p_PetscObject,-1,PETSC_OBJECT_COOKIE,0,"PetscObject",comm,PetscObjectDestroy_PetscObject,0);
 55:   /* records not yet defined in PetscObject 
 56:   o->data        = 0;
 57:   o->setupcalled = 0;
 58:   */
 59:   *obj = o;
 60:   return(0);
 61: }

 65: /*@C
 66:    PetscObjectCreateGeneric - Creates a PetscObject

 68:    Collective on PetscObject

 70:    Input Parameter:
 71: +  comm - An MPI communicator
 72: .  cookie - The class cookie
 73: -  name - The class name

 75:    Output Parameter:
 76: .  obj - The object

 78:    Level: developer

 80:    Notes: This is a template intended as a starting point to cut and paste with PetscObjectDestroy_PetscObject()
 81:           to make new object classes.

 83:     Concepts: destroying object
 84:     Concepts: freeing object
 85:     Concepts: deleting object

 87: @*/
 88: PetscErrorCode  PetscObjectCreateGeneric(MPI_Comm comm, PetscCookie cookie, const char name[], PetscObject *obj)
 89: {
 90:   PetscObject    o;

 95: #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
 96:   PetscInitializePackage(PETSC_NULL);
 97: #endif
 98:   PetscHeaderCreate(o,_p_PetscObject,-1,cookie,0,name,comm,PetscObjectDestroy_PetscObject,0);
 99:   /* records not yet defined in PetscObject 
100:   o->data        = 0;
101:   o->setupcalled = 0;
102:   */
103:   *obj = o;
104:   return(0);
105: }

109: /*@
110:    PetscObjectDestroy - Destroys any PetscObject, regardless of the type. 

112:    Collective on PetscObject

114:    Input Parameter:
115: .  obj - any PETSc object, for example a Vec, Mat or KSP.
116:          This must be cast with a (PetscObject), for example, 
117:          PetscObjectDestroy((PetscObject)mat);

119:    Level: beginner

121:     Concepts: destroying object
122:     Concepts: freeing object
123:     Concepts: deleting object

125: @*/
126: PetscErrorCode  PetscObjectDestroy(PetscObject obj)
127: {

132:   if (obj->bops->destroy) {
133:     (*obj->bops->destroy)(obj);
134:   } else {
135:     SETERRQ1(PETSC_ERR_PLIB,"This PETSc object of class %s does not have a generic destroy routine",obj->class_name);
136:   }
137:   return(0);
138: }

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

145:    Collective on PetscObject

147:    Input Parameters:
148: +  obj - any PETSc object, for example a Vec, Mat or KSP.
149:          This must be cast with a (PetscObject), for example, 
150:          PetscObjectView((PetscObject)mat,viewer);
151: -  viewer - any PETSc viewer

153:    Level: intermediate

155: @*/
156: PetscErrorCode  PetscObjectView(PetscObject obj,PetscViewer viewer)
157: {

162:   if (!viewer) {
163:     PetscViewerASCIIGetStdout(obj->comm,&viewer);
164:   }

167:   if (obj->bops->view) {
168:     (*obj->bops->view)(obj,viewer);
169:   } else {
170:     SETERRQ(PETSC_ERR_SUP,"This PETSc object does not have a generic viewer routine");
171:   }
172:   return(0);
173: }

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

180:    Not Collective

182:    Input Parameters:
183: +  obj - any PETSc object, for example a Vec, Mat or KSP.
184:          This must be cast with a (PetscObject), for example, 
185:          PetscObjectDestroy((PetscObject)mat);
186: -  type_name - string containing a type name

188:    Output Parameter:
189: .  same - PETSC_TRUE if they are the same, else PETSC_FALSE
190:   
191:    Level: intermediate

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

195:    Concepts: comparing^object types
196:    Concepts: types^comparing
197:    Concepts: object type^comparing

199: @*/
200: PetscErrorCode  PetscTypeCompare(PetscObject obj,const char type_name[],PetscTruth *same)
201: {

205:   if (!obj) {
206:     *same = PETSC_FALSE;
207:   } else if (!type_name && !obj->type_name) {
208:     *same = PETSC_TRUE;
209:   } else if (!type_name || !obj->type_name) {
210:     *same = PETSC_FALSE;
211:   } else {
215:     PetscStrcmp((char*)(obj->type_name),type_name,same);
216:   }
217:   return(0);
218: }

220: #define MAXREGDESOBJS 256
221: static int         PetscObjectRegisterDestroy_Count = 0;
222: static PetscObject PetscObjectRegisterDestroy_Objects[MAXREGDESOBJS];

226: /*@C
227:    PetscObjectRegisterDestroy - Registers a PETSc object to be destroyed when
228:      PetscFinalize() is called.

230:    Collective on PetscObject

232:    Input Parameter:
233: .  obj - any PETSc object, for example a Vec, Mat or KSP.
234:          This must be cast with a (PetscObject), for example, 
235:          PetscObjectRegisterDestroy((PetscObject)mat);

237:    Level: developer

239:    Notes:
240:       This is used by, for example, PETSC_VIEWER_XXX_() routines to free the viewer
241:     when PETSc ends.

243: .seealso: PetscObjectRegisterDestroyAll()
244: @*/
245: PetscErrorCode  PetscObjectRegisterDestroy(PetscObject obj)
246: {
249:   if (PetscObjectRegisterDestroy_Count < MAXREGDESOBJS) {
250:     PetscObjectRegisterDestroy_Objects[PetscObjectRegisterDestroy_Count++] = obj;
251:   } else {
252:     SETERRQ1(PETSC_ERR_PLIB,"No more room in array, limit %d \n recompile src/sys/objects/destroy.c with larger value for MAXREGDESOBJS\n",MAXREGDESOBJS);
253: 
254:   }
255:   return(0);
256: }

260: /*@C
261:    PetscObjectRegisterDestroyAll - Frees all the PETSc objects that have been registered
262:      with PetscObjectRegisterDestroy(). Called by PetscFinalize()

264:    Collective on individual PetscObjects

266:    Level: developer

268: .seealso: PetscObjectRegisterDestroy()
269: @*/
270: PetscErrorCode  PetscObjectRegisterDestroyAll(void)
271: {
273:   int i;

276:   for (i=0; i<PetscObjectRegisterDestroy_Count; i++) {
277:     PetscObjectDestroy(PetscObjectRegisterDestroy_Objects[i]);
278:   }
279:   PetscObjectRegisterDestroy_Count = 0;
280:   return(0);
281: }