Date: 2022-03-31T04:49:13+00:00

Actual source code: petscfptimpl.h

      2: #if !defined(PETSCFPIMPL_H)
      3: #define PETSCFPIMPL_H
      4: #include <petscviewertypes.h>
      5: #include <petscsys.h>
      6: /*
      7:     Function pointer table that maps from function pointers to their string representation

     10: */
     11: typedef struct _n_PetscFPT* PetscFPT;
     12: struct _n_PetscFPT {
     13:   void     **functionpointer;
     14:   char     **functionname;
     15:   PetscInt count;
     16:   PetscInt tablesize;
     17: };
     18: PETSC_INTERN PetscFPT PetscFPTData;

     20: static inline PetscErrorCode PetscFPTView(PetscViewer viewer)
     21: {
     22:   if (PetscFPTData) {
     23:     for (PetscInt i = 0; i < PetscFPTData->tablesize; ++i) {
     24:       if (PetscFPTData->functionpointer[i]) {
     25:         printf("%s()\n",PetscFPTData->functionname[i]);
     26:       }
     27:     }
     28:   }
     29:   return 0;
     30: }

     32: static inline PetscErrorCode PetscFPTDestroy(void)
     33: {
     34:   PetscFPT data = PetscFPTData;

     36:   PetscFPTData = NULL;
     37:   if (!data) return 0;
     38:   PetscFree(data->functionpointer);
     39:   PetscFree(data->functionname);
     40:   PetscFree(data);
     41:   return 0;
     42: }

     44: /*
     45:    PetscFPTCreate  Creates a PETSc look up table from function pointers to strings

     47:    Input Parameters:
     48: .     n - expected number of keys

     50: */
     51: static inline PetscErrorCode  PetscFPTCreate(PetscInt n)
     52: {
     53:   PetscFPT _PetscFPTData;

     56:   /* Cannot use PetscNew() here because it is not yet defined in the include file chain */
     57:   PetscMalloc(sizeof(struct _n_PetscFPT),&_PetscFPTData);
     58:   _PetscFPTData->tablesize = (3*n)/2 + 17;
     59:   if (_PetscFPTData->tablesize < n) _PetscFPTData->tablesize = PETSC_MAX_INT/4; /* overflow */
     60:   PetscCalloc(sizeof(void*)*_PetscFPTData->tablesize,&_PetscFPTData->functionpointer);
     61:   PetscMalloc(sizeof(char**)*_PetscFPTData->tablesize,&_PetscFPTData->functionname);
     62:   _PetscFPTData->count = 0;
     63:   PetscFPTData = _PetscFPTData;
     64:   return(0);
     65: }

     67: static inline unsigned long PetscFPTHashPointer(void *ptr)
     68: {
     69: #define PETSC_FPT_HASH_FACT 79943
     70:   return((PETSC_FPT_HASH_FACT*((size_t)ptr))%PetscFPTData->tablesize);
     71: }

     73: static inline PetscErrorCode PetscFPTAdd(void* key,const char* data)
     74: {
     76:   if (!PetscFPTData) return 0;
     77:   for (PetscInt i = 0, hash = (PetscInt)PetscFPTHashPointer(key); i < PetscFPTData->tablesize; ++i) {
     78:     if (PetscFPTData->functionpointer[hash] == key) {
     79:       PetscFPTData->functionname[hash] = (char*) data;
     80:       return 0;
     81:     } else if (!PetscFPTData->functionpointer[hash]) {
     82:       PetscFPTData->count++;
     83:       PetscFPTData->functionpointer[hash] = key;
     84:       PetscFPTData->functionname[hash] = (char*) data;
     85:       return 0;
     86:     }
     87:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
     88:   }
     89:   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Function pointer table is full");
     90: }

     92: /*
     93:     PetscFPTFind - checks if a function pointer is in the table

     95:     If data==0, then no entry exists

     97: */
     98: static inline PetscErrorCode  PetscFPTFind(void* key,char const **data)
     99: {
    100:   PetscInt hash,ii = 0;

    102:   *data = NULL;
    103:   if (!PetscFPTData) return(0);
    104:   hash = PetscFPTHashPointer(key);
    105:   while (ii++ < PetscFPTData->tablesize) {
    106:     if (!PetscFPTData->functionpointer[hash]) break;
    107:     else if (PetscFPTData->functionpointer[hash] == key) {
    108:       *data = PetscFPTData->functionname[hash];
    109:       break;
    110:     }
    111:     hash = (hash == (PetscFPTData->tablesize-1)) ? 0 : hash+1;
    112:   }
    113:   return 0;
    114: }

    116: #endif