Beispiel #1
0
const iovec
Buffer::readBuffer(size_t length, bool coalesce) const
{
    iovec result;
    result.iov_base = NULL;
    result.iov_len = 0;
    if (length == (size_t)~0)
        length = readAvailable();
    MORDOR_ASSERT(length <= readAvailable());
    if (readAvailable() == 0)
        return result;
    // Optimize case where all that is requested is contained in the first
    // buffer
    if (m_segments.front().readAvailable() >= length) {
        SegmentData data = m_segments.front().readBuffer().slice(0, length);
        result.iov_base = data.start();
        result.iov_len = iovLength(data.length());
        return result;
    }
    // If they don't want us to coalesce, just return as much as we can from
    // the first segment
    if (!coalesce) {
        SegmentData data = m_segments.front().readBuffer();
        result.iov_base = data.start();
        result.iov_len = iovLength(data.length());
        return result;
    }
    // Breaking constness!
    Buffer* _this = const_cast<Buffer*>(this);
    // try to avoid allocation
    if (m_writeIt != m_segments.end() && m_writeIt->writeAvailable()
        >= readAvailable()) {
        copyOut(m_writeIt->writeBuffer().start(), readAvailable());
        Segment newSegment = Segment(m_writeIt->writeBuffer().slice(0,
            readAvailable()));
        _this->m_segments.clear();
        _this->m_segments.push_back(newSegment);
        _this->m_writeAvailable = 0;
        _this->m_writeIt = _this->m_segments.end();
        invariant();
        SegmentData data = newSegment.readBuffer().slice(0, length);
        result.iov_base = data.start();
        result.iov_len = iovLength(data.length());
        return result;
    }
    Segment newSegment = Segment(readAvailable());
    copyOut(newSegment.writeBuffer().start(), readAvailable());
    newSegment.produce(readAvailable());
    _this->m_segments.clear();
    _this->m_segments.push_back(newSegment);
    _this->m_writeAvailable = 0;
    _this->m_writeIt = _this->m_segments.end();
    invariant();
    SegmentData data = newSegment.readBuffer().slice(0, length);
    result.iov_base = data.start();
    result.iov_len = iovLength(data.length());
    return result;
}
Beispiel #2
0
Datei: luit.c Projekt: aosm/X11
static int
convert(int ifd, int ofd)
{
    int rc, i;
    unsigned char buf[BUFFER_SIZE];

    rc = droppriv();
    if(rc < 0) {
        perror("Couldn't drop priviledges");
        exit(1);
    }

    while(1) {
        i = read(ifd, buf, BUFFER_SIZE);
        if(i <= 0) {
            if(i < 0) {
                perror("Read error");
                exit(1);
            }
            break;
        }
        copyOut(outputState, ofd, buf, i);
    }
    return 0;
}
Beispiel #3
0
std::string
Buffer::toString() const
{
    if (m_readAvailable == 0)
        return std::string();

    std::string result;
    result.resize(m_readAvailable);
    copyOut(&result[0], result.size());
    return result;
}
int fei::Vector_core::copyOutFieldData(int fieldID,
					   int idType,
					   int numIDs,
					   const int* IDs,
					   double* data,
					   int vectorIndex)
{
  if (vecSpace_.get() == NULL) ERReturn(-1);

  int fieldSize = vecSpace_->getFieldSize(fieldID);

  if (haveFEVector_) {
    snl_fei::RecordCollection* collection = NULL;
    CHK_ERR( vecSpace_->getRecordCollection(idType, collection) );
    int nodeNumber;
    int dofOffset;
    int numInstances;
    int foffset;
    std::vector<int>& eqnNums = vecSpace_->getEqnNumbers();
    int* vspcEqnPtr = eqnNums.size() > 0 ? &eqnNums[0] : NULL;

    int offset = 0;
    for(int i=0; i<numIDs; ++i) {
      fei::Record<int>* node = collection->getRecordWithID(IDs[i]);
      if (node == NULL) {
        ERReturn(-1);
      }

      nodeNumber = node->getNumber();
      int* eqnNumbers = vspcEqnPtr+node->getOffsetIntoEqnNumbers();
      node->getFieldMask()->getFieldEqnOffset(fieldID, foffset, numInstances);
      dofOffset = eqnNumbers[foffset] - eqnNumbers[0];
      for(int j=0; j<fieldSize; ++j) {
	CHK_ERR( copyOut_FE(nodeNumber, dofOffset+j, data[offset++]));
      }
    }
  }
  else {
    work_indices_.resize(numIDs*fieldSize*2);
    int* indicesPtr = &work_indices_[0];

    CHK_ERR( vecSpace_->getGlobalIndices(numIDs, IDs, idType,
					 fieldID, indicesPtr) );

    CHK_ERR( copyOut(numIDs*fieldSize, indicesPtr, data) );
  }

  return(0);
}
int fei::Vector_core::writeToFile(const char* filename,
                                    bool matrixMarketFormat)
{
  int numProcs = fei::numProcs(comm_);
  int localProc =fei::localProc(comm_);

  double coef;

  static char mmbanner[] = "%%MatrixMarket matrix array real general";

  for(int p=0; p<numProcs; ++p) {
    fei::Barrier(comm_);
    if (p != localProc) continue;

    FEI_OFSTREAM* outFile = NULL;
    if (p==0) {
      outFile = new FEI_OFSTREAM(filename, IOS_OUT);
      FEI_OFSTREAM& ofref = *outFile;
      if (matrixMarketFormat) {
        ofref << mmbanner << FEI_ENDL;
        ofref << eqnComm_->getGlobalOffsets()[numProcs] << " 1" << FEI_ENDL;
      }
      else {
        ofref << eqnComm_->getGlobalOffsets()[numProcs] << FEI_ENDL;
      }
    }
    else outFile = new FEI_OFSTREAM(filename, IOS_APP);
    FEI_OFSTREAM& ofref = *outFile;
    ofref.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
    ofref.precision(13);

    for(int i=firstLocalOffset_; i<=lastLocalOffset_; ++i) {
      CHK_ERR( copyOut(1, &i, &coef) );
      if (matrixMarketFormat) {
        ofref << " " << coef << FEI_ENDL;
      }
      else {
        ofref << i << " " << coef << FEI_ENDL;
      }
    }
    
    delete outFile;
  } 
    
  return(0);
}
//----------------------------------------------------------------------------
int VectorReducer::copyOutFieldData(int fieldID,
                                    int idType,
                                    int numIDs,
                                    const int* IDs,
                                    double* data,
                                    int vectorIndex)
{
  fei::SharedPtr<fei::VectorSpace> vspace = target_->getVectorSpace();
  int fieldSize = vspace->getFieldSize(fieldID);
  int numIndices = numIDs*fieldSize;
  std::vector<int> indices(numIndices);
  int err = vspace->getGlobalIndices(numIDs, IDs, idType, fieldID, &indices[0]);
  if (err != 0) {
    throw std::runtime_error("fei::VectorReducer::copyOutFieldData ERROR in vspace->getGlobalIndices.");
  }

  return(copyOut(numIndices, &indices[0], data, vectorIndex));
}
Beispiel #7
0
std::string
Buffer::getDelimited(char delimiter, bool eofIsDelimiter, bool includeDelimiter)
{
    ptrdiff_t offset = find(delimiter, ~0);
    MORDOR_ASSERT(offset >= -1);
    if (offset == -1 && !eofIsDelimiter)
        MORDOR_THROW_EXCEPTION(UnexpectedEofException());
    eofIsDelimiter = offset == -1;
    if (offset == -1)
        offset = readAvailable();;
    std::string result;
    result.resize(offset + (eofIsDelimiter ? 0 : (includeDelimiter ? 1 : 0)));
    copyOut(&result[0], result.size());
    consume(result.size());
    if (!eofIsDelimiter && !includeDelimiter)
        consume(1u);
    return result;
}
Beispiel #8
0
/**
 * 
 * Gets an InvocationImpl from the store using a MIDlet suiteId
 * and optional classname.
 * Getting an Invocation from the store removes it from the store.
 * If an OutOfMemory exception is thrown the matched Invocation
 * is NOT removed from the queue. If the heap memory can be
 * replenished then the operation can be retried.
 *
 * @param invoc an Invocation Object to fill in
 * @param suiteId to match a pending invocation
 * @param classname to match a pending invocation
 * @param mode one of {@link #MODE_REQUEST}, {@link #MODE_RESPONSE},
 *    or {@link #MODE_CLEANUP}
 * @param blocking true to block until a matching invocation is available
 * @return 1 if a matching invocation was found and returned 
 *    in its entirety; zero if there was no matching invocation;
 *    -1 if the sizes of the arguments or parameter array were wrong
 * @see StoredInvoc
 */
