-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
c++ 就是这样的,要极大的满足用户的自定义需求。而用户非常有可能,是需要 定制 promise 对象的内存分配的。因此,学习 STL 容器的做法,让用户可以设 定 Allocator 模板参数,从而定制化内存需求。
- Loading branch information
Showing
4 changed files
with
123 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
add_executable(test_custom_allocator test.cpp) | ||
target_link_libraries(test_custom_allocator ucoro) | ||
|
||
add_test(NAME test_custom_allocator COMMAND test_custom_allocator) | ||
set_target_properties(test_custom_allocator PROPERTIES FOLDER "ucoro_tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
#include "ucoro/awaitable.hpp" | ||
#include <iostream> | ||
|
||
struct my_allocator | ||
{ | ||
using value_type = void; | ||
|
||
value_type* allocate(std::size_t size) | ||
{ | ||
return std::malloc(size); | ||
} | ||
|
||
void deallocate(value_type* ptr, std::size_t size) | ||
{ | ||
free(ptr); | ||
} | ||
|
||
}; | ||
|
||
// 只有 test2 用自定义分配器分配 | ||
// 验证混合使用 分配器的awaitable也是没问题的 | ||
ucoro::awaitable<int, my_allocator> test() | ||
{ | ||
throw std::runtime_error("test throw"); | ||
co_return 1; | ||
} | ||
|
||
|
||
ucoro::awaitable<void> test2() | ||
{ | ||
throw std::runtime_error("test throw"); | ||
co_return; | ||
} | ||
|
||
ucoro::awaitable<int> coro_compute() | ||
{ | ||
try | ||
{ | ||
sync_await(test2()); | ||
} | ||
catch(const std::exception& e) | ||
{ | ||
std::cerr << "exception in test2: " << e.what() << '\n'; | ||
} | ||
|
||
co_return co_await test(); | ||
} | ||
|
||
int main(int argc, char** argv) | ||
{ | ||
try | ||
{ | ||
std::string str = "hello"; | ||
sync_await(coro_compute(), str); | ||
} | ||
catch (std::exception& e) | ||
{ | ||
std::cerr << "exception: " << e.what() << std::endl; | ||
} | ||
|
||
return 0; | ||
} |