| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #ifndef exception_h__
 | 
					
						
							|  |  |  |  | #define exception_h__
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #include "setjmp.h"
 | 
					
						
							| 
									
										
										
										
											2025-06-24 17:59:23 +08:00
										 |  |  |  | #include "stdlib.h"
 | 
					
						
							|  |  |  |  | // #include "mystdlib.h"
 | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | #define THROW_LOG__MAX_SIZE 1024
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | typedef struct _jmp_fram{ | 
					
						
							|  |  |  |  |   struct _jmp_fram *next; | 
					
						
							|  |  |  |  |   struct _jmp_fram *last; | 
					
						
							| 
									
										
										
										
											2025-06-24 17:59:23 +08:00
										 |  |  |  |   // map_def *mem;
 | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  |   jmp_buf fram; | 
					
						
							|  |  |  |  | }jmp_fram; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | typedef struct{ | 
					
						
							|  |  |  |  |   size_t jmp_num; | 
					
						
							|  |  |  |  |   jmp_fram *jmp_head; | 
					
						
							|  |  |  |  |   int ret; | 
					
						
							|  |  |  |  |   char *file; | 
					
						
							|  |  |  |  |   int line; | 
					
						
							|  |  |  |  |   char log[THROW_LOG__MAX_SIZE]; | 
					
						
							|  |  |  |  | }exception_def; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define jmp_next(j) {\
 | 
					
						
							|  |  |  |  |   if((*j)){\ | 
					
						
							|  |  |  |  |     (*j)->next=calloc(sizeof(jmp_fram),1);\ | 
					
						
							|  |  |  |  |     (*j)->next->last=(*j);\ | 
					
						
							|  |  |  |  |     (*j)=(*j)->next;\ | 
					
						
							|  |  |  |  |   }else{\ | 
					
						
							|  |  |  |  |     (*j)=calloc(sizeof(jmp_fram),1);\ | 
					
						
							|  |  |  |  |   }\ | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define jmp_last(j) {\
 | 
					
						
							|  |  |  |  |   if((*j)){\ | 
					
						
							|  |  |  |  |     jmp_fram *l=(*j)->last;\ | 
					
						
							| 
									
										
										
										
											2025-06-24 17:59:23 +08:00
										 |  |  |  |     /*if(l){\
 | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  |       __mem_mov(&(l)->mem,&(*j)->mem);\ | 
					
						
							| 
									
										
										
										
											2025-06-24 17:59:23 +08:00
										 |  |  |  |     }*/\ | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  |     free(*j);\ | 
					
						
							|  |  |  |  |     (*j)=l;\ | 
					
						
							|  |  |  |  |     if(*j){\ | 
					
						
							|  |  |  |  |       (*j)->next=0;\ | 
					
						
							|  |  |  |  |     }\ | 
					
						
							|  |  |  |  |   }\ | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define jmp_clear(j) {\
 | 
					
						
							|  |  |  |  |   if((*j)){\ | 
					
						
							|  |  |  |  |     jmp_fram *l=(*j)->last;\ | 
					
						
							| 
									
										
										
										
											2025-06-24 17:59:23 +08:00
										 |  |  |  |     /*__mem_clear(&(*j)->mem);*/\ | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  |     free(*j);\ | 
					
						
							|  |  |  |  |     (*j)=l;\ | 
					
						
							|  |  |  |  |     if(*j){\ | 
					
						
							|  |  |  |  |       (*j)->next=0;\ | 
					
						
							|  |  |  |  |     }\ | 
					
						
							|  |  |  |  |   }\ | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-25 19:10:40 +08:00
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  try 的时候保存当前回溯点,如果setjmp返回不为0 则已经产生异常回溯了, | 
					
						
							|  |  |  |  |  这时最后一个回溯点的使命已经完成,所以删除最后一个回溯点 | 
					
						
							|  |  |  |  | */ | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  | #define __try(){\
 | 
					
						
							|  |  |  |  |   exception_def *f=exception();\ | 
					
						
							|  |  |  |  |   jmp_fram **h=&f->jmp_head;\ | 
					
						
							|  |  |  |  |   int ret;\ | 
					
						
							|  |  |  |  |   jmp_next(h);\ | 
					
						
							| 
									
										
										
										
											2025-06-25 19:10:40 +08:00
										 |  |  |  |   ret = setjmp(((*h)->fram));\ | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  |   if(ret){\ | 
					
						
							|  |  |  |  |     jmp_clear(h);\ | 
					
						
							|  |  |  |  |   }\ | 
					
						
							|  |  |  |  |   f->ret=ret;\ | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define try_ __try();if(exception()->ret==0){
 | 
					
						
							| 
									
										
										
										
											2025-06-25 19:10:40 +08:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | /*
 | 
					
						
							|  |  |  |  |   如果try一直执行到这里则不会产生回溯了,最后一个回溯点的使命也已经完成了, | 
					
						
							|  |  |  |  |   所以这里删除最后一个回溯点 | 
					
						
							|  |  |  |  | */ | 
					
						
							| 
									
										
										
										
											2024-06-18 19:37:43 +08:00
										 |  |  |  | #define catch_ {\
 | 
					
						
							|  |  |  |  |   exception_def *f=exception();\ | 
					
						
							|  |  |  |  |   jmp_fram **h=&f->jmp_head;\ | 
					
						
							|  |  |  |  |   jmp_last(h);\ | 
					
						
							|  |  |  |  | }}else | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #define throw_(fmt,...) __throw(__FILE__,__LINE__,fmt,##__VA_ARGS__)
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | exception_def *exception(); | 
					
						
							|  |  |  |  | void __throw(char *file,int line,const char *fmt,...); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | int test_add(int a,int b); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | #endif
 | 
					
						
							|  |  |  |  | 
 |