Quellcode durchsuchen

Enumeration typedefs have _e suffix

Serj Kalichev vor 3 Jahren
Ursprung
Commit
3f2f757bdb
6 geänderte Dateien mit 43 neuen und 31 gelöschten Zeilen
  1. 3 3
      faux/list.h
  2. 1 1
      faux/list/list.c
  3. 17 2
      faux/sched.h
  4. 18 8
      faux/sched/ev.c
  5. 3 16
      faux/sched/private.h
  6. 1 1
      faux/sched/sched.c

+ 3 - 3
faux/list.h

@@ -12,12 +12,12 @@
 typedef enum {
 	FAUX_LIST_SORTED = BOOL_TRUE,
 	FAUX_LIST_UNSORTED = BOOL_FALSE
-	} faux_list_sorted_t;
+	} faux_list_sorted_e;
 
 typedef enum {
 	FAUX_LIST_UNIQUE = BOOL_TRUE,
 	FAUX_LIST_NONUNIQUE = BOOL_FALSE
-	} faux_list_unique_t;
+	} faux_list_unique_e;
 
 typedef struct faux_list_node_s faux_list_node_t;
 typedef struct faux_list_s faux_list_t;
@@ -38,7 +38,7 @@ void *faux_list_each(faux_list_node_t **iter);
 void *faux_list_eachr(faux_list_node_t **iter);
 
 // list_t methods
-faux_list_t *faux_list_new(faux_list_sorted_t sorted, faux_list_unique_t unique,
+faux_list_t *faux_list_new(faux_list_sorted_e sorted, faux_list_unique_e unique,
 	faux_list_cmp_fn cmpFn, faux_list_kcmp_fn kcmpFn,
 	faux_list_free_fn freeFn);
 void faux_list_free(faux_list_t *list);

+ 1 - 1
faux/list/list.c

@@ -196,7 +196,7 @@ void *faux_list_eachr(faux_list_node_t **iter)
  * @param [in] freeFn Callback function to free user data.
  * @return Newly created bidirectional list or NULL on error.
  */
-faux_list_t *faux_list_new(faux_list_sorted_t sorted, faux_list_unique_t unique,
+faux_list_t *faux_list_new(faux_list_sorted_e sorted, faux_list_unique_e unique,
 	faux_list_cmp_fn cmpFn, faux_list_kcmp_fn kcmpFn,
 	faux_list_free_fn freeFn)
 {

+ 17 - 2
faux/sched.h

@@ -15,7 +15,7 @@
 typedef enum {
 	FAUX_SCHED_PERIODIC = BOOL_TRUE,
 	FAUX_SCHED_ONCE = BOOL_FALSE
-	} faux_sched_periodic_t;
+	} faux_sched_periodic_e;
 
 typedef struct faux_ev_s faux_ev_t;
 typedef struct faux_sched_s faux_sched_t;
@@ -24,9 +24,24 @@ typedef faux_list_node_t faux_sched_node_t;
 
 C_DECL_BEGIN
 
+// Time event
+faux_ev_t *faux_ev_new(const struct timespec *time,
+	int ev_id, void *data, faux_list_free_fn free_data_cb);
+void faux_ev_free(void *ptr);
+bool_t faux_ev_periodic(faux_ev_t *ev,
+	const struct timespec *interval, unsigned int cycle_num);
+int faux_ev_dec_cycles(faux_ev_t *ev, unsigned int *new_cycle_num);
+int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time);
+int faux_ev_reschedule_period(faux_ev_t *ev);
+int faux_ev_time_left(faux_ev_t *ev, struct timespec *left);
+int faux_ev_id(const faux_ev_t *ev);
+void *faux_ev_data(const faux_ev_t *ev);
+const struct timespec *faux_ev_time(const faux_ev_t *ev);
+faux_sched_periodic_e faux_ev_is_periodic(faux_ev_t *ev);
+
+// Time event scheduler
 faux_sched_t *faux_sched_new(void);
 void faux_sched_free(faux_sched_t *sched);
-
 int faux_sched_once(
 	faux_sched_t *sched, const struct timespec *time, int ev_id, void *data);
 int faux_sched_once_delayed(faux_sched_t *sched,

+ 18 - 8
faux/sched/ev.c

@@ -79,7 +79,8 @@ int faux_ev_compare_data(const void *key, const void *list_item)
  * @param [in] data Pointer to arbitrary linked data.
  * @return Allocated and initialized ev object.
  */
-faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data)
+faux_ev_t *faux_ev_new(const struct timespec *time,
+	int ev_id, void *data, faux_list_free_fn free_data_cb)
 {
 	faux_ev_t *ev = NULL;
 
@@ -91,6 +92,7 @@ faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data)
 	// Initialize
 	ev->id = ev_id;
 	ev->data = data;
+	ev->free_data_cb = free_data_cb;
 	ev->periodic = FAUX_SCHED_ONCE; // Not periodic by default
 	ev->cycle_num = 0;
 	faux_nsec_to_timespec(&(ev->period), 0l);
@@ -110,6 +112,8 @@ void faux_ev_free(void *ptr)
 
 	if (!ev)
 		return;
+	if (ev->free_data_cb)
+		ev->free_data_cb(ev->data);
 	faux_free(ev);
 }
 
