[svn-commits] twilson: branch twilson/calendaring r167056 - in /team/twilson/calendaring: i...

SVN commits to the Digium repositories svn-commits at lists.digium.com
Wed Dec 31 16:47:35 CST 2008


Author: twilson
Date: Wed Dec 31 16:47:34 2008
New Revision: 167056

URL: http://svn.digium.com/view/asterisk?view=rev&rev=167056
Log:
Don't delete all calendars and re-add on reload.  Mark as needing to be deleted and merge/delete when necessary. (also as per russell on reviewboard).  I also fixed a memory leak that arose from a utility function that I created that initialized an events stringfield for me, but I forgot to remove the manual initializations.

Modified:
    team/twilson/calendaring/include/asterisk/calendar.h
    team/twilson/calendaring/main/calendar.c
    team/twilson/calendaring/res/res_caldav.c
    team/twilson/calendaring/res/res_icalendar.c

Modified: team/twilson/calendaring/include/asterisk/calendar.h
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/include/asterisk/calendar.h?view=diff&rev=167056&r1=167055&r2=167056
==============================================================================
--- team/twilson/calendaring/include/asterisk/calendar.h (original)
+++ team/twilson/calendaring/include/asterisk/calendar.h Wed Dec 31 16:47:34 2008
@@ -120,7 +120,8 @@
 	int timeframe;       /*!< Span (in mins) of calendar data to pull with each request */
 	pthread_t thread;    /*!< The thread that the calendar is loaded/updated in */
 	ast_cond_t unload;
-	int unloading;
+	int unloading:1;
+	int pending_deletion:1;
 	struct ao2_container *events;  /*!< The events that are known at this time */
 };
 

Modified: team/twilson/calendaring/main/calendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/main/calendar.c?view=diff&rev=167056&r1=167055&r2=167056
==============================================================================
--- team/twilson/calendaring/main/calendar.c (original)
+++ team/twilson/calendaring/main/calendar.c Wed Dec 31 16:47:34 2008
@@ -196,22 +196,28 @@
 {
 	struct ast_calendar *cal;
 	struct ast_variable *v;
-
-	if (!(cal = ao2_alloc(sizeof(*cal), calendar_destructor))) {
-		ast_log(LOG_ERROR, "Could not allocate calendar structure. Stopping.\n");
-		return NULL;
-	}
-
-	if (!(cal->events = ao2_container_alloc(MAX_BUCKETS, event_hash_fn, event_cmp_fn))) {
-		ast_log(LOG_ERROR, "Could not allocate events container for %s\n", cat);
-		cal = unref_calendar(cal);
-		return NULL;
-	}
-
-	if (ast_string_field_init(cal, 32)) {
-		ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", cat);
-		cal = unref_calendar(cal);
-		return NULL;
+	int new_calendar = 0;
+
+	if (!(cal = find_calendar(cat))) {
+		new_calendar = 1;
+		if (!(cal = ao2_alloc(sizeof(*cal), calendar_destructor))) {
+			ast_log(LOG_ERROR, "Could not allocate calendar structure. Stopping.\n");
+			return NULL;
+		}
+
+		if (!(cal->events = ao2_container_alloc(MAX_BUCKETS, event_hash_fn, event_cmp_fn))) {
+			ast_log(LOG_ERROR, "Could not allocate events container for %s\n", cat);
+			cal = unref_calendar(cal);
+			return NULL;
+		}
+
+		if (ast_string_field_init(cal, 32)) {
+			ast_log(LOG_ERROR, "Couldn't create string fields for %s\n", cat);
+			cal = unref_calendar(cal);
+			return NULL;
+		}
+	} else {
+		cal->pending_deletion = 0;
 	}
 
 	ast_string_field_set(cal, name, cat);
@@ -242,8 +248,12 @@
 		}
 	}
 
-	cal->thread = AST_PTHREADT_NULL;
-	ast_cond_init(&cal->unload, NULL);
+	if (new_calendar) {
+		cal->thread = AST_PTHREADT_NULL;
+		ast_cond_init(&cal->unload, NULL);
+		ao2_link(calendars, cal);
+		ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal);
+	}
 
 	return cal;
 }
@@ -268,10 +278,6 @@
 			ast_calendar_unregister(tech);
 			return -1;
 		}
