Guest User

Untitled

a guest
Sep 18th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.73 KB | None | 0 0
  1. class coroutine {
  2. public:
  3. void (*action1)(int);
  4. Stack local_stack;
  5. coroutine(void (*action)(int ), int id);
  6.  
  7. ~coroutine();
  8.  
  9. static void yield();
  10.  
  11. };
  12.  
  13. coroutine :: coroutine(void (*action)(transfer_t), int id) {
  14. global_coro=this;
  15. coro_array[id]=this;
  16. action1=action;
  17. m_id=id;
  18. fcontext_t f_ctx = make_fcontext(global_coro->local_stack.local_stack, 10, global_coro->action1); //inside the memorypool a stack of 50 blocks with each having size 10 is being created
  19. context_array[id].fctx=f_ctx;
  20. }
  21.  
  22. coroutine::~coroutine(){}
  23.  
  24. void coroutine::yield(){
  25. if(global_coro->m_id==0){
  26. global_coro=coro_array[1];
  27. }
  28. else if (global_coro->m_id==1){
  29. global_coro=coro_array[0];
  30. }
  31. transfer_t tr = jump_fcontext(context_array[1].fctx, (void*)global_coro->m_id);
  32. }
  33. void coroutine::run(){
  34. if(global_coro->m_id==0){
  35. global_coro=coro_array[1];
  36. }
  37. else if (global_coro->m_id==1){
  38. global_coro=coro_array[0];
  39. }
  40.  
  41. transfer_t tr = jump_fcontext(context_array[0].fctx, (void*) global_coro->m_id);
  42. }
  43.  
  44. extern MemoryPool memPoolObj;
  45.  
  46. class Stack {
  47. public:
  48. void *local_stack;
  49. MemoryPool& m_memPool=memPoolObj;
  50. Stack();
  51. ~Stack();
  52. };
  53.  
  54. #include "Stack.h"
  55. MemoryPool memPoolObj;
  56. Stack::Stack() {
  57. auto *local_stack= m_memPool.Allocate();
  58. }
  59. Stack::~Stack() {}
  60.  
  61. typedef void* fcontext_t;
  62.  
  63. struct transfer_t {
  64. fcontext_t fctx;
  65. void * data;
  66. };
  67.  
  68. extern "C"
  69. transfer_t jump_fcontext( fcontext_t const to, void * vp);
  70. extern "C"
  71. fcontext_t make_fcontext( void * sp, std::size_t size, void (* fn)( transfer_t) );
  72.  
  73. #include "coroutine.h"
  74. void workPackage(transfer_t tr){
  75. printf("Coroutine with id %d is calledn", tr.data);
  76. coroutine::yield();
  77. printf("Coroutine with id %d is resumed after first yieldn", tr.data);
  78. coroutine::yield();
  79. printf("Coroutine with id %d is resumed after second yieldn",tr.data);
  80. }
  81.  
  82.  
  83. int main() {
  84. coroutine Coro1(workPackage, 1);
  85. coroutine Coro2(workPackage, 2);
  86. coroutine::run();
  87. printf("Main is finished n");
  88. }
  89.  
  90. ==4775== Memcheck, a memory error detector
  91. ==4775== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
  92. ==4775== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
  93. ==4775== Command: ./coroutines
  94. ==4775==
  95. ==4775== Invalid write of size 8
  96. ==4775== at 0x10919B: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  97. ==4775== by 0x109097: main (main.cpp:28)
  98. ==4775== Address 0x5b3cc68 is 24 bytes before a block of size 500 alloc'd
  99. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  100. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  101. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  102. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  103. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  104. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  105. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  106. ==4775==
  107. ==4775== Invalid write of size 4
  108. ==4775== at 0x10919F: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  109. ==4775== by 0x109097: main (main.cpp:28)
  110. ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
  111. ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
  112. ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
  113. ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
  114. ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
  115. ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
  116. ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
  117. ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
  118. ==4775==
  119. ==4775== Invalid write of size 2
  120. ==4775== at 0x1091A2: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  121. ==4775== by 0x109097: main (main.cpp:28)
  122. ==4775== Address 0x5b3cc44 is 4 bytes after a block of size 72,704 alloc'd
  123. ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
  124. ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
  125. ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
  126. ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
  127. ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
  128. ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
  129. ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
  130. ==4775==
  131. ==4775== Invalid write of size 8
  132. ==4775== at 0x1091AC: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  133. ==4775== by 0x109097: main (main.cpp:28)
  134. ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
  135. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  136. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  137. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  138. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  139. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  140. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  141. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  142. ==4775==
  143. ==4775== Invalid write of size 8
  144. ==4775== at 0x1091B7: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  145. ==4775== by 0x109097: main (main.cpp:28)
  146. ==4775== Address 0x5b3cc70 is 16 bytes before a block of size 500 alloc'd
  147. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  148. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  149. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  150. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  151. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  152. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  153. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  154. ==4775==
  155. ==4775== Invalid write of size 8
  156. ==4775== at 0x10919B: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  157. ==4775== by 0x1090AF: main (main.cpp:29)
  158. ==4775== Address 0x5b3cc68 is 24 bytes before a block of size 500 alloc'd
  159. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  160. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  161. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  162. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  163. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  164. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  165. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  166. ==4775==
  167. ==4775== Invalid write of size 4
  168. ==4775== at 0x10919F: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  169. ==4775== by 0x1090AF: main (main.cpp:29)
  170. ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
  171. ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
  172. ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
  173. ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
  174. ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
  175. ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
  176. ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
  177. ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
  178. ==4775==
  179. ==4775== Invalid write of size 2
  180. ==4775== at 0x1091A2: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  181. ==4775== by 0x1090AF: main (main.cpp:29)
  182. ==4775== Address 0x5b3cc44 is 4 bytes after a block of size 72,704 alloc'd
  183. ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
  184. ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
  185. ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
  186. ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
  187. ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
  188. ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
  189. ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
  190. ==4775==
  191. ==4775== Invalid write of size 8
  192. ==4775== at 0x1091AC: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  193. ==4775== by 0x1090AF: main (main.cpp:29)
  194. ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
  195. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  196. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  197. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  198. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  199. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  200. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  201. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  202. ==4775==
  203. ==4775== Invalid write of size 8
  204. ==4775== at 0x1091B7: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  205. ==4775== by 0x1090AF: main (main.cpp:29)
  206. ==4775== Address 0x5b3cc70 is 16 bytes before a block of size 500 alloc'd
  207. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  208. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  209. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  210. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  211. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  212. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  213. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  214. ==4775==
  215. ==4775== Warning: client switching stacks? SP change: 0x1fff000510 --> 0x5b3cc40
  216. ==4775== to suppress, use: --max-stackframe=137326508240 or greater
  217. ==4775== Invalid read of size 8
  218. ==4775== at 0x108FC1: jump_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  219. ==4775== by 0x37F00001F7F: ???
  220. ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
  221. ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
  222. ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
  223. ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
  224. ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
  225. ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
  226. ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  227. ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
  228. ==4775==
  229. ==4775== Invalid read of size 4
  230. ==4775== at 0x108FC6: jump_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
  231. ==4775== by 0x37F00001F7F: ???
  232. ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
  233. ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
  234. ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
  235. ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
  236. ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
  237. ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
  238. ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
  239. ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
  240. ==4775==
Add Comment
Please, Sign In to add comment