libcopp  2.2.0
coroutine_context_split_segment.cpp
Go to the documentation of this file.
1 // Copyright 2023 owent
2 
5 
6 #include <cstdio>
7 #include <cstring>
8 #include <iostream>
9 
10 #include "frame/test_macros.h"
11 #ifdef LIBCOPP_MACRO_USE_SEGMENTED_STACKS
12 
13 typedef copp::coroutine_context_container<copp::allocator::stack_allocator_split_segment> test_split_segment_context;
14 
15 class test_split_segment_foo_runner {
16  private:
17  void stack_test(int loop) {
18  char a = 100;
19  char b[1024 * 1024] = {0};
20  char c = 200;
21  CASE_EXPECT_TRUE((&a > &c) == (&a > b));
22  end_addr_ = (intptr_t)(&c);
23  if (loop > 0) stack_test(loop - 1);
24  }
25 
26  public:
27  int operator()(void *) {
28  int a = 0;
29  begin_addr_ = (intptr_t)(&a);
30  end_addr_ = begin_addr_;
31 
32  stack_test(4);
33 
34  copp::this_coroutine::get<test_split_segment_context>()->yield();
35 
36  stack_test(20);
37  return 0;
38  }
39 
40  intptr_t begin_addr_;
41  intptr_t end_addr_;
42 };
43 
44 CASE_TEST(coroutine, context_split_segment_stack) {
45  test_split_segment_foo_runner runner;
46  {
47  test_split_segment_context::ptr_t co = test_split_segment_context::create(&runner, 2 * 1024 * 1024);
48 
49  co->start();
50  co->resume();
51 
52  intptr_t dis = (runner.end_addr_ > runner.begin_addr_) ? (runner.end_addr_ - runner.begin_addr_)
53  : (runner.begin_addr_ - runner.end_addr_);
54 
55  CASE_EXPECT_GE(dis, 4 * 1024 * 1024);
56  }
57 }
58 
59 #endif
#define CASE_TEST(test_name, case_name)
Definition: test_macros.h:46
#define CASE_EXPECT_TRUE(c)
Definition: test_macros.h:94
#define CASE_EXPECT_GE(l, r)
Definition: test_macros.h:101