// mMul memory-wise matrix* mMul(matrix a, matrix b){ int i, j, k; matrix* resultMatrix; uint8_t factor; uint8_t *aVector, *bVector, *resVector; // Check dimension correctness if(a.nColumns != b.nRows){ printf("mMul : Error in Matrix dimensions. Cannot continue\n"); exit(1); } resultMatrix = mCreate(a.nRows, b.nColumns); for(i = 0; i < resultMatrix->nRows; i++){ aVector = a.data[i]; resVector = resultMatrix->data[i]; for(j = 0; j < a.nColumns; j++){ factor = aVector[j]; if(factor != 0x00){ bVector = b.data[j]; for(k = 0; k < b.nColumns; k++){ resVector[k] = gadd(resVector[k], gmul(factor, bVector[k])); } } } } return resultMatrix; }
matrix* mCopy(matrix orig){ int i; matrix* resultMatrix = mCreate(orig.nRows, orig.nColumns); for(i = 0; i < resultMatrix->nRows; i++){ memcpy(resultMatrix->data[i], orig.data[i], resultMatrix->nColumns * sizeof(uint8_t)); } return resultMatrix; }
pluginInterface *pluginLoader::create(){ if(mPlugin == NULL){ #if _DEBUG_LEVEL >= 1 std::cerr << "plugin not loaded!" << std::endl; #endif return NULL; } pluginInterface *pli = mCreate(); mInstances.push_back(pli); return pli; }
matrix* getRandomMatrix(int rows, int columns){ int i, j; matrix* resultMatrix = mCreate(rows, columns); for(i = 0; i < resultMatrix->nRows; i++){ for(j = 0; j < resultMatrix->nColumns; j++){ resultMatrix->data[i][j] = getRandom(); } } return resultMatrix; }
matrix* getIdentityMatrix(int rows){ // Returns square identity matrix int i, j; matrix* resultMatrix = mCreate(rows, rows); for(i = 0; i < resultMatrix->nRows; i++){ for(j = 0; j < resultMatrix->nColumns; j++){ if(i==j){ resultMatrix->data[i][j] = 1; } else { resultMatrix->data[i][j] = 0; } } } return resultMatrix; }
int matrixTest(){ matrix* a, *b, *c; // Create & Destroy Matrices a = getRandomMatrix(1000,1000); b = getRandomMatrix(1000,1000); c = mMul(*a, *b); mFree(a); mFree(b); mFree(c); mFree(mCreate(0,0)); return true; }
// ------------------------------------------------------------------------------------------------ int TProblemManager::LoadProblemLibrary(const std::string& libPath) { if (mLibHandle) FreeProblemLibrary(); #ifdef WIN32 mLibHandle = LoadLibrary(TEXT(libPath.c_str())); if (!mLibHandle) { std::cerr << "Cannot load library: " << TEXT(libPath.c_str()) << std::endl; return TProblemManager::ERROR_; } #else mLibHandle = dlopen(libPath.c_str(), RTLD_LAZY); if (!mLibHandle) { std::cerr << dlerror() << std::endl; return TProblemManager::ERROR_; } #endif #ifdef WIN32 mCreate = (create_t*) GetProcAddress(mLibHandle, "create"); mDestroy = (destroy_t*) GetProcAddress(mLibHandle, "destroy"); if (!mCreate || !mDestroy) { std::cerr << "Cannot load symbols: " << GetLastError() << std::endl; FreeLibHandler(); return TProblemManager::ERROR_; } #else dlerror(); mCreate = (create_t*) dlsym(mLibHandle, "create"); char* dlsym_error = dlerror(); if (dlsym_error) { mCreate = NULL; std::cerr << dlsym_error << std::endl; FreeLibHandler(); return TProblemManager::ERROR_; } mDestroy = (destroy_t*) dlsym(mLibHandle, "destroy"); dlsym_error = dlerror(); if (dlsym_error) { mCreate = NULL; mDestroy = NULL; std::cerr << dlsym_error << std::endl; FreeLibHandler(); return TProblemManager::ERROR_; } #endif mProblem = mCreate(); if (!mProblem) { FreeLibHandler(); mCreate = NULL; mDestroy = NULL; std::cerr << "Cannot create problem instance" << std::endl; } return TProblemManager::OK_; }
void handleInCoded(decoderstate* state, uint8_t* buffer, int size) { do_debug("in handleInCoded\n"); datapacket* packet = bufferToData(buffer, size); matrix *coeffs, *tmp; int bufLen, i, delta; uint8_t* dataVector; uint8_t* coeffVector; uint8_t ackBuffer[100]; uint16_t loss, total; //printf("Data received :\n"); //dataPacketPrint(*packet); do_debug("p->packetNumber = %2x\n", packet->packetNumber); // ~~ Allocate blocks & coefficient matrix if necessary ~~ while(state->currBlock + state->numBlock - 1 < packet->blockNo) { do_debug("CurrBlock = %d, numBlock = %d, blockNo of received Data = %d\n", state->currBlock, state->numBlock, packet->blockNo); state->blocks = realloc(state->blocks, (state->numBlock + 1) * sizeof(matrix*)); state->blocks[state->numBlock] = mCreate(BLKSIZE, PACKETSIZE); state->coefficients = realloc(state->coefficients, (state->numBlock + 1) * sizeof(matrix*)); state->coefficients[state->numBlock] = mCreate(BLKSIZE, BLKSIZE); state->nPacketsInBlock = realloc(state->nPacketsInBlock, (state->numBlock + 1) * sizeof(int)); state->nPacketsInBlock[state->numBlock] = 0; state->isSentPacketInBlock = realloc(state->isSentPacketInBlock, (state->numBlock + 1) * sizeof(int*)); state->isSentPacketInBlock[state->numBlock] = malloc(BLKSIZE * sizeof(int)); for(i = 0; i<BLKSIZE; i++) { state->isSentPacketInBlock[state->numBlock][i] = false; } state->numBlock ++; } if(packet->blockNo >= state->currBlock) { if((packet->packetNumber & BITMASK_NO) >= state->nPacketsInBlock[packet->blockNo - state->currBlock]) { // Try to append // Compute coefficients coeffs = mCreate(1, BLKSIZE); dataVector = calloc(PACKETSIZE, sizeof(uint8_t)); coeffVector = calloc(BLKSIZE, sizeof(uint8_t)); if(((packet->packetNumber) & BITMASK_FLAG) == FLAG_CLEAR) { coeffs->data[0][((packet->packetNumber) & BITMASK_NO)] = 1; } else if(((packet->packetNumber) & BITMASK_FLAG) == FLAG_CODED) { srandom(packet->seqNo); tmp = getRandomMatrix(1, BLKSIZE); memcpy(coeffs->data[0], tmp->data[0], packet->packetNumber & BITMASK_NO); mFree(tmp); } else { printf("handleInCoded : received a bogus data packet. DIE."); exit(1); } // ~~ Append to the matrix and eventually decode ~~ memcpy(dataVector, packet->payloadAndSize, packet->size); memcpy(coeffVector, coeffs->data[0], BLKSIZE); mFree(coeffs); if(appendCodedPayload(state, coeffVector, dataVector, packet->blockNo - state->currBlock)) { do_debug("Received an innovative packet\n"); state->stats_nInnovative++; } else { do_debug("Received packet was not innovative. Drop.\n"); if(packet->blockNo - state->currBlock == 0) { state->stats_nAppendedNotInnovativeGaloisFirstBlock++; } else { state->stats_nAppendedNotInnovativeGaloisOtherBlock++; } } free(dataVector); free(coeffVector); } else { do_debug("Received packet has NO chance to be innovative. Drop.\n"); state->stats_nAppendedNotInnovativeCounter++; } } else { do_debug("Packet received for an outdated block. Drop.\n"); state->stats_nOutdated++; } // ~~ Try to decode ~~ if((state->numBlock > 0) && (state->nPacketsInBlock[0] > 0)) { do_debug("Calling extractData() while numBlock = %d, currBlock = %d, nPacketInBlock[0] = %d\n", state->numBlock, state->currBlock, state->nPacketsInBlock[0]); extractData(state); } // ~~ Update the loss information buffer ~~ delta = packet->seqNo - state->lastSeqReceived; if(delta > 0) { for(i = state->lossBuffer->currentIndex + 1; i < (state->lossBuffer->currentIndex + delta); i++) { state->lossBuffer->isReceived[i % LOSS_BUFFER_SIZE] = false; } state->lossBuffer->isReceived[(state->lossBuffer->currentIndex + delta) % LOSS_BUFFER_SIZE] = true; state->lossBuffer->currentIndex = (state->lossBuffer->currentIndex + delta) % LOSS_BUFFER_SIZE; } else { state->lossBuffer->isReceived[(state->lossBuffer->currentIndex + delta + LOSS_BUFFER_SIZE) % LOSS_BUFFER_SIZE] = true; } state->lastSeqReceived = max(state->lastSeqReceived, packet->seqNo); // ~~ Send an ACK back ~~ ackpacket ack; ack.ack_dofs = malloc(DOFS_LENGTH * sizeof(uint8_t)); for(i = 0; i < DOFS_LENGTH; i++) { if(state->numBlock > i) { // If the i-th block is allocated // Include the number of packets received ack.ack_dofs[i] = state->nPacketsInBlock[i]; } else { // Otherwise, let it just be zero ack.ack_dofs[i] = 0; } } ack.ack_seqNo = packet->seqNo; ack.ack_currBlock = state->currBlock; countLoss(*state, &loss, &total); ack.ack_loss = loss; ack.ack_total = total; //printf("ACK to send :\n"); //ackPacketPrint(ack); ackPacketToBuffer(ack, ackBuffer, &bufLen); free(ack.ack_dofs); state->ackToSend = realloc(state->ackToSend, (state->nAckToSend + 1) * sizeof(uint8_t*)); state->ackToSendSize = realloc(state->ackToSendSize, (state->nAckToSend + 1) * sizeof(int)); state->ackToSend[state->nAckToSend] = malloc(bufLen * sizeof(uint8_t)); memcpy(state->ackToSend[state->nAckToSend], ackBuffer, bufLen); state->ackToSendSize[state->nAckToSend] = bufLen; state->nAckToSend ++; free(packet->payloadAndSize); free(packet); }
NEOERR *skipNewList(skipList *skip, int threaded, int root, int maxLevel, int flushLimit, skipFreeValue freeValue, void *ctx) { NEOERR *err; skipList list; UINT32 i; *skip = NULL; if(! (list = calloc(1, sizeof(struct skipList_struct)))) return nerr_raise(NERR_NOMEM, "Unable to allocate memore for skiplist"); if (maxLevel == 0) return nerr_raise(NERR_ASSERT, "maxLevel must be greater than 0"); if(maxLevel >= SKIP_MAXLEVEL) /* check limits */ maxLevel = SKIP_MAXLEVEL-1; if(root > 4) root = 4; else if(root < 2) root = 2; list->maxLevel = maxLevel; /* init list constants */ list->randLimit = 1.0 / (double)root; list->threaded = threaded; list->freeValue = freeValue; list->freeValueCtx = ctx; do { if(threaded) { list->flushLimit = flushLimit; err = mCreate(&list->read); if (err != STATUS_OK) break; err = mCreate(&list->write); if (err != STATUS_OK) break; err = cCreate(&list->resume); if (err != STATUS_OK) break; err = cCreate(&list->flush); if (err != STATUS_OK) break; } err = skipAllocItem(&(list->header), list->maxLevel, 0, NULL); if (err != STATUS_OK) break; err = skipAllocItem(&(list->tail), list->maxLevel, (UINT32)-1, NULL); if (err != STATUS_OK) break; err = skipAllocItem(&(list->deleted), 0, 0, NULL); if (err != STATUS_OK) break; for(i = 0; /* init header and tail */ i <= list->maxLevel; i++) { list->tail->next[i] = NULL; list->header->next[i] = list->tail; } list->deleted->next[1] = list->tail; *skip = list; return STATUS_OK; /* return new list */ } while(FALSE); if(list->header) /* failed to make list, bail */ free(list->header); free(list); return nerr_pass(err); }