<commit_before>//
//  fibers.cpp
//  fibio
//
//  Created by <NAME> on 14-3-1.
//  Copyright (c) 2014 0d0a.com. All rights reserved.
//

#include <memory>
#include <algorithm>
#include <fibio/fibers/fiber.hpp>
#include <fibio/fibers/fss.hpp>
#include <fibio/fibers/mutex.hpp>
#include <fibio/fibers/condition_variable.hpp>

#include "fiber_object.hpp"
#include "scheduler_object.hpp"

#ifdef HAVE_VALGRIND_H
#include <unordered_map>
#include <boost/coroutine/stack_allocator.hpp>
#include <boost/coroutine/stack_context.hpp>
#include "valgrind.h"
#endif  // defined(HAVE_VALGRIND_H)

static const auto NOT_A_FIBER=fibio::fiber_exception(boost::system::errc::no_such_process);
static const auto DEADLOCK=fibio::fiber_exception(boost::system::errc::resource_deadlock_would_occur);

namespace fibio { namespace fibers { namespace detail {
#ifdef BOOST_USE_SEGMENTED_STACKS
#   define BOOST_COROUTINE_STACK_ALLOCATOR boost::coroutines::basic_segmented_stack_allocator
#else
#   define BOOST_COROUTINE_STACK_ALLOCATOR boost::coroutines::basic_standard_stack_allocator
#endif

    // Define a fibio_stack_allocator, use valgrind_stack_allocator when building
    //  with valgrind support
#ifdef HAVE_VALGRIND_H
    // Wraps boost::coroutine::stack_allocator, and if Valgrind is installed
    // will register stacks, so that Valgrind is not confused.
    template<typename traitsT>
    class valgrind_stack_allocator {
        typedef traitsT traits_type;
        BOOST_COROUTINE_STACK_ALLOCATOR<traitsT> allocator;
        std::unordered_map<void*, unsigned> stack_ids;
        
    public:
        void inline allocate( boost::coroutines::stack_context &sc, std::size_t size) {
            allocator.allocate(sc, size);
            auto res = stack_ids.insert({sc.sp, VALGRIND_STACK_REGISTER(sc.sp, (((char*)sc.sp) - sc.size))});
            (void)res;
            assert(res.second);
        }
        
        void inline deallocate( boost::coroutines::stack_context & sc) {
            auto id = stack_ids.find(sc.sp);
            assert(id != stack_ids.end());<commit_msg>Boost 1.57 has built-in valgrind support<commit_after>//
//  fibers.cpp
//  fibio
//
//  Created by <NAME> on 14-3-1.
//  Copyright (c) 2014 0d0a.com. All rights reserved.
//

#include <memory>
#include <algorithm>
#include <fibio/fibers/fiber.hpp>
#include <fibio/fibers/fss.hpp>
#include <fibio/fibers/mutex.hpp>
#include <fibio/fibers/condition_variable.hpp>

#include "fiber_object.hpp"
#include "scheduler_object.hpp"

#if defined(HAVE_VALGRIND_H) && (BOOST_VERSION<105700)
#include <unordered_map>
#include <boost/coroutine/stack_allocator.hpp>
#include <boost/coroutine/stack_context.hpp>
#include "valgrind.h"
#endif  // defined(HAVE_VALGRIND_H)

static const auto NOT_A_FIBER=fibio::fiber_exception(boost::system::errc::no_such_process);
static const auto DEADLOCK=fibio::fiber_exception(boost::system::errc::resource_deadlock_would_occur);

namespace fibio { namespace fibers { namespace detail {
#ifdef BOOST_USE_SEGMENTED_STACKS
#   define BOOST_COROUTINE_STACK_ALLOCATOR boost::coroutines::basic_segmented_stack_allocator
#else
#   define BOOST_COROUTINE_STACK_ALLOCATOR boost::coroutines::basic_standard_stack_allocator
#endif

    // Define a fibio_stack_allocator, use valgrind_stack_allocator when building
    //  with valgrind support
    // Valgrind support is built-in since Boost 1.57
#if defined(HAVE_VALGRIND_H) && (BOOST_VERSION<105700)
    // Wraps boost::coroutine::stack_allocator, and if Valgrind is installed
    // will register stacks, so that Valgrind is not confused.
    template<typename traitsT>
    class valgrind_stack_allocator {
        typedef traitsT traits_type;
        BOOST_COROUTINE_STACK_ALLOCATOR<traitsT> allocator;
        std::unordered_map<void*, unsigned> stack_ids;
        
    public:
        void inline allocate( boost::coroutines::stack_context &sc, std::size_t size) {
            allocator.allocate(sc, size);
            auto res = stack_ids.insert({sc.sp, VALGRIND_STACK_REGISTER(sc.sp, (((char*)sc.sp) - sc.size))});
            (void)res;
            assert(res.second);
        }
        
        void inline deallocate( boost::coroutines::stack_context & sc) {
            auto id = stack_ids.find(sc.sp);
            assert(id != stack_ids.end());
{'fraction_non_alphanumeric': 0.11282051282051282, 'fraction_numerical': 0.008857808857808859, 'mean_word_length': 4.120525059665871, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1523014', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1438, 'n_words': 283}