Exemple #1
0
int main(int argc, char *argv[]) {
  int fd, newFd;

  if (argc < 2 || strcmp(argv[1], "--help") == 0) {
    usageErr("%s filename\n", argv[0]);
  }

  fd = openFile(argv[1]);
  newFd = dup2(fd, 150);

  if (newFd == -1) {
    error("Couldn't dup file");
  }

  checkOpenFlags(fd, newFd);
  checkOffset(fd, newFd);

  if (write(fd, "Hello!", 6) != 6) {
    error("Couldn't write whole buffer to file");
  }

  checkOffset(fd, newFd);

  if (close(fd) == -1) {
    error("Failed to close file");
  }

  exit(EXIT_SUCCESS);
}
Exemple #2
0
Structure* Structure::nonPropertyTransition(JSGlobalData& globalData, Structure* structure, NonPropertyTransition transitionKind)
{
    unsigned attributes = toAttributes(transitionKind);
    IndexingType indexingType = newIndexingType(structure->indexingTypeIncludingHistory(), transitionKind);
    
    if (JSGlobalObject* globalObject = structure->m_globalObject.get()) {
        if (globalObject->isOriginalArrayStructure(structure)) {
            Structure* result = globalObject->originalArrayStructureForIndexingType(indexingType);
            if (result->indexingTypeIncludingHistory() == indexingType) {
                structure->notifyTransitionFromThisStructure();
                return result;
            }
        }
    }
    
    if (Structure* existingTransition = structure->m_transitionTable.get(0, attributes)) {
        ASSERT(existingTransition->m_attributesInPrevious == attributes);
        ASSERT(existingTransition->indexingTypeIncludingHistory() == indexingType);
        return existingTransition;
    }
    
    Structure* transition = create(globalData, structure);
    transition->setPreviousID(globalData, transition, structure);
    transition->m_attributesInPrevious = attributes;
    transition->m_indexingType = indexingType;
    transition->propertyTable().set(globalData, transition, structure->takePropertyTableOrCloneIfPinned(globalData, transition));
    transition->m_offset = structure->m_offset;
    checkOffset(transition->m_offset, transition->inlineCapacity());
    
    structure->m_transitionTable.add(globalData, transition);
    transition->checkOffsetConsistency();
    return transition;
}
Exemple #3
0
void BinaryNode::splice(qore_offset_t offset, qore_offset_t length, BinaryNode* extract) {
   checkOffset(offset, length);
  //printd(5, "BinaryNode::splice(offset="QSD", length="QSD", priv->len="QSD")\n", offset, length, len);

   if (offset == (qore_offset_t)len || !length)
      return;

   qore_size_t end;
   if (length > (qore_offset_t)(len - offset)) {
      end = len;
      length = len - offset;
   }
   else
      end = offset + length;

   // add to extract string if any
   if (extract && length)
      extract->append((char*)ptr + offset, length);

   // move down entries if necessary
   if (end != len)
      memmove((char*)ptr + offset, (char*)ptr + end, len - end);

   // calculate new length
   len -= length;
}
Exemple #4
0
Structure* Structure::addPropertyTransition(JSGlobalData& globalData, Structure* structure, PropertyName propertyName, unsigned attributes, JSCell* specificValue, PropertyOffset& offset)
{
    // If we have a specific function, we may have got to this point if there is
    // already a transition with the correct property name and attributes, but
    // specialized to a different function.  In this case we just want to give up
    // and despecialize the transition.
    // In this case we clear the value of specificFunction which will result
    // in us adding a non-specific transition, and any subsequent lookup in
    // Structure::addPropertyTransitionToExistingStructure will just use that.
    if (specificValue && structure->m_transitionTable.contains(propertyName.uid(), attributes))
        specificValue = 0;

    ASSERT(!structure->isDictionary());
    ASSERT(structure->isObject());
    ASSERT(!Structure::addPropertyTransitionToExistingStructure(structure, propertyName, attributes, specificValue, offset));
    
    if (structure->m_specificFunctionThrashCount == maxSpecificFunctionThrashCount)
        specificValue = 0;

    if (structure->transitionCount() > s_maxTransitionLength) {
        Structure* transition = toCacheableDictionaryTransition(globalData, structure);
        ASSERT(structure != transition);
        offset = transition->putSpecificValue(globalData, propertyName, attributes, specificValue);
        if (transition->outOfLineSize() > transition->outOfLineCapacity())
            transition->growOutOfLineCapacity();
        return transition;
    }
    
    Structure* transition = create(globalData, structure);

    transition->m_cachedPrototypeChain.setMayBeNull(globalData, transition, structure->m_cachedPrototypeChain.get());
    transition->m_previous.set(globalData, transition, structure);
    transition->m_nameInPrevious = propertyName.uid();
    transition->m_attributesInPrevious = attributes;
    transition->m_specificValueInPrevious.setMayBeNull(globalData, transition, specificValue);

    if (structure->m_propertyTable) {
        if (structure->m_isPinnedPropertyTable)
            transition->m_propertyTable = structure->m_propertyTable->copy(globalData, transition, structure->m_propertyTable->size() + 1);
        else
            transition->m_propertyTable = structure->m_propertyTable.release();
    } else {
        if (structure->m_previous)
            transition->materializePropertyMap(globalData);
        else
            transition->createPropertyMap();
    }

    offset = transition->putSpecificValue(globalData, propertyName, attributes, specificValue);
    if (transition->outOfLineSize() > transition->outOfLineCapacity())
        transition->growOutOfLineCapacity();

    transition->m_offset = offset;
    checkOffset(transition->m_offset, transition->inlineCapacity());
    structure->m_transitionTable.add(globalData, transition);
    return transition;
}
Exemple #5
0
int BinaryNode::substr(BinaryNode& b, qore_offset_t offset) const {
   printd(5, "BinaryNode::substr(offset: "QSD") this: %p len: "QSD")\n", offset, this, len);

   checkOffset(offset);
   if (offset == (qore_offset_t)len)
      return -1;

   b.append((char*)ptr + offset, len - offset);
   return 0;
}
Exemple #6
0
void BinaryNode::checkOffset(qore_offset_t& offset, qore_offset_t& num) const {
   checkOffset(offset);

   if (num < 0) {
      num = len + num - offset;
      if (num < 0)
         num = 0;
      return;
   }
}
Exemple #7
0
int BinaryNode::substr(BinaryNode& b, qore_offset_t offset, qore_offset_t length) const {
   printd(5, "BinaryNode::substr(offset: "QSD", length: "QSD") this: %p len: "QSD"\n", offset, length, this, len);

   checkOffset(offset, length);

   if (offset == (qore_offset_t)len)
      return -1;

   if (length > (qore_offset_t)(len - offset))
      length = len - offset;

   b.append((char*)ptr + offset, length);
   return 0;
}
Exemple #8
0
void BinaryNode::splice(qore_offset_t offset, qore_offset_t length, const void* data, qore_size_t data_len, BinaryNode* extract) {
   //printd(5, "BinaryNode::splice() before offset: %lld length: %lld (len: %lld data_len: %lld)\n", offset, length, len, data_len);
   checkOffset(offset, length);

   if (offset == (qore_offset_t)len) {
      if (!data_len)
         return;
      length = 0;
   }

   //printd(5, "BinaryNode::splice(offset="QSD", length="QSD", priv->len="QSD")\n", offset, length, len);

   qore_size_t end;
   if (length > (qore_offset_t)(len - offset)) {
      end = len;
      length = len - offset;
   }
   else
      end = offset + length;

   // add to extract string if any
   if (extract && length)
      extract->append((char*)ptr + offset, length);

   // get number of entries to insert
   if ((qore_offset_t)data_len > length) { // make bigger
      qore_size_t ol = len;

      // resize buffer
      ptr = q_realloc(ptr, len - length + data_len);

      // move trailing entries forward if necessary
      if (end != ol)
         memmove((char*)ptr + (end - length + data_len), (char*)ptr + end, ol - end);
   }
   else if (length > (qore_offset_t)data_len) // make smaller
      memmove((char*)ptr + offset + data_len, (char*)ptr + offset + length, len - offset - data_len);

   memcpy((char*)ptr + offset, data, data_len);

   // calculate new length
   len = len - length + data_len;
}
Exemple #9
0
Structure* Structure::nonPropertyTransition(JSGlobalData& globalData, Structure* structure, NonPropertyTransition transitionKind)
{
    unsigned attributes = toAttributes(transitionKind);
    IndexingType indexingType = newIndexingType(structure->indexingTypeIncludingHistory(), transitionKind);
    
    if (JSGlobalObject* globalObject = structure->m_globalObject.get()) {
        if (globalObject->isOriginalArrayStructure(structure)) {
            Structure* result = globalObject->originalArrayStructureForIndexingType(indexingType);
            if (result->indexingTypeIncludingHistory() == indexingType) {
                structure->notifyTransitionFromThisStructure();
                return result;
            }
        }
    }
    
    if (Structure* existingTransition = structure->m_transitionTable.get(0, attributes)) {
        ASSERT(existingTransition->m_attributesInPrevious == attributes);
        ASSERT(existingTransition->indexingTypeIncludingHistory() == indexingType);
        return existingTransition;
    }
    
    Structure* transition = create(globalData, structure);
    transition->m_previous.set(globalData, transition, structure);
    transition->m_attributesInPrevious = attributes;
    transition->m_indexingType = indexingType;
    transition->m_offset = structure->m_offset;
    checkOffset(transition->m_offset, transition->inlineCapacity());
    
    if (structure->m_propertyTable) {
        if (structure->m_isPinnedPropertyTable)
            transition->m_propertyTable = structure->m_propertyTable->copy(globalData, transition, structure->m_propertyTable->size() + 1);
        else
            transition->m_propertyTable = structure->m_propertyTable.release();
    } else {
        if (structure->m_previous)
            transition->materializePropertyMap(globalData);
        else
            transition->createPropertyMap();
    }
    
    structure->m_transitionTable.add(globalData, transition);
    return transition;
}
// Returns a valid offset for the new patch to apply
Point RandomOffsetChooser::getNewOffset(bool *foundMask) const {

    int minX, maxX, minY, maxY;
    getBoundaries(*outputMask, &minX, &maxX, &minY, &maxY);

    if (maxX < 0 || maxY < 0) {
        *foundMask = false;
        return Point(randRange(0, outputMask->width()  - patch->width()  + 1),
                     randRange(0, outputMask->height() - patch->height() + 1));
    } else {
        *foundMask = true;

        int offX, offY;
        do {
            offX = randRange(max(0, minX - patch->width()  + 1), min(maxX, outputMask->width()  - patch->width()  + 1));
            offY = randRange(max(0, minY - patch->height() + 1), min(maxY, outputMask->height() - patch->height() + 1));
        } while (!checkOffset(offX, offY));

        return Point(offX, offY);
    }
}
Exemple #11
0
QPointF CalculateLaylines::getNextPoint( const QVector<QPointF> &route, const QPointF &boatPos, const float &offset) {

    int nearest_point = getNearestPoint(route, boatPos);

    if ( checkIntersection( "obstacles_r", boatPos ) ) {
        // if we are inside an obstacle, don't even try
        return boatPos;
    }

    // we got the nearest point in the route
    // but could be far, let's find out the real
    // nearest point of the route by making a
    // projection towards it...
    // START SEARCH OF PROJECTION FOR FINETUNE


    //2. We'll get projections to each point on the route:
    QPointF projection_point;
    QLineF route_line, projection_line;
    QPointF a, b, c;
    if ( route.size() >= 2 ) {

        // If nearest is the last one:
        if ( route.size() - nearest_point == 1 ) {

            a = UwMath::toConformal( route.at( nearest_point - 1 ) );
            b = UwMath::toConformal( route.at( nearest_point ) );
            c = UwMath::toConformal( (const QPointF)boatPos );

            projection_point = UwMath::getProjectionPoint( a, b, c);
            UwMath::fromConformal( projection_point);

            route_line.setP1( route.at( nearest_point - 1 ));
            route_line.setP2( route.at( nearest_point ));
            projection_line.setP1( boatPos);
            projection_line.setP2( projection_point);

            // If nearest is the first one:
        } else if ( nearest_point == 0 ) {

            a = UwMath::toConformal( route.at( nearest_point) );
            b = UwMath::toConformal( route.at( nearest_point + 1) );
            c = UwMath::toConformal( (const QPointF)boatPos );

            projection_point = UwMath::getProjectionPoint( a, b, c);
            UwMath::fromConformal( projection_point);

            route_line.setP1( route.at( nearest_point));
            route_line.setP2( route.at( nearest_point + 1));
            projection_line.setP1( boatPos);
            projection_line.setP2( projection_point);

            // If nearest is not first or last one:
        } else {

            a = UwMath::toConformal( route.at( nearest_point) );
            b = UwMath::toConformal( route.at( nearest_point + 1) );
            c = UwMath::toConformal( (const QPointF)boatPos );

            projection_point = UwMath::getProjectionPoint( a, b, c);
            UwMath::fromConformal( projection_point);

            route_line.setP1( route.at( nearest_point));
            route_line.setP2( route.at( nearest_point + 1));
            projection_line.setP1( boatPos);
            projection_line.setP2( projection_point);

            // We must check if our position is between nearest and nearest + 1
            // or between nearest - 1 and nearest:
            if ( !checkIntersection(route_line, projection_line) ) {
                a = UwMath::toConformal( route.at( nearest_point - 1 ) );
                b = UwMath::toConformal( route.at( nearest_point ) );
                c = UwMath::toConformal( (const QPointF)boatPos );

                projection_point = UwMath::getProjectionPoint( a, b, c);
                UwMath::fromConformal( projection_point);

                route_line.setP1( route.at( nearest_point - 1 ));
                route_line.setP2( route.at( nearest_point ));
                projection_line.setP1( boatPos);
                projection_line.setP2( projection_point);
            }
        }

    }


    // Next we'll find the checkpoint:

    //3. We'll check if there are obstacles in the projeted triangle:
    if ( route.size() < 1 ) {


    } else if ( route.size() == 1 ) {

        // Route to process only has one point:
        QLineF heading( boatPos, route.at( nearest_point));

        bool hobs_r = checkIntersection( "obstacles_r", heading );
        bool hobs_l = checkIntersection( "obstacles_l", heading );

        // Finetune checkpoint in the heading:
        if ( hobs_r || hobs_l ) {
            this->obstacleFound = true;
            bool ready = false;

            QLineF last_heading;

            while ( !ready ) {

                if ( ( hobs_r && checkIntersection( "obstacles_r", heading ) ) ||
                     ( hobs_l && checkIntersection( "obstacles_l", heading ) ) ) {

                    last_heading = heading;
                    heading = UwMath::lineToHalf( heading);

                } else {
                    if (  checkOffset( heading, last_heading, offset ) )
                        ready = true;
                    else
                        heading = UwMath::avgLine( heading, last_heading);
                }
            }
            //We won't return the point between boat and checkpoint as the next checkpoint on long-term route:
            return heading.p2();

        } else {
            //We won't return the point between boat and checkpoint as the next checkpoint on long-term route:
            return heading.p2();
        }

    } else if ( route.size() > 1 ) {
        // Route to process has more than 1 point:
        int i = nearest_point;

        QPolygonF triangle;
        // 1st point:
        triangle << boatPos;

        // 2nd point:
        if ( checkIntersection( route_line, projection_line) ) {
            triangle << projection_point;
        } else {
            triangle << route.at( nearest_point);
        }

        // 3rd point:
        if ( route.at( nearest_point) == route_line.p2() ) {
            triangle << route.at( nearest_point);
            i = nearest_point - 1;
        } else if ( route.at( nearest_point) == route_line.p1() ) {
            triangle << route.at( nearest_point + 1);
        }

        //        // ########################################################################
        //        // SPECIAL CHECK to find obstacles in the heading of the first triangle  ##
        //        // ########################################################################'
        if ( triangle.at( 0 ) == boatPos ) {
            QLineF heading( triangle.at( 0), triangle.at( 1));
            QPolygonF last_triangle;

            bool hobs_r = checkIntersection( "obstacles_r", heading, triangle );
            bool hobs_l = checkIntersection( "obstacles_l", heading, triangle );

            // FINETUNE CHECKPOINT IN THE HEADING
            if ( hobs_r || hobs_l ) {

                bool ready = false;

                QLineF last_heading;

                while ( !ready ) {

                    if ( ( hobs_r && checkIntersection( "obstacles_r", heading, triangle) ) ||
                         ( hobs_l && checkIntersection( "obstacles_l", heading, triangle)) ) {

                        last_heading = heading;
                        heading = UwMath::lineToHalf( heading);
                        last_triangle = triangle;
                        triangle = UwMath::triangleToHalf(triangle);

                    } else {
                        //  if (  checkOffset( heading, last_heading, offset ) )
                        if ( checkOffset_OnePointVersion( last_triangle, triangle, offset ))
                            ready = true;
                        else
                            triangle = UwMath::avgTriangle_OnePointVersion( triangle, last_triangle);
                        //  heading = UwMath::avgLine( heading, last_heading);
                    }
                }

                return heading.p2();  //We won't return the point between boat and checkpoint as the next checkpoint on long-term route:
                // return triangle.at(2);

            }
        }
        //qDebug() << Q_FUNC_INFO << ": TriangleCount at specialCheck: " << triangleCount;
        // ########################################################################
        // END SPECIAL CHECK                                                     ##
        // ########################################################################

        bool obs_r = checkIntersection( "obstacles_r", triangle, triangle );
        bool obs_l = checkIntersection( "obstacles_l", triangle, triangle );

        //Note: This doesn't work correctly. Here the search for the route fails instantly when obstacles
        //are found on the route. What if there is obstacle-free route on the next route interval:
        while ( !obs_r && !obs_l &&  i < (route.size()/* - 2*/ )) {
            triangle.clear();

            triangle << boatPos;
            triangle << route.at( i);
            triangle << route.at( i + 1);

            obs_r = checkIntersection( "obstacles_r", triangle, triangle );
            obs_l = checkIntersection( "obstacles_l", triangle, triangle );

            // Finetune checkpoint:

            if ( obs_r || obs_l ) {

                bool ready = false;

                QPolygonF last_triangle;

                while ( !ready ) {
                    if ( obs_r && checkIntersection( "obstacles_r", triangle, triangle) ||
                         obs_l && checkIntersection( "obstacles_l", triangle, triangle) ) {

                        last_triangle = triangle;
                        triangle = UwMath::triangleToHalf_OnePointVersion( triangle);


                    } else {

                        if (checkOffset_OnePointVersion( last_triangle, triangle, offset ) ){
                            ready = true;
                        }
                        else{
                            triangle = UwMath::avgTriangle_OnePointVersion( triangle, last_triangle);
                        }
                    }
                    //If the points in the line segment on the side of the route there is no space for the boat in
                    //the map anymore and we can quit the search:
                    if(triangle.at(1) == triangle.at(2))
                        ready = true;
                }
            }
            i++;
        }
        return triangle.at( 2);

    }
    // End the process of searching for the checkpoints.

}
Exemple #12
0
void CalculateLaylines::getPath( const bool &side, const float &offset, const int &max_turns,
                                 const double &windAngle, const double &layLinesAngle,
                                 const QPointF &boatPos, const QPointF &destinyPos,
                                 const QPolygonF &obstacles_shape, QVector<QPointF> &Path )
{
    //Input has to be Geographical data because we check against PostGIS

    QPointF TPleft, TPright;
    bool sideRight = side;
    bool ready, obs_r, obs_l = false;
    int count = 0;
    // Path is returned by reference, clear:
    Path.clear();
    // First point in the path is the origin:
    Path.append( boatPos);
    QPolygonF present_triangle;
    QPolygonF last_triangle;

    // While the last point in the path is not the destiny
    // AND we don't exceed the maximum turning points setting we'll execute following:

    while ( Path.last() != destinyPos && count < max_turns ) {
        // Clear triangle:
        present_triangle.clear();

        // Get the turning points between the last point and dest.
        // For the first run last point in the path is origin:
        UwMath::getTurningPoints( TPleft, TPright,
                                  windAngle, layLinesAngle,
                                  Path.last(), destinyPos);

        // Build a new triangle
        present_triangle << Path.last();
        if (sideRight) present_triangle << TPright;
        else present_triangle << TPleft;
        present_triangle << destinyPos;

        // Check for obstacles in the triangle
        obs_r = checkIntersection( "obstacles_r", present_triangle, obstacles_shape );
        obs_l = checkIntersection( "obstacles_l", present_triangle, obstacles_shape );

        if ( obs_r || obs_l ) {
            obstacleFound = true;
            // Obstacles found: let's reduce the triangle until they disapear:
            ready = false;

            last_triangle = present_triangle;
            // Reduce triangle to half:
            present_triangle = UwMath::triangleToHalf( present_triangle );

            while ( !ready ) {
                // Check again, put booleans first and checkIntersection won't even be called:
                if ( ( obs_r && checkIntersection( "obstacles_r", present_triangle, obstacles_shape ) ) ||
                     ( obs_l && checkIntersection( "obstacles_l", present_triangle, obstacles_shape ) ) ) {

                    // Still obstacles: reduce again!
                    last_triangle = present_triangle;
                    present_triangle = UwMath::triangleToHalf( present_triangle );

                } else {

                    // No more obstacles:
                    if (  checkOffset( last_triangle, present_triangle, destinyPos, offset ) ) {
                        // The distance is acceptable, finetune done.
                        ready = true;
                    } else {
                        // We are too far, let's increase the triangle a bit:
                        present_triangle = UwMath::avgTriangle( present_triangle, last_triangle);
                    }
                }

            }
        }

        // This triangle has no obstacles inside:
        Path.append( present_triangle.at( 1 ) );
        Path.append( present_triangle.at( 2 ) );
        // Add a turn:
        count++;

        // We continue in the other side:
        sideRight = !sideRight;
    }
}
Exemple #13
0
void
loadDataFromFile(StorageCacheImpl* cache)
{
    bool structIsCorrect = true;
    char* name = NULL;
    unsigned int nameLen;
    unsigned int numExtra;
    unsigned int curFunc = 0;
    unsigned int curPatt = 0;
    unsigned int func;
    unsigned int patt;
    HfInfo file;

    if ( hfOpenRead(&file, cache->fpath) == FILE_NOT_FOUND ) {
        cache->isPopulate = false;
        return;
    }

    // Read file Header
    loadHeader(&file);

    // Read pattern header
    structIsCorrect &= loadPatternDataFromFile(&file, &name, &nameLen,
    		&numExtra);

    while (structIsCorrect)
    {
        unsigned int func = curFunc;
        unsigned int patt = curPatt;
        bool ret;
        BlasPatternInfo* bPatt = getPatternInfo(cache, func, patt);


        while (bPatt != NULL && memcmp(name, bPatt->name, nameLen) != 0 ) {
            nextPattern(cache, &func, &patt);
            bPatt = getPatternInfo(cache, func, patt);
        }

        if (bPatt != NULL) {
            bPatt->sstatus = SS_CORRECT_DATA;

            // Read pattern data
        	ret = readPatternData(&file, bPatt, numExtra);

        	// go to next pattern
        	nextPattern(cache, &func, &patt);
        	// if the pattern is read witch error or not completely
        	if (!ret) {
        		bPatt = getPatternInfo(cache, func, patt);
        	    hfJump(&file, bPatt->offset);
        	}

        	curFunc = func;
            curPatt = patt;
        }
        free(name);
        name = NULL;
        structIsCorrect &= loadPatternDataFromFile(&file, &name, &nameLen,
    			&numExtra );
    }

    for (func =0; func < BLAS_FUNCTIONS_NUMBER; ++ func) {
    	BlasFunctionInfo* bFunc = &cache->functionInfo[func];

    	for (patt =0; patt < bFunc->numPatterns; ++ patt){
    		BlasPatternInfo* bPatt = &bFunc->pattInfo[patt];
    		if (bPatt->sstatus == SS_NOLOAD) {
    			POSFILE ret = findPattern(&file, bPatt->name);
    			if (ret != 0) {
    				loadPatternDataFromFile(&file, &name, &nameLen,
    	    			&numExtra );
    				readPatternData(&file, bPatt, numExtra);
    			}
    		}
    	}
    }

    free(name);
    cache->isPopulate = true;
    hfClose(&file);
    checkOffset(cache->functionInfo);
}