Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class coroutine {
- public:
- void (*action1)(int);
- Stack local_stack;
- coroutine(void (*action)(int ), int id);
- ~coroutine();
- static void yield();
- };
- coroutine :: coroutine(void (*action)(transfer_t), int id) {
- global_coro=this;
- coro_array[id]=this;
- action1=action;
- m_id=id;
- 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
- context_array[id].fctx=f_ctx;
- }
- coroutine::~coroutine(){}
- void coroutine::yield(){
- if(global_coro->m_id==0){
- global_coro=coro_array[1];
- }
- else if (global_coro->m_id==1){
- global_coro=coro_array[0];
- }
- transfer_t tr = jump_fcontext(context_array[1].fctx, (void*)global_coro->m_id);
- }
- void coroutine::run(){
- if(global_coro->m_id==0){
- global_coro=coro_array[1];
- }
- else if (global_coro->m_id==1){
- global_coro=coro_array[0];
- }
- transfer_t tr = jump_fcontext(context_array[0].fctx, (void*) global_coro->m_id);
- }
- extern MemoryPool memPoolObj;
- class Stack {
- public:
- void *local_stack;
- MemoryPool& m_memPool=memPoolObj;
- Stack();
- ~Stack();
- };
- #include "Stack.h"
- MemoryPool memPoolObj;
- Stack::Stack() {
- auto *local_stack= m_memPool.Allocate();
- }
- Stack::~Stack() {}
- typedef void* fcontext_t;
- struct transfer_t {
- fcontext_t fctx;
- void * data;
- };
- extern "C"
- transfer_t jump_fcontext( fcontext_t const to, void * vp);
- extern "C"
- fcontext_t make_fcontext( void * sp, std::size_t size, void (* fn)( transfer_t) );
- #include "coroutine.h"
- void workPackage(transfer_t tr){
- printf("Coroutine with id %d is calledn", tr.data);
- coroutine::yield();
- printf("Coroutine with id %d is resumed after first yieldn", tr.data);
- coroutine::yield();
- printf("Coroutine with id %d is resumed after second yieldn",tr.data);
- }
- int main() {
- coroutine Coro1(workPackage, 1);
- coroutine Coro2(workPackage, 2);
- coroutine::run();
- printf("Main is finished n");
- }
- ==4775== Memcheck, a memory error detector
- ==4775== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
- ==4775== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
- ==4775== Command: ./coroutines
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x10919B: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x109097: main (main.cpp:28)
- ==4775== Address 0x5b3cc68 is 24 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid write of size 4
- ==4775== at 0x10919F: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x109097: main (main.cpp:28)
- ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
- ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
- ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
- ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
- ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
- ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
- ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
- ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
- ==4775==
- ==4775== Invalid write of size 2
- ==4775== at 0x1091A2: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x109097: main (main.cpp:28)
- ==4775== Address 0x5b3cc44 is 4 bytes after a block of size 72,704 alloc'd
- ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
- ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
- ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
- ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
- ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
- ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
- ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x1091AC: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x109097: main (main.cpp:28)
- ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x1091B7: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x109097: main (main.cpp:28)
- ==4775== Address 0x5b3cc70 is 16 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x10919B: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x1090AF: main (main.cpp:29)
- ==4775== Address 0x5b3cc68 is 24 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid write of size 4
- ==4775== at 0x10919F: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x1090AF: main (main.cpp:29)
- ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
- ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
- ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
- ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
- ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
- ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
- ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
- ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
- ==4775==
- ==4775== Invalid write of size 2
- ==4775== at 0x1091A2: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x1090AF: main (main.cpp:29)
- ==4775== Address 0x5b3cc44 is 4 bytes after a block of size 72,704 alloc'd
- ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
- ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
- ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
- ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
- ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
- ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
- ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x1091AC: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x1090AF: main (main.cpp:29)
- ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid write of size 8
- ==4775== at 0x1091B7: make_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x1090AF: main (main.cpp:29)
- ==4775== Address 0x5b3cc70 is 16 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Warning: client switching stacks? SP change: 0x1fff000510 --> 0x5b3cc40
- ==4775== to suppress, use: --max-stackframe=137326508240 or greater
- ==4775== Invalid read of size 8
- ==4775== at 0x108FC1: jump_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x37F00001F7F: ???
- ==4775== Address 0x5b3cc78 is 8 bytes before a block of size 500 alloc'd
- ==4775== at 0x4C2DC6F: operator new[](unsigned long) (vg_replace_malloc.c:423)
- ==4775== by 0x108AB0: MemoryPool::createPool(unsigned long, unsigned int) (MemoryPool.cpp:26)
- ==4775== by 0x108A6A: MemoryPool::MemoryPool() (MemoryPool.cpp:19)
- ==4775== by 0x108D15: __static_initialization_and_destruction_0(int, int) (Stack.cpp:9)
- ==4775== by 0x108D45: _GLOBAL__sub_I_memPoolObj (Stack.cpp:12)
- ==4775== by 0x10921C: __libc_csu_init (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x5791FF9: (below main) (in /usr/lib/libc-2.27.so)
- ==4775==
- ==4775== Invalid read of size 4
- ==4775== at 0x108FC6: jump_fcontext (in /home/khm31335/eclipse-workspace/coroutines/Debug/coroutines)
- ==4775== by 0x37F00001F7F: ???
- ==4775== Address 0x5b3cc40 is 0 bytes after a block of size 72,704 alloc'd
- ==4775== at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
- ==4775== by 0x4EC5256: pool (eh_alloc.cc:123)
- ==4775== by 0x4EC5256: __static_initialization_and_destruction_0 (eh_alloc.cc:262)
- ==4775== by 0x4EC5256: _GLOBAL__sub_I_eh_alloc.cc (eh_alloc.cc:338)
- ==4775== by 0x400F109: call_init.part.0 (in /usr/lib/ld-2.27.so)
- ==4775== by 0x400F205: _dl_init (in /usr/lib/ld-2.27.so)
- ==4775== by 0x4000FE9: ??? (in /usr/lib/ld-2.27.so)
- ==4775==
Add Comment
Please, Sign In to add comment