Changes in src/libcfa/exception.c [ff7ff14a:86d5ba7c]
- File:
-
- 1 edited
-
src/libcfa/exception.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/exception.c
rff7ff14a r86d5ba7c 10 10 // Created On : Mon Jun 26 15:13:00 2017 11 11 // Last Modified By : Andrew Beach 12 // Last Modified On : Mon Jul 31 13:51:00 201713 // Update Count : 412 // Last Modified On : Fri Jul 28 12:41:00 2017 13 // Update Count : 3 14 14 // 15 15 … … 109 109 110 110 // MEMORY MANAGEMENT (still for integers) 111 // May have to move to cfa for constructors and destructors (references). 112 113 struct __cfaehm__node { 114 struct __cfaehm__node * next; 115 }; 116 117 #define NODE_TO_EXCEPT(node) ((exception *)(1 + (node))) 118 #define EXCEPT_TO_NODE(except) ((struct __cfaehm__node *)(except) - 1) 111 // May have to move to cfa for constructors and destructors. 119 112 120 113 // Creates a copy of the indicated exception and sets current_exception to it. … … 122 115 struct exception_context_t * context = this_exception_context(); 123 116 124 // Allocate memory for the exception. 125 struct __cfaehm__node * store = malloc( 126 sizeof( except ) + sizeof( struct __cfaehm__node ) ); 127 128 if ( ! store ) { 129 // Failure: cannot allocate exception. Terminate thread. 130 abort(); // <- Although I think it might be the process. 131 } 132 133 // Add the node to the list: 134 store->next = EXCEPT_TO_NODE(context->current_exception); 135 context->current_exception = NODE_TO_EXCEPT(store); 117 // Try to use the context's store, otherwise use the heap. 118 if ( 0 == context->built_in_storage ) { 119 context->current_exception = &context->built_in_storage; 120 } else { 121 exception * new_copy = malloc( sizeof( exception/*int*/ ) ); 122 if ( ! new_copy ) { 123 // Failure: cannot allocate exception. Terminate thread. 124 exit(1); // <- thread or program? 125 } 126 context->current_exception = new_copy; 127 } 136 128 137 129 // Copy the exception to storage. … … 144 136 145 137 // DEBUG 146 printf( "Deleting Exception %d\n", *except); 147 148 // Remove the exception from the list. 149 struct __cfaehm__node * to_free = EXCEPT_TO_NODE(except); 150 struct __cfaehm__node * node; 138 printf( "Deleting Exception %d (%s)\n", *except, 139 (&context->built_in_storage == except) ? "builtin" : "dynamic" ); 151 140 152 141 if ( context->current_exception == except ) { 153 node = to_free->next; 154 context->current_exception = (node) ? NODE_TO_EXCEPT(node) : 0; 142 // TODO: This should restore it to the last exception. 143 context->current_exception = NULL; 144 } 145 if ( &context->built_in_storage == except ) { 146 // You can't throw the exception '0'. 147 context->built_in_storage = 0; 155 148 } else { 156 node = EXCEPT_TO_NODE(context->current_exception); 157 // It may always be in the first or second position. 158 while( to_free != node->next ) { 159 node = node->next; 160 } 161 node->next = to_free->next; 162 } 163 164 // Free the old exception node. 165 free( to_free ); 149 // Only secondary or too large exceptions are thrown. 150 free( except ); 151 } 166 152 } 167 153
Note:
See TracChangeset
for help on using the changeset viewer.