@@ -119,24 +123,30 @@ void faux_ev_free(void *ptr)
  * By default new events are not periodic.
  *
  * @param [in] ev Allocated and initialized ev object.
- * @param [in] period Period of periodic event.
+ * @param [in] period Period of periodic event. If NULL then non-periodic event.
  * @param [in] cycle_num Number of cycles. FAUX_SHED_INFINITE - infinite.
- * @return 0 - success, < 0 on error.
+ * @return BOOL_TRUE - success, BOOL_FALSE on error.
  */
-int faux_ev_periodic(faux_ev_t *ev,
+bool_t faux_ev_periodic(faux_ev_t *ev,
 	const struct timespec *period, unsigned int cycle_num)
 {
 	assert(ev);
 	assert(period);
+	if (!ev)
+		return BOOL_FALSE;
+	if (!period) {
+		ev->periodic = FAUX_SCHED_ONCE;
+		return BOOL_TRUE;
+	}
 	// When cycle_num == 0 then periodic has no meaning
-	if (!ev || !period || cycle_num == 0)
-		return -1;
+	if (0 == cycle_num)
+		return BOOL_FALSE;
 
 	ev->periodic = FAUX_SCHED_PERIODIC;
 	ev->cycle_num = cycle_num;
 	ev->period = *period;
 
-	return 0;
+	return BOOL_TRUE;
 }
 
 
@@ -145,7 +155,7 @@ int faux_ev_periodic(faux_ev_t *ev,
  * @param [in] ev Allocated and initialized ev object.
  * @return FAUX_SCHED_PERIODIC - periodic, FAUX_SCHED_ONCE - non-periodic.
  */
-faux_sched_periodic_t faux_ev_is_periodic(faux_ev_t *ev)
+faux_sched_periodic_e faux_ev_is_periodic(faux_ev_t *ev)
 {
 	assert(ev);
 	if (!ev)

+ 3 - 16
faux/sched/private.h

@@ -8,11 +8,13 @@ struct faux_ev_s {
 	struct timespec time; // Planned time of event
 	struct timespec period; // Period for periodic event
 	unsigned int cycle_num; // Number of cycles for periodic event
-	faux_sched_periodic_t periodic; // Periodic flag
+	faux_sched_periodic_e periodic; // Periodic flag
 	int id; // Type of event
 	void *data; // Arbitrary data linked to event
+	faux_list_free_fn free_data_cb; // Callback to free user data
 };
 
+
 struct faux_sched_s {
 	faux_list_t *list;
 };
@@ -24,19 +26,4 @@ int faux_ev_compare(const void *first, const void *second);
 int faux_ev_compare_id(const void *key, const void *list_item);
 int faux_ev_compare_data(const void *key, const void *list_item);
 
-faux_ev_t *faux_ev_new(const struct timespec *time, int ev_id, void *data);
-void faux_ev_free(void *ptr);
-
-int faux_ev_periodic(faux_ev_t *ev,
-	const struct timespec *interval, unsigned int cycle_num);
-int faux_ev_dec_cycles(faux_ev_t *ev, unsigned int *new_cycle_num);
-int faux_ev_reschedule(faux_ev_t *ev, const struct timespec *new_time);
-int faux_ev_reschedule_period(faux_ev_t *ev);
-int faux_ev_time_left(faux_ev_t *ev, struct timespec *left);
-
-int faux_ev_id(const faux_ev_t *ev);
-void *faux_ev_data(const faux_ev_t *ev);
-const struct timespec *faux_ev_time(const faux_ev_t *ev);
-faux_sched_periodic_t faux_ev_is_periodic(faux_ev_t *ev);
-
 C_DECL_END

+ 1 - 1
faux/sched/sched.c

@@ -99,7 +99,7 @@ static int _sched_ev(faux_sched_t *sched, faux_ev_t *ev)
  * @return 0 - success, < 0 on error.
  */
 static int _sched(faux_sched_t *sched, const struct timespec *time,
-	int ev_id, void *data, faux_sched_periodic_t periodic,
+	int ev_id, void *data, faux_sched_periodic_e periodic,
 	const struct timespec *period, unsigned int cycle_num)
 {
 	faux_ev_t *ev = NULL;