/***************************************************************************** * Function - Alloc * DESCRIPTION: Allocates a number of bytes by use of AllocBlock. * If required, the block size is reduced to try to get the small * chunks of fragmented memory (if anything available) * * Return: The number of bytes allocated * *****************************************************************************/ int TestAlloc::Alloc(int bytes) { int block_size = MAX_BLOCK_SIZE; int block_count = 0; if (bytes < 0) { bytes = 0x7FFFFFFF; } if (block_size > bytes) { block_size = bytes; } mAllocated = 0; while (block_size >= MIN_BLOCK_SIZE) { while (mAllocated+block_size <= bytes && AllocBlock(block_size) == true) { mAllocated += block_size; block_count++; } block_size /= 2; if (block_size < 2*MIN_BLOCK_SIZE && block_size > MIN_BLOCK_SIZE) { block_size = MIN_BLOCK_SIZE; } } return mAllocated; }
CDataQueue::CDataQueue() { #ifdef _DEBUG memcount=0; #endif SYSTEM_INFO sinfo; ::GetSystemInfo(&sinfo); blocksize=sinfo.dwPageSize; head=tail=AllocBlock(); nowreadpos=nowwritepos=0; }
void CDataQueue::WantWrite(char** buf,int wantwrite,int* canwrite) { if(nowwritepos==blocksize-sizeof(DataBlock *)) { DataBlock* newblock=AllocBlock(); tail->NextBlock=newblock; tail=newblock; nowwritepos=0; } *canwrite=min(wantwrite,(int)(blocksize-sizeof(DataBlock *)-nowwritepos)); *buf=&(tail->data[nowwritepos]); }
CodedBlockPtr CodeTorrent::AllocCodedBlock(int numblks, int blksize) { CodedBlock *blk = NULL; //printf("numblks %d blksize %d\n", numblks, blksize); blk = (CodedBlock *) malloc(sizeof(CodedBlock)); assert(blk); blk->coeffs = AllocCoeffs(numblks); blk->sums = AllocBlock(blksize); blk->block_size = blksize; blk->num_blocks_gen = numblks; assert(blk->coeffs); assert(blk->sums); return blk; }
/* ** Add additional blocks to the block list in order to cover ** all elements on the element list. ** ** If any old blocks are found on the element list, they must ** be left over from a prior rendering. Unlink and delete them. */ void HtmlFormBlocks(HtmlWidget *htmlPtr){ HtmlElement *pElem; if( htmlPtr->lastBlock ){ pElem = FillOutBlock(htmlPtr, htmlPtr->lastBlock); }else{ pElem = htmlPtr->pFirst; } while( pElem ){ int cnt; pElem = FindStartOfNextBlock(htmlPtr, pElem, &cnt); if( pElem ){ HtmlBlock *pNew = AllocBlock(); if( htmlPtr->lastBlock ){ htmlPtr->lastBlock->base.count += cnt; } AppendBlock(htmlPtr, pElem, pNew); pElem = FillOutBlock(htmlPtr, pNew); } } }
static bool_t AllocBlockGroup(block* Block, bool_t Optional) { int n; blockgroup* g; for (g=ARRAYBEGIN(BlockGroup,blockgroup);g!=ARRAYEND(BlockGroup,blockgroup);++g) if (g->Mask && g->Mask != (1<<BLOCKGROUP)-1) break; if (g==ARRAYEND(BlockGroup,blockgroup)) { if (!Optional) return 0; for (g=ARRAYBEGIN(BlockGroup,blockgroup);g!=ARRAYEND(BlockGroup,blockgroup);++g) if (!g->Mask) break; if (g==ARRAYEND(BlockGroup,blockgroup)) { if (!ArrayAppend(&BlockGroup,NULL,sizeof(blockgroup),64)) return 0; g=ARRAYEND(BlockGroup,blockgroup)-1; g->Mask = 0; } if (!AllocBlock(BLOCKSIZE*BLOCKGROUP,&g->Block,1,HEAP_ANY)) return 0; } for (n=0;n<BLOCKGROUP;++n) if (!(g->Mask & (1<<n))) { g->Mask |= (1<<n); Block->Id = ((g-ARRAYBEGIN(BlockGroup,blockgroup))<<8)+(n+1); Block->Ptr = g->Block.Ptr + n*BLOCKSIZE; return 1; } return 0; }
static transfer_index_t* CompressTransferIndicies(transfer_raw_index_t* tRaw, const unsigned rawSize, unsigned* iSize) { unsigned x; unsigned size = rawSize; unsigned compressed_count = 0; transfer_raw_index_t* raw = tRaw; transfer_raw_index_t* end = tRaw + rawSize - 1; // -1 since we are comparing current with next and get errors when bumping into the 'end' transfer_index_t CompressedArray[MAX_PATCHES]; // somewhat big stack object (1 Mb with 256k patches) transfer_index_t* compressed = CompressedArray; for (x = 0; x < size; x++, raw++, compressed++) { compressed->index = (*raw); compressed->size = GetLengthOfRun(raw, end); // Zero based (count 0 still implies 1 item in the list, so 256 max entries result) raw += compressed->size; x += compressed->size; compressed_count++; // number of entries in compressed table } *iSize = compressed_count; if (compressed_count) { unsigned compressed_array_size = sizeof(transfer_index_t) * compressed_count; transfer_index_t* rval = (transfer_index_t*)AllocBlock(compressed_array_size); ThreadLock(); g_transfer_index_bytes += compressed_array_size; ThreadUnlock(); memcpy(rval, CompressedArray, compressed_array_size); return rval; } else { return NULL; } }
// encode a block from generation "gen" CodedBlockPtr SingleBlockEncoder::EncodeSingleBlock(int gen, int blockIdInGen, BlockPtr fragBlockData, int fragDataSize, int num_blocks_gen) { int block_size = fragDataSize; // int buffer_size = 2 * block_size; if(fragDataSize != block_size){ //TODO: Report error return NULL; } // create a new copy CodedBlockPtr cb_to = AllocCodedBlock(num_blocks_gen, block_size); cb_to->gen = gen; cb_to->num_blocks_gen = num_blocks_gen; cb_to->block_size = block_size; //Make a fake data to encode std::vector<BlockPtr> fakeData; BlockPtr pblk; for(int i=0;i<num_blocks_gen;i++){ pblk = AllocBlock((block_size)); memset(pblk, 0, (block_size)); if(i==blockIdInGen){ memcpy(pblk, fragBlockData, (block_size)); } fakeData.push_back(pblk); } nc->EncodeSingleBlock(fakeData, cb_to, blockIdInGen); //release fake data for(int i=0;i<(int) fakeData.size();i++){ FreeBlock(fakeData[i]); } fakeData.clear(); return cb_to; }
static transfer_index_t* CompressTransferIndicies(const transfer_raw_index_t* tRaw, const unsigned rawSize, unsigned* iSize) { unsigned x; unsigned size = rawSize; unsigned compressed_count = 0; transfer_raw_index_t* raw = tRaw; transfer_raw_index_t* end = tRaw + rawSize; transfer_index_t CompressedArray[MAX_PATCHES]; // somewhat big stack object (1 Mb with 256k patches) transfer_index_t* compressed = CompressedArray; for (x = 0; x < size; x++, raw++, compressed++) { compressed->index = (*raw); compressed->size = 0; compressed_count++; // number of entries in compressed table } *iSize = compressed_count; if (compressed_count) { unsigned compressed_array_size = sizeof(transfer_index_t) * compressed_count; transfer_index_t* rval = AllocBlock(compressed_array_size); ThreadLock(); g_transfer_index_bytes += compressed_array_size; ThreadUnlock(); memcpy(rval, CompressedArray, compressed_array_size); return rval; } else { return NULL; } }
static int amf_parse_object(flv* p, format_reader* Reader, const char *key, int64_t max_pos, int depth) { AMFDataType amf_type; char str_val[256]; conv c; double num_val; num_val = 0; amf_type = Reader->Read8(Reader); switch(amf_type) { case AMF_DATA_TYPE_NUMBER: { c.src = Reader->ReadBE64(Reader); num_val =c.dst; } break; case AMF_DATA_TYPE_BOOL: num_val = Reader->Read8(Reader); break; case AMF_DATA_TYPE_STRING: if(amf_get_string(p,Reader, str_val, sizeof(str_val)) < 0) return -1; break; case AMF_DATA_TYPE_OBJECT: { while(Reader->FilePos < max_pos - 2 && amf_get_string(p,Reader, str_val, sizeof(str_val)) > 0) { if(amf_parse_object(p,Reader, str_val, max_pos, depth + 1) < 0) { return -1; //if we couldn't skip, bomb out. } } if(Reader->Read8(Reader) != AMF_END_OF_OBJECT) return -1; } break; case AMF_DATA_TYPE_NULL: case AMF_DATA_TYPE_UNDEFINED: case AMF_DATA_TYPE_UNSUPPORTED: break; //these take up no additional space case AMF_DATA_TYPE_MIXEDARRAY: Reader->Skip(Reader,4); //skip 32-bit max array index while(Reader->FilePos < max_pos - 2 && amf_get_string(p,Reader, str_val, sizeof(str_val)) > 0) { //this is the only case in which we would want a nested parse to not skip over the object if(amf_parse_object(p,Reader, str_val, max_pos, depth + 1) < 0) return -1; } if(Reader->Read8(Reader) != AMF_END_OF_OBJECT) return -1; break; case AMF_DATA_TYPE_ARRAY: { unsigned int arraylen, i; arraylen = Reader->ReadBE32(Reader); if(strcmp(key,"times")==0||strcmp(key,"filepositions")==0) { if(p->IndexNum == 0) { int32_t size; p->IndexNum = arraylen; size = sizeof(flvindex)*p->IndexNum; size = ((size+SAFETAIL-1)/SAFETAIL)*SAFETAIL; if (!AllocBlock(size,&p->IndexBuffer,0,HEAP_ANY)) return ERR_OUT_OF_MEMORY; } } p->IndexCur = 0; for(i = 0; i < arraylen && Reader->FilePos < max_pos - 1; i++) { if(amf_parse_object(p,Reader, key, max_pos, depth + 1) < 0) return -1; //if we couldn't skip, bomb out. } } break; case AMF_DATA_TYPE_DATE: Reader->Skip(Reader, 8 + 2); //timestamp (double) and UTC offset (int16) break; default: //unsupported type, we couldn't skip return -1; } if(depth == 1 &&key) { //only look for metadata values when we are not nested and key != NULL if(amf_type == AMF_DATA_TYPE_BOOL) { } else if(amf_type == AMF_DATA_TYPE_NUMBER) { if(!tcscmp(key, "duration")) p->Format.Duration = Scale(num_val, TICKSPERSEC, 1); } else if (amf_type == AMF_DATA_TYPE_STRING) { } else if(amf_type == AMF_DATA_TYPE_DATE) { } } else if(key) { if(tcscmp(key,"times")==0) { ((flvindex*)IndexBuffer(p,p->IndexCur))->times = num_val; p->IndexCur++; } else if(tcscmp(key,"filepositions")==0) { ((flvindex*)IndexBuffer(p,p->IndexCur))->pos = num_val; p->IndexCur++; } } return 0; }
void CodeTorrent::LoadFile(int gen) { BlockPtr pblk; // before begining, erase everything in data CleanData(); // MOS - no data signals error //printf("loading filename=%s\n",filename); if (!(fp = fopen(filename, "rb"))) { HAGGLE_ERR("CODETORRENT ERROR: cannot open %s\n", filename); return; // MOS - need error handling } if(fp) HAGGLE_DBG2("Opening file %s for writing with file descriptor %d\n", filename, fileno(fp)); int i, j; int n_items = 0; int pos = 0; int temp; bool last_gen = false; int last_block_size; int gen_size = block_size * num_blocks_gen[gen]; // set gen_size for this gen unsigned char *fbuf = new unsigned char[gen_size]; for (i = 0; i < gen; i++) pos += num_blocks_gen[i]; pos *= block_size; fseek(fp, pos, SEEK_SET); // move file cursor to begining of n-th generation // read one generation temp = fread(fbuf, 1, gen_size, fp); //printf("read one generation %s\n",fbuf); if (temp + pos == file_size && gen == num_gens - 1) { last_gen = true; } else if (temp != gen_size) { HAGGLE_ERR("temp %d gen_size %d\n",temp,gen_size); #ifdef WIN32 HAGGLE_ERR("%s: fread(2) \n", strerror(errno)); #else HAGGLE_ERR("CODETORRENT ERROR: unexpected codetorrent read result\n"); #endif //printf("Press <enter>..."); //getchar(); //abort(); fclose(fp); // MOS delete[] fbuf; // MOS return; // MOS - need error handling } fclose(fp); // before begining, erase everything in data // CleanData(); // MOS - see above int numblockslen = num_blocks_gen[gen]; //printf("numblockslen =%d\n", numblockslen); // N.B. data is stored "unpacked" e.g., 4bit symbol => 8bit. for (i = 0; i < numblockslen; i++) { pblk = AllocBlock((block_size)); memset(pblk, 0, (block_size)); // if this is the last block if (last_gen && i == num_blocks_gen[gen] - 1) { last_block_size = temp - (num_blocks_gen[gen] - 1) * block_size; for (j = 0; j < (is_sim ? 1 : last_block_size); j++) { pblk[j] = NthSymbol(fbuf, field_size, block_size * i + j); } if (!is_sim) { for (; j < block_size; j++) { pblk[j] = 0; // padding zeros } } } else { for (j = 0; j < (is_sim ? 1 : block_size); j++) { pblk[j] = NthSymbol(fbuf, field_size, block_size * i + j); } } //printf("pushing back block |%u|\n", pblk); data.push_back(pblk); } // record which gen is in memory! gen_in_memory = gen; delete[] fbuf; }
bool readtransfers(const char* const transferfile, const long numpatches) { FILE* file; long total_patches; file = fopen(transferfile, "rb"); if (file != NULL) { unsigned amtread; patch_t* patch; Log("Reading transfers file [%s]\n", transferfile); amtread = fread(&total_patches, sizeof(total_patches), 1, file); if (amtread != 1) { goto FailedRead; } if (total_patches != numpatches) { goto FailedRead; } long patchcount = total_patches; for (patch = g_patches; patchcount-- > 0; patch++) { amtread = fread(&patch->iIndex, sizeof(patch->iIndex), 1, file); if (amtread != 1) { goto FailedRead; } if (patch->iIndex) { patch->tIndex = (transfer_index_t*)AllocBlock(patch->iIndex * sizeof(transfer_index_t *)); hlassume(patch->tIndex != NULL, assume_NoMemory); amtread = fread(patch->tIndex, sizeof(transfer_index_t), patch->iIndex, file); if (amtread != patch->iIndex) { goto FailedRead; } } amtread = fread(&patch->iData, sizeof(patch->iData), 1, file); if (amtread != 1) { goto FailedRead; } if (patch->iData) { #ifdef HLRAD_HULLU if(g_rgb_transfers) { #ifdef HLRAD_TRANSFERDATA_COMPRESS patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * vector_size[g_rgbtransfer_compress_type] + unused_size); #else patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(patch->iData * sizeof(rgb_transfer_data_t *)); //wrong? --vluzacn #endif hlassume(patch->tRGBData != NULL, assume_NoMemory); #ifdef HLRAD_TRANSFERDATA_COMPRESS amtread = fread(patch->tRGBData, vector_size[g_rgbtransfer_compress_type], patch->iData, file); #else amtread = fread(patch->tRGBData, sizeof(rgb_transfer_data_t), patch->iData, file); #endif } else { #ifdef HLRAD_TRANSFERDATA_COMPRESS patch->tData = (transfer_data_t*)AllocBlock(patch->iData * float_size[g_transfer_compress_type] + unused_size); #else patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *)); #endif hlassume(patch->tData != NULL, assume_NoMemory); #ifdef HLRAD_TRANSFERDATA_COMPRESS amtread = fread(patch->tData, float_size[g_transfer_compress_type], patch->iData, file); #else amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file); #endif } #else #ifdef HLRAD_TRANSFERDATA_COMPRESS patch->tData = (transfer_data_t*)AllocBlock(patch->iData * float_size[g_transfer_compress_type] + unused_size); #else patch->tData = (transfer_data_t*)AllocBlock(patch->iData * sizeof(transfer_data_t *)); #endif hlassume(patch->tData != NULL, assume_NoMemory); #ifdef HLRAD_TRANSFERDATA_COMPRESS amtread = fread(patch->tData, float_size[g_transfer_compress_type], patch->iData, file); #else amtread = fread(patch->tData, sizeof(transfer_data_t), patch->iData, file); #endif #endif if (amtread != patch->iData) { goto FailedRead; } } } fclose(file); //Warning("Finished reading transfers file [%s] %d\n", transferfile); Warning("Finished reading transfers file [%s]\n", transferfile); //--vluzacn return true; } Warning("Failed to open transfers file [%s]\n", transferfile); return false; FailedRead: { unsigned x; patch_t* patch = g_patches; for (x = 0; x < g_num_patches; x++, patch++) { FreeBlock(patch->tData); FreeBlock(patch->tIndex); patch->iData = 0; patch->iIndex = 0; patch->tData = NULL; patch->tIndex = NULL; } } fclose(file); unlink(transferfile); return false; }
/* EXPORT->New: create a new element from heap x */ void *New(MemHeap *x,size_t size) { void *q; BlockP newp; size_t num,bytes,*ip,chdr; Boolean noSpace; Ptr *pp; if (x->elemSize <= 0) HError(5174,"New: heap %s not initialised", (x->name==NULL)? "Unnamed":x->name); switch(x->type){ case MHEAP: /* Element is taken from first available slot in block list. If none found a new block is allocated with num elems determined by the curElem, the grow factor growf and the upper limit maxElem. */ if (size != 0 && size != x->elemSize) HError(5173,"New: MHEAP req for %u size elem from heap %s size %u", size,x->name,x->elemSize); noSpace = x->totUsed == x->totAlloc; if (noSpace || (q=GetElem(x->heap,x->elemSize,x->type)) == NULL) { if (!noSpace) BlockReorder(&(x->heap),1); if (noSpace || (q=GetElem(x->heap,x->elemSize,x->type)) == NULL) { num = (size_t) ((double)x->curElem * (x->growf + 1.0) + 0.5); if (num>x->maxElem) num = x->maxElem; newp = AllocBlock(x->elemSize, num, x->type); x->totAlloc += num; x->curElem = num; newp->next = x->heap; x->heap = newp; if ((q=GetElem(x->heap,x->elemSize,x->type)) == NULL) HError(5191,"New: null elem but just made block in heap %s", x->name); } } x->totUsed++; if (trace&T_MHP) printf("HMem: %s[M] %u bytes at %p allocated\n",x->name,size,q); return q; case CHEAP: chdr = MRound(sizeof(size_t)); q = malloc(size+chdr); if (q==NULL) HError(5105,"New: memory exhausted"); x->totUsed += size; x->totAlloc += size+chdr; ip = (size_t *)q; *ip = size; if (trace&T_CHP) printf("HMem: %s[C] %u+%u bytes at %p allocated\n",x->name,chdr,size,q); return (Ptr)((ByteP)q+chdr); case MSTAK: /* set required size - must alloc on double boundaries */ if (x->protectStk) size += sizeof(Ptr); size = MRound(size); /* get elem from current block if possible */ if ((q=GetElem(x->heap,size,x->type)) == NULL) { /* no space - so add a new (maybe bigger) block */ bytes = (size_t)((double)x->curElem * (x->growf + 1.0) + 0.5); if (bytes > x->maxElem) bytes = x->maxElem; x->curElem = bytes; if (bytes < size) bytes = size; bytes = MRound(bytes); newp = AllocBlock(1, bytes, x->type); x->totAlloc += bytes; newp->next = x->heap; x->heap = newp; if ((q=GetElem(x->heap,size,x->type)) == NULL) HError(5191,"New: null elem but just made block in heap %s", x->name); } x->totUsed += size; if (trace&T_STK) printf("HMem: %s[S] %u bytes at %p allocated\n",x->name,size,q); if (x->protectStk) { pp = (Ptr *)((long)q + size - sizeof(Ptr)); /* #### fix this! */ *pp = q; } return q; } return NULL; /* just to keep compiler happy */ }
int main(int argc, char **argv) { if (argc < 3) { PrintUsage(stderr, argv[0]); return -1; } if (!strcmp(argv[1], "create")) { if (argc < 4) { fprintf(stderr, "ERROR: incorrect create usage\n"); return -2; } char *outbfile = argv[2]; int n = atoi(argv[3]); /* Calculate btree size */ int depth = CalcMinimumDepth(n); int sz = CalcTreeSize2(n, depth); fprintf(stderr, "n = %i, depth = %i, size = %i\n", n, depth, sz); /* Create memory buffer */ mem = (char *) malloc(sz + 1); mem[sz] = 123; // Magic Marker to detect overflow memSize = sz; /* Init top node */ BlockAddrT top = AllocBlock(0x0); Node topNode; NodeInit(&topNode); topNode.depth = depth - 1; // total depth vs depth(rank?) of this node NodeSave(&topNode, top); BgTree tree; tree.topNode = top; /* Read in data */ KeyT highestKey = 0; for (int i=0; i<n; i++) { KeyT key; BlockAddrT item; int res = fscanf(stdin, "%x\t%x", &key, &item); assert(res == 2); if (key < highestKey) { fprintf(stderr, "ERROR: Key out of order on line %i. Input must be sorted!\n", i+1); return -3; } highestKey = key; //printf("GOT %i %i\n", key, item); TreeAppend(tree, key, item); } /* Set keys for non-leaf nodes */ //NodeVisitDFS(&SetKeysVisitor, top); /* Write memory to file */ assert(mem[sz] == 123); fprintf(stderr, "MEM: %i of %i allocated was used\n", memLast, memSize); FILE *f = fopen(outbfile, "wb"); if (!f) { fprintf(stderr, "ERROR: Failed to open file %s for writing\n", outbfile); return -9; } int res = fwrite(mem, 1, memLast, f); if (res != memLast) { fprintf(stderr, "ERROR: Could not write all data to file %s\n", outbfile); return -4; } fclose(f); } // end of "create" command if (!strcmp(argv[1], "search")) { if (argc < 4) { fprintf(stderr, "ERROR: search usage incorrect, not enough arguments\n"); return -11; } FILE *blobfile = 0x0; if (argc > 4) { blobfile = fopen(argv[4],"rb"); if (!blobfile) { fprintf(stderr, "ERROR: failed to open Blob File %s\n", argv[4]); return -19; } } char *schema=0x0; if (argc > 5) { schema = argv[5]; } char *inbfile = argv[2]; KeyT key; int res; if (argv[3][0] == 'S') { key = CalcCRC(&argv[3][1], strlen(&argv[3][1])); //fprintf(stderr, "CRC32=%x for %s len=%i\n", key, &argv[3][1], (int) strlen(&argv[3][1])); printf("%x", key); if (mem) free(mem); exit(0); } else { // assume hex res = sscanf(argv[3], "%x", &key); if (res != 1) { fprintf(stderr, "ERROR: Unable to parse query key argument %s\n", argv[3]); return -12; } } if (LoadBGT(inbfile) != 0) return -99; /* Perform Search */ BAT outParent; int outIndex; BAT found_temp; BAT found = FindInternal(0, key, &outParent, &outIndex); while (found != BAInvalid) { // if (found == BAInvalid) { // printf("%x NOT FOUND!\n", key); // } else { //printf("%x\t%08x", key, found); if (schema && blobfile) { for (char *p = &schema[0]; *p; ++p) { if ((*p == 's') || (*p == 'K')) { char buf[2000000]; fseek(blobfile, found, SEEK_SET); int sz = UnpackStringFromFile(blobfile, buf, 2000000); if (sz < 0) { fprintf(stderr, "ERROR: Failed to read String from blob file\n"); return -20; } found += sz; buf[sz] = 0x0; // not null terminated by default printf("\t%s", buf); } else if (*p == 'i') { int32_t v; fseek(blobfile, found, SEEK_SET); v = UnpackIntFromFile(blobfile); ERRORassert(); found += 4; //printf("\t%i", v); } else if ((*p == 'I') || (*p == 'x') || (*p == 'X')) { uint32_t v; fseek(blobfile, found, SEEK_SET); v = UnpackUIntFromFile(blobfile); ERRORassert(); //if (*p == 'I') // printf("\t%u", v); //else // printf("\t%x", v); found += 4; } else { fprintf(stderr, "ERROR: Unsupported schema character '%c'\n", *p); return -23; } } } printf("\n"); // found_temp = NodeNextLeaf(NodeParent(found), found); //found_temp = FindInternal(0, key, &outParent, &outIndex); key++; found_temp = FindInternal(0, key, &outParent, &outIndex); found = found_temp; } if (blobfile) fclose(blobfile); } else if (!strcmp(argv[1],"printtree")) { if (LoadBGT(argv[2]) != 0) { printf("Error Loading BGT\n"); return -99; } BgTree dummy; TreeInit(&dummy,0x0); NodeVisitDFS(dummy, &PrintVisitor, 0); } if (mem) free(mem); }
void CTextTraceFile::LoadThread() { HRESULT hr = S_OK; DWORD cbRead; DWORD cbToRead; LARGE_INTEGER liPos; m_pCallback->OnLoadBegin(); for (;; ) { LoadBlock * pNew = nullptr; if (m_bReverse) { ATLASSERT(false); } else { DWORD cbRollover = 0; if (m_Blocks.size() > 0) { LoadBlock * pEnd = m_Blocks.back(); if (pEnd->nFileStop > m_nStop) { return; } // copy end of line from previous buffer // we have to copy page aligned block and record the start of next data assert(pEnd->cbBuf >= pEnd->cbLastFullLineEnd); cbRollover = pEnd->cbBuf - pEnd->cbLastFullLineEnd; DWORD cbRolloverRounded = (cbRollover + m_PageSize - 1) & (~(m_PageSize - 1)); IFC(AllocBlock(cbRolloverRounded + m_BlockSize, &pNew)); pNew->cbFirstFullLineStart = cbRolloverRounded - cbRollover; memcpy(pNew->pbBuf + pNew->cbFirstFullLineStart, pEnd->pbBuf + pEnd->cbLastFullLineEnd, cbRollover); // at this point we can decommit unnecessary pages for unicode // this will waste address space but keep memory usage low // nFileStart is in file offset // cbLastFullLineEnd is in buffer pNew->nFileStart = pEnd->nFileStop; pNew->cbWriteStart = cbRolloverRounded; // if we are in unicode mode, trim previous block if (m_bUnicode) TrimBlock(pEnd); } else { IFC(AllocBlock(m_BlockSize, &pNew)); // we are going forward so start pos is null pNew->cbWriteStart = 0; } pNew->nFileStop = pNew->nFileStart + m_BlockSize; // we actually have data in the buffer, so set cbData pNew->cbData = cbRollover; } liPos.QuadPart = (__int64) pNew->nFileStart; SetFilePointerEx(m_hFile, liPos, NULL, FILE_BEGIN); cbToRead = m_BlockSize; if (m_bReverse) { ATLASSERT(false); } else { // append data after rollover string if (!ReadFile(m_hFile, pNew->pbBuf + pNew->cbWriteStart, cbToRead, &cbRead, NULL)) { hr = HRESULT_FROM_WIN32(GetLastError()); goto Cleanup; } } pNew->cbData = cbRead + pNew->cbData; // parse data IFC(ParseBlock(pNew, pNew->cbFirstFullLineStart, pNew->cbData, &pNew->cbDataEnd, &pNew->cbLastFullLineEnd)); { LockGuard guard(m_Lock); // append block m_Blocks.push_back(pNew); } m_pCallback->OnLoadBlock(); if (cbRead != m_BlockSize) { break; } } Cleanup: m_pCallback->OnLoadEnd(hr); }
void MakeScales(const int threadnum) { int i; unsigned j; vec3_t delta; vec_t dist; int count; float trans; patch_t* patch; patch_t* patch2; vec3_t origin; vec_t area; const vec_t* normal1; const vec_t* normal2; #ifdef HLRAD_HULLU #ifdef HLRAD_TRANSPARENCY_CPP unsigned int fastfind_index = 0; #endif #endif vec_t total; #ifdef HLRAD_TRANSFERDATA_COMPRESS transfer_raw_index_t* tIndex; float* tData; transfer_raw_index_t* tIndex_All = (transfer_raw_index_t*)AllocBlock(sizeof(transfer_index_t) * MAX_PATCHES); float* tData_All = (float*)AllocBlock(sizeof(float) * MAX_PATCHES); #else transfer_raw_index_t* tIndex; transfer_data_t* tData; transfer_raw_index_t* tIndex_All = (transfer_raw_index_t*)AllocBlock(sizeof(transfer_index_t) * MAX_PATCHES); transfer_data_t* tData_All = (transfer_data_t*)AllocBlock(sizeof(transfer_data_t) * MAX_PATCHES); #endif count = 0; while (1) { i = GetThreadWork(); if (i == -1) break; patch = g_patches + i; patch->iIndex = 0; patch->iData = 0; #ifndef HLRAD_TRANSNONORMALIZE total = 0.0; #endif tIndex = tIndex_All; tData = tData_All; VectorCopy(patch->origin, origin); normal1 = getPlaneFromFaceNumber(patch->faceNumber)->normal; area = patch->area; #ifdef HLRAD_TRANSLUCENT vec3_t backorigin; vec3_t backnormal; if (patch->translucent_b) { VectorMA (patch->origin, -(g_translucentdepth + 2*PATCH_HUNT_OFFSET), normal1, backorigin); VectorSubtract (vec3_origin, normal1, backnormal); } #endif #ifdef HLRAD_DIVERSE_LIGHTING bool lighting_diversify; vec_t lighting_power; vec_t lighting_scale; int miptex = g_texinfo[g_dfaces[patch->faceNumber].texinfo].miptex; lighting_power = g_lightingconeinfo[miptex][0]; lighting_scale = g_lightingconeinfo[miptex][1]; lighting_diversify = (lighting_power != 1.0 || lighting_scale != 1.0); #endif // find out which patch2's will collect light // from patch // HLRAD_NOSWAP: patch collect light from patch2 for (j = 0, patch2 = g_patches; j < g_num_patches; j++, patch2++) { vec_t dot1; vec_t dot2; #ifdef HLRAD_HULLU vec3_t transparency = {1.0,1.0,1.0}; #endif #ifdef HLRAD_TRANSLUCENT bool useback; useback = false; #endif if (!g_CheckVisBit(i, j #ifdef HLRAD_HULLU , transparency #ifdef HLRAD_TRANSPARENCY_CPP , fastfind_index #endif #endif ) || (i == j)) { #ifdef HLRAD_TRANSLUCENT if (patch->translucent_b) { if ((i == j) || !CheckVisBitBackwards(i, j, backorigin, backnormal #ifdef HLRAD_HULLU , transparency #endif )) { continue; } useback = true; } else { continue; } #else continue; #endif } normal2 = getPlaneFromFaceNumber(patch2->faceNumber)->normal; // calculate transferemnce VectorSubtract(patch2->origin, origin, delta); #ifdef HLRAD_TRANSLUCENT if (useback) { VectorSubtract (patch2->origin, backorigin, delta); } #endif #ifdef HLRAD_ACCURATEBOUNCE // move emitter back to its plane VectorMA (delta, -PATCH_HUNT_OFFSET, normal2, delta); #endif dist = VectorNormalize(delta); dot1 = DotProduct(delta, normal1); #ifdef HLRAD_TRANSLUCENT if (useback) { dot1 = DotProduct (delta, backnormal); } #endif dot2 = -DotProduct(delta, normal2); #ifdef HLRAD_ACCURATEBOUNCE #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN bool light_behind_surface = false; if (dot1 <= NORMAL_EPSILON) { light_behind_surface = true; } #else if (dot1 <= NORMAL_EPSILON) { continue; } #endif if (dot2 * dist <= MINIMUM_PATCH_DISTANCE) { continue; } #endif #ifdef HLRAD_DIVERSE_LIGHTING if (lighting_diversify #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN && !light_behind_surface #endif ) { dot1 = lighting_scale * pow (dot1, lighting_power); } #endif trans = (dot1 * dot2) / (dist * dist); // Inverse square falloff factoring angle between patch normals #ifdef HLRAD_TRANSWEIRDFIX #ifdef HLRAD_NOSWAP if (trans * patch2->area > 0.8f) trans = 0.8f / patch2->area; #else // HLRAD_TRANSWEIRDFIX: // we should limit "trans(patch2receive) * patch1area" // instead of "trans(patch2receive) * patch2area". // also raise "0.4f" to "0.8f" ( 0.8/Q_PI = 1/4). if (trans * area > 0.8f) trans = 0.8f / area; #endif #endif #ifdef HLRAD_ACCURATEBOUNCE if (dist < patch2->emitter_range - ON_EPSILON) { #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN if (light_behind_surface) { trans = 0.0; } #endif vec_t sightarea; const vec_t *receiver_origin; const vec_t *receiver_normal; const Winding *emitter_winding; receiver_origin = origin; receiver_normal = normal1; #ifdef HLRAD_TRANSLUCENT if (useback) { receiver_origin = backorigin; receiver_normal = backnormal; } #endif emitter_winding = patch2->winding; sightarea = CalcSightArea (receiver_origin, receiver_normal, emitter_winding, patch2->emitter_skylevel #ifdef HLRAD_DIVERSE_LIGHTING , lighting_power, lighting_scale #endif ); vec_t frac; frac = dist / patch2->emitter_range; frac = (frac - 0.5f) * 2.0f; // make a smooth transition between the two methods frac = max (0, min (frac, 1)); trans = frac * trans + (1 - frac) * (sightarea / patch2->area); // because later we will multiply this back } #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN else { if (light_behind_surface) { continue; } } #endif #endif #ifdef HLRAD_ACCURATEBOUNCE_REDUCEAREA trans *= patch2->exposure; #endif #ifdef HLRAD_HULLU trans = trans * VectorAvg(transparency); //hullu: add transparency effect #endif #ifdef HLRAD_TRANSLUCENT if (patch->translucent_b) { if (useback) { trans *= VectorAvg (patch->translucent_v); } else { trans *= 1 - VectorAvg (patch->translucent_v); } } #endif #ifndef HLRAD_ACCURATEBOUNCE if (trans >= 0) #endif { #ifndef HLRAD_TRANSWEIRDFIX #ifdef HLRAD_NOSWAP send = trans * area; #else send = trans * patch2->area; #endif // Caps light from getting weird if (send > 0.4f) { #ifdef HLRAD_NOSWAP trans = 0.4f / area; #else trans = 0.4f / patch2->area; #endif send = 0.4f; } #endif /*HLRAD_TRANSWEIRDFIX*/ #ifndef HLRAD_TRANSNONORMALIZE total += send; #endif #ifdef HLRAD_TRANSFERDATA_COMPRESS trans = trans * patch2->area; #else // scale to 16 bit (black magic) // BUG: (in MakeRGBScales) convert to integer will lose data. --vluzacn #ifdef HLRAD_NOSWAP trans = trans * patch2->area * INVERSE_TRANSFER_SCALE; #else trans = trans * area * INVERSE_TRANSFER_SCALE; #endif /*HLRAD_NOSWAP*/ if (trans >= TRANSFER_SCALE_MAX) { trans = TRANSFER_SCALE_MAX; } #endif } #ifndef HLRAD_ACCURATEBOUNCE else { #if 0 Warning("transfer < 0 (%f): dist=(%f)\n" " dot1=(%f) patch@(%4.3f %4.3f %4.3f) normal(%4.3f %4.3f %4.3f)\n" " dot2=(%f) patch@(%4.3f %4.3f %4.3f) normal(%4.3f %4.3f %4.3f)\n", trans, dist, dot1, patch->origin[0], patch->origin[1], patch->origin[2], patch->normal[0], patch->normal[1], patch->normal[2], dot2, patch2->origin[0], patch2->origin[1], patch2->origin[2], patch2->normal[0], patch2->normal[1], patch2->normal[2]); #endif trans = 0.0; } #endif #ifdef HLRAD_ACCURATEBOUNCE if (trans <= 0.0) { continue; } #endif *tData = trans; *tIndex = j; tData++; tIndex++; patch->iData++; count++; } // copy the transfers out if (patch->iData) { #ifdef HLRAD_TRANSFERDATA_COMPRESS unsigned data_size = patch->iData * float_size[g_transfer_compress_type] + unused_size; #else unsigned data_size = patch->iData * sizeof(transfer_data_t); #endif patch->tData = (transfer_data_t*)AllocBlock(data_size); patch->tIndex = CompressTransferIndicies(tIndex_All, patch->iData, &patch->iIndex); hlassume(patch->tData != NULL, assume_NoMemory); hlassume(patch->tIndex != NULL, assume_NoMemory); ThreadLock(); g_transfer_data_bytes += data_size; ThreadUnlock(); #ifdef HLRAD_REFLECTIVITY total = 1 / Q_PI; #else #ifdef HLRAD_TRANSNONORMALIZE #ifdef HLRAD_TRANSTOTAL_HACK total = g_transtotal_hack / Q_PI; #else total = 0.5 / Q_PI; #endif #else // BAD assumption when there is SKY. // // normalize all transfers so exactly 50% of the light // is transfered to the surroundings // total = 0.5 / total; #endif #endif { #ifdef HLRAD_TRANSFERDATA_COMPRESS unsigned x; transfer_data_t* t1 = patch->tData; float* t2 = tData_All; float f; for (x = 0; x < patch->iData; x++, t1+=float_size[g_transfer_compress_type], t2++) { f = (*t2) * total; float_compress (g_transfer_compress_type, t1, &f); } #else unsigned x; transfer_data_t* t1 = patch->tData; transfer_data_t* t2 = tData_All; for (x = 0; x < patch->iData; x++, t1++, t2++) { (*t1) = (*t2) * total; } #endif } } } FreeBlock(tIndex_All); FreeBlock(tData_All); ThreadLock(); g_total_transfer += count; ThreadUnlock(); }
void MakeRGBScales(const int threadnum) { int i; unsigned j; vec3_t delta; vec_t dist; int count; float trans[3]; float trans_one; patch_t* patch; patch_t* patch2; vec3_t origin; vec_t area; const vec_t* normal1; const vec_t* normal2; #ifdef HLRAD_TRANSPARENCY_CPP unsigned int fastfind_index = 0; #endif vec_t total; #ifdef HLRAD_TRANSFERDATA_COMPRESS transfer_raw_index_t* tIndex; float* tRGBData; transfer_raw_index_t* tIndex_All = (transfer_raw_index_t*)AllocBlock(sizeof(transfer_index_t) * MAX_PATCHES); float* tRGBData_All = (float*)AllocBlock(sizeof(float[3]) * MAX_PATCHES); #else transfer_raw_index_t* tIndex; rgb_transfer_data_t* tRGBData; transfer_raw_index_t* tIndex_All = (transfer_raw_index_t*)AllocBlock(sizeof(transfer_index_t) * MAX_PATCHES); rgb_transfer_data_t* tRGBData_All = (rgb_transfer_data_t*)AllocBlock(sizeof(rgb_transfer_data_t) * MAX_PATCHES); #endif count = 0; while (1) { i = GetThreadWork(); if (i == -1) break; patch = g_patches + i; patch->iIndex = 0; patch->iData = 0; #ifndef HLRAD_TRANSNONORMALIZE total = 0.0; #endif tIndex = tIndex_All; tRGBData = tRGBData_All; VectorCopy(patch->origin, origin); normal1 = getPlaneFromFaceNumber(patch->faceNumber)->normal; area = patch->area; #ifdef HLRAD_TRANSLUCENT vec3_t backorigin; vec3_t backnormal; if (patch->translucent_b) { VectorMA (patch->origin, -(g_translucentdepth + 2*PATCH_HUNT_OFFSET), normal1, backorigin); VectorSubtract (vec3_origin, normal1, backnormal); } #endif #ifdef HLRAD_DIVERSE_LIGHTING bool lighting_diversify; vec_t lighting_power; vec_t lighting_scale; int miptex = g_texinfo[g_dfaces[patch->faceNumber].texinfo].miptex; lighting_power = g_lightingconeinfo[miptex][0]; lighting_scale = g_lightingconeinfo[miptex][1]; lighting_diversify = (lighting_power != 1.0 || lighting_scale != 1.0); #endif // find out which patch2's will collect light // from patch // HLRAD_NOSWAP: patch collect light from patch2 for (j = 0, patch2 = g_patches; j < g_num_patches; j++, patch2++) { vec_t dot1; vec_t dot2; vec3_t transparency = {1.0,1.0,1.0}; #ifdef HLRAD_TRANSLUCENT bool useback; useback = false; #endif if (!g_CheckVisBit(i, j , transparency #ifdef HLRAD_TRANSPARENCY_CPP , fastfind_index #endif ) || (i == j)) { #ifdef HLRAD_TRANSLUCENT if (patch->translucent_b) { if (!CheckVisBitBackwards(i, j, backorigin, backnormal #ifdef HLRAD_HULLU , transparency #endif ) || (i==j)) { continue; } useback = true; } else { continue; } #else continue; #endif } normal2 = getPlaneFromFaceNumber(patch2->faceNumber)->normal; // calculate transferemnce VectorSubtract(patch2->origin, origin, delta); #ifdef HLRAD_TRANSLUCENT if (useback) { VectorSubtract (patch2->origin, backorigin, delta); } #endif #ifdef HLRAD_ACCURATEBOUNCE // move emitter back to its plane VectorMA (delta, -PATCH_HUNT_OFFSET, normal2, delta); #endif dist = VectorNormalize(delta); dot1 = DotProduct(delta, normal1); #ifdef HLRAD_TRANSLUCENT if (useback) { dot1 = DotProduct (delta, backnormal); } #endif dot2 = -DotProduct(delta, normal2); #ifdef HLRAD_ACCURATEBOUNCE #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN bool light_behind_surface = false; if (dot1 <= NORMAL_EPSILON) { light_behind_surface = true; } #else if (dot1 <= NORMAL_EPSILON) { continue; } #endif if (dot2 * dist <= MINIMUM_PATCH_DISTANCE) { continue; } #endif #ifdef HLRAD_DIVERSE_LIGHTING if (lighting_diversify #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN && !light_behind_surface #endif ) { dot1 = lighting_scale * pow (dot1, lighting_power); } #endif trans_one = (dot1 * dot2) / (dist * dist); // Inverse square falloff factoring angle between patch normals #ifdef HLRAD_TRANSWEIRDFIX #ifdef HLRAD_NOSWAP if (trans_one * patch2->area > 0.8f) { trans_one = 0.8f / patch2->area; } #else if (trans_one * area > 0.8f) { trans_one = 0.8f / area; } #endif #endif #ifdef HLRAD_ACCURATEBOUNCE if (dist < patch2->emitter_range - ON_EPSILON) { #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN if (light_behind_surface) { trans_one = 0.0; } #endif vec_t sightarea; const vec_t *receiver_origin; const vec_t *receiver_normal; const Winding *emitter_winding; receiver_origin = origin; receiver_normal = normal1; #ifdef HLRAD_TRANSLUCENT if (useback) { receiver_origin = backorigin; receiver_normal = backnormal; } #endif emitter_winding = patch2->winding; sightarea = CalcSightArea (receiver_origin, receiver_normal, emitter_winding, patch2->emitter_skylevel #ifdef HLRAD_DIVERSE_LIGHTING , lighting_power, lighting_scale #endif ); vec_t frac; frac = dist / patch2->emitter_range; frac = (frac - 0.5f) * 2.0f; // make a smooth transition between the two methods frac = max (0, min (frac, 1)); trans_one = frac * trans_one + (1 - frac) * (sightarea / patch2->area); // because later we will multiply this back } #ifdef HLRAD_ACCURATEBOUNCE_ALTERNATEORIGIN else { if (light_behind_surface) { continue; } } #endif #endif #ifdef HLRAD_ACCURATEBOUNCE_REDUCEAREA trans_one *= patch2->exposure; #endif VectorFill(trans, trans_one); VectorMultiply(trans, transparency, trans); //hullu: add transparency effect #ifdef HLRAD_TRANSLUCENT if (patch->translucent_b) { if (useback) { for (int x = 0; x < 3; x++) { trans[x] = patch->translucent_v[x] * trans[x]; } } else { for (int x = 0; x < 3; x++) { trans[x] = (1 - patch->translucent_v[x]) * trans[x]; } } } #endif #ifdef HLRAD_RGBTRANSFIX #ifdef HLRAD_ACCURATEBOUNCE if (trans_one <= 0.0) { continue; } #else if (trans_one >= 0) #endif { #ifndef HLRAD_TRANSWEIRDFIX #ifdef HLRAD_NOSWAP send = trans_one * area; #else send = trans_one * patch2->area; #endif if (send > 0.4f) { #ifdef HLRAD_NOSWAP trans_one = 0.4f / area; #else trans_one = 0.4f / patch2->area; #endif send = 0.4f; VectorFill(trans, trans_one); VectorMultiply(trans, transparency, trans); } #endif /*HLRAD_TRANSWEIRDFIX*/ #ifndef HLRAD_TRANSNONORMALIZE total += send; #endif #else /*HLRAD_RGBTRANSFIX*/ #ifdef HLRAD_ACCURATEBOUNCE if (VectorAvg(trans) <= 0.0) { continue; } #else if (VectorAvg(trans) >= 0) #endif { /////////////////////////////////////////RED send = trans[0] * patch2->area; // Caps light from getting weird if (send > 0.4f) { trans[0] = 0.4f / patch2->area; send = 0.4f; } #ifndef HLRAD_TRANSNONORMALIZE total += send / 3.0f; #endif /////////////////////////////////////////GREEN send = trans[1] * patch2->area; // Caps light from getting weird if (send > 0.4f) { trans[1] = 0.4f / patch2->area; send = 0.4f; } #ifndef HLRAD_TRANSNONORMALIZE total += send / 3.0f; #endif /////////////////////////////////////////BLUE send = trans[2] * patch2->area; // Caps light from getting weird if (send > 0.4f) { trans[2] = 0.4f / patch2->area; send = 0.4f; } #ifndef HLRAD_TRANSNONORMALIZE total += send / 3.0f; #endif #endif /*HLRAD_RGBTRANSFIX*/ #ifdef HLRAD_TRANSFERDATA_COMPRESS VectorScale(trans, patch2 -> area, trans); #else // scale to 16 bit (black magic) #ifdef HLRAD_NOSWAP VectorScale(trans, patch2 -> area * INVERSE_TRANSFER_SCALE, trans); #else VectorScale(trans, area * INVERSE_TRANSFER_SCALE, trans); #endif /*HLRAD_NOSWAP*/ if (trans[0] >= TRANSFER_SCALE_MAX) { trans[0] = TRANSFER_SCALE_MAX; } if (trans[1] >= TRANSFER_SCALE_MAX) { trans[1] = TRANSFER_SCALE_MAX; } if (trans[2] >= TRANSFER_SCALE_MAX) { trans[2] = TRANSFER_SCALE_MAX; } #endif } #ifndef HLRAD_ACCURATEBOUNCE else { #if 0 Warning("transfer < 0 (%4.3f %4.3f %4.3f): dist=(%f)\n" " dot1=(%f) patch@(%4.3f %4.3f %4.3f) normal(%4.3f %4.3f %4.3f)\n" " dot2=(%f) patch@(%4.3f %4.3f %4.3f) normal(%4.3f %4.3f %4.3f)\n", trans[0], trans[1], trans[2], dist, dot1, patch->origin[0], patch->origin[1], patch->origin[2], patch->normal[0], patch->normal[1], patch->normal[2], dot2, patch2->origin[0], patch2->origin[1], patch2->origin[2], patch2->normal[0], patch2->normal[1], patch2->normal[2]); #endif VectorFill(trans,0.0); } #endif #ifdef HLRAD_TRANSFERDATA_COMPRESS VectorCopy(trans, tRGBData); *tIndex = j; tRGBData+=3; tIndex++; patch->iData++; #else VectorCopy(trans, *tRGBData); *tIndex = j; tRGBData++; tIndex++; patch->iData++; #endif count++; } // copy the transfers out if (patch->iData) { #ifdef HLRAD_TRANSFERDATA_COMPRESS unsigned data_size = patch->iData * vector_size[g_rgbtransfer_compress_type] + unused_size; #else unsigned data_size = patch->iData * sizeof(rgb_transfer_data_t); #endif patch->tRGBData = (rgb_transfer_data_t*)AllocBlock(data_size); patch->tIndex = CompressTransferIndicies(tIndex_All, patch->iData, &patch->iIndex); hlassume(patch->tRGBData != NULL, assume_NoMemory); hlassume(patch->tIndex != NULL, assume_NoMemory); ThreadLock(); g_transfer_data_bytes += data_size; ThreadUnlock(); #ifdef HLRAD_REFLECTIVITY total = 1 / Q_PI; #else #ifdef HLRAD_TRANSNONORMALIZE #ifdef HLRAD_TRANSTOTAL_HACK total = g_transtotal_hack / Q_PI; #else total = 0.5 / Q_PI; #endif #else // // normalize all transfers so exactly 50% of the light // is transfered to the surroundings // total = 0.5 / total; #endif #endif { #ifdef HLRAD_TRANSFERDATA_COMPRESS unsigned x; rgb_transfer_data_t* t1 = patch->tRGBData; float* t2 = tRGBData_All; float f[3]; for (x = 0; x < patch->iData; x++, t1+=vector_size[g_rgbtransfer_compress_type], t2+=3) { VectorScale( t2, total, f ); vector_compress (g_rgbtransfer_compress_type, t1, &f[0], &f[1], &f[2]); } #else unsigned x; rgb_transfer_data_t* t1 = patch->tRGBData; rgb_transfer_data_t* t2 = tRGBData_All; for (x = 0; x < patch->iData; x++, t1++, t2++) { VectorScale( *t2, total, *t1 ); } #endif } } } FreeBlock(tIndex_All); FreeBlock(tRGBData_All); ThreadLock(); g_total_transfer += count; ThreadUnlock(); } #ifdef SYSTEM_WIN32 #pragma warning(pop) #endif /* * ============= * SwapTransfersTask * * Change transfers from light sent out to light collected in. * In an ideal world, they would be exactly symetrical, but * because the form factors are only aproximated, then normalized, * they will actually be rather different. * ============= */ #ifndef HLRAD_NOSWAP void SwapRGBTransfers(const int patchnum) { patch_t* patch = &g_patches[patchnum]; transfer_index_t* tIndex = patch->tIndex; rgb_transfer_data_t* tRGBData= patch->tRGBData; unsigned x; for (x = 0; x < patch->iIndex; x++, tIndex++) { unsigned size = (tIndex->size + 1); unsigned patchnum2 = tIndex->index; unsigned y; for (y = 0; y < size; y++, tRGBData++, patchnum2++) { patch_t* patch2 = &g_patches[patchnum2]; if (patchnum2 > patchnum) { // done with this list return; } else if (!patch2->iData) { // Set to zero in this impossible case Log("patch2 has no iData\n"); VectorFill(*tRGBData, 0); continue; } else { transfer_index_t* tIndex2 = patch2->tIndex; rgb_transfer_data_t* tRGBData2 = patch2->tRGBData; int offset = FindTransferOffsetPatchnum(tIndex2, patch2, patchnum); if (offset >= 0) { rgb_transfer_data_t tmp; VectorCopy(*tRGBData, tmp) VectorCopy(tRGBData2[offset], *tRGBData); VectorCopy(tmp, tRGBData2[offset]); } else { // Set to zero in this impossible case Log("FindTransferOffsetPatchnum returned -1 looking for patch %d in patch %d's transfer lists\n", patchnum, patchnum2); VectorFill(*tRGBData, 0); return; } } } } }
afs_int32 ka_NewKey(struct ubik_trans *tt, afs_int32 tentryaddr, struct kaentry *tentry, struct ktc_encryptionKey *key) { struct kaOldKeys okeys; /* old keys block */ afs_int32 okeysaddr, nextaddr; /* offset of old keys block */ afs_int32 prevptr, nextprevptr; int code, i; Date now = time(0); afs_int32 newkeyver; /* new key version number */ afs_int32 newtotalkeyentries = 0, oldtotalkeyentries = 0, keyentries; int addednewkey = 0, modified; #ifdef AUTH_DBM_LOG int foundcurrentkey = 0; #endif es_Report("Newkey for %s.%s\n", tentry->userID.name, tentry->userID.instance); newkeyver = ntohl(tentry->key_version) + 1; if ((newkeyver < 1) || (newkeyver >= MAXKAKVNO)) newkeyver = 1; /* An entry may have more than one oldkeys blocks. The entry * points to the most current, but all the oldkeys blocks for an * entry are not linked together. All oldkeys blocks for all * entries are linked together off of the header. So we follow * this link. */ for (prevptr = 0, okeysaddr = ntohl(cheader.kvnoPtr); okeysaddr; prevptr = nextprevptr, okeysaddr = nextaddr) { /* foreacholdkeysblock */ /* Read the oldKeys block */ code = karead(tt, okeysaddr, (char *)&okeys, sizeof(okeys)); if (code) return code; nextaddr = ntohl(okeys.next); nextprevptr = DOFFSET(okeysaddr, &okeys, &okeys.next); /* We only want oldkey blocks that belong to this entry */ if (ntohl(okeys.entry) != tentryaddr) continue; modified = 0; /* This oldkeys block has not been modified */ keyentries = 0; /* Number of valid key entries in the block */ for (i = 0; i < NOLDKEYS; i++) { /* foreachkey */ /* Keep count of number of entries found */ if (okeys.keys[i].superseded != 0) { oldtotalkeyentries++; } /* If we find the entry that is not superseded, then supersede it */ if (ntohl(okeys.keys[i].superseded) == NEVERDATE) { okeys.keys[i].superseded = htonl(now); modified = 1; #ifdef AUTH_DBM_LOG if (foundcurrentkey) { ViceLog(0, ("Warning: Entry %s.%s contains more than one valid key: fixing\n", tentry->userID.name, tentry->userID.instance)); } foundcurrentkey = 1; #endif } /* If we find an oldkey of the same version or * an old key that has expired, then delete it. */ if ((ntohl(okeys.keys[i].version) == newkeyver) || ((now - ntohl(okeys.keys[i].superseded) > maxKeyLifetime))) { okeys.keys[i].superseded = 0; okeys.keys[i].version = htonl(-1); memset(&okeys.keys[i].key, 0, sizeof(struct ktc_encryptionKey)); modified = 1; es_Report("Dropped oldkey %d seconds old with kvno %d\n", now - ntohl(okeys.keys[i].superseded), ntohl(okeys.keys[i].version)); } /* Add our key here if its free */ if (!addednewkey && (okeys.keys[i].superseded == 0)) { okeys.keys[i].version = htonl(newkeyver); okeys.keys[i].superseded = htonl(NEVERDATE); memcpy(&okeys.keys[i].key, key, sizeof(struct ktc_encryptionKey)); modified = 1; addednewkey = okeysaddr; } /* Keep count of number of entries found */ if (okeys.keys[i].superseded != 0) { keyentries++; newtotalkeyentries++; } } /* foreachkey */ /* If we modified the block, write it out */ if (modified && keyentries) { code = kawrite(tt, okeysaddr, (char *)&okeys, sizeof(okeys)); if (code) return code; } /* If there are no more entries in this oldkeys block, delete it */ if (keyentries == 0) { if (!prevptr) { code = set_header_word(tt, kvnoPtr, okeys.next); } else { code = kawrite(tt, prevptr, (char *)&okeys.next, sizeof(afs_int32)); } if (code) return code; code = FreeBlock(tt, okeysaddr); if (code) return code; nextprevptr = prevptr; /* won't bump prevptr */ } } /* foreacholdkeysblock */ /* If we could not add the key, create a new oldkeys block */ if (!addednewkey) { /* Allocate and fill in an oldkeys block */ addednewkey = AllocBlock(tt, (struct kaentry *)&okeys); if (!addednewkey) return KACREATEFAIL; okeys.flags = htonl(KAFOLDKEYS); okeys.entry = htonl(tentryaddr); okeys.keys[0].version = htonl(newkeyver); okeys.keys[0].superseded = htonl(NEVERDATE); memcpy(&okeys.keys[0].key, key, sizeof(struct ktc_encryptionKey)); newtotalkeyentries++; /* Thread onto the header's chain of oldkeys */ okeys.next = cheader.kvnoPtr; code = set_header_word(tt, kvnoPtr, htonl(addednewkey)); if (code) return code; /* Write the oldkeys block out */ code = kawrite(tt, addednewkey, (char *)&okeys, sizeof(okeys)); if (code) return code; es_Report("New oldkey block allocated at %d\n", addednewkey); } #ifdef AUTH_DBM_LOG if (oldtotalkeyentries != ntohl(tentry->misc.asServer.nOldKeys)) { ViceLog(0, ("Warning: Entry %s.%s reports %d oldkeys, found %d: fixing\n", tentry->userID.name, tentry->userID.instance, ntohl(tentry->misc.asServer.nOldKeys), oldtotalkeyentries)); } #endif /* Update the tentry. We rely on caller to write it out */ tentry->misc.asServer.oldKeys = htonl(addednewkey); tentry->misc.asServer.nOldKeys = htonl(newtotalkeyentries); tentry->key_version = htonl(newkeyver); memcpy(&tentry->key, key, sizeof(tentry->key)); /* invalidate key caches everywhere */ code = inc_header_word(tt, specialKeysVersion); if (code) return code; es_Report("New kvno is %d, now are %d oldkeys\n", newkeyver, newtotalkeyentries); return 0; }
afs_int32 SaveText(struct rx_call *call, afs_uint32 lockHandle, afs_int32 textType, afs_int32 offset, afs_int32 flags, charListT *charListPtr) { struct ubik_trans *ut; struct block diskBlock; dbadr diskBlockAddr; afs_int32 remainingInBlock, chunkSize; struct textBlock *tbPtr; afs_int32 textLength = charListPtr->charListT_len; char *textptr = charListPtr->charListT_val; afs_int32 code; LogDebug(5, "SaveText: type %d, offset %d, length %d\n", textType, offset, textLength); if (callPermitted(call) == 0) return (BUDB_NOTPERMITTED); if ((textLength > BLOCK_DATA_SIZE) || (offset < 0)) return (BUDB_BADARGUMENT); code = InitRPC(&ut, LOCKWRITE, 1); if (code) return (code); /* fetch the lock state */ if (checkLockHandle(ut, lockHandle) == 0) ABORT(BUDB_NOTLOCKED); if ((textType < 0) || (textType >= TB_NUM)) ABORT(BUDB_BADARGUMENT); tbPtr = &db.h.textBlock[textType]; LogDebug(5, "SaveText: lockHandle %d textType %d offset %d flags %d txtlength %d\n", lockHandle, textType, offset, flags, textLength); if (offset == 0) { /* release any blocks from previous transactions */ diskBlockAddr = ntohl(tbPtr->newTextAddr); freeOldBlockChain(ut, diskBlockAddr); if (textLength) { code = AllocBlock(ut, &diskBlock, &diskBlockAddr); if (code) ABORT(code); LogDebug(5, "allocated block %d\n", diskBlockAddr); /* set block type */ diskBlock.h.type = text_BLOCK; /* save it in the database header */ tbPtr->newsize = 0; tbPtr->newTextAddr = htonl(diskBlockAddr); dbwrite(ut, (char *)tbPtr - (char *)&db.h, (char *)tbPtr, sizeof(struct textBlock)); } else { tbPtr->newsize = 0; tbPtr->newTextAddr = 0; } } else { /* non-zero offset */ int nblocks; if (offset != ntohl(tbPtr->newsize)) ABORT(BUDB_BADARGUMENT); /* locate the block to which offset refers */ nblocks = offset / BLOCK_DATA_SIZE; diskBlockAddr = ntohl(tbPtr->newTextAddr); if (diskBlockAddr == 0) ABORT(BUDB_BADARGUMENT); code = dbread(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); while (nblocks--) { diskBlockAddr = ntohl(diskBlock.h.next); code = dbread(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); } } /* diskBlock and diskBlockAddr now point to the last block in the chain */ while (textLength) { /* compute the transfer size */ remainingInBlock = (BLOCK_DATA_SIZE - (offset % BLOCK_DATA_SIZE)); chunkSize = MIN(remainingInBlock, textLength); /* copy in the data */ memcpy(&diskBlock.a[offset % BLOCK_DATA_SIZE], textptr, chunkSize); /* LogDebug(5, "text is %s\n", textptr); */ textLength -= chunkSize; textptr += chunkSize; offset += chunkSize; tbPtr->newsize = htonl(ntohl(tbPtr->newsize) + chunkSize); if (textLength > 0) { afs_int32 prevBlockAddr; afs_int32 linkOffset; afs_int32 linkValue; /* have to add another block to the chain */ code = dbwrite(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); prevBlockAddr = (afs_int32) diskBlockAddr; code = AllocBlock(ut, &diskBlock, &diskBlockAddr); if (code) ABORT(code); LogDebug(5, "allocated block %d\n", diskBlockAddr); /* set block type */ diskBlock.h.type = text_BLOCK; /* now have to update the previous block's link */ linkOffset = (afs_int32) ((char*)& diskBlock.h.next - (char*)& diskBlock); linkValue = htonl(diskBlockAddr); code = dbwrite(ut, (afs_int32) prevBlockAddr + linkOffset, (char *)&linkValue, sizeof(afs_int32)); if (code) ABORT(code); } else { /* just write the old block */ code = dbwrite(ut, diskBlockAddr, (char *)&diskBlock, sizeof(diskBlock)); if (code) ABORT(code); } } if (flags & BUDB_TEXT_COMPLETE) { /* done */ /* this was the last chunk of text */ diskBlockAddr = ntohl(tbPtr->textAddr); freeOldBlockChain(ut, diskBlockAddr); tbPtr->textAddr = tbPtr->newTextAddr; tbPtr->newTextAddr = 0; tbPtr->size = tbPtr->newsize; tbPtr->newsize = 0; tbPtr->version = htonl(ntohl(tbPtr->version) + 1); /* saveTextToFile(ut, tbPtr); */ } /* update size and other text header info */ code = dbwrite(ut, (char *)tbPtr - (char *)&db.h, (char *)tbPtr, sizeof(struct textBlock)); if (code) ABORT(code); /*error_exit: */ code = ubik_EndTrans(ut); return (code); abort_exit: ubik_AbortTrans(ut); return (code); }