Beispiel #1
0
unsigned int receiveEncodedSymbFLY(LTdecoderFLY *decoder, Symbol* encodedSymbols,
                                   unsigned int numberEncodedSymbols) {

    unsigned int i;
    unsigned int count = 0;
    Node *ptr= NULL;

    if (decoder == NULL) {
        print_error("(libLT_C:LTdecoderFLY.c:receiveEncodedSymbFLY) ERROR - Decoder pointer not correctly passed.\n");
        return 0;
    }

    if ((encodedSymbols == NULL)||(numberEncodedSymbols == 0))
        return 0;

    for (i = 0; i < numberEncodedSymbols; i++) {

        Symbol* currSymb = &(encodedSymbols[i]);

        // Create Node
        if (decoder->RXsymbList == NULL) {
            ptr = addNode(NULL);
            decoder->RXsymbList = ptr;
        } else {
            ptr = addNode(decoder->lastRXsymbol);
            decoder->lastRXsymbol->next = ptr;
        }
        decoder->lastRXsymbol = ptr;
        count++;

        // Alloc a new symbol in the decoder
        ptr->data = (Symbol *) chk_calloc(1, sizeof(Symbol));
        Symbol *newSymbol = (Symbol *)ptr->data;

        // Deg
        newSymbol->deg = currSymb->deg;

        // Symb Len
        newSymbol->symbLen = currSymb->symbLen;

        // Header
        newSymbol->header =
            (unsigned int *) chk_malloc(newSymbol->deg, sizeof(unsigned int));
        memcpy(newSymbol->header,
               currSymb->header,
               newSymbol->deg*sizeof(unsigned int));

        // Info
        newSymbol->info =
            (char *) chk_malloc(currSymb->symbLen, sizeof(char));
        memcpy(newSymbol->info,
               currSymb->info,
               currSymb->symbLen*sizeof(char));

        // Update the number of received symbols
        decoder->NumRecSymbols++;
    }
    return count;
}
Beispiel #2
0
int addBucket_nw(LTdecoder* decoder, unsigned long seed, int symblen,
                 unsigned int k, unsigned int w, unsigned int s, double lt_delta,
                 double lt_c, double eps, unsigned int N, DegDistr code, unsigned int nw, unsigned int Ns) {

    int nameNewBucket = 0;
    Node *current = decoder->bucketList;
    Node *ptr= NULL;

    if (decoder->bucketList == NULL) {
        ptr = addNode(NULL);
        decoder->bucketList = ptr;
    } else {
        while (current->next != NULL) {
            current = current->next;
            nameNewBucket++;
        }
        ptr = addNode(current);
        current->next = ptr;
    }

    ptr->data = (void *) chk_malloc(1, sizeof(Bucket));

    ((Bucket *)ptr->data)->matrix.symbols = NULL;
    ((Bucket *)ptr->data)->matrix.cumulative = NULL;
    ((Bucket *)ptr->data)->nameBucket = nameNewBucket;

    if (nw == 0)
        initBucket((Bucket *) ptr->data, seed, symblen, k, w, s, lt_delta,
                   lt_c, eps, N, code);
    else
        initBucket_nw((Bucket *) ptr->data, seed, symblen, k, w, s, lt_delta,
                      lt_c, eps, N, code, nw, Ns);

    return nameNewBucket;
}
void *chk_realloc(void *ptr, size_t size)
{
    struct hdr *hdr;

//  log_message("%s: %s\n", __FILE__, __FUNCTION__);

    if (!size) {
        chk_free(ptr);
        return NULL;
    }

    if (!ptr)
        return chk_malloc(size);

    hdr = meta(ptr);

    if (del(hdr) < 0) {
        intptr_t bt[MAX_BACKTRACE_DEPTH];
        int depth;
        depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
        if (hdr->tag == BACKLOG_TAG) {
            log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
                       user(hdr), size, hdr->size);
            log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(hdr->bt, hdr->bt_depth);
            /* hdr->freed_bt_depth should be nonzero here */
            log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
            log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            print_backtrace(bt, depth);

             /* We take the memory out of the backlog and fall through so the
             * reallocation below succeeds.  Since we didn't really free it, we
             * can default to this behavior.
             */
            del_from_backlog(hdr);
        }
        else {
            log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
                       user(hdr), size);
            print_backtrace(bt, depth);
            // just get a whole new allocation and leak the old one
            return dlrealloc(0, size);
            // return dlrealloc(user(hdr), size); // assuming it was allocated externally
        }
    }

    hdr = dlrealloc(hdr, sizeof(struct hdr) + size + sizeof(struct ftr));
    if (hdr) {
        hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, size);
        return user(hdr);
    }

    return NULL;
}
Beispiel #4
0
unsigned int genRXSymb(Bucket *bucket, char *encodedInfo, unsigned int length,
                       Node **RXsymbList, Node **lastRXsymbol, unsigned int start) {

    unsigned int i;
    unsigned int count = 0;
    Node *ptr= NULL;
    unsigned int packetSymbols = (unsigned int)(length/(bucket->symbLen));


    for (i=0; i<packetSymbols; i++) {

        // Create Node
        if ((*RXsymbList) == NULL) {
            ptr = addNode(NULL);
            (*RXsymbList) = ptr;
        } else {
            ptr = addNode((*lastRXsymbol));
            (*lastRXsymbol)->next = ptr;
        }
        (*lastRXsymbol) = ptr;
        count++;

        ptr->data = (void *) chk_malloc(1, sizeof(Symbol));

        // Deg
        ((Symbol *)ptr->data)->deg
        = bucket->matrix.symbols[start+i].deg;

        // Header
        ((Symbol *)ptr->data)->header = (unsigned int *) chk_malloc(
                                            ((Symbol *)ptr->data)->deg, sizeof(unsigned int));
        memcpy(((Symbol *)ptr->data)->header,
               bucket->matrix.symbols[start+i].header,
               ((Symbol *)ptr->data)->deg * sizeof(unsigned int));

        // Info
        ((Symbol *)ptr->data)->info
        = (char *) chk_malloc(bucket->symbLen, sizeof(char));
        memcpy(((Symbol *)ptr->data)->info,
               &encodedInfo[i*bucket->symbLen],
               bucket->symbLen * sizeof(char));

    }
    return count;
}
Beispiel #5
0
LTdecoder* mallocDecoder(void) {
    LTdecoder *dec = chk_malloc(1, sizeof(LTdecoder));

    dec->RXsymbList = NULL;
    dec->lastRXsymbol = NULL;
    dec->decoded = NULL;
    dec->bucketList = NULL;
    dec->NumRecSymbols = 0;
    dec->NumDecSymbols = 0;
    dec->decodingStatus = NULL;

    // SIMD
    dec->xorType = isSIMD();

    return dec;
}
Beispiel #6
0
LTdecoderFLY* mallocDecoderFLY(unsigned int decodedSymbolsBufferSize) {
    LTdecoderFLY *dec = chk_malloc(1, sizeof(LTdecoderFLY));

    dec->RXsymbList = NULL;
    dec->lastRXsymbol = NULL;
    dec->bucketFLYList = NULL;
    dec->NumRecSymbols = 0;
    dec->NumDecSymbols = 0;
    dec->decoded = NULL;
    dec->k = decodedSymbolsBufferSize;
    dec->decodingStatus = NULL;

    // SIMD
    dec->xorType = isSIMD();

    return dec;
}
Beispiel #7
0
LTencoderFLY* mallocEncoderFLY(unsigned long seed) {

    LTencoderFLY *encoder = chk_malloc(1, sizeof(LTencoderFLY));
    encoder->refRandomSeed = seed;	// set the first randomSeed

    encoder->xorType	= isSIMD();
    encoder->totGenSymbols	= 0;

    // Create the LThashTable for the degree distributions
    encoder->DistributionsTable = create_hashtable(MINHASHTABLESIZE, hashfromkey, equalkeys);
    if (encoder->DistributionsTable == NULL)
        exit(-1); /*oom*/

#ifdef DEBUGencoderPrintBucketRandomSeed
    printf("Encoder[malloc()] - Seed: %lu\n", encoder->refRandomSeed);
#endif

    return encoder;
}
Beispiel #8
0
void reinitDecoder(LTdecoder *decoder) {
    Node *temp= NULL;

    if ((Bucket *)decoder->bucketList == NULL) {
        print_error("ERROR: reinitDecoder - no bucket.\n");
        exit(EXIT_FAILURE);
    }

    unsigned int k = ((Bucket *)decoder->bucketList->data)->matrix.conf.k;
    unsigned int symbLen = ((Bucket *)decoder->bucketList->data)->symbLen;

    if (decoder->bucketList != NULL) {
        decoder->NumDecSymbols = 0;
        decoder->NumRecSymbols = 0;

        // Free RXsymbList list
        if (decoder->RXsymbList != NULL) {

            while (decoder->RXsymbList != NULL) {
                temp = decoder->RXsymbList->next;

                if (decoder->RXsymbList->data != NULL) {
                    // Free Symbol structure
                    free(((Symbol *)decoder->RXsymbList->data)->header);
                    free(((Symbol *)decoder->RXsymbList->data)->info);

                    free(decoder->RXsymbList->data);
                    decoder->RXsymbList->data = NULL;
                }

                free(decoder->RXsymbList);
                decoder->RXsymbList = temp;
            }
        }

        // Reset decoded symbol vector
        if (decoder->decoded == NULL) {
            decoder->decoded = (char *) chk_malloc(k*symbLen, sizeof(char));
        }
    } else
        print_error("Error: reinitDecoder failed - No buckets allocated.\n");
}
Beispiel #9
0
void interleaver2(char **vect, unsigned int vectlen, unsigned int blklen) {
    int i, j;
    int z = 0;
    unsigned int Nblks = vectlen/blklen;
    char *tmp = NULL;

    if (vectlen%blklen) {
        print_error("Warning: Interleaving not done because the input vector cannot be evenly divided in blocks as specified!\n");
    } else {
        tmp = (char*) chk_malloc(vectlen, sizeof(char));
        for (i=0; i<blklen; i++) {
            for (j=0; j<Nblks; j++) {
                tmp[z] = (*vect)[j*blklen+i];
                z++;
            }
        }
        memcpy((*vect), tmp, vectlen*sizeof(char));
        free(tmp);
    }
    return;
}
Beispiel #10
0
int set_breakpoint(mach_port_t task, vm_address_t target_addr, struct breakpoint_entry *brk_ptr_list)
{
    register struct breakpoint_entry *p;
    int alloc_required = 1;
    unsigned char breakpoint_code = 0xcc;	/* int3 */

    /* search the entry */
    for(p = brk_ptr_list; p->next != NULL; p = p->next) {
        if (p->addr == target_addr) {
            alloc_required = 0;
            break;
        }
    }
    if (alloc_required == 1 && p->addr == target_addr) {
        alloc_required = 0;
    }

    /* new entry -- allocate and initialize */
    if (alloc_required == 1) {
        p->next = chk_malloc(sizeof(struct breakpoint_entry));
        /* skip */
        p = p->next;
        memset(p, 0x00, sizeof(struct breakpoint_entry));
        p->addr = target_addr;
        /* save original instruction code */
        read_write_process_memory(task, target_addr, &(p->orig_inst_code), NULL, sizeof(unsigned char));
    }


    /* set int3(0xcc) instruction code */
    if (read_write_process_memory(task, target_addr, NULL, &breakpoint_code, sizeof(unsigned char)) ) {
        p->valid = 1;
        /* for debugging
        fprintf(stderr, "set break at 0x%llx\n", target_addr);
        */
        return 0;
    } else {
        return -1;
    }
}
extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
    if (DebugCallsDisabled()) {
        return g_malloc_dispatch->memalign(alignment, bytes);
    }

    if (alignment <= MALLOC_ALIGNMENT) {
        return chk_malloc(bytes);
    }

    // Make the alignment a power of two.
    if (!powerof2(alignment)) {
        alignment = BIONIC_ROUND_UP_POWER_OF_2(alignment);
    }

    // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
    // we will align by at least MALLOC_ALIGNMENT bytes
    // and at most alignment-MALLOC_ALIGNMENT bytes
    size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
    if (size < bytes) { // Overflow.
        return NULL;
    }

    void* base = g_malloc_dispatch->malloc(sizeof(hdr_t) + size + sizeof(ftr_t));
    if (base != NULL) {
        // Check that the actual pointer that will be returned is aligned
        // properly.
        uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
        if ((ptr % alignment) != 0) {
            // Align the pointer.
            ptr += ((-ptr) % alignment);
        }

        hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
        hdr->base = base;
        hdr->bt_depth = GET_BACKTRACE(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, bytes);
        return user(hdr);
    }
    return base;
}
Beispiel #12
0
Datei: pf.c Projekt: aosm/X11apps
void process_line (char *buffer)
{
    int len;
    int i;
    char *cp;

    /* copy buffer since it may point to unwritable date */
    len = strlen(buffer);
    cp = chk_malloc(len + 1);
    strcpy(cp, buffer);
    buffer = cp;
    
    for (i = 0; i < len; i++) {		/* look for blank lines */
	register char c = buffer[i];
	if (!(isspace(c) || c == '\n')) break;
    }
    if (i == len) return;

    cp = &buffer[i];

    if (*cp == '!') return;		/* look for comments */
    len -= (cp - buffer);		/* adjust len by how much we skipped */

					/* pipe through cpp */

					/* strip trailing space */
    for (i = len-1; i >= 0; i--) {
	register char c = cp[i];
	if (!(isspace(c) || c == '\n')) break;
    }
    if (i >= 0) cp[len = (i+1)] = '\0';  /* nul terminate */

    if (verbose) {
	printf ("! %d:  %s\n", lineno+1, cp);
    }

    /* handle input */
    handle_line (cp, len);
}
Beispiel #13
0
void parse_opt(int argc, char **argv, struct execute_context *exec_ctx)
{
    int i;
    int j;
    int optdict_index;
    char *p;
    /* initializing and store the arguments */
    exec_ctx->argc = (unsigned int)argc;
    exec_ctx->argv = argv;
    exec_ctx->opt_flags = 0x00;
    exec_ctx->fullpath = NULL;

    /* parse options for this program */
    for(i = 1; i <= exec_ctx->argc; i++) {
        if ( (*exec_ctx->argv[i]) == '-') {
            optdict_index = lookup_optnum(exec_ctx->argv[i]);
            if (optdict_index != -1) {
                debug_printf(stderr, "argument for this program: %s\n", exec_ctx->argv[i]);
                exec_ctx->opt_flags |= options[optdict_index].opt_num;
            }
        } else {
            break;
        }
    }
    /* copy options for forked (target) program */
    exec_ctx->passing_args_count = exec_ctx->argc - i;
    exec_ctx->passing_args = chk_malloc( sizeof(char*) * (exec_ctx->passing_args_count + 1) );
    memset(exec_ctx->passing_args, 0x00, sizeof(char*) * (exec_ctx->passing_args_count + 1) );
    for(j = 0 ; i <= exec_ctx->passing_args_count; i++, j++) {
        exec_ctx->passing_args[j] = exec_ctx->argv[i];
        debug_printf(stderr, "passing: %s\n", exec_ctx->argv[i]);
    }
    p = search_binary_path(exec_ctx->passing_args[0]);
    if (p != NULL) {
        exec_ctx->passing_args[0] = p;
    }
    return;
}
extern "C" void* chk_memalign(size_t alignment, size_t bytes) {
    if (alignment <= MALLOC_ALIGNMENT) {
        return chk_malloc(bytes);
    }

    // Make the alignment a power of two.
    if (alignment & (alignment-1)) {
        alignment = 1L << (31 - __builtin_clz(alignment));
    }

    // here, alignment is at least MALLOC_ALIGNMENT<<1 bytes
    // we will align by at least MALLOC_ALIGNMENT bytes
    // and at most alignment-MALLOC_ALIGNMENT bytes
    size_t size = (alignment-MALLOC_ALIGNMENT) + bytes;
    if (size < bytes) { // Overflow.
        return NULL;
    }

    void* base = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
    if (base != NULL) {
        // Check that the actual pointer that will be returned is aligned
        // properly.
        uintptr_t ptr = reinterpret_cast<uintptr_t>(user(reinterpret_cast<hdr_t*>(base)));
        if ((ptr % alignment) != 0) {
            // Align the pointer.
            ptr += ((-ptr) % alignment);
        }

        hdr_t* hdr = meta(reinterpret_cast<void*>(ptr));
        hdr->base = base;
        hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, bytes);
        return user(hdr);
    }
    return base;
}
extern "C" void* chk_realloc(void* ptr, size_t size) {
//  log_message("%s: %s\n", __FILE__, __FUNCTION__);

    if (!ptr) {
        return chk_malloc(size);
    }

#ifdef REALLOC_ZERO_BYTES_FREE
    if (!size) {
        chk_free(ptr);
        return NULL;
    }
#endif

    hdr_t* hdr = meta(ptr);

    if (del(hdr) < 0) {
        uintptr_t bt[MAX_BACKTRACE_DEPTH];
        int depth = get_backtrace(bt, MAX_BACKTRACE_DEPTH);
        if (hdr->tag == BACKLOG_TAG) {
            log_message("+++ REALLOCATION %p SIZE %d OF FREED MEMORY!\n",
                       user(hdr), size, hdr->size);
            log_message("+++ ALLOCATION %p SIZE %d ALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(hdr->bt, hdr->bt_depth);
            /* hdr->freed_bt_depth should be nonzero here */
            log_message("+++ ALLOCATION %p SIZE %d FIRST FREED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(hdr->freed_bt, hdr->freed_bt_depth);
            log_message("+++ ALLOCATION %p SIZE %d NOW BEING REALLOCATED HERE:\n",
                       user(hdr), hdr->size);
            log_backtrace(bt, depth);

             /* We take the memory out of the backlog and fall through so the
             * reallocation below succeeds.  Since we didn't really free it, we
             * can default to this behavior.
             */
            del_from_backlog(hdr);
        } else {
            log_message("+++ REALLOCATION %p SIZE %d IS CORRUPTED OR NOT ALLOCATED VIA TRACKER!\n",
                       user(hdr), size);
            log_backtrace(bt, depth);
            // just get a whole new allocation and leak the old one
            return dlrealloc(0, size);
            // return dlrealloc(user(hdr), size); // assuming it was allocated externally
        }
    }

    if (hdr->base != hdr) {
        // An allocation from memalign, so create another allocation and
        // copy the data out.
        void* newMem = dlmalloc(sizeof(hdr_t) + size + sizeof(ftr_t));
        if (newMem) {
            memcpy(newMem, hdr, sizeof(hdr_t) + hdr->size);
            dlfree(hdr->base);
            hdr = static_cast<hdr_t*>(newMem);
        } else {
            dlfree(hdr->base);
            hdr = NULL;
        }
    } else {
        hdr = static_cast<hdr_t*>(dlrealloc(hdr, sizeof(hdr_t) + size + sizeof(ftr_t)));
    }
    if (hdr) {
        hdr->base = hdr;
        hdr->bt_depth = get_backtrace(hdr->bt, MAX_BACKTRACE_DEPTH);
        add(hdr, size);
        return user(hdr);
    }

    return NULL;
}
Beispiel #16
0
unsigned long deserializeSymbol(char* tlvData, unsigned long long startPos, Symbol* encodedSymbol) {

    Tbool isID = False;
    Tbool isINFO = False;
    Tbool isENCSYMBS = False;

    unsigned long long pos = startPos;

    char checkType = tlvData[startPos];
    if (checkType != TLV_DATA__SYMBOL) {
        print_error("deserializeSymbol: Impossible to read symbol at position %lu", pos);
        return 0;
    }
    pos += sizeof(char);

    // Read the length of the Symbol
    unsigned int length;
    memcpy(&length, (char*)(tlvData+pos), sizeof(unsigned int));
    pos += sizeof(unsigned int);

    while (pos-startPos < length) {

        char type = tlvData[pos];
        pos += sizeof(char);

        switch (type) {
        case TLV_DATA__ID: {
            // Read the id
            memcpy(&encodedSymbol->id, (char*)(tlvData+pos), sizeof(unsigned int));
            pos += sizeof(unsigned int);
            // Set the corresponding flag to true
            isID = True;
            break;
        }
        case TLV_DATA__INFO: {
            // Read symblen
            memcpy(&encodedSymbol->symbLen, (char*)(tlvData+pos), sizeof(int));
            pos += sizeof(int);
            // Read info
            encodedSymbol->info = (char*) chk_malloc(encodedSymbol->symbLen, sizeof(char));
            memcpy(encodedSymbol->info, (char*)(tlvData+pos), encodedSymbol->symbLen*sizeof(char));
            pos += encodedSymbol->symbLen*sizeof(char);
            // Set the corresponding flag to true
            isINFO = True;
            break;
        }
        case TLV_DATA__ENCODING_SYMBS: {
            // Read degree
            memcpy(&encodedSymbol->deg, (char*)(tlvData+pos), sizeof(unsigned int));
            pos += sizeof(unsigned int);
            // Read the encoding symbol
            encodedSymbol->header = (unsigned int*) chk_malloc(encodedSymbol->deg, sizeof(unsigned int));
            memcpy(encodedSymbol->header, (char*)(tlvData+pos), encodedSymbol->deg*sizeof(unsigned int));
            pos += encodedSymbol->deg*sizeof(unsigned int);
            // Set the corresponding flag to true
            isENCSYMBS = True;
            break;
        }
        default: {
            print_error("deserializeSymbol: Not recognized symbol type!");
            free(encodedSymbol);
            return 0;
        }
        } // end switch
    } // end while

    // Check that the symbol is compliant to either implicit or explicit
    if (!((isENCSYMBS && isINFO)||(isID && isINFO))) {
        print_error("deserializeSymbol: symbol is not compliant to either implicit nor explicit!");
        free(encodedSymbol);
        return 0;
    }

    return pos;
}
Beispiel #17
0
unsigned int receiveSymbFLY(LTdecoderFLY *decoder, BucketFLY *bucket,
                            char *encodedInfo,			// encodedInfo is the vector of encoded bits
                            unsigned int encodedInfoLength,  	// length of the encodedInfo vector (thus number of encoded bits)
                            unsigned int IDfirstSymb,			// ID of the first encoded symbol
                            unsigned long offset) {				// ID of the first source symbol considered in the encoding window

    unsigned int i;
    unsigned int count = 0;
    Node *ptr= NULL;
    unsigned int packetSymbols = (unsigned int)(encodedInfoLength/(bucket->symbLen));

    if (IDfirstSymb+packetSymbols > bucket->genSymbs) {
        createHeadersFly(bucket,
                         (IDfirstSymb+packetSymbols)-(bucket->genSymbs),
                         offset);
    }

    for (i=0; i<packetSymbols; i++) {

        // Create Node
        if (decoder->RXsymbList == NULL) {
            ptr = addNode(NULL);
            decoder->RXsymbList = ptr;
        } else {
            ptr = addNode(decoder->lastRXsymbol);
            decoder->lastRXsymbol->next = ptr;
        }
        decoder->lastRXsymbol = ptr;
        count++;

        // Alloc a new symbol in the decoder
        ptr->data = (Symbol *) chk_calloc(1, sizeof(Symbol));
        Symbol *newSymbol = (Symbol *)ptr->data;

        // Create pointer to the current symbol in the bucket
        Symbol* currSymbol = &(bucket->symbols[IDfirstSymb+i]);

        // Deg
        newSymbol->deg = currSymbol->deg;
        // Symb Len
        newSymbol->symbLen = currSymbol->symbLen;
        // Header
        newSymbol->header =
            (unsigned int *)chk_malloc(newSymbol->deg, sizeof(unsigned int));
        memcpy(newSymbol->header,
               currSymbol->header,
               newSymbol->deg*sizeof(unsigned int));
        // Info
        newSymbol->info =
            (char *)chk_malloc(bucket->symbLen, sizeof(char));
        memcpy(newSymbol->info,
               &encodedInfo[i*bucket->symbLen],
               bucket->symbLen*sizeof(char));

#ifdef DEBUGdecoderPrintHeaders
        int deb_index;
        printf("DECODER[%d] - symb[%d]\t: deg=%3d\t -> ",
               bucket->nameBucket, IDfirstSymb+i, newSymbol->deg);
        for (deb_index=0; deb_index<newSymbol->deg; deb_index++)
            printf("%3d ", currSymbol->header[deb_index]);
        printf("\n");
#endif

    }
    // Update the number of received symbols
    decoder->NumRecSymbols += count;

    return count;
}
Beispiel #18
0
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;
}
Beispiel #19
0
// Processes the received symbols
Tbool processReceivedFLY(LTdecoderFLY* decoder) {
    Tbool one_decoded = False;
    unsigned int currDeg = 0;
    unsigned int realDeg = 0;
    unsigned int check = 0;
    Node *nextNode= NULL;
    int symbLen = 0;
    unsigned int k = decoder->k;

    // Start from the first received symbol (if exists)
    Node *current = NULL;
    current = decoder->RXsymbList;

    // All the symbols in the decode MUST have the same length!!
    if (current->data != NULL)
        symbLen = ((Symbol *)decoder->RXsymbList->data)->symbLen;
    else if (decoder->bucketFLYList != NULL)
        symbLen = ((BucketFLY *)decoder->bucketFLYList->data)->symbLen;
    if (symbLen==0) {
        print_error("ERROR: processReceivedFLY. The symbol length cannot be defined.\n");
        exit(EXIT_FAILURE);
    }

    // If the decoded buffer has not been allocated yet, allocate it
    if (decoder->decoded == NULL) {
        decoder->decoded = (char *) chk_malloc(k*symbLen, sizeof(char));
    }
    // If the decodingStatus buffer has not been allocated yet, allocate it
    if (decoder->decodingStatus == NULL) {
        decoder->decodingStatus = (char *) chk_calloc(k*symbLen, sizeof(char));
    }

    while (current != NULL) {

        nextNode = current->next;

        if (current->data == NULL) {
            print_error("ERROR: process_received - current data empty.\n");
            exit(EXIT_FAILURE);
        }

        Symbol *currSymb = (Symbol *) current->data;

        // Get the degree of the current encoded symbol
        currDeg = currSymb->deg;
        if ((currDeg<1)&&(currDeg>k)) {	// It should never happen!!
            print_error("ERROR: process_received - out of bounds degree found.\n");
            rmSymbol(&(decoder->RXsymbList), current);
        }

#ifdef DEBUGPrintReceivedSymbolHeaders
        unsigned int debIndex;
        printf("Decoding Header: Deg = %d \t Check Symbs =", currDeg);
        for (debIndex = 0; debIndex < currDeg; debIndex++)
            printf(" %d", currSymb->header[debIndex]);
        printf("\n");
#endif


        if (currDeg == 1) { // If this symbol has degree 1 then directly decode it
            check = currSymb->header[0];

            // Check that the encoded information exists
            if (currSymb->info == NULL) {
                print_error("ERROR: symbol info not allocated!\n");
                exit(EXIT_FAILURE);
            }
            // Check that the header exists
            if (currSymb->header == NULL) {
                print_error("ERROR: symbol header not allocated!\n");
                exit(EXIT_FAILURE);
            }
            // Check that the symbol in the header is within the k input symbols
            if (check >= k) {
                print_error("ERROR: decoded symbol header out of buffer.\n");
                exit(EXIT_FAILURE);
            }

            // Check if the symbol in the header has not being decoded yet.
            if (isSymbolDecoded(decoder->decodingStatus, check*symbLen, symbLen) == False) {

                // Copy the information in the decoded information array
                memcpy(&decoder->decoded[check*symbLen],
                       currSymb->info,
                       symbLen*sizeof(char));
                // Set the bytes in the decoding status array to 0x01 to indicate that the symbols is decoded.
                memset(&decoder->decodingStatus[check*symbLen],
                       0x01,
                       symbLen*sizeof(char));
                one_decoded = True;
                decoder->NumDecSymbols++;
            }

#ifdef DEBUGprintReleasedSymbol
            printReleasedSymbol(check, k);
#endif

            rmSymbol(&(decoder->RXsymbList), current);

        } else {
            // Compute the real degree of the symbol
            realDeg = computeRealDegree(currSymb,
                                        decoder->decodingStatus,
                                        symbLen, currDeg, k);
            switch (realDeg) {
            case 0:
                rmSymbol(&(decoder->RXsymbList), current);
                break;
            case 1:
                check = reduceDegree(&currSymb,
                                     decoder->decoded, decoder->decodingStatus, currDeg, symbLen, realDeg,
                                     decoder->xorType);

                if (currSymb->info == NULL) {
                    print_error("ERROR: symbol info not allocated!\n");
                    exit(EXIT_FAILURE);
                }

                if (check>=k) {
                    print_error("ERROR: decoded symbol out of buffer.\n");
                    exit(EXIT_FAILURE);
                }

                if (decoder->decodingStatus[check*symbLen] == 0x00) {
                    memcpy(&decoder->decoded[check*symbLen],
                           currSymb->info,
                           symbLen*sizeof(char));
                    memset(&decoder->decodingStatus[check*symbLen],
                           0x01,
                           symbLen*sizeof(char));
                    one_decoded = True;
                    decoder->NumDecSymbols++;
                }
                rmSymbol(&(decoder->RXsymbList), current);

#ifdef DEBUGprintReleasedSymbol
                printReleasedSymbol(check, k);
#endif

                break;
            default:
                break;
            }
        }
        current = nextNode;
    }
    // Update lastRXsymbol (necessary for adding new received symbols)
    current = decoder->RXsymbList;
    while (current != NULL) {
        if (current->next == NULL) {
            decoder->lastRXsymbol = current;
            break;
        }
        current = current->next;
    }
    return one_decoded;
}
Beispiel #20
0
/*
 * 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;
}
extern "C" void* chk_memalign(size_t, size_t bytes) {
//  log_message("%s: %s\n", __FILE__, __FUNCTION__);
    // XXX: it's better to use malloc, than being wrong
    return chk_malloc(bytes);
}
Beispiel #22
0
unsigned int LTencodeSymbolFLY_buffer(LTencoderFLY *encoder, int symbLen,
                                      char* info, unsigned long info_firstSymbolId,
                                      Symbol* encodedSymbols, unsigned int N,
                                      unsigned long window_firstSymbolId) {
    unsigned int genDeg = 0;
    unsigned int *selectedSymbols = NULL;
    unsigned int ii, jj, j, kk;
    double sel;

    unsigned int w = encoder->currConfig->config->w;
    double *cumulative = encoder->currConfig->cumulative;
    unsigned int IDfirstSymbol = encoder->currConfig->genSymbols;

    // The output must be already allocated
    if ((symbLen < 1)||(info == NULL)||(encodedSymbols == NULL)) {
        print_error("ERROR - encoding process - no encoded information generated.\n");
        if (symbLen < 1)
            print_error("                           symbLen = %d\n.", symbLen);
        if (info == NULL)
            print_error("                           info vector not allocated.\n");
        if (encodedSymbols == NULL)
            print_error("                           encoded symbol list not allocated.\n");
        exit(EXIT_FAILURE);
    }

    // Allocate the vector for the selected symbols
    selectedSymbols = (unsigned int*) chk_calloc(w, sizeof(unsigned int));

    for (ii=0; ii<N; ii++) {

        Symbol* currSymb = &(encodedSymbols[ii]);

        genDeg = degree(cumulative, w, real(&(encoder->currConfig)->randomGenerator));
        if (genDeg > w)
            genDeg = w;

        if (genDeg == w) {
            chooseAllInWindowCore(w, window_firstSymbolId, selectedSymbols, genDeg);

        } else {
            for (jj=0; jj<genDeg; jj++) {
                sel	= real(&(encoder->currConfig)->randomGenerator);
                selectedSymbols[jj]	= rand2windowCore(w, window_firstSymbolId, sel);

                if (verifySelectedSymbol(selectedSymbols, jj) == False) {
                    jj--;		// Doesn't repeat encoding symbols
                }
            }
            // This encoder can sort the selected symbols if the following line is uncommented.
            //sort(selectedSymbols, 0, genDeg-1);	// Sort the chosen encoding symbols' list
        }

        // Allocate memory for the header
        currSymb->header = (unsigned int*) chk_malloc(genDeg, sizeof(unsigned int));

        // Allocate memory for the encoded information
        currSymb->info = (char*) chk_malloc(symbLen, sizeof(char));

        // Saves the degree in the symbol's header
        currSymb->deg = genDeg;

        // Saves the symbol length in the symbol's header
        currSymb->symbLen = symbLen;

        // Assign the id to the new symbol
        currSymb->id = encoder->currConfig->genSymbols+1;

        // Copy the Info of the first input symbol indicated in the header
        memcpy(currSymb->header, selectedSymbols, genDeg*sizeof(unsigned int));

#ifdef DEBUGencoderPrintHeaders
        int deb_index;
        printf("ENCODER - symb[%d]\t: deg=%3d\t -> ", (int) encoder->currConfig->genSymbols, genDeg);
        for (deb_index=0; deb_index<genDeg; deb_index++)
            printf("%3d ", selectedSymbols[deb_index]);
        printf("\n");
#endif

        // Saves the selected source symbols' ID in the current Symbol
        memcpy(currSymb->info, &info[(selectedSymbols[0]-info_firstSymbolId)*symbLen], symbLen*sizeof(char));

        // EX-OR the other symbols of the header
        if (encoder->xorType != NO) {
            for (j=1; j<genDeg; j++) {
                // EX-OR COMPUTATION
                calcXOR(&info[(selectedSymbols[j]-info_firstSymbolId)*symbLen], currSymb->info, symbLen, encoder->xorType);
            }
        } else {
            for (j=1; j<genDeg; j++) {
                // EX-OR COMPUTATION
                for (kk=0; kk<symbLen; kk++) {
                    currSymb->info[kk] ^= info[(selectedSymbols[j]-info_firstSymbolId)*symbLen + kk];
                }
            }
        }

        // Increase the number of generated symbols
        encoder->currConfig->genSymbols++;
        encoder->totGenSymbols++;
    }

#ifdef DEBUGencoderPrintHeaders
    printf("\n");
#endif

    // free and initialize the tmp symbol
    free(selectedSymbols);
    selectedSymbols = NULL;

    return IDfirstSymbol;
}
Beispiel #23
0
// Processes the received symbols
Tbool processReceived(LTdecoder* decoder) {
    Tbool one_decoded = False;
    unsigned int curr_degree = 0;
    unsigned int real_degree = 0;
    unsigned int check = 0;
    Node *nextNode= NULL;
    int symbLen = 0;
    unsigned int k = 0;

    // Start from the first received symbol (if exists)
    Node *current = NULL;
    current = decoder->RXsymbList;

    // All the symbols in the decode MUST have the same length!!
    symbLen = ((Bucket *)decoder->bucketList->data)->symbLen;
    k = ((Bucket *)decoder->bucketList->data)->matrix.conf.k;
    if ((symbLen==0)||(k==0)) {
        print_error("ERROR: process_received - either symbLen or k invalid.\n");
        exit(EXIT_FAILURE);
    }

    // If the decoded buffer has not been allocated yet, allocate it
    if (decoder->decoded == NULL) {
        decoder->decoded = (char *) chk_malloc(k*symbLen, sizeof(char));
//        memset(decoder->decoded, 'X', k*symbLen*sizeof(char));
    }
    // If the decodingStatus buffer has not been allocated yet, allocate it
    if (decoder->decodingStatus == NULL) {
        decoder->decodingStatus = (char *) chk_malloc(k*symbLen, sizeof(char));
        memset(decoder->decodingStatus, 0x00, k*symbLen*sizeof(char));
    }

    while (current != NULL) {

        nextNode = current->next;

        if (current->data == NULL) {
            print_error("ERROR: process_received - current data empty.\n");
            exit(EXIT_FAILURE);
        }

        curr_degree = ((Symbol *) current->data)->deg;

        if ((curr_degree<1)&&(curr_degree>k)) {	// It shouldn't happen
            print_error("ERROR: process_received - out of bounds degree found.\n");
            rmSymbol(&(decoder->RXsymbList), current);
        }

        if (curr_degree == 1) { // If this symbol has deg = 1 then directly decode it
            check = ((Symbol *) current->data)->header[0];

            if ((check > k)||(check < 0)) {
                print_error("ERROR: encoded symbols contain a check out of bounds!\n");
                exit(EXIT_FAILURE);
            }

            // Check if the symbol in the header has not being decoded yet.
            if (isSymbolDecoded(decoder->decodingStatus, check*symbLen, symbLen) == False) {

                // Copy the information in the decoded information array
                memcpy(&decoder->decoded[check*symbLen],
                       ((Symbol *) current->data)->info,
                       symbLen*sizeof(char));
                // Set the bytes in the decoding status array to 0x01 to indicate that the symbols is decoded.
                memset(&decoder->decodingStatus[check*symbLen],
                       0x01,
                       symbLen*sizeof(char));
                one_decoded = True;
                decoder->NumDecSymbols++;
            }

#ifdef DEBUGprintReleasedSymbol
            printReleasedSymbol(check, k);
#endif

            rmSymbol(&(decoder->RXsymbList), current);

        } else {
            // Compute the real degree of the symbol
            real_degree = computeRealDegree(((Symbol *) current->data),
                                            decoder->decodingStatus,
                                            symbLen, curr_degree, k);
            switch (real_degree) {
            case 0:
                rmSymbol(&(decoder->RXsymbList), current);
                break;
            case 1:
                check = reduceDegree((Symbol **)(&current->data),
                                     decoder->decoded, decoder->decodingStatus, curr_degree, symbLen, real_degree,
                                     decoder->xorType);

                if (((Symbol *) current->data)->info == NULL) {
                    print_error("ERROR: symbol info not allocated!\n");
                    exit(EXIT_FAILURE);
                }

                if (check>=k) {
                    print_error("ERROR: decoded symbol out of buffer.\n");
                    exit(EXIT_FAILURE);
                }

                if (decoder->decodingStatus[check*symbLen] == 0x00) {
                    memcpy(&decoder->decoded[check*symbLen],
                           ((Symbol *) current->data)->info,
                           symbLen*sizeof(char));
                    memset(&decoder->decodingStatus[check*symbLen],
                           0x01,
                           symbLen*sizeof(char));
                    one_decoded = True;
                    decoder->NumDecSymbols++;
                }
                rmSymbol(&(decoder->RXsymbList), current);

#ifdef DEBUGprintReleasedSymbol
                printReleasedSymbol(check, k);
#endif

                break;
            default:
                break;
            }
        }

        if (decoder->NumDecSymbols == k) {
            return False;
        }
        current = nextNode;
    }
    // Update lastRXsymbol (necessary for adding new received symbols)
    current = decoder->RXsymbList;
    while (current != NULL) {
        if (current->next == NULL) {
            decoder->lastRXsymbol = current;
            break;
        }
        current = current->next;
    }
    return one_decoded;
}