diff --git a/channels.c b/channels.c index a3b38a59..e0a079b8 100644 --- a/channels.c +++ b/channels.c @@ -371,7 +371,7 @@ channel_delete(channel_t *ch) s->ths_channel = NULL; } - epg_destroy_by_channel(ch); + epg_unlink_from_channel(ch); fprintf(stderr, "!!!!!//autorec_destroy_by_channel(ch);\n"); diff --git a/epg.c b/epg.c index 6b9044ce..9d78b84b 100644 --- a/epg.c +++ b/epg.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "tvhead.h" #include "channels.h" @@ -133,6 +134,7 @@ epg_event_find_by_start(channel_t *ch, time_t start, int create) e = skel; skel = NULL; + e->e_refcount = 1; e->e_channel = ch; epg_event_changed(e); } @@ -160,13 +162,9 @@ epg_event_find_by_time(channel_t *ch, time_t t) /** * */ -void +static void epg_event_destroy(event_t *e) { - channel_t *ch = e->e_channel; - - RB_REMOVE(&ch->ch_epg_events, e, e_channel_link); - if(e->e_content_type != NULL) LIST_REMOVE(e, e_content_type_link); @@ -175,6 +173,21 @@ epg_event_destroy(event_t *e) free(e); } +/** + * + */ +static void +epg_event_unref(event_t *e) +{ + if(e->e_refcount > 1) { + e->e_refcount--; + return; + } + assert(e->e_refcount == 1); + epg_event_destroy(e); +} + + #if 0 /** * @@ -225,12 +238,15 @@ epg_channel_maintain() * */ void -epg_destroy_by_channel(channel_t *ch) +epg_unlink_from_channel(channel_t *ch) { event_t *e; - while((e = ch->ch_epg_events.root) != NULL) - epg_event_destroy(e); + while((e = ch->ch_epg_events.root) != NULL) { + RB_REMOVE(&ch->ch_epg_events, e, e_channel_link); + e->e_channel = NULL; + epg_event_unref(e); + } } diff --git a/epg.h b/epg.h index b4f02fd5..2d51d34c 100644 --- a/epg.h +++ b/epg.h @@ -47,6 +47,8 @@ typedef struct event { struct channel *e_channel; RB_ENTRY(event) e_channel_link; + int e_refcount; + LIST_ENTRY(event) e_content_type_link; epg_content_type_t *e_content_type; @@ -81,9 +83,7 @@ event_t *epg_event_find_by_start(channel_t *ch, time_t start, int create); event_t *epg_event_find_by_time(channel_t *ch, time_t t); -void epg_event_destroy(event_t *e); - -void epg_destroy_by_channel(channel_t *ch); +void epg_unlink_from_channel(channel_t *ch); /**