Exemplo n.º 1
0
 char const*
 parse_token_to_eol(
     char const* p,
     char const* last,
     char const*& token_last,
     error_code& ec)
 {
     for(;; ++p)
     {
         if(p >= last)
         {
             ec = error::need_more;
             return p;
         }
         if(BOOST_UNLIKELY(! is_print(*p)))
             if((BOOST_LIKELY(static_cast<
                     unsigned char>(*p) < '\040') &&
                 BOOST_LIKELY(*p != '\011')) ||
                 BOOST_UNLIKELY(*p == '\177'))
                 goto found_control;
     }
 found_control:
     if(BOOST_LIKELY(*p == '\r'))
     {
         if(++p >= last)
         {
             ec = error::need_more;
             return last;
         }
         if(*p++ != '\n')
         {
             ec = error::bad_line_ending;
             return last;
         }
         token_last = p - 2;
     }
 #if 0
     // VFALCO This allows `\n` by itself
     //        to terminate a line
     else if(*p == '\n')
     {
         token_last = p;
         ++p;
     }
 #endif
     else
     {
         // invalid character
         return nullptr;
     }
     return p;
 }
 packaged_task & operator=( packaged_task && other) noexcept {
     if ( BOOST_LIKELY( this != & other) ) {
         packaged_task tmp{ std::move( other) };
         swap( tmp);
     }
     return * this;
 }
Exemplo n.º 3
0
 promise_base & operator=( promise_base && other) noexcept {
     if ( BOOST_LIKELY( this != & other) ) {
         promise_base tmp{ std::move( other) };
         swap( tmp);
     }
     return * this;
 }
