8 /* Callback functions prototypes */
9 static unsigned int state_hashf(const void *pkey
);
10 static int state_cmpf(const void *parg1
, const void *parg2
);
11 static void state_printf(const void *pkey
, const void *pdata
);
13 stret_t
state_init(state_t
**ppstate
, size_t size
, unsigned int factor
)
15 /* Allocate memory state's event table */
16 if ((*ppstate
= malloc(sizeof **ppstate
)) == NULL
)
19 if (((*ppstate
)->evttable
= malloc(sizeof *(*ppstate
)->evttable
)) == NULL
) {
24 /* Initialize hash table that stores the events the state can process */
25 if (htable_init((*ppstate
)->evttable
, size
, factor
,
26 state_hashf
, state_cmpf
, state_printf
) == HT_NOMEM
) {
27 free((*ppstate
)->evttable
);
32 /* Allocate memory for state's key */
33 if (((*ppstate
)->st_key
= malloc(sizeof *(*ppstate
)->st_key
)) == NULL
) {
34 free((*ppstate
)->evttable
);
39 /* Initialize flags */
40 STATE_MARK_AS_UNREACHABLE(*ppstate
);
45 stret_t
state_add_evt(state_t
*pstate
, unsigned int key
, const char *pdesc
,
46 void (*pactionf
)(void *pdata
), state_t
*pnewstate
)
51 /* Allocate memory for new key */
52 if ((pkey
= malloc(sizeof *pkey
)) == NULL
)
57 /* Allocate memory for new event */
58 if ((pevt
= malloc(sizeof *pevt
)) == NULL
) {
63 /* Fill in structure's members */
64 strncpy(pevt
->evt_desc
, pdesc
, MAX_EVT_DESC
);
65 pevt
->evt_actionf
= pactionf
;
66 pevt
->evt_newstate
= pnewstate
;
68 /* Insert event to hash table */
69 if (htable_insert(pstate
->evttable
, pkey
, pevt
) == HT_EXISTS
) {
78 stret_t
state_rem_evt(state_t
*pstate
, unsigned int key
)
80 if (htable_free_obj(pstate
->evttable
, &key
, HT_FREEKEY
| HT_FREEDATA
) == HT_NOTFOUND
)
86 unsigned int state_get_key(state_t
*pstate
)
88 return *pstate
->st_key
;
91 stret_t
state_free(state_t
*pstate
)
93 htable_free_all_obj(pstate
->evttable
, HT_FREEKEY
| HT_FREEDATA
);
94 htable_free(pstate
->evttable
);
95 free(pstate
->evttable
);
101 void state_print_evts(const state_t
*pstate
, FILE *fp
)
103 htable_print(pstate
->evttable
, fp
);
106 /* Callback funtions */
107 static unsigned int state_hashf(const void *pkey
)
109 return *(const unsigned int *) pkey
;
112 static int state_cmpf(const void *parg1
, const void *parg2
)
114 unsigned int a
= *(const unsigned int *) parg1
;
115 unsigned int b
= *(const unsigned int *) parg2
;
125 static void state_printf(const void *pkey
, const void *pdata
)
129 pevt
= (const event_t
*) pdata
;
130 printf("evtkey: %u\tdesc: %s\tnewstate: %u",
131 *(const unsigned int *) pkey
,
133 *(const unsigned int *) pevt
->evt_newstate
->st_key
);