/* * This function should be called before using any of sychronization * primitives below. * Return: 1 = success, 0 = error; */ int init_sync( void ) { #ifdef HAVE_SGI_SPROC ArenaName = tempnam( "", "LOCK" ); if (!ArenaName) { fprintf(stderr,"Unable to allocate arena.\n"); return 0; } /* * Increase CONF_INITUSERS to 20 to avoid the error: * New process pid 20029 could not join I/O arena:No space left on device * Need to raise CONF_INITUSERS? (usconfig(3P)) */ usconfig( CONF_INITUSERS, 20 ); /* * This options causes the temporary lock file to be unlinked automatically. */ usconfig( CONF_ARENATYPE, US_SHAREDONLY ); /* * Initialize the shared arena */ Arena = usinit(ArenaName); if (!Arena) { fprintf(stderr,"Unable to allocate arena.\n"); return 0; } #endif ALLOC_LOCK( RWlock ); return 1; }
void init_queue( void ) { ALLOC_LOCK( qlock ); ALLOC_SEM( qnotempty, 0 ); qsize = 0; qhead = qtail = 0; qwaiters = 0; }
/******************************************************************** * @创建人 :揭成 * @功能:释放内存块 * * @输入:pMmem pmem * *@输出: 申请的内存指针 ********************************************************************/ void GUI_free( void*pmem ) { uint8_t offset = 0; if ( pmem < ( void* )GUI_malloc_mem[0] || pmem > ( void* )GUI_malloc_mem[GUI_MEM_NBLK-1] ) return; ALLOC_LOCK(); offset = (( uint8_t* )pmem - ( uint8_t* )GUI_malloc_mem ) / GUI_BLOCK_SIZE; /*计算出偏移*/ GUI_mem_ctl[offset] = GUI_MEM_IS_IDLE; /*释放主占块*/ offset++; while ( GUI_mem_ctl[offset] == GUI_MEM_IS_SOCCUPY ) { GUI_mem_ctl[offset] = GUI_MEM_IS_IDLE; /*释放附属块*/ offset++; } ALLOC_UNLOCK(); return; }
/******************************************************************** * @创建人 :揭成 * @功能:申请nblk个内存块 * * @输入:pMmem pmem * *@输出: 申请的内存指针 ********************************************************************/ void*GUI_malloc( uint32_t mem_size ) { char*ptemp; uint8_t offset; uint8_t nblk; mem_size += 1; nblk = MSIZE_TO_NBLK( mem_size ); if ( !mem_size ) return PNULL; ALLOC_LOCK(); if ( nblk <= GUI_MEM_NBLK ) /*判断NBLK的合法性*/ { GUI_memset( GUI_mem_pdest, GUI_MEM_IS_IDLE, sizeof( char )*nblk ); /*将要找的内存合成字符串*/ GUI_mem_pdest[nblk] = PNULL; /*为字符串补一个结尾*/ ptemp = GUI_string_locate( ( char* )( GUI_mem_ctl ), ( char* )GUI_mem_pdest ); /*定位字符串*/ if ( ptemp != PNULL ) { *ptemp = GUI_MEM_IS_MOCCUPY; /*设置优先级对应的PRO为主占*/ GUI_memset(( uint8_t* )( ptemp + 1 ), GUI_MEM_IS_SOCCUPY, ( nblk - 1 ) ); /*其余为附属*/ offset = ( uint8_t* )ptemp - ( uint8_t* )( GUI_mem_ctl ); /*计算出偏移*/ ptemp = ( char* )GUI_malloc_mem + (( GUI_BLOCK_SIZE ) * offset ); /*计算出空闲内存块指针头*/ ALLOC_UNLOCK(); if ((( long )ptemp % 2 ) == 0 ) return ptemp; /*返回任务申请到的指针*/ else return (( char* )ptemp + 1 ); } } nblk = GUI_MEM_NBLK; ALLOC_UNLOCK(); return PNULL; }
int init_record_cache( Irregular_Context itx, int maxbytes, float *ratio ) { int i, j; int numfloatvars, numcharvars, numberofchars; int recordsize; int soundingsize; int totalnumrecs; /* dyamically allocate memory for RecordTable since */ /* MAXTIMES*MAXRECS is way too large */ for (i = 0; i < itx->NumTimes; i++){ itx->RecordTable[i] = (struct irreg_rec *) calloc(itx->NumRecs[i], sizeof(struct irreg_rec)); } ALLOC_LOCK(itx->Mutex); /*********************************/ /* figure out size of one record */ /*********************************/ numfloatvars = 0; numcharvars = 0; numberofchars = 0; soundingsize = 0; for (i = 0; i < itx->NumVars; i++){ if (itx->Variable[i]->VarType == NUMERICAL_VAR_1D ){ numfloatvars++; } else if (itx->Variable[i]->VarType == NUMERICAL_VAR_2D ){ soundingsize += itx->Levels; } else if (itx->Variable[i]->VarType == CHARACTER_VAR){ numberofchars += itx->Variable[i]->CharVarLength; numcharvars++; } else{ printf("Error in init_record_cache\n"); return -1; } } if (itx->Type == SOUNDING){ recordsize = numfloatvars * sizeof(double) + soundingsize * sizeof(double) + itx->Levels * sizeof(float) + numberofchars * sizeof(char); } else{ recordsize = numfloatvars * sizeof(double) + soundingsize * sizeof(double) + numberofchars * sizeof(char); } itx->CharArrayLength = numberofchars; /****************************************************/ /* figure out how many records can be put in memory */ /****************************************************/ itx->MaxCachedRecs = (int) maxbytes / recordsize; /**********************/ /* get total num recs */ /**********************/ totalnumrecs = 0; for (i = 0; i < itx->NumTimes; i++){ totalnumrecs += itx->NumRecs[i]; } if (itx->MaxCachedRecs >= totalnumrecs){ /* all records can be cached */ itx->MaxCachedRecs = totalnumrecs; printf("Reading all records\n"); *ratio = 1.0; } else{ *ratio = ((float) itx->MaxCachedRecs) / ((float) totalnumrecs); } itx->NumCachedRecs = 0; printf("Cache size: %d records\n", itx->MaxCachedRecs); /****************************/ /* allocate irregular cache */ /****************************/ itx->RecordCache = (struct cache_irreg_rec *) i_allocate( itx, itx->MaxCachedRecs * sizeof(struct cache_irreg_rec) ); /************/ /* check it */ /************/ if (!itx->RecordCache){ printf("Error1: out of memory. Couldn't allocate cache space.\n"); return 0; } /******************************************************************/ /* allocate memory for RecGeoPosition, this simply contains the */ /* lat, lon, alt for all the records. This was removed from the */ /* cache_irreg_rec becuase if all the records can not fit into */ /* the cache, they will have to be loaded at some point in time */ /* into the cache in order to see whether or not to be used in a */ /* graphic, by loading all the lat, lon, and alts and keeping them*/ /* this will eliminate this problem and the record cache will be */ /* more effective */ /******************************************************************/ for (i = 0; i < itx->NumTimes; i++){ itx->RecGeoPosition[i] = (struct rec_geo_position *) i_allocate( itx, itx->NumRecs[i] * sizeof(struct rec_geo_position) ); if (!itx->RecGeoPosition[i]){ printf("Not enough memory to allocate for RecGeoPosition\n"); return 0; } } itx->CacheClock = 1; for (i=0;i<itx->MaxCachedRecs;i++){ /************************/ /* alloc DataType array */ /************************/ itx->RecordCache[i].DataType = i_allocate( itx, itx->NumVars*sizeof(int)); /************/ /* check it */ /************/ if (!itx->RecordCache[i].DataType){ printf("Error3: out of memory. Couldn't allocate cache space.\n"); return 0; } /***************/ /* alloc Value */ /***************/ itx->RecordCache[i].Value = i_allocate( itx, itx->NumVars*sizeof(double)); /************/ /* check it */ /************/ if (!itx->RecordCache[i].Value){ printf("Error4: out of memory. Couldn't allocate cache space.\n"); return 0; } /*************************/ /* alloc Sounding Values */ /*************************/ if (soundingsize){ itx->RecordCache[i].SoundingValue = i_allocate( itx, soundingsize * sizeof(double)); /************/ /* check it */ /************/ if (!itx->RecordCache[i].SoundingValue){ printf("Error5: out of memory. Couldn't allocate cache space.\n"); return 0; } itx->RecordCache[i].SoundingLevel = i_allocate( itx, itx->Levels * sizeof(float)); if(!itx->RecordCache[i].SoundingLevel){ printf("Error6: out of memory. Couldn't allocate cache space.\n"); return 0; } } /***********************/ /* Alloc char var data */ /***********************/ itx->RecordCache[i].CharData = i_allocate( itx, numberofchars*sizeof(char )); /************/ /* check it */ /************/ if (!itx->RecordCache[i].CharData){ printf("Error7: out of memory. Couldn't allocate cache space.\n"); return 0; } itx->RecordCache[i].Locked = 0; itx->RecordCache[i].Timestep = 0; } for (i=0; i<itx->NumTimes; i++){ for (j = 0; j < itx->NumRecs[i]; j++){ itx->RecordTable[i][j].CachePos = -1; itx->RecordTable[i][j].DataType = NULL; itx->RecordTable[i][j].Value = NULL; itx->RecordTable[i][j].SoundingValue = NULL; itx->RecordTable[i][j].SoundingLevel = NULL; itx->RecordTable[i][j].CharData = NULL; } } return 1; }