void createHeadersFly(BucketFLY *bucket, unsigned int newHeadersToGenerate, // Number of symbols to be retrieved unsigned int offset) { double sel; unsigned int genDeg; unsigned int i, j; unsigned int w = bucket->w; double *cumulative = bucket->cumulative; if (newHeadersToGenerate<1) return; if ((bucket->genSymbs == 0)&&(bucket->symbols == NULL)) bucket->symbols = (Symbol *) chk_calloc(newHeadersToGenerate, sizeof(Symbol)); else { bucket->symbols = (Symbol *) chk_realloc(bucket->symbols, bucket->genSymbs + newHeadersToGenerate, sizeof(Symbol)); for (i=0; i<newHeadersToGenerate; i++) { bucket->symbols[bucket->genSymbs+i].info = NULL; bucket->symbols[bucket->genSymbs+i].header = NULL; bucket->symbols[bucket->genSymbs+i].deg = 0; bucket->symbols[bucket->genSymbs+i].symbLen = bucket->symbLen; } } unsigned int *selectedSymbols = NULL; selectedSymbols = (unsigned int*) chk_malloc(w, sizeof(unsigned int)); for (i=0; i<newHeadersToGenerate; i++) { // Create header (the symbols are chosen between 0 and w) // therefore shifting is needed. genDeg = degree(cumulative, w, real(&bucket->buckRan)); if (genDeg > w) genDeg = w; if (genDeg == w) { chooseAllInWindowCore(w, offset, selectedSymbols, genDeg); } else { for (j=0; j<genDeg; j++) { sel = real(&bucket->buckRan); selectedSymbols[j] = rand2windowCore(w, offset, sel); if (verifySelectedSymbol(selectedSymbols, j) == False) { j--; // Doesn't repeat encoding symbols } } // sort(selectedSymbols, 0, genDeg-1); // Sort the chosen encoding symbols' list } #ifdef DEBUGdecoderPrintHeaders int deb_index; printf("BUCKET[%d] - symb[%d]\t: deg=%3d\t -> ", bucket->nameBucket, bucket->genSymbs, genDeg); for (deb_index=0; deb_index<genDeg; deb_index++) printf("%3d ", selectedSymbols[deb_index]); printf("\n"); #endif bucket->symbols[bucket->genSymbs].header = (unsigned int *) chk_malloc(genDeg, sizeof(unsigned int *)); memcpy(bucket->symbols[bucket->genSymbs].header, selectedSymbols, genDeg*sizeof(unsigned int)); bucket->symbols[bucket->genSymbs].deg = genDeg; bucket->symbols[bucket->genSymbs].symbLen = bucket->symbLen; bucket->genSymbs++; } #ifdef DEBUGdecoderPrintHeaders printf("\n"); #endif free(selectedSymbols); selectedSymbols = NULL; return; }
unsigned long serializeSymbolList_explicit_append(Symbol* encodedSymbols, unsigned int N, char** pTlvData) { char version = 0x01; // explicit if (encodedSymbols == NULL) { print_error("serializeSymbolList_explicit_append: The symbol list is empty!"); return 0; } if (N == 0) { print_error("serializeSymbolList_explicit_append: The number of symbols to be converted to TLV format has to specified!"); return 0; } // If the TLV-like buffer is empty, call serializeSymbolList_explicit() if (*pTlvData == NULL) { return serializeSymbolList_explicit(encodedSymbols, N, pTlvData); } char* tlvData = NULL; // ====== Read Header ====== unsigned long long pos = 0; unsigned short headerLen; char bufferVersion; unsigned long long oldDataLen; unsigned int oldN; readSerializedBasicHeader(tlvData, &headerLen, &bufferVersion, &oldN, &oldDataLen, &pos); if (headerLen > pos) { // TODO readSerializedHeaderExtensions(tlvData, &pos); } // Check version if (bufferVersion != version) { print_error("serializeSymbolList_explicit_append: The TLV-like buffer is not explicit as required by this function.\n" "New symbols not appended.\n"); return (2+headerLen+oldDataLen); } // ====== Create the data_buff ====== char* data_buff = NULL; unsigned long data_buff_len = 0; unsigned int i_symb = 0; for (i_symb = 0; i_symb < N; i_symb++) { char *symb_buff = NULL; // Serialize the current symbol unsigned int tlvSymbLen = serializeSymbol(&encodedSymbols[i_symb], version, &symb_buff); // Reallocate the data_buff data_buff = (char*) chk_realloc(data_buff, data_buff_len+tlvSymbLen, sizeof(char)); // Copy symb_buff at the end of the data_buff memcpy((char*)(data_buff+data_buff_len), symb_buff, tlvSymbLen*sizeof(char)); // Update the length variable of the data_buff data_buff_len += tlvSymbLen; // free the data_buff variable free(data_buff); } // Overwrite the total number of symbols that will be present in the new stream (this filed starts at the 6th byte of the header) unsigned int newN = oldN + N; memcpy((char*)(tlvData+5), &newN, sizeof(unsigned int)); // Now we have the value of the data_buff_len and we can update the field in the header (that starts at the 18th byte of the header) unsigned long newDataLen = oldDataLen + data_buff_len; memcpy((char*)(tlvData+17), &newDataLen, sizeof(unsigned long)); // Reallocate the TLV-like buffer tlvData = (char*) chk_realloc(tlvData, (2+headerLen)+newDataLen, sizeof(char)); // copy the data_buff with the serialized new symbols memcpy((char*)(tlvData+(2+headerLen)+oldDataLen), data_buff, newDataLen*sizeof(char)); // Make pTlvData point to the beginning of the generated data. *pTlvData = tlvData; return (2+headerLen)+newDataLen; }
/* * Dynamically allocate & initialize a new 1d histogram */ hist1d_p hist1d_new(int id, char *name, int nbins, float low, float high) { int i; hist1d_p h = 0; #if 0 fprintf(dbgout, "Input (1) -> %d %s %d %6.1f %6.1f\n", id, name, nbins, low, high); #endif if (hist1d_idtoptr(id) != 0) { fprintf(dbgout, "ID %d already in use!\n", id); return 0; /* ID already in use */ } h = (hist1d_p) chk_malloc(sizeof(hist1d_t)); h->typetag = hist1d_typetag; h->hid = id; h->imanaged = -1; if (id != 0) { /* add to "managed" list */ int i = 0; for (i = 0; i < nmanaged; i++) if (managed[i].p == 0) { h->imanaged = i; /* re-use old (deleted) index */ break; } if (h->imanaged == -1) { /* there is no existing index to re-use */ if (nmanaged >= mmanaged) { /* need more memory */ int minmanaged = 16; mmanaged = (mmanaged < minmanaged) ? minmanaged : 2*mmanaged; managed = chk_realloc(managed, mmanaged*sizeof(managed[0])); assert(managed != 0); } assert(nmanaged < mmanaged); h->imanaged = nmanaged++; } assert(h->imanaged >= 0 && h->imanaged < nmanaged); managed[h->imanaged].id = h->hid; managed[h->imanaged].p = h; } h->nbins = nbins; h->low = low; h->high = high; h->ncalls = 0; h->nunder = 0; h->nover = 0; h->bin = (float *) chk_malloc(nbins*sizeof(float)); h->error = (float *) chk_malloc(nbins*sizeof(float)); for (i = 0; i < nbins; i++) { h->bin[i] = 0.0; h->error[i] = 0.0; } h->name = (char *) chk_malloc(1+strlen(name)); strcpy(h->name, name); fprintf(dbgout, "hist1d_new: created at p = %p id = %d \"%s\" with\n", h, h->hid, h->name); fprintf(dbgout, " %d bins from %7.2f to %7.2f\n", h->nbins, h->low, h->high); nhist++; return h; }
unsigned long long serializeSymbolList_explicit(Symbol* encodedSymbols, unsigned int N, char** pTlvData) { char* tlvData = NULL; char version = 0x01; // explicit short int valueHeaderLen = 0; unsigned int totHeaderLen = 0; unsigned int i = 0; if (encodedSymbols == NULL) { print_error("symbolList2TLV_explicit: The symbol list is empty!"); return 0; } if (N == 0) { print_error("symbolList2TLV_explicit: The number of symbols to be converted to TLV format has to specified!"); return 0; } // Compute header length valueHeaderLen = sizeof(char) + // TLV_HEADER__VERSION sizeof(char) + // TLV_HEADER__VERSION value sizeof(char) + // TLV_HEADER__NUM_SYMBS sizeof(unsigned int) + // TLV_HEADER__NUM_SYMBS value sizeof(char) + // TLV_HEADER__DATA_LENGTH sizeof(unsigned long long); // TLV_HEADER__DATA_LENGTH value totHeaderLen = sizeof(char) + // TLV_HEADER__LENGTH sizeof(char) + // TLV_HEADER__LENGTH value valueHeaderLen; // Allocate buffer for header char* header_buff = (char*) chk_calloc(totHeaderLen, sizeof(char)); // Create header header_buff[i] = TLV_HEADER__LENGTH; i += sizeof(char); header_buff[i] = (char) valueHeaderLen; i += sizeof(char); header_buff[i] = TLV_HEADER__VERSION; i += sizeof(char); header_buff[i] = version; // explicit i += sizeof(char); header_buff[i] = TLV_HEADER__NUM_SYMBS; i += sizeof(char); memcpy((char*)(header_buff+i), (unsigned int*) &N, sizeof(unsigned int)); i += sizeof(unsigned int); header_buff[i] = TLV_HEADER__DATA_LENGTH; i += sizeof(char); // We still have to add the total length of the data! // Create the data_buff char* data_buff = NULL; unsigned long long data_buff_len = 0; unsigned int i_symb = 0; for (i_symb = 0; i_symb < N; i_symb++) { char *symb_buff = NULL; unsigned int tlvSymbLen = 0; // Serialize the current symbol tlvSymbLen = serializeSymbol(&encodedSymbols[i_symb], version, &symb_buff); // Reallocate the data_buff data_buff = (char*) chk_realloc(data_buff, data_buff_len+tlvSymbLen, sizeof(char)); // Copy symb_buff at the end of the data_buff memcpy((char*)(data_buff + data_buff_len), symb_buff, tlvSymbLen*sizeof(char)); // Update the length variable of the data_buff data_buff_len += tlvSymbLen; // free the symb_buff variable free(symb_buff); } // Now we have the value of the data_buff_len and we can update the field in the header memcpy((char*)(header_buff+i), &data_buff_len, sizeof(unsigned long long)); i += sizeof(unsigned long long); // Create the tlv stream tlvData = (char*) chk_calloc(totHeaderLen+data_buff_len, sizeof(char)); // Copy the header memcpy(tlvData, header_buff, totHeaderLen*sizeof(char)); // Copy the serialized data memcpy((char*)(tlvData+totHeaderLen), data_buff, data_buff_len*sizeof(char)); // Free temporary buffers free(header_buff); free(data_buff); // Make pTlvData point to the beginning of the generated data. *pTlvData = tlvData; return (unsigned long long) totHeaderLen+data_buff_len; }