-
-		ao2_link(calendars, cal);
-
-		ast_pthread_create(&cal->thread, NULL, cal->tech->load_calendar, cal);
 
 		cal = unref_calendar(cal);
 	}
@@ -340,11 +346,6 @@
 	ast_string_field_free_memory(event);
 
 	return;
-}
-
-static int cb_true(void *user_data, void *arg, int flags)
-{
-	return CMP_MATCH;
 }
 
 /* This is only called from ao2_callbacks that are going to unref the event for us,
@@ -1078,12 +1079,6 @@
 	}
 
 	if (!(event = ast_calendar_event_alloc(cal))) {
-		ast_log(LOG_ERROR, "Could not allocate memory for event\n");
-		goto write_cleanup;
-	}
-
-	if (ast_string_field_init(event, 32)) {
-		ast_log(LOG_ERROR, "Could not allocate memory for event stringfields\n");
 		goto write_cleanup;
 	}
 
@@ -1371,12 +1366,28 @@
 	.read = calendar_event_read,
 };
 
+static int cb_pending_deletion(void *user_data, void *arg, int flags)
+{
+	struct ast_calendar *cal = user_data;
+
+	cal->pending_deletion = 1;
+
+	return CMP_MATCH;
+}
+
+static int cb_rm_pending_deletion(void *user_data, void *arg, int flags)
+{
+	struct ast_calendar *cal = user_data;
+
+	return cal->pending_deletion ? CMP_MATCH : 0;
+}
+
 int ast_calendar_reload()
 {
 	struct ast_calendar_tech *iter;
 
-	/* Delete all calendars */
-	ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, cb_true, NULL);
+	/* Mark existing calendars for deletion */
+	ao2_callback(calendars, OBJ_NODATA | OBJ_MULTIPLE, cb_pending_deletion, NULL);
 	load_config(NULL);
 
 	AST_LIST_LOCK(&techs);
@@ -1386,6 +1397,9 @@
 		}
 	}
 	AST_LIST_UNLOCK(&techs);
+
+	/* Delete calendars that no longer show up in the config */
+	ao2_callback(calendars, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE, cb_rm_pending_deletion, NULL);
 
 	return 0;
 }

Modified: team/twilson/calendaring/res/res_caldav.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_caldav.c?view=diff&rev=167056&r1=167055&r2=167056
==============================================================================
--- team/twilson/calendaring/res/res_caldav.c (original)
+++ team/twilson/calendaring/res/res_caldav.c Wed Dec 31 16:47:34 2008
@@ -152,6 +152,7 @@
 	ne_add_request_header(req, "Content-type", ast_strlen_zero(content_type) ? "text/xml" : content_type);
 
 	ret = ne_request_dispatch(req);
+	ne_request_destroy(req);
 
 	if (ret != NE_OK || !ast_str_strlen(response)) {
 		ast_log(LOG_WARNING, "Unknown response to CalDAV calendar %s, request %s to %s: %s\n", pvt->owner->name, method, pvt->url, ne_get_error(pvt->session));
@@ -316,12 +317,6 @@
 		return;
 	}
 
-	if (ast_string_field_init(event, 32)) {
-		ast_log(LOG_ERROR, "Could not allocate string fields for event!\n");
-		event = ast_calendar_unref_event(event);
-		return;
-	}
-
 	start = icaltime_from_timet_with_zone(span->start, 0, utc);
 	end = icaltime_from_timet_with_zone(span->end, 0, utc);
 	event->start = span->start;

Modified: team/twilson/calendaring/res/res_icalendar.c
URL: http://svn.digium.com/view/asterisk/team/twilson/calendaring/res/res_icalendar.c?view=diff&rev=167056&r1=167055&r2=167056
==============================================================================
--- team/twilson/calendaring/res/res_icalendar.c (original)
+++ team/twilson/calendaring/res/res_icalendar.c Wed Dec 31 16:47:34 2008
@@ -180,12 +180,6 @@
 		return;
 	}
 
-	if (ast_string_field_init(event, 32)) {
-		ast_log(LOG_ERROR, "Could not allocate string fields for event!\n");
-		event = ast_calendar_unref_event(event);
-		return;
-	}
-
 	start = icaltime_from_timet_with_zone(span->start, 0, utc);
 	end = icaltime_from_timet_with_zone(span->end, 0, utc);
 	event->start = span->start;




More information about the svn-commits mailing list