Changeset cbce272 for src/libcfa


Ignore:
Timestamp:
Aug 9, 2017, 2:08:14 PM (7 years ago)
Author:
Andrew Beach <ajbeach@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
65cdc1e
Parents:
e195093
Message:

Structure based exception handling.

Location:
src/libcfa
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/libcfa/exception.c

    re195093 rcbce272  
    1010// Created On       : Mon Jun 26 15:13:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Mon Jul 31 13:51:00 2017
    13 // Update Count     : 4
     12// Last Modified On : Fri Aug  4 15:20:00 2017
     13// Update Count     : 6
    1414//
     15
     16#include <stddef.h> // for size_t
    1517
    1618#include "exception.h"
     
    3234#include "lsda.h"
    3335
     36
     37// Base exception vtable is abstract, you should not have base exceptions.
     38struct __cfaehm__base_exception_t_vtable
     39                ___cfaehm__base_exception_t_vtable_instance = {
     40        .parent = NULL,
     41        .size = 0,
     42        .copy = NULL,
     43        .free = NULL,
     44        .msg = NULL
     45};
     46
     47
    3448// Temperary global exception context. Does not work with concurency.
    3549struct exception_context_t {
     
    3953    exception * current_exception;
    4054    int current_handler_index;
    41 
    42         // Storage to avoid using the heap for exceptions.
    43         exception built_in_storage;
    44 } shared_stack = {NULL, NULL, 0, 0, 0};
     55} shared_stack = {NULL, NULL, 0, 0};
    4556
    4657// Get the current exception context.
     
    6980
    7081        // DEBUG
    71         printf("Throwing resumption exception %d\n", *except);
     82        printf("Throwing resumption exception\n");
    7283
    7384        struct __cfaehm__try_resume_node * original_head = shared_stack.current_resume;
     
    8394        }
    8495
    85         printf("Unhandled exception %d\n", *except);
     96        printf("Unhandled exception\n");
    8697        shared_stack.current_resume = original_head;
    8798
     
    124135        // Allocate memory for the exception.
    125136        struct __cfaehm__node * store = malloc(
    126                 sizeof( except ) + sizeof( struct __cfaehm__node ) );
     137                sizeof( struct __cfaehm__node ) + except->virtual_table->size );
    127138
    128139        if ( ! store ) {
     
    136147
    137148        // Copy the exception to storage.
    138         *context->current_exception = *except;
     149        except->virtual_table->copy( context->current_exception, except );
    139150}
    140151
     
    144155
    145156        // DEBUG
    146         printf( "Deleting Exception %d\n", *except);
     157        printf( "Deleting Exception\n");
    147158
    148159        // Remove the exception from the list.
     
    163174
    164175        // Free the old exception node.
     176        except->virtual_table->free( except );
    165177        free( to_free );
    166178}
    167179
    168180// If this isn't a rethrow (*except==0), delete the provided exception.
    169 void __cfaehm__cleanup_terminate( exception ** except ) {
    170         if ( *except ) __cfaehm__delete_exception( *except );
     181void __cfaehm__cleanup_terminate( void * except ) {
     182        if ( *(void**)except ) __cfaehm__delete_exception( *(exception**)except );
    171183}
    172184
  • src/libcfa/exception.h

    re195093 rcbce272  
    1010// Created On       : Mon Jun 26 15:11:00 2017
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Fri Jul 27 12:42:00 2017
    13 // Update Count     : 4
     12// Last Modified On : Fri Aug  4 15:20:00 2017
     13// Update Count     : 5
    1414//
    1515
     
    2121#endif
    2222
    23 #if 1
    24 typedef int exception;
    25 #else
    26 struct exception_t;
    27 struct exception_t_vtable {
    28         struct exception_t_vtable const * parent;
     23struct __cfaehm__base_exception_t;
     24typedef struct __cfaehm__base_exception_t exception;
     25struct __cfaehm__base_exception_t_vtable {
     26        const struct __cfaehm__base_exception_t_vtable * parent;
    2927        size_t size;
    30         void (*copy)(struct exception_t *this, struct exception_t * other);
    31         void (*free)(struct exception_t *this);
    32         const char (*msg)(struct exception_t *this);
     28        void (*copy)(struct __cfaehm__base_exception_t *this,
     29                     struct __cfaehm__base_exception_t * other);
     30        void (*free)(struct __cfaehm__base_exception_t *this);
     31        const char (*msg)(struct __cfaehm__base_exception_t *this);
    3332};
    34 struct exception_t {
    35         struct exception_vtable const * virtual_table;
     33struct __cfaehm__base_exception_t {
     34        struct __cfaehm__base_exception_t_vtable const * virtual_table;
    3635};
    37 typedef struct exception_t exception;
    38 #endif
     36extern struct __cfaehm__base_exception_t_vtable
     37        ___cfaehm__base_exception_t_vtable_instance;
    3938
    4039
     
    5150
    5251// Clean-up the exception in catch blocks.
    53 void __cfaehm__cleanup_terminate(exception ** except);
     52void __cfaehm__cleanup_terminate(void * except);
    5453
    5554// Data structure creates a list of resume handlers.
Note: See TracChangeset for help on using the changeset viewer.