Skip to content

Commit

Permalink
prepare for stack_allocator_pool and stack_pool_manager
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed May 8, 2017
1 parent 9be2c5e commit c75af74
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 69 deletions.
41 changes: 17 additions & 24 deletions include/libcopp/coroutine/coroutine_context_container.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,48 @@


#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#pragma once
#endif

#include <cstddef>

#include <libcopp/utils/errno.h>
#include <libcopp/coroutine/coroutine_context_base.h>
#include <libcopp/stack/stack_traits.h>
#include <libcopp/stack/stack_allocator.h>
#include <libcopp/stack/stack_traits.h>
#include <libcopp/utils/errno.h>

namespace copp {
namespace detail{
namespace detail {

/**
* @brief coroutine container
* contain stack context, stack allocator and runtime fcontext
*/
template<typename TCOC, typename TALLOC>
template <typename TCOC, typename TALLOC>
class coroutine_context_container : public TCOC {
public:
typedef TCOC coroutine_context_type;
typedef TCOC base_type;
typedef TALLOC allocator_type;

COROUTINE_CONTEXT_BASE_USING_BASE(base_type)
COROUTINE_CONTEXT_BASE_USING_BASE(base_type)
public:
coroutine_context_container() UTIL_CONFIG_NOEXCEPT : base_type(), alloc_(){}
coroutine_context_container(const allocator_type& alloc) UTIL_CONFIG_NOEXCEPT : alloc_(alloc){}
coroutine_context_container() UTIL_CONFIG_NOEXCEPT : base_type(), alloc_() {}
coroutine_context_container(const allocator_type &alloc) UTIL_CONFIG_NOEXCEPT : alloc_(alloc) {}

~coroutine_context_container() {
_reset_stack();
}
~coroutine_context_container() { _reset_stack(); }

/**
* @brief get stack allocator
* @return stack allocator
*/
inline const allocator_type& get_allocator() const UTIL_CONFIG_NOEXCEPT {
return alloc_;
}
inline const allocator_type &get_allocator() const UTIL_CONFIG_NOEXCEPT { return alloc_; }

/**
* @brief get stack allocator
* @return stack allocator
*/
inline allocator_type& get_allocator() UTIL_CONFIG_NOEXCEPT {
return alloc_;
}
inline allocator_type &get_allocator() UTIL_CONFIG_NOEXCEPT { return alloc_; }


protected:
Expand All @@ -77,7 +71,8 @@ namespace copp {
* @param func fcontext callback(set NULL to use default callback)
* @return COPP_EC_SUCCESS or error code
*/
int create(coroutine_runnable_base* runner, std::size_t stack_size_, void(*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
int create(coroutine_runnable_base *runner, std::size_t stack_size_,
void (*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
if (NULL != callee_stack_.sp) {
return COPP_EC_ALREADY_INITED;
}
Expand All @@ -97,7 +92,7 @@ namespace copp {
* @param func fcontext callback(set NULL to use default callback)
* @return COPP_EC_SUCCESS or error code
*/
int create(coroutine_runnable_base* runner, void(*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
int create(coroutine_runnable_base *runner, void (*func)(::copp::fcontext::transfer_t) = NULL) UTIL_CONFIG_NOEXCEPT {
if (NULL != callee_stack_.sp) {
return COPP_EC_ALREADY_INITED;
}
Expand All @@ -112,17 +107,15 @@ namespace copp {
}

private:
coroutine_context_container(const coroutine_context_container&) UTIL_CONFIG_DELETED_FUNCTION;
coroutine_context_container(const coroutine_context_container &) UTIL_CONFIG_DELETED_FUNCTION;

private:
allocator_type alloc_; /** stack allocator **/
};
}

typedef detail::coroutine_context_container<
detail::coroutine_context_base,
allocator::default_statck_allocator
> coroutine_context_default;
typedef detail::coroutine_context_container<detail::coroutine_context_base, allocator::default_statck_allocator>
coroutine_context_default;
}

#endif
10 changes: 10 additions & 0 deletions include/libcopp/stack/allocator/stack_allocator_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,18 @@ namespace copp {
* @param max_size buffer size
*/
stack_allocator_memory(void *start_ptr, std::size_t max_size) UTIL_CONFIG_NOEXCEPT;
stack_allocator_memory(stack_allocator_memory &other) UTIL_CONFIG_NOEXCEPT;
#if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
stack_allocator_memory(stack_allocator_memory &&other) UTIL_CONFIG_NOEXCEPT;
#endif
~stack_allocator_memory();

stack_allocator_memory &operator=(stack_allocator_memory &other) UTIL_CONFIG_NOEXCEPT;
#if defined(UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES) && UTIL_CONFIG_COMPILER_CXX_RVALUE_REFERENCES
stack_allocator_memory &operator=(stack_allocator_memory &&other) UTIL_CONFIG_NOEXCEPT;
#endif
void swap(stack_allocator_memory &other);

/**
* specify memory section allocated
* @param start_ptr buffer start address
Expand Down
12 changes: 5 additions & 7 deletions include/libcopp/stack/allocator/stack_allocator_split_segment.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@


#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#pragma once
#endif

#include <cstddef>

#include <libcopp/utils/features.h>

#ifdef COPP_HAS_ABI_HEADERS
# include COPP_ABI_PREFIX
#include COPP_ABI_PREFIX
#endif

namespace copp {
Expand All @@ -23,8 +23,7 @@ namespace copp {
* @brief memory allocator
* this allocator will create buffer using gcc split segment api
*/
class stack_allocator_split_segment
{
class stack_allocator_split_segment {
public:
stack_allocator_split_segment() UTIL_CONFIG_NOEXCEPT;
~stack_allocator_split_segment();
Expand All @@ -42,12 +41,11 @@ namespace copp {
*/
void deallocate(stack_context &) UTIL_CONFIG_NOEXCEPT;
};

}
}
}

#ifdef COPP_HAS_ABI_HEADERS
# include COPP_ABI_SUFFIX
#include COPP_ABI_SUFFIX
#endif

#endif
59 changes: 29 additions & 30 deletions include/libcopp/utils/errno.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@


#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#pragma once
#endif

namespace copp
{
/**
* error code
*/
enum copp_error_code
{
COPP_EC_SUCCESS = 0, //!< COPP_EC_SUCCESS

COPP_EC_UNKNOWN = -101, //!< COPP_EC_UNKNOWN
COPP_EC_EXTERNAL_INSERT_FAILED = -102, //!< COPP_EC_EXTERNAL_INSERT_FAILED
COPP_EC_EXTERNAL_ERASE_FAILED = -103, //!< COPP_EC_EXTERNAL_ERASE_FAILED

COPP_EC_ALLOC_STACK_FAILED = -201, //!< COPP_EC_ALLOC_STACK_FAILED

COPP_EC_NOT_INITED = -1001,//!< COPP_EC_NOT_INITED
COPP_EC_ALREADY_INITED = -1002,//!< COPP_EC_ALREADY_INITED
COPP_EC_ACCESS_VIOLATION = -1003,//!< COPP_EC_ACCESS_VIOLATION
COPP_EC_NOT_READY = -1004,//!< COPP_EC_NOT_READY
COPP_EC_NOT_RUNNING = -1005,//!< COPP_EC_NOT_RUNNING
COPP_EC_IS_RUNNING = -1006,//!< COPP_EC_IS_RUNNING
COPP_EC_ALREADY_FINISHED = -1007,//!< COPP_EC_ALREADY_FINISHED
COPP_EC_NOT_FOUND = -1008,//!< COPP_EC_NOT_FOUND
COPP_EC_ALREADY_EXIST = -1009,//!< COPP_EC_ALREADY_EXIST
COPP_EC_ARGS_ERROR = -1010,//!< COPP_EC_ARGS_ERROR
COPP_EC_CAST_FAILED = -1011,//!< COPP_EC_CAST_FAILED

COPP_EC_FCONTEXT_MAKE_FAILED = -2001,//!< COPP_EC_FCONTEXT_MAKE_FAILED
namespace copp {
/**
* error code
*/
enum copp_error_code {
COPP_EC_SUCCESS = 0, //!< COPP_EC_SUCCESS

COPP_EC_UNKNOWN = -101, //!< COPP_EC_UNKNOWN
COPP_EC_EXTERNAL_INSERT_FAILED = -102, //!< COPP_EC_EXTERNAL_INSERT_FAILED
COPP_EC_EXTERNAL_ERASE_FAILED = -103, //!< COPP_EC_EXTERNAL_ERASE_FAILED
COPP_EC_IN_RESET = -104, //!< COPP_EC_IN_RESET

COPP_EC_ALLOC_STACK_FAILED = -201, //!< COPP_EC_ALLOC_STACK_FAILED

COPP_EC_NOT_INITED = -1001, //!< COPP_EC_NOT_INITED
COPP_EC_ALREADY_INITED = -1002, //!< COPP_EC_ALREADY_INITED
COPP_EC_ACCESS_VIOLATION = -1003, //!< COPP_EC_ACCESS_VIOLATION
COPP_EC_NOT_READY = -1004, //!< COPP_EC_NOT_READY
COPP_EC_NOT_RUNNING = -1005, //!< COPP_EC_NOT_RUNNING
COPP_EC_IS_RUNNING = -1006, //!< COPP_EC_IS_RUNNING
COPP_EC_ALREADY_FINISHED = -1007, //!< COPP_EC_ALREADY_FINISHED
COPP_EC_NOT_FOUND = -1008, //!< COPP_EC_NOT_FOUND
COPP_EC_ALREADY_EXIST = -1009, //!< COPP_EC_ALREADY_EXIST
COPP_EC_ARGS_ERROR = -1010, //!< COPP_EC_ARGS_ERROR
COPP_EC_CAST_FAILED = -1011, //!< COPP_EC_CAST_FAILED

COPP_EC_FCONTEXT_MAKE_FAILED = -2001, //!< COPP_EC_FCONTEXT_MAKE_FAILED
};
}
}

#endif
Loading

0 comments on commit c75af74

Please sign in to comment.