KNIEXPORT KNI_RETURNTYPE_INT
Java_com_sun_midp_content_InvocationStore_get0(void) {
    int ret = 0;          /* return value = nothing matched */
    KNI_StartHandles(4);
    KNI_DeclareHandle(obj);      /* multipurpose handle */
    KNI_DeclareHandle(argsObj);      /* handle for argument array */
    KNI_DeclareHandle(invocObj);  /* Arg1: Invocation object; non-null */
    KNI_DeclareHandle(classname); /* Arg3: non-null classname */
    int mode = MODE_REQUEST;      /* Arg4: mode for get */
    jboolean blocking = KNI_FALSE; /* Arg5: true if should block */

    /* Argument indices must match Java native method declaration */
#define getInvokeObjArg 1
#define getSuiteIdArg 2
#define getClassnameArg 3
#define getModeArg 4
#define getBlockingArg 5

    StoredLink* match = NULL;

    SuiteIdType desiredSuiteId;
    pcsl_string desiredClassname = PCSL_STRING_NULL_INITIALIZER;

    do {/* Block to break out of on exceptions */
        /* Check if blocked invocation was cancelled. */
        if (isThreadCancelled()) {
            /* blocking is always false to cleanup and exit immediately */
            break;
        }
    
        /* Get the desired blocking mode. */
        blocking = KNI_GetParameterAsBoolean(getBlockingArg);
    
        if (!isEmpty()) {
            /* Queue is not empty, get InvocationImpl obj and init. */
            KNI_GetParameterAsObject(getInvokeObjArg, invocObj);
            init(invocObj, obj);
    
            /* Get the desired type of invocation. */
            mode = KNI_GetParameterAsInt(getModeArg);
            if (mode == MODE_TID || 
                mode == MODE_TID_NEXT ||
                mode == MODE_TID_PREV) {
                int tid = KNI_GetIntField(invocObj, tidFid);
                if (tid != 0) {
                    match = invocFindTid(tid);
                } else {
                    /* start with the root */
                    match = invocQueue;
                }
                if (match != NULL) {
                    /* Link to the next or previous depending on the mode. */
                    if (mode == MODE_TID_NEXT) {
                        match = match->flink;
                    } else if (mode == MODE_TID_PREV) {
                        match = match->blink;
                    }
                }
            } else {
                desiredSuiteId = KNI_GetParameterAsInt(getSuiteIdArg);
                KNI_GetParameterAsObject(getClassnameArg, classname);
                if (PCSL_STRING_OK != midp_jstring_to_pcsl_string(classname, 
                                        &desiredClassname)) {
                    KNI_ThrowNew(midpOutOfMemoryError, 
                       "InvocationStore_get0 no memory for [desiredClassname]");
                    break;
                }
                match = invocFind(desiredSuiteId, &desiredClassname, mode);
            }
        }
    } while (KNI_FALSE);

    /* Always free strings allocated */
    pcsl_string_free(&desiredClassname);

    if (match != NULL) {
        StoredInvoc *invoc = match->invoc;
        int st = copyOut(invoc, mode, invocObj, argsObj, obj);
        switch (st) {
        case 1:
            /* If a match was found and successfully copied;
             * do final set of actions on the Invocation selected.
             */
            switch (mode) {
            case MODE_REQUEST:
                if (invoc->status == STATUS_INIT) {
                    /* 
                     * Returning new request, change status to ACTIVE
                     * Keep this entry in the queue.
                     */
                    invoc->status = STATUS_ACTIVE;
                    KNI_SetIntField(invocObj, statusFid, invoc->status);
                }
                break;
            case MODE_RESPONSE:
                if (invoc->status >= STATUS_OK &&
                    invoc->status <= STATUS_INITIATED) {
                    /*
                     * Remove responses from the list and free.
                     */
                    removeEntry(match);
                    invocFree(invoc);
                }
                break;
            case MODE_LREQUEST:
            case MODE_LRESPONSE:
            case MODE_CLEANUP:
            case MODE_TID:
            case MODE_TID_NEXT:
            case MODE_TID_PREV:
            default:
                /* No additional action */
                break;
            }
            /* Returning an Invocation */
            ret = 1;
            break;
        case 0:
            /* Insufficient memory for strings. */
            KNI_ThrowNew(midpOutOfMemoryError, "invocStore returning strings");
            KNI_ReleaseHandle(invocObj);
            ret = 0;
            break;
        case -1:
            /* Either args array or data array is incorrect size. */
            ret = -1;
        }
    } else {
        /* No match found. */
        /* If blocking, setup to block. */
        if (blocking) {
            blockThread();
            /* Fall into the return to manage handles correctly */
        }
        ret = 0;
    }
    KNI_EndHandles();
    KNI_ReturnInt(ret);
}
Beispiel #9
0
Datei: luit.c Projekt: aosm/X11
void
parent(int pid, int pty)
{
    unsigned char buf[BUFFER_SIZE];
    int i;
    int val;
    int rc;

    if(verbose) {
        reportIso2022(outputState);
    }

#ifdef SIGWINCH
    installHandler(SIGWINCH, sigwinchHandler);
#endif
    installHandler(SIGCHLD, sigchldHandler);

    rc = copyTermios(0, pty);
    if(rc < 0)
        FatalError("Couldn't copy terminal settings\n");

    rc = setRawTermios();
    if(rc < 0)
        FatalError("Couldn't set terminal to raw\n");

    val = fcntl(0, F_GETFL, 0);
    if(val >= 0) {
        fcntl(0, F_SETFL, val | O_NONBLOCK);
    }
    val = fcntl(pty, F_GETFL, 0);
    if(val >= 0) {
        fcntl(pty, F_SETFL, val | O_NONBLOCK);
    }

    setWindowSize(0, pty);

    for(;;) {
        rc = waitForInput(0, pty);

        if(sigwinch_queued) {
            sigwinch_queued = 0;
            setWindowSize(0, pty);
        }

        if(sigchld_queued && exitOnChild)
            break;

        if(rc > 0) {
            if(rc & 2) {
                i = read(pty, buf, BUFFER_SIZE);
                if((i == 0) || ((i < 0) && (errno != EAGAIN)))
                    break;
                if(i > 0)
                    copyOut(outputState, 0, buf, i);
            }
            if(rc & 1) {
                i = read(0, buf, BUFFER_SIZE);
                if((i == 0) || ((i < 0) && (errno != EAGAIN)))
                    break;
                if(i > 0)
                    copyIn(inputState, pty, buf, i);
            }
        }
    }

    restoreTermios();
}
Beispiel #10
0
int fei::Vector_core::writeToStream(FEI_OSTREAM& ostrm,
					bool matrixMarketFormat)
{
  int numProcs = fei::numProcs(comm_);
  int local_proc =fei::localProc(comm_);

  double coef;

  static char mmbanner[] = "%%MatrixMarket matrix array real general";

  IOS_FMTFLAGS oldf = ostrm.setf(IOS_SCIENTIFIC, IOS_FLOATFIELD);
  ostrm.precision(13);

  for(int proc=0; proc<numProcs; ++proc) {
    fei::Barrier(comm_);
    if (proc != local_proc) continue;

    if (proc==0) {
      if (matrixMarketFormat) {
	ostrm << mmbanner << FEI_ENDL;
	ostrm << eqnComm_->getGlobalOffsets()[numProcs] << " 1" << FEI_ENDL;
      }
      else {
	ostrm << eqnComm_->getGlobalOffsets()[numProcs] << FEI_ENDL;
      }
    }

    for(int p=0; p<local_proc; ++p) {
      for(size_t ii=0; ii<remotelyOwned_[p]->size(); ++ii) {
	if (matrixMarketFormat) {
	  ostrm << " " << remotelyOwned_[p]->coefs()[ii] << FEI_ENDL;
	}
	else {
	  ostrm << " " << remotelyOwned_[p]->indices()[ii] << " "
		<< remotelyOwned_[p]->coefs()[ii] << FEI_ENDL;
	}
      }
    }

    for(int i=firstLocalOffset_; i<=lastLocalOffset_; ++i) {
      CHK_ERR( copyOut(1, &i, &coef) );
      if (matrixMarketFormat) {
	ostrm << " " << coef << FEI_ENDL;
      }
      else {
	ostrm << " " << i << " " << coef << FEI_ENDL;
      }
    }

    for(int p=local_proc+1; p<numProcs; ++p) {
      for(size_t ii=0; ii<remotelyOwned_[p]->size(); ++ii) {
	if (matrixMarketFormat) {
	  ostrm << " " << remotelyOwned_[p]->coefs()[ii] << FEI_ENDL;
	}
	else {
	  ostrm << " " << remotelyOwned_[p]->indices()[ii] << " "
		<< remotelyOwned_[p]->coefs()[ii] << FEI_ENDL;
	}
      }
    }
  }

  ostrm.setf(oldf, IOS_FLOATFIELD);

  return(0);
}
Beispiel #11
0
int main()
{
    system("clear");
    BookList_t*List = malloc(sizeof(BookList_t));
    if (!List) return 0;
        List->first = NULL;
        List->last  = NULL;
    int temp = 0;
    int choice;
MainMenu:system ("clear");
    mainmenu();           //MainMenu Iniziale
    printf("\nChoice: ");
    scanf("%d", &choice);
    while(choice!=5)            //Riferito al mainmenu iniziale, esco solo con 5 -> END PROGRAM
    {
        switch (choice)
        {
                
//::::::::::::::::::::::::::::::::::::::::FUNC:INSERT:::::::::::::::::::::::::::::::::::::::::::
                
            case 1:
                while (1){
                system("clear");
                printf("Insert the element:\n1)At the top of the list.\n2)At the end of the list.\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                if (temp == 1) {                //Insert Head________________________
                    system("clear");
                    printf("Enter the book:\n");
                    insHead(List);
                    printList(List);
                    printf("Press any number to continue..");
                    scanf("%d", &temp);
                    system("clear");
                    break;}
                else  if (temp == 2){           //Insert Tail________________________
                    system("clear");
                    printf("Enter the book:\n");
                    insTail(List);
                    printList(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    system("clear");
                    break;}
                else if (temp == 0) {
                    goto MainMenu;
                }
                
                
                
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                }
                }
//::::::::::::::::::::::::::::::::::::::::FUNC:REMOVE:::::::::::::::::::::::::::::::::::::::::::
                
                
            case 2:
                while (1){
                system("clear");
                printf("Remove the element:\n1)At the top of the list.\n2)At the end of the list \n3)Remove from id.\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                if (temp == 1) {                //Remove Head________________________
                system ("clear");
                printf("Book removed successfully.\n");
                rmvHead(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 2){            //Remove Tail________________________
                system ("clear");
                rmvTail(List);
                printf("Book removed successfully.\n");
                    printf("Press any number to continue..");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 3){            //Remove From ID_____________________
                    system ("clear");
                    printf("Enter the book's id you want to delete:\n");
                    Cell_t* Ptr = searchId(List);
                    Cell_t *Temp = List->first;
                    if (Ptr != NULL)
                        while(Temp != Ptr)
                        {
                            Temp = Temp->pNext;
                            Temp->pNext = NULL;
                            free(Ptr);
                            Ptr = Temp;
                            printf("Press any number to continue..");
                            scanf("%d", &temp);
                            break;
                        }
                        }
                        else if (temp == 0) {
                            goto MainMenu;
                        }
                        else {
                            printf ("Please Insert a valid Number\a\n");
                            printf("Press any number to continue..\n");
                            scanf("%d", &temp);
                        }
                }

//::::::::::::::::::::::::::::::::::::FUNC:LOAN:RETURN:::::::::::::::::::::::::::::::::::::::::
                
            case 3:
                while (1){
                system("clear");
                printf("Which action do you want to do?:\n1)Loan a book.\n2)Return a book.\n\n0)Main Menu\n\n");
                scanf("%d", &temp);
                
                if (temp == 1){
                    system ("clear");   //Copyout________________
                    copyOut(List);
                    printf("Function worked correctly.\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;}
                else if (temp == 2){
                                        //Copyin________________________
                system ("clear");
                    copyIn(List);
                    printf("Function worked correctly.\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;
                }
                    else if (temp == 0)
                        goto MainMenu;
                
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                }
                }
//::::::::::::::::::::::::::::::::::::::FUNC:SEE:LIST:::::::::::::::::::::::::::::::::::::::::::
            
            case 4:
                while (1){
                system("clear");
                printf("What do you want to see?\n1)The List\n2)A specific book\n3)To check if the book is in the list\n\n0)Main Menu\n\n");
                scanf ("%d", &temp);
                
                if (temp == 1){ //see the list_________________________________________
                    system ("clear");
                    printList(List);
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                    break;
                }
                
                else if (temp ==2){ //see a specific book______________________________
                     system ("clear");
                     printf("Enter the id:\n");
                     Cell_t *p = searchId(List);
                    if(p != NULL){
                        printElem(p->book);
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    else { printf("No book with this id available.\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                }
                else if (temp==3){//To controll if the book is in the list_____________
                    system ("clear");
                    printf("Enter the id to see if the book is in the list:\n");
                    Cell_t *ren = searchId(List);
                    if (ren != NULL) {
                        printf("TRUE - In List\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    else {
                        printf("FALSE - Not in list\n");
                        printf("Press any number to continue..\n");
                        scanf("%d", &temp);
                        break;}
                    
                                }
                else if (temp == 0) {
                    goto MainMenu;
                                    }
                else {
                    printf ("Please Insert a valid Number\a\n");
                    printf("Press any number to continue..\n");
                    scanf("%d", &temp);
                     }
            }
                
                    
                
        }
    
 
//::::::::::::::::::::::::::::::::::::::FINE:FUNZIONI:::::::::::::::::::::::::::::::::::::::::::
       
                
        system ("clear");
        mainmenu();
        printf("\nChoice: ");
        scanf("%d", &choice);
    } //Fine While
    system ("clear");
    int e = 0;
finalpiont: printf("\nExiting...\n\nAre you sure?\n1)Yes\n2)No\n");
        scanf("%d", &e);
        if (e == 1){
            system ("clear");
            printf("\nThanks for using -Book's Registration Program-\n\nCopyright © 2016 Matteo Peruzzi. All rights reserved.\n\n\n");
            return 0;
        }
    else if (e == 2) {
        system("clear");
        goto MainMenu;
    }
    else {
        system ("clear");
        printf ("Please Insert a valid Number\a\n");
        goto finalpiont;
    }

}