1 /*
2  * %CopyrightBegin%
3  *
4  * Copyright Ericsson AB and Kjell Winblad 2019. All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * %CopyrightEnd%
19  */
20 
21 /*
22  * Description:
23  *
24  * Author: Kjell Winblad
25  *
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #define YCF_YIELD()
32 
33 
fun(int level)34 void fun(int level){
35   void* my_mem = malloc(1000);
36   /*special_code_start:ON_DESTROY_STATE*/
37   if(0){
38     printf("FREE AT LEVEL %d\n", level);
39     free(my_mem);
40   }
41   /*special_code_end*/
42   /*special_code_start:ON_DESTROY_STATE_OR_RETURN*/
43   if(0){
44     printf("I got destroyed or returned %d\n", level);
45   }
46   /*special_code_end*/
47   printf("LEVEL %d\n", level);
48   if (level == 10) {
49     YCF_YIELD();
50     printf("SHOULD NOT BE PRINTED 1\n");
51     return;
52   }else {
53     fun(level + 1);
54     printf("SHOULD NOT BE PRINTED 2\n");
55   }
56 }
57 
allocator(size_t size,void * context)58 void* allocator(size_t size, void* context){
59   (void)context;
60   return malloc(size);
61 }
62 
freer(void * data,void * context)63 void freer(void* data, void* context){
64   (void)context;
65   free(data);
66 }
67 
main(int argc,const char * argv[])68 int main( int argc, const char* argv[] )
69 {
70 #ifdef YCF_YIELD_CODE_GENERATED
71   void* wb = NULL;
72   long nr_of_reductions = 1;
73   do {
74     fun_ycf_gen_yielding(&nr_of_reductions,&wb,NULL,allocator,freer,NULL,0,NULL,1);
75     if(wb != NULL){
76       printf("TRAPPED\n");
77       fun_ycf_gen_destroy(wb);
78       printf("DESTROYED\n");
79       wb = NULL;
80       break;
81     }
82   } while(wb != NULL);
83   if(wb != NULL){
84     free(wb);
85   }
86 #else
87   fun(1);
88 #endif
89   printf("RETURNED\n");
90   return 0;
91 }
92 
93