usermode/library/mtm/src/useraction.c

00001 /*
00002     Copyright (C) 2011 Computer Sciences Department, 
00003     University of Wisconsin -- Madison
00004 
00005     ----------------------------------------------------------------------
00006 
00007     This file is part of Mnemosyne: Lightweight Persistent Memory, 
00008     originally developed at the University of Wisconsin -- Madison.
00009 
00010     Mnemosyne was originally developed primarily by Haris Volos
00011     with contributions from Andres Jaan Tack.
00012 
00013     ----------------------------------------------------------------------
00014 
00015     Mnemosyne is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU General Public License
00017     as published by the Free Software Foundation, version 2
00018     of the License.
00019  
00020     Mnemosyne is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023     GNU General Public License for more details.
00024 
00025     You should have received a copy of the GNU General Public License
00026     along with this program; if not, write to the Free Software
00027     Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00028     Boston, MA  02110-1301, USA.
00029 
00030 ### END HEADER ###
00031 */
00032 
00033 #include "mtm_i.h"
00034 #include "useraction.h"
00035 
00036 #define USERACTION_LIST_SIZE 8
00037 
00038 typedef struct mtm_user_action_s      mtm_user_action_t;
00039 
00040 struct mtm_user_action_s
00041 {
00042         _ITM_userCommitFunction  fn;
00043         void                     *arg;
00044 };
00045 
00046 struct mtm_user_action_list_s
00047 {
00048         mtm_user_action_t *array;
00049         int               nb_entries;      /* Number of entries */
00050         int               size;            /* Size of array */
00051 };
00052 
00053 
00054 
00055 int
00056 mtm_useraction_list_alloc(mtm_user_action_list_t **listp)
00057 {
00058         mtm_user_action_list_t *list;
00059 
00060         list = (mtm_user_action_list_t *) malloc (sizeof(mtm_user_action_list_t));
00061         
00062         if (list) {
00063                 list->array = (mtm_user_action_t *) realloc(NULL, sizeof(mtm_user_action_t) * USERACTION_LIST_SIZE);
00064                 list->size = USERACTION_LIST_SIZE;
00065                 *listp = list;
00066                 return 0;
00067         }
00068 
00069         listp = NULL;
00070         return -1;
00071 }
00072 
00073 
00074 int
00075 mtm_useraction_list_free(mtm_user_action_list_t **listp)
00076 {
00077         assert(listp != NULL);
00078 
00079         mtm_user_action_list_t *list = *listp;
00080         
00081         if (list) {
00082                 free (list->array);
00083                 free (list);
00084         }
00085         *listp = NULL;
00086 
00087         return 0;
00088 }
00089 
00090 
00091 
00092 int
00093 mtm_useraction_clear(mtm_user_action_list_t *list)
00094 {
00095         assert(list != NULL);
00096 
00097         list->nb_entries = 0;
00098 
00099         return 0;
00100 }
00101 
00102 
00103 void
00104 mtm_useraction_list_run (mtm_user_action_list_t *list, int reverse)
00105 {
00106         mtm_user_action_t *action;
00107         int               i;
00108 
00109         for (i=0; i<list->nb_entries; i++) {
00110                 if (reverse) {
00111                         action = &list->array[list->nb_entries - i - 1];
00112                 } else {        
00113                         action = &list->array[i];
00114                 }
00115                 action->fn (action->arg);
00116         }
00117 }
00118 
00119 
00120 void
00121 mtm_useraction_addUserCommitAction(mtm_tx_t * tx,
00122                                    _ITM_userCommitFunction fn,
00123                                    _ITM_transactionId tid, 
00124                                    void *arg)
00125 {
00126         mtm_user_action_list_t *list = tx->commit_action_list;
00127         mtm_user_action_t      *action;
00128 
00129         if (list->nb_entries == list->size) {
00130                 list->size *= 2;
00131                 list->array = (mtm_user_action_t *) realloc(list->array, sizeof(mtm_user_action_t) * list->size);
00132         }
00133 
00134         action = &list->array[list->nb_entries++];
00135         action->fn = fn;
00136         action->arg = arg;
00137 }
00138 
00139 
00140 void
00141 mtm_useraction_addUserUndoAction(mtm_tx_t * tx,
00142                                  const _ITM_userUndoFunction fn, 
00143                                  void *arg)
00144 {
00145         mtm_user_action_list_t *list = tx->undo_action_list;
00146         mtm_user_action_t      *action;
00147 
00148         if (list->nb_entries == list->size) {
00149                 list->size *= 2;
00150                 list->array = (mtm_user_action_t *) realloc(list->array, sizeof(mtm_user_action_t) * list->size);
00151         }
00152 
00153         action = &list->array[list->nb_entries++];
00154         action->fn = fn;
00155         action->arg = arg;
00156 }

Generated on Sat Apr 23 11:43:36 2011 for Mnemosyne by  doxygen 1.4.7