void* ResourceArray::_extract(unsigned index, byte R) { void **res = mRes; byte *types = mResTypes; if(index&DYNAMIC_PLACEHOLDER_BIT) { res = mDynRes; types = mDynResTypes; index = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(index, mDynResSize); } else { TESTINDEX(index, mResSize); } if(types[index] != R) { BIG_PHAT_ERROR(ERR_RES_INVALID_TYPE); } #ifdef RESOURCE_MEMORY_LIMIT switch(types[index]) { #define CASE_SUBMEM(R, T, D) case R: mResmem -= size_##R((T*)res[index]); break; TYPES(CASE_SUBMEM); } if(mResmem > mResmemMax) { DEBIG_PHAT_ERROR; } #endif //RESOURCE_MEMORY_LIMIT void* temp = res[index]; res[index] = NULL; types[index] = RT_PLACEHOLDER; return temp; }
void ResourceArray::_destroy(unsigned index) { void **res = mRes; byte *types = mResTypes; if(index&DYNAMIC_PLACEHOLDER_BIT) { res = mDynRes; types = mDynResTypes; index = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(index, mDynResSize); } else { TESTINDEX(index, mResSize); } MYASSERT(types[index] != RT_FLUX, ERR_RES_DESTROY_FLUX); #ifdef RESOURCE_MEMORY_LIMIT switch(types[index]) { #define CASE_SUBMEM(R, T, D) case R: mResmem -= size_##R((T*)res[index]); break; TYPES(CASE_SUBMEM); } if(mResmem > mResmemMax) { DEBIG_PHAT_ERROR; } #endif //RESOURCE_MEMORY_LIMIT __destroy(res[index], types[index], index); res[index] = NULL; types[index] = RT_PLACEHOLDER; }
/** * * s[i:j] * s[ * * Case 1: empty list * Case 2: one element * Case 3: * */ int *cjld_list_slice(cjld_list *list, int start, int stop) { TESTPTR(cjld_list_insert, list); TESTINDEX(cjld_list_insert, start); TESTINDEX(cjld_list_insert, stop); }//
byte ResourceArray::get_type(unsigned index) { if(index&DYNAMIC_PLACEHOLDER_BIT) { index&=~DYNAMIC_PLACEHOLDER_BIT; TESTINDEX(index, mDynResSize); return mDynResTypes[index]; } else { TESTINDEX(index, mResSize); return mResTypes[index]; } }
int Zoltan_Timer_Reset( ZTIMER *zt, int ts_idx, /* Index of the timer to reset */ int use_barrier, /* Flag indicating whether to perform a barrier operation before starting the timer. */ const char *name /* Name of this timer */ ) { /* Initialize a timer for INUSE; reset its values to zero. */ static char *yo = "Zoltan_Timer_Reset"; ZTIMER_TS *ts; TESTTIMER(zt, yo); TESTINDEX(zt, ts_idx, yo); ts = &(zt->Times[ts_idx]); ts->Status = INUSE; ts->Start_Time = 0.; ts->Stop_Time = 0.; ts->My_Tot_Time = 0.; ts->Use_Barrier = use_barrier; strncpy(ts->Name, name, MAXNAMELEN); ts->Name[MAXNAMELEN] = '\0'; ts->Start_File[0] = '\0'; ts->Start_Line = -1; ts->Stop_File[0] = '\0'; ts->Stop_Line = -1; return ZOLTAN_OK; }
/** * Delete and add a resource ("dadd"). * @param index Resource index. * @param obj Pointer to object data. * @param type Resource type. */ int ResourceArray::_dadd(unsigned index, void* obj, byte type) { if(index&DYNAMIC_PLACEHOLDER_BIT) { unsigned pIndex = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(pIndex, mDynResSize); if(mDynRes[pIndex] != NULL) { _destroy(index); } return _add(index, obj, type); } else { TESTINDEX(index, mResSize); if(mRes[index] != NULL) { _destroy(index); } return _add(index, obj, type); } }
/** * Destroy a placeholder. Mark this placeholder as free * and store the index in the pool of free placeholders. * @param index The placeholder handle. * @return Status code. */ int ResourceArray::_maDestroyPlaceholder(unsigned index) { // The handle must be a dynamic placeholder. if (!(index & DYNAMIC_PLACEHOLDER_BIT)) { // TODO: Use MYASSERT_IF_PANICS_ENABLED when // conditional panics are to be used. BIG_PHAT_ERROR(ERR_RES_PLACEHOLDER_NOT_DYNAMIC); return -2; } // Get the index into the dynamic resource array. unsigned i = index & (~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(i, mDynResSize); // The placeholder must not have been destroyed. if (RT_NIL == mDynResTypes[i]) { // TODO: Use MYASSERT_IF_PANICS_ENABLED when // conditional panics are to be used. BIG_PHAT_ERROR(ERR_RES_PLACEHOLDER_ALREADY_DESTROYED); return -2; } // Set the handle type to RT_NIL. This marks the // placeholder as destroyed. mDynResTypes[i] = RT_NIL; // Put handle into the pool. // Create or expand the pool as needed. if (0 == mDynResPoolCapacity) { // Create the initial pool. mDynResPoolCapacity = 2; mDynResPool = new unsigned[mDynResPoolCapacity]; MYASSERT(mDynResPool != NULL, ERR_OOM); } else if (mDynResPoolSize + 1 > mDynResPoolCapacity) { // Expand the pool. unsigned* oldDynResPool = mDynResPool; mDynResPool = new unsigned[mDynResPoolCapacity * 2]; MYASSERT(mDynResPool != NULL, ERR_OOM); // Copy from old to new and delete old. memcpy( mDynResPool, oldDynResPool, mDynResPoolCapacity * sizeof(unsigned)); delete []oldDynResPool; // Set new capacity. mDynResPoolCapacity = mDynResPoolCapacity * 2; } // Increment pool size. ++mDynResPoolSize; // Add free handle index last in array (push to stack). mDynResPool[mDynResPoolSize - 1] = index; return RES_OK; }
/** * It is used to see if a resource is already loaded. * Works with both static and dynamic resources. * @param index The handle to the resource. * @return true if a resource is already loaded, false if not. */ bool ResourceArray::is_loaded(unsigned index) { void **res = mRes; if(index&DYNAMIC_PLACEHOLDER_BIT) { res = mDynRes; index = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(index, mDynResSize); } else { TESTINDEX(index, mResSize); } if (res[index] != NULL) { return true; } return false; }
/** * Gets a pointer to the data for the given handle. * @param index The resource handle. * @param R The resource type. * @return a pointer to the data for the given handle. */ void* ResourceArray::_get(unsigned index, byte R) { void **res = mRes; byte *types = mResTypes; if(index&DYNAMIC_PLACEHOLDER_BIT) { res = mDynRes; types = mDynResTypes; index = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(index, mDynResSize); } else { TESTINDEX(index, mResSize); } if(types[index] != R) { BIG_PHAT_ERROR(ERR_RES_INVALID_TYPE); } return res[index]; }
/** * Add a resource. * @param index Resource index. * @param obj Pointer to object data. * @param type Resource type. */ int ResourceArray::_add(unsigned index, void* obj, byte type) { void **res = mRes; byte *types = mResTypes; if(index&DYNAMIC_PLACEHOLDER_BIT) { res = mDynRes; types = mDynResTypes; index = index&(~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(index, mDynResSize); } else { TESTINDEX(index, mResSize); } // obj is the resource data. If the resource is NULL // and not a placeholder or in flux (resource is changing) // then create a panic. if (obj == NULL && (type != RT_PLACEHOLDER && type != RT_FLUX)) { DEBIG_PHAT_ERROR; } // Resource at this index must not be in use, but it can be a // placeholder. If the resource is in use, a panic is generated. if(res[index] != NULL || types[index] != RT_PLACEHOLDER) { BIG_PHAT_ERROR(ERR_RES_OVERWRITE); } #ifdef RESOURCE_MEMORY_LIMIT int oldResmem = mResmem; switch(type) { #define CASE_ADDMEM(R, T, D) case R: mResmem += size_##R((T*)obj); break; TYPES(CASE_ADDMEM); } if(mResmem >= mResmemMax) { //BIG_PHAT_ERROR(ERR_RES_OOM); mResmem = oldResmem; __destroy(obj, type, index); //avoids memory leaks return RES_OUT_OF_MEMORY; } #endif //RESOURCE_MEMORY_LIMIT res[index] = obj; types[index] = type; return RES_OK; }
/** * Case 1: HEAD insertion * Case 2: TAIL insertion * Case 3: In between */ int cjld_list_insert(cjld_list *list, void *el, int id, int pos) { TESTPTR(cjld_list_insert, list); TESTINDEX(cjld_list_insert, pos); TESTINDEXU(cjld_list_insert, pos, list->count ); int i=0; cjld_snode *current = NULL, *previous = NULL, *node = NULL; //create node node = (cjld_snode *) malloc( sizeof(cjld_snode *) ); node->el = el; node->id = id; //case 1 if (0==pos) { node->next = list->head; list->head = node; list->count++; return 1; } //case 2 & 3: need to walk the list // as it is single-linked // NOTE: tail pointer never needs to be updated current = list->head; previous = NULL; //walk till pos while( i != pos ) { previous=current; current=current->next; i++; }//while //insert in-between previous->next = node; node->next = current; return 1; }//
/** * Check if a handle refers to an existing dynamic resource object. * @param index Placeholder handle. * @return Non zero if data object exists, zero if it does not exist. */ int ResourceArray::isDynamicResource(unsigned index) { // The handle must be a dynamic placeholder. if (!(index & DYNAMIC_PLACEHOLDER_BIT)) { return 0; } // Get the index into the dynamic resource array. unsigned i = index & (~DYNAMIC_PLACEHOLDER_BIT); TESTINDEX(i, mDynResSize); // The placeholder must not have been destroyed. if (RT_NIL == mDynResTypes[i]) { return 0; } // If the resource is a non-NULL object it exists. return NULL != mDynRes[i]; }
int Zoltan_Timer_Start( ZTIMER *zt, /* Ptr to Timer object */ int ts_idx, /* Index of the timer to use */ MPI_Comm comm, /* Communicator to use for synchronization, if requested */ char *filename, /* Filename of file calling the Start */ int lineno /* Line number where Start was called */ ) { ZTIMER_TS *ts; static char *yo = "Zoltan_Timer_Start"; TESTTIMER(zt, yo); TESTINDEX(zt, ts_idx, yo); ts = &(zt->Times[ts_idx]); if (ts->Status > RUNNING) { char msg[256]; sprintf(msg, "Cannot start timer %d at %s:%d; timer already running from %s:%d.", ts_idx, filename, lineno, ts->Start_File, ts->Start_Line); FATALERROR(yo, msg) }