8 /* Callback functions prototypes */
9 static unsigned int state_hashf(const void *key
);
10 static int state_cmpf(const void *arg1
, const void *arg2
);
11 static void state_printf(const void *key
, const void *data
);
13 stret_t
state_init(state_t
**state
, size_t size
, unsigned int factor
)
15 /* Allocate memory state's event table */
16 if ((*state
= malloc(sizeof *state
)) == NULL
)
19 if (((*state
)->evttable
= malloc(sizeof *(*state
)->evttable
)) == NULL
) {
24 if (htable_init((*state
)->evttable
, size
, factor
,
25 state_hashf
, state_cmpf
, state_printf
) == HT_NOMEM
) {
26 free((*state
)->evttable
);
34 stret_t
state_add_evt(state_t
*state
, unsigned int key
, const char *desc
, void (*actionf
)(void *data
), state_t
*newstate
)
39 /* Allocate memory for new key */
40 if ((pkey
= malloc(sizeof *pkey
)) == NULL
)
45 /* Allocate memory for new event */
46 if ((pevt
= malloc(sizeof *pevt
)) == NULL
) {
51 /* Fill in structure's members */
52 strncpy(pevt
->evt_desc
, desc
, MAX_EVT_DESC
);
53 pevt
->evt_actionf
= actionf
;
54 pevt
->evt_newstate
= newstate
;
56 /* Insert event to hash table */
57 if (htable_insert(state
->evttable
, pkey
, pevt
) == HT_EXISTS
) {
66 stret_t
state_rem_evt(state_t
*state
, unsigned int key
)
68 if (htable_free_obj(state
->evttable
, &key
) == HT_NOTFOUND
)
74 stret_t
state_free(state_t
*state
)
76 htable_free_all_obj(state
->evttable
, 2);
77 htable_free(state
->evttable
);
78 free(state
->evttable
);
84 void state_print_evts(const state_t
*state
)
86 htable_print(state
->evttable
);
89 /* Callback funtions */
90 static unsigned int state_hashf(const void *key
)
92 return *(const unsigned int *)key
;
95 static int state_cmpf(const void *arg1
, const void *arg2
)
97 unsigned int a
= *(const unsigned int *)arg1
;
98 unsigned int b
= *(const unsigned int *)arg2
;
108 static void state_printf(const void *key
, const void *data
)
110 printf("key: %d\tdesc: %s ",
111 *(const unsigned int *)key
,
112 ((const event_t
*)data
)->evt_desc
);