Exemplo n.º 4
0
std::size_t pool_resource::pool_cached_blocks(std::size_t pool_idx) const
{
   if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
      return m_pool_data[pool_idx].cache_count();
   }
   else{
      return 0u;
   }
}
Exemplo n.º 5
0
std::size_t pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx) const
{
   if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
      return m_pool_data[pool_idx].next_blocks_per_chunk;
   }
   else{
      return 1u;
   }
}
Exemplo n.º 6
0
std::size_t pool_resource::pool_count() const
{
   if(BOOST_LIKELY((0 != m_pool_data))){
      return m_pool_count;
   }
   else{
      return priv_pool_index(m_options.largest_required_pool_block)+1u;
   }
}
Exemplo n.º 7
0
void Hook_Shutdown()
{
    if (!lockstep_hooked)
    {
        return;
    }

#ifdef _WIN32
    Remove_Hook((void *)GetTickCount, Real_GetTickCount, (void *)Fake_GetTickCount);
#else
    Remove_Hook((void *)gettimeofday, Real_gettimeofday, (void *)Fake_gettimeofday);
#endif
    Remove_Hook((void *)SDL_GetTicks, Real_SDL_GetTicks, (void *)Fake_SDL_GetTicks);
    LOCKSTEP_DEBUG("unhooked ticks");

    if (BOOST_LIKELY(lockstep_renderer == enabler->renderer))
    {
        enabler->renderer = lockstep_renderer->real_renderer;
    }
    LOCKSTEP_DEBUG("deleting df_ai_renderer");
    delete lockstep_renderer;
    lockstep_renderer = nullptr;

    lockstep_hooked = false;

    if (lockstep_want_shutdown_now)
    {
        if (df::viewscreen *screen = Gui::getCurViewscreen(true))
        {
            screen->breakdown_level = interface_breakdown_types::QUIT;
        }
    }
    else if (disabling_plugin)
    {
        enabled = false;
        disabling_plugin = false;
        check_enabled(Core::getInstance().getConsole());
    }
}
Exemplo n.º 8
0
void interprocess_semaphore::create_or_open(const wchar_t* name, permissions const& perms)
{
#if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
    boost::detail::winapi::HANDLE_ h = boost::detail::winapi::CreateSemaphoreExW
    (
        reinterpret_cast< boost::detail::winapi::SECURITY_ATTRIBUTES_* >(perms.get_native()),
        0, // initial count
        (std::numeric_limits< boost::detail::winapi::LONG_ >::max)(), // max count
        name,
        0u, // flags
        boost::detail::winapi::SYNCHRONIZE_ | boost::detail::winapi::SEMAPHORE_MODIFY_STATE_ | boost::detail::winapi::SEMAPHORE_QUERY_STATE_
    );
#else
    boost::detail::winapi::HANDLE_ h = boost::detail::winapi::CreateSemaphoreW
    (
        reinterpret_cast< boost::detail::winapi::SECURITY_ATTRIBUTES_* >(perms.get_native()),
        0, // initial count
        (std::numeric_limits< boost::detail::winapi::LONG_ >::max)(), // max count
        name
    );
#endif
    if (h == NULL)
    {
        boost::detail::winapi::DWORD_ err = boost::detail::winapi::GetLastError();
        if (BOOST_LIKELY(err == ERROR_ALREADY_EXISTS))
        {
            open(name);
            return;
        }
        else
        {
            BOOST_LOG_THROW_DESCR_PARAMS(boost::log::system_error, "Failed to create an interprocess semaphore object", (err));
        }
    }

    m_sem.init(h);
}
Exemplo n.º 9
0
void Hook_Update()
{
    if (BOOST_LIKELY(SDL_ThreadID() == enabler->renderer_threadid))
    {
        return;
    }

    if (lockstep_want_shutdown)
    {
        while (!lockstep_ready_for_shutdown)
        {
            tthread::this_thread::yield();
        }
        LOCKSTEP_DEBUG("trying to unhook");
        lockstep_want_shutdown = false;
        lockstep_ready_for_shutdown = false;
        Hook_Shutdown();
    }
    else if (!lockstep_hooked && config.lockstep && !Hook_Want_Disable() && !lockstep_want_shutdown_now)
    {
        LOCKSTEP_DEBUG("trying to hook");
        if (init->display.flag.is_set(init_display_flags::TEXT))
        {
            auto & out = Core::getInstance().getConsole();
            out << COLOR_LIGHTRED << "[df-ai] lockstep mode does not work with PRINT_MODE:TEXT. Disabling lockstep in the df-ai config.";
            out << COLOR_RESET << std::endl;
            config.set(out, config.lockstep, false);
            return;
        }

        if (strict_virtual_cast<df::viewscreen_movieplayerst>(Gui::getCurViewscreen(false)))
        {
            LOCKSTEP_DEBUG("not hooking while movie player is present");
            return;
        }

        lockstep_hooked = true;

#ifdef _WIN32
        lockstep_tick_count = GetTickCount();
        LOCKSTEP_DEBUG("initial lockstep_tick_count is " << lockstep_tick_count);
        Add_Hook((void *)GetTickCount, Real_GetTickCount, (void *)Fake_GetTickCount);
#else
        struct timeval tv;
        gettimeofday(&tv, nullptr);
        lockstep_tick_count = tv.tv_sec * 1000 + tv.tv_usec / 1000;
        LOCKSTEP_DEBUG("initial lockstep_tick_count is " << lockstep_tick_count);
        Add_Hook((void *)gettimeofday, Real_gettimeofday, (void *)Fake_gettimeofday);
#endif
        LOCKSTEP_DEBUG("hooked time");
        Add_Hook((void *)SDL_GetTicks, Real_SDL_GetTicks, (void *)Fake_SDL_GetTicks);
        LOCKSTEP_DEBUG("hooked ticks");
        enabler->outstanding_gframes = 0;
        enabler->outstanding_frames = 0;

        lockstep_renderer = new df_ai_renderer(enabler->renderer);
        enabler->renderer = lockstep_renderer;

        CoreSuspendReleaseMain releaser;

        SDL_SemWait(enabler->async_zoom.sem);
        enabler->async_zoom.queue.push_back(zoom_commands::zoom_reset);
        SDL_SemPost(enabler->async_zoom.sem);
        SDL_SemPost(enabler->async_zoom.sem_fill);

        while (lockstep_hooked && !lockstep_want_shutdown)
        {
            while (SDL_SemTryWait(enabler->async_tobox.sem_fill) == 0)
            {
                SDL_SemWait(enabler->async_tobox.sem);
                auto msg = enabler->async_tobox.queue.front();
                enabler->async_tobox.queue.pop_front();
                switch (msg.cmd)
                {
                case df::enabler::T_async_tobox::T_queue::pause:
                case df::enabler::T_async_tobox::T_queue::render:
                {
                    df::enabler::T_async_frombox::T_queue complete;
                    complete.msg = df::enabler::T_async_frombox::T_queue::complete;
                    SDL_SemWait(enabler->async_frombox.sem);
                    enabler->async_frombox.queue.push_back(complete);
                    SDL_SemPost(enabler->async_frombox.sem);
                    SDL_SemPost(enabler->async_frombox.sem_fill);
                    break;
                }
                case df::enabler::T_async_tobox::T_queue::start:
                case df::enabler::T_async_tobox::T_queue::inc:
                case df::enabler::T_async_tobox::T_queue::set_fps:
                    break;
                }
                SDL_SemPost(enabler->async_tobox.sem);
            }

            tthread::this_thread::sleep_for(tthread::chrono::seconds(1));
        }
    }
}
Exemplo n.º 10
0
static void lockstep_loop()
{
    CoreSuspendClaimMain claimMain;
    while (BOOST_LIKELY(!Hook_Want_Disable()))
    {
        LOCKSTEP_DEBUG("calling mainloop (A)");
        if (BOOST_UNLIKELY(lockstep_mainloop()))
        {
            LOCKSTEP_DEBUG("want shutdown (A)");
            Hook_Shutdown_Now();
            break;
        }
        LOCKSTEP_DEBUG("calling DFHack (A)");
        SDL_NumJoysticks();
        if (BOOST_UNLIKELY(Hook_Want_Disable()))
        {
            LOCKSTEP_DEBUG("want disable (A)");
            break;
        }
        lockstep_tick_count += 10;
        if (BOOST_LIKELY(!enabler->flag.bits.maxfps && !ui->main.autosave_request))
        {
            LOCKSTEP_DEBUG("calling mainloop (B)");
            if (BOOST_UNLIKELY(lockstep_mainloop()))
            {
                LOCKSTEP_DEBUG("want shutdown (B)");
                Hook_Shutdown_Now();
                break;
            }
            LOCKSTEP_DEBUG("calling DFHack (B)");
            SDL_NumJoysticks();
            if (BOOST_UNLIKELY(Hook_Want_Disable()))
            {
                LOCKSTEP_DEBUG("want disable (B)");
                break;
            }
        }
        else
        {
            LOCKSTEP_DEBUG((enabler->flag.bits.maxfps ? "maxfps" : "autosave_request"));
        }
        lockstep_tick_count += 10;
        enabler->last_tick = lockstep_tick_count;
        enabler->clock = lockstep_tick_count;
        LOCKSTEP_DEBUG("draining events");
        if (BOOST_UNLIKELY(lockstep_drain_sdl()))
        {
            LOCKSTEP_DEBUG("user requested quit");
            break;
        }
        LOCKSTEP_DEBUG("swap_arrays");
        lockstep_swap_arrays();
        LOCKSTEP_DEBUG("render_things");
        lockstep_render_things();
        LOCKSTEP_DEBUG("update_all");
        enabler->renderer->update_all();
        LOCKSTEP_DEBUG("render");
        enabler->renderer->render();
    }

    extern std::unique_ptr<AI> dwarfAI;
    if (dwarfAI && dwarfAI->camera.movie_started_in_lockstep)
    {
        LOCKSTEP_DEBUG("stopping movie started in lockstep to avoid corruption");
        // Stop the current CMV so it doesn't get corrupted on the next frame.
        lockstep_handlemovie(true);
    }
}
Exemplo n.º 11
0
static void lockstep_handlemovie(bool flushall)
{
    //SAVE A MOVIE FRAME INTO THE CURRENT MOVIE BUFFER
    if (gview->supermovie_on == 1)
    {
        if (BOOST_UNLIKELY(gview->supermovie_delaystep > 0 && !flushall))
        {
            gview->supermovie_delaystep--;
        }
        else
        {
            if (BOOST_LIKELY(!flushall))
            {
                gview->supermovie_delaystep = gview->supermovie_delayrate;
            }

            if (!flushall || gview->supermovie_delaystep == 0)
            {
                extern std::unique_ptr<AI> dwarfAI;
                //SAVING CHARACTERS, THEN COLORS
                short x2, y2;
                for (x2 = 0; x2 < init->display.grid_x; x2++)
                {
                    for (y2 = 0; y2 < init->display.grid_y; y2++)
                    {
                        gview->supermoviebuffer[gview->supermovie_pos] = gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 0];

                        gview->supermovie_pos++;
                    }
                    if (BOOST_LIKELY(dwarfAI && dwarfAI->camera.movie_started_in_lockstep))
                    {
                        for (y2 = 0; y2 < init->display.grid_y; y2++)
                        {
                            gview->supermoviebuffer[gview->supermovie_pos] = x2 < 80 && y2 < 25 ? dwarfAI->lockstep_log_buffer[y2][x2] : ' ';

                            gview->supermovie_pos++;
                        }
                    }
                }
                char frame_col;
                for (x2 = 0; x2 < init->display.grid_x; x2++)
                {
                    for (y2 = 0; y2 < init->display.grid_y; y2++)
                    {
                        frame_col = gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 1];
                        frame_col |= (gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 2] << 3);
                        if (gps->screen[x2 * gps->dimy * 4 + y2 * 4 + 3])
                        {
                            frame_col |= 64;
                        }
                        gview->supermoviebuffer[gview->supermovie_pos] = frame_col;

                        gview->supermovie_pos++;
                    }
                    if (BOOST_LIKELY(dwarfAI && dwarfAI->camera.movie_started_in_lockstep))
                    {
                        for (y2 = 0; y2 < init->display.grid_y; y2++)
                        {
                            gview->supermoviebuffer[gview->supermovie_pos] = 7;

                            gview->supermovie_pos++;
                        }
                    }
                }
            }

            int frame_size = init->display.grid_x * init->display.grid_y * 2;
            extern std::unique_ptr<AI> dwarfAI;
            if (BOOST_LIKELY(dwarfAI->camera.movie_started_in_lockstep))
            {
                frame_size *= 2;
            }
            if (BOOST_UNLIKELY(gview->supermovie_pos + frame_size >= MOVIEBUFFSIZE || flushall || !dwarfAI || !dwarfAI->camera.movie_started_in_lockstep))
            {
                int length = lockstep_write_movie_chunk();

                if (length > 5000000 || dwarfAI == nullptr || !dwarfAI->camera.movie_started_in_lockstep)
                {
                    lockstep_finish_movie();
                }
                else
                {
                    gview->supermovie_pos = 0;
                }
            }
        }
    }
}
Exemplo n.º 12
0
static int32_t lockstep_write_movie_chunk()
{
    int32_t inputsize = gview->supermovie_pos;
    if (BOOST_UNLIKELY(inputsize > MOVIEBUFFSIZE))
    {
        inputsize = MOVIEBUFFSIZE;
    }

    //DUMP CURRENT BUFFER INTO A COMPRESSION STREAM
    z_stream c_stream;
    int err;

    c_stream.zalloc = (alloc_func)0;
    c_stream.zfree = (free_func)0;
    c_stream.opaque = (voidpf)0;

    err = deflateInit(&c_stream, Z_BEST_COMPRESSION);
    if (CHECK_ERR(err, "deflateInit"))
    {
        return 5000001;
    }

    c_stream.next_out = (Bytef*)gview->supermoviebuffer_comp;
    c_stream.avail_out = COMPMOVIEBUFFSIZE;

    c_stream.next_in = (Bytef*)gview->supermoviebuffer;
    c_stream.avail_in = inputsize;

    while (int32_t(c_stream.total_in) != inputsize && c_stream.total_out < COMPMOVIEBUFFSIZE)
    {
        //c_stream.avail_in = c_stream.avail_out = 1; // force small buffers
        err = deflate(&c_stream, Z_NO_FLUSH);
        if (CHECK_ERR(err, "deflate"))
        {
            return 5000001;
        }
    }

    // Finish the stream, still forcing small buffers:
    for (;;)
    {
        err = deflate(&c_stream, Z_FINISH);
        if (err == Z_STREAM_END)
        {
            break;
        }
        if (CHECK_ERR(err, "deflate"))
        {
            return 5000001;
        }
    }

    err = deflateEnd(&c_stream);
    if (CHECK_ERR(err, "deflateEnd"))
    {
        return 5000001;
    }

    int length = 0;

    if (c_stream.total_out > 0)
    {
        if (BOOST_UNLIKELY(gview->first_movie_write))
        {
            //GET RID OF ANY EXISTING MOVIES IF THIS IS THE FIRST TIME THROUGH
            unlink(gview->movie_file.c_str());
        }

        //OPEN UP THE MOVIE FILE AND APPEND
        std::ofstream f(gview->movie_file, std::ios::out | std::ios::binary | std::ios::app);

        if (BOOST_LIKELY(f.is_open()))
        {
            //WRITE A HEADER
            if (BOOST_UNLIKELY(gview->first_movie_write))
            {
                const int32_t movie_version = 10000;
                f.write((const char *)&movie_version, sizeof(int32_t));

                int32_t header[3];
                header[0] = init->display.grid_x;
                header[1] = init->display.grid_y;
                extern std::unique_ptr<AI> dwarfAI;
                if (BOOST_LIKELY(dwarfAI->camera.movie_started_in_lockstep))
                {
                    header[1] *= 2;
                }
                header[2] = gview->supermovie_delayrate;
                f.write((const char *)&header, sizeof(header));
            }

            //WRITE IT
            uint32_t compsize = c_stream.total_out;
            f.write((const char *)&compsize, sizeof(uint32_t));
            f.write((const char *)gview->supermoviebuffer_comp, c_stream.total_out);

            f.seekp(0, std::ios::end);
            length = f.tellp();
        }
        else
        {
            gview->supermovie_on = 0;
        }

        gview->first_movie_write = 0;
    }

    return length;
}
Exemplo n.º 13
0
processed_transaction database::_apply_transaction(const signed_transaction& trx)
{ try {
   uint32_t skip = get_node_properties().skip_flags;

   if( true || !(skip&skip_validate) )   /* issue #505 explains why this skip_flag is disabled */
      trx.validate();

   auto& trx_idx = get_mutable_index_type<transaction_index>();
   const chain_id_type& chain_id = get_chain_id();
   auto trx_id = trx.id();
   FC_ASSERT( (skip & skip_transaction_dupe_check) ||
              trx_idx.indices().get<by_trx_id>().find(trx_id) == trx_idx.indices().get<by_trx_id>().end() );
   transaction_evaluation_state eval_state(this);
   const chain_parameters& chain_parameters = get_global_properties().parameters;
   eval_state._trx = &trx;

   if( !(skip & (skip_transaction_signatures | skip_authority_check) ) )
   {
      auto get_active = [&]( account_id_type id ) { return &id(*this).active; };
      auto get_owner  = [&]( account_id_type id ) { return &id(*this).owner;  };
      trx.verify_authority( chain_id, get_active, get_owner, get_global_properties().parameters.max_authority_depth );
   }

   //Skip all manner of expiration and TaPoS checking if we're on block 1; It's impossible that the transaction is
   //expired, and TaPoS makes no sense as no blocks exist.
   if( BOOST_LIKELY(head_block_num() > 0) )
   {
      if( !(skip & skip_tapos_check) )
      {
         const auto& tapos_block_summary = block_summary_id_type( trx.ref_block_num )(*this);

         //Verify TaPoS block summary has correct ID prefix, and that this block's time is not past the expiration
         FC_ASSERT( trx.ref_block_prefix == tapos_block_summary.block_id._hash[1] );
      }

      fc::time_point_sec now = head_block_time();

      FC_ASSERT( trx.expiration <= now + chain_parameters.maximum_time_until_expiration, "",
                 ("trx.expiration",trx.expiration)("now",now)("max_til_exp",chain_parameters.maximum_time_until_expiration));
      FC_ASSERT( now <= trx.expiration, "", ("now",now)("trx.exp",trx.expiration) );
   }

   //Insert transaction into unique transactions database.
   if( !(skip & skip_transaction_dupe_check) )
   {
      create<transaction_object>([&](transaction_object& transaction) {
         transaction.trx_id = trx_id;
         transaction.trx = trx;
      });
   }

   eval_state.operation_results.reserve(trx.operations.size());

   //Finally process the operations
   processed_transaction ptrx(trx);
   _current_op_in_trx = 0;
   for( const auto& op : ptrx.operations )
   {
      eval_state.operation_results.emplace_back(apply_operation(eval_state, op));
      ++_current_op_in_trx;
   }
   ptrx.operation_results = std::move(eval_state.operation_results);

   //Make sure the temp account has no non-zero balances
   const auto& index = get_index_type<account_balance_index>().indices().get<by_account_asset>();
   auto range = index.equal_range( boost::make_tuple( GRAPHENE_TEMP_ACCOUNT ) );
   std::for_each(range.first, range.second, [](const account_balance_object& b) { FC_ASSERT(b.balance == 0); });

   return ptrx;
} FC_CAPTURE_AND_RETHROW( (trx) ) }