0001-fix-lua_State-usage-for-callbacks.patch 20.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
From 09fd539b42fd74e13b46970b8718f11bebbb23c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Thu, 15 Oct 2015 13:44:00 +0300
Subject: [PATCH] fix lua_State usage for callbacks

We cannot store the lua_State at callback registration time, as that
lua coroutine might've been GC'ed at the callback invocation time.

This patch solves it by storing the invoking lua_State before calling
any dbus function that might call the callbacks. The callbacks are
then executed in the lua_State which called the dbus method invoking
the callback.

As side effect, we need to only track the lua reference for callbacks,
and avoid one malloc/free pair.
---
 src/connection.c   | 127 ++++++++++++++++++++++-------------------------------
 src/ldbus.c        |  20 +++++++++
 src/ldbus.h        |  23 +++++++---
 src/pending_call.c |  26 ++++-------
 src/timeout.c      |  26 +++--------
 src/timeout.h      |   4 +-
 src/watch.c        |  28 +++---------
 src/watch.h        |   4 +-
 8 files changed, 115 insertions(+), 143 deletions(-)

diff --git a/src/connection.c b/src/connection.c
index 8bbe74b..3c71034 100644
--- a/src/connection.c
+++ b/src/connection.c
@@ -46,10 +46,10 @@ static int ldbus_connection_gc(lua_State *L) {
 		   If you dbus_connection_unref() for the last time without closing the connection,
 		   the results are undefined
 		*/
-		dbus_connection_close(udata->connection);
+		ldbus_call(L, dbus_connection_close(udata->connection));
 	}
 
-	dbus_connection_unref(udata->connection);
+	ldbus_call(L, dbus_connection_unref(udata->connection));
 
 	return 0;
 }
@@ -96,7 +96,7 @@ static int ldbus_connection_send(lua_State *L) {
 	DBusMessage *message = check_DBusMessage(L, 2);
 
 	unsigned int serial;
-	lua_pushboolean(L, dbus_connection_send(connection, message, &serial));
+	lua_pushboolean(L, ldbus_wrap(L, dbus_connection_send(connection, message, &serial)));
 	lua_pushinteger(L, serial);
 
 	return 2;
@@ -108,7 +108,7 @@ static int ldbus_connection_send_with_reply(lua_State *L) {
 	int timeout_milliseconds = lua_isnoneornil(L, 3) ? (-1) : (luaL_checknumber(L, 3) * 1000);
 
 	DBusPendingCall *pending;
-	if (!dbus_connection_send_with_reply(connection, message, &pending, timeout_milliseconds)) {
+	if (!ldbus_wrap(L, dbus_connection_send_with_reply(connection, message, &pending, timeout_milliseconds))) {
 		return luaL_error(L, LDBUS_NO_MEMORY);
 	}
 
@@ -123,7 +123,7 @@ static int ldbus_connection_send_with_reply_and_block(lua_State *L) {
 	DBusMessage *reply;
 	DBusError error;
 	dbus_error_init(&error);
-	reply = dbus_connection_send_with_reply_and_block(connection, message, timeout_milliseconds, &error);
+	reply = ldbus_wrap(L, dbus_connection_send_with_reply_and_block(connection, message, timeout_milliseconds, &error));
 	if (dbus_error_is_set(&error)) {
 		lua_pushnil(L);
 		lua_pushstring(L, error.message);
@@ -147,7 +147,7 @@ static int ldbus_connection_read_write_dispatch(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
 	int timeout_milliseconds = luaL_optinteger(L, 2, -1);
 
-	lua_pushboolean(L, dbus_connection_read_write_dispatch(connection, timeout_milliseconds));
+	lua_pushboolean(L, ldbus_wrap(L, dbus_connection_read_write_dispatch(connection, timeout_milliseconds)));
 
 	return 1;
 }
@@ -156,7 +156,7 @@ static int ldbus_connection_read_write(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
 	int timeout_milliseconds = luaL_optinteger(L, 2, -1);
 
-	lua_pushboolean(L, dbus_connection_read_write(connection, timeout_milliseconds));
+	lua_pushboolean(L, ldbus_wrap(L, dbus_connection_read_write(connection, timeout_milliseconds)));
 
 	return 1;
 }
@@ -164,7 +164,7 @@ static int ldbus_connection_read_write(lua_State *L) {
 static int ldbus_connection_pop_message(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
 
-	DBusMessage *message = dbus_connection_pop_message(connection);
+	DBusMessage *message = ldbus_wrap(L, dbus_connection_pop_message(connection));
 
 	if (message == NULL) {
 		lua_pushnil(L);
@@ -183,16 +183,15 @@ static int ldbus_connection_get_dispatch_status(lua_State *L) {
 
 static int ldbus_connection_dispatch(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
-
-	lua_pushstring(L, DispatchStatus_lst [ dbus_connection_dispatch(connection) ]);
+	lua_pushstring(L, DispatchStatus_lst [ ldbus_wrap(L, dbus_connection_dispatch(connection)) ]);
 	return 1;
 }
 
 static int ldbus_connection_set_watch_functions(lua_State *L) {
-	ldbus_watch_udata *data;
-
 	DBusConnection *connection = check_DBusConnection(L, 1);
 	int has_toggle = lua_isnil(L, 4);
+	intptr_t ref;
+
 	lua_settop(L, 4);
 	/* Place a table below the 3 callback argument */
 	lua_createtable(L, 0, 3);
@@ -206,25 +205,22 @@ static int ldbus_connection_set_watch_functions(lua_State *L) {
 	luaL_requiref(L, "ldbus.watch", luaopen_ldbus_watch, FALSE);
 	lua_pop(L, 1);
 
-	if ((data = malloc(sizeof(ldbus_watch_udata))) == NULL) return luaL_error(L, LDBUS_NO_MEMORY);
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-
-	if (!dbus_connection_set_watch_functions(connection,
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	if (!ldbus_wrap(L, dbus_connection_set_watch_functions(connection,
 			ldbus_watch_add_function, ldbus_watch_remove_function,
 			has_toggle ? NULL : ldbus_watch_toggled_function,
-			(void *)data, ldbus_watch_free_data_function)) {
-		free(data);
+			(void *) ref, ldbus_watch_free_data_function))) {
+		luaL_unref(L, LUA_REGISTRYINDEX, ref);
 		return luaL_error(L, LDBUS_NO_MEMORY);
-	};
+	}
 	lua_pushboolean(L, TRUE);
 	return 1;
 }
 
 static int ldbus_connection_set_timeout_functions(lua_State *L) {
-	ldbus_timeout_udata *data;
-
 	DBusConnection *connection = check_DBusConnection(L, 1);
+	intptr_t ref;
+
 	lua_settop(L, 4);
 	/* Place a table below the 3 callback argument */
 	lua_createtable(L, 0, 3);
@@ -238,52 +234,41 @@ static int ldbus_connection_set_timeout_functions(lua_State *L) {
 	luaL_requiref(L, "ldbus.timeout", lua_open_ldbus_timeout, FALSE);
 	lua_pop(L, 1);
 
-	if ((data = malloc(sizeof(ldbus_timeout_udata))) == NULL) return luaL_error(L, LDBUS_NO_MEMORY);
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-
-	if (!dbus_connection_set_timeout_functions(connection,
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	if (!ldbus_wrap(L, dbus_connection_set_timeout_functions(connection,
 			ldbus_timeout_add_function, ldbus_timeout_remove_function, ldbus_timeout_toggled_function,
-			(void *)data, ldbus_timeout_free_data_function)) {
-		free(data);
+			(void *)ref, ldbus_timeout_free_data_function))) {
+		luaL_unref(L, LUA_REGISTRYINDEX, ref);
 		return luaL_error(L, LDBUS_NO_MEMORY);
 	};
 	lua_pushboolean(L, TRUE);
 	return 1;
 }
 
-static void free_data_function(void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
-	luaL_unref(L, LUA_REGISTRYINDEX, ref);
-	free(data);
-}
-
 static void wakeup_main_function(void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+
 	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
 	lua_pcall(L, 0, 0, 0);
 }
 
 static int ldbus_connection_set_wakeup_main_function(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
-	ldbus_callback_udata *data;
+	intptr_t ref;
+
 	luaL_checktype(L, 2, LUA_TFUNCTION);
 	lua_settop(L, 2);
-	if ((data = malloc(sizeof(ldbus_callback_udata))) == NULL) {
-		return luaL_error(L, LDBUS_NO_MEMORY);
-	}
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	dbus_connection_set_wakeup_main_function(connection, wakeup_main_function, data, free_data_function);
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	dbus_connection_set_wakeup_main_function(connection, wakeup_main_function, (void*) ref, ldbus_free_ref);
 	lua_pushboolean(L, 1);
 	return 1;
 }
 
 static void dispatch_status_function(DBusConnection *connection, DBusDispatchStatus new_status, void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+
 	UNUSED(connection);
 	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
 	lua_pushstring(L, DispatchStatus_lst[new_status]);
@@ -292,15 +277,12 @@ static void dispatch_status_function(DBusConnection *connection, DBusDispatchSta
 
 static int ldbus_connection_set_dispatch_status_function(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
-	ldbus_callback_udata *data;
+	intptr_t ref;
+
 	luaL_checktype(L, 2, LUA_TFUNCTION);
 	lua_settop(L, 2);
-	if ((data = malloc(sizeof(ldbus_callback_udata))) == NULL) {
-		return luaL_error(L, LDBUS_NO_MEMORY);
-	}
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	dbus_connection_set_dispatch_status_function(connection, dispatch_status_function, data, free_data_function);
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	dbus_connection_set_dispatch_status_function(connection, dispatch_status_function, (void *) ref, ldbus_free_ref);
 	lua_pushboolean(L, 1);
 	return 1;
 }
@@ -356,16 +338,17 @@ static int ldbus_connection_has_messages_to_send(lua_State *L) {
 }
 
 static void unregister_function(DBusConnection *connection, void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+
 	UNUSED(connection);
 	luaL_unref(L, LUA_REGISTRYINDEX, ref);
-	free(data);
 }
 
 static DBusHandlerResult message_function(DBusConnection *connection, DBusMessage *message, void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+
 	UNUSED(connection);
 	if (!lua_checkstack(L, 2)) {
 		return DBUS_HANDLER_RESULT_NEED_MEMORY;
@@ -396,16 +379,13 @@ static const DBusObjectPathVTable VTable = {
 static int ldbus_connection_register_object_path(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
 	const char *path = luaL_checkstring(L, 2);
-	ldbus_callback_udata *data;
+	intptr_t ref;
+
 	luaL_checktype(L, 3, LUA_TFUNCTION);
 	lua_settop(L, 3);
-	if ((data = malloc(sizeof(ldbus_callback_udata))) == NULL) {
-		return luaL_error(L, LDBUS_NO_MEMORY);
-	}
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	if (!dbus_connection_register_object_path(connection, path, &VTable, data)) {
-		free(data);
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	if (!dbus_connection_register_object_path(connection, path, &VTable, (void*) ref)) {
+		luaL_unref(L, LUA_REGISTRYINDEX, ref);
 		return luaL_error(L, "unknown error");
 	}
 	lua_pushboolean(L, 1);
@@ -415,16 +395,13 @@ static int ldbus_connection_register_object_path(lua_State *L) {
 static int ldbus_connection_register_fallback(lua_State *L) {
 	DBusConnection *connection = check_DBusConnection(L, 1);
 	const char *path = luaL_checkstring(L, 2);
-	ldbus_callback_udata *data;
+	intptr_t ref;
+
 	luaL_checktype(L, 3, LUA_TFUNCTION);
 	lua_settop(L, 3);
-	if ((data = malloc(sizeof(ldbus_callback_udata))) == NULL) {
-		return luaL_error(L, LDBUS_NO_MEMORY);
-	}
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	if (!dbus_connection_register_fallback(connection, path, &VTable, data)) {
-		free(data);
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	if (!dbus_connection_register_fallback(connection, path, &VTable, (void*) ref)) {
+		luaL_unref(L, LUA_REGISTRYINDEX, ref);
 		return luaL_error(L, "unknown error");
 	}
 	lua_pushboolean(L, 1);
diff --git a/src/ldbus.c b/src/ldbus.c
index 0ce3333..ab32257 100644
--- a/src/ldbus.c
+++ b/src/ldbus.c
@@ -12,6 +12,26 @@
 #include "watch.h"
 #include "ldbus.h"
 
+static __thread lua_State *__ldbus_state;
+
+lua_State *ldbus_get_state()
+{
+	return __ldbus_state;
+}
+
+lua_State *ldbus_set_state(lua_State *L)
+{
+	lua_State *old = __ldbus_state;
+	__ldbus_state = L;
+	return old;
+}
+
+void ldbus_free_ref(void *data) {
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+	luaL_unref(L, LUA_REGISTRYINDEX, ref);
+}
+
 int tostring(lua_State *L) {
 	if (!luaL_getmetafield(L, 1, "__udtype")) {
 		lua_pushstring(L, "object with a generic __tostring metamethod but no __type metafield");
diff --git a/src/ldbus.h b/src/ldbus.h
index a33422f..9c01263 100644
--- a/src/ldbus.h
+++ b/src/ldbus.h
@@ -1,23 +1,36 @@
 #ifndef LDBUS_H
 #define LDBUS_H
 
+#include <stdint.h>
 #include <lua.h>
 
 #define UNUSED(x) (void)(x)
 #define LDBUS_INTERNAL __attribute__ ((visibility ("internal")))
 #define LDBUS_NO_MEMORY "no memory"
 
-typedef struct {
-	lua_State* L;
-	int ref;
-} ldbus_callback_udata;
-
 enum ldbus_callback_indexes {
 	DBUS_LUA_FUNC_ADD,
 	DBUS_LUA_FUNC_REMOVE,
 	DBUS_LUA_FUNC_TOGGLE
 };
 
+#define ldbus_wrap(L, func) __extension__ ({	\
+	lua_State *old = ldbus_set_state(L);	\
+	__auto_type result = (func);		\
+	ldbus_set_state(old);			\
+	result;					\
+})
+
+#define ldbus_call(L, func) __extension__ ({	\
+	lua_State *old = ldbus_set_state(L);	\
+	(func);					\
+	ldbus_set_state(old);			\
+})
+
+LDBUS_INTERNAL lua_State *ldbus_get_state();
+LDBUS_INTERNAL lua_State *ldbus_set_state(lua_State *);
+LDBUS_INTERNAL void ldbus_free_ref(void *ref);
+
 LDBUS_INTERNAL int tostring(lua_State *);
 
 int luaopen_ldbus(lua_State *);
diff --git a/src/pending_call.c b/src/pending_call.c
index 0dbc6ef..98b5d11 100644
--- a/src/pending_call.c
+++ b/src/pending_call.c
@@ -17,36 +17,28 @@ static DBusPendingCall* checkPendingCall(lua_State *L, int arg) {
 
 static int ldbus_pending_call_unref(lua_State *L) {
 	DBusPendingCall* pending = checkPendingCall(L, 1);
-	dbus_pending_call_unref(pending);
+	ldbus_call(L, dbus_pending_call_unref(pending));
 	return 0;
 }
 
 static void pending_notify_function(DBusPendingCall *pending, void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
+
 	UNUSED(pending);
 	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
 	lua_pcall(L, 0, 0, 0);
 }
 
-static void free_data_function(void *data) {
-	lua_State *L = ((ldbus_callback_udata*)data)->L;
-	int ref = ((ldbus_callback_udata*)data)->ref;
-	luaL_unref(L, LUA_REGISTRYINDEX, ref);
-	free(data);
-}
-
 static int ldbus_pending_call_set_notify(lua_State *L) {
 	DBusPendingCall* pending = checkPendingCall(L, 1);
-	ldbus_callback_udata *data;
+	intptr_t ref;
+
 	luaL_checktype(L, 2, LUA_TFUNCTION);
 	lua_settop(L, 2);
-	if ((data = malloc(sizeof(ldbus_callback_udata))) == NULL) {
-		return luaL_error(L, LDBUS_NO_MEMORY);
-	}
-	data->L = L;
-	data->ref = luaL_ref(L, LUA_REGISTRYINDEX);
-	if (!dbus_pending_call_set_notify(pending, pending_notify_function, data, free_data_function)) {
+	ref = luaL_ref(L, LUA_REGISTRYINDEX);
+	if (!ldbus_wrap(L, dbus_pending_call_set_notify(pending, pending_notify_function, (void*) ref, ldbus_free_ref))) {
+		luaL_unref(L, LUA_REGISTRYINDEX, ref);
 		return luaL_error(L, LDBUS_NO_MEMORY);
 	}
 	lua_pushboolean(L, 1);
diff --git a/src/timeout.c b/src/timeout.c
index 573700d..b4cdfe5 100644
--- a/src/timeout.c
+++ b/src/timeout.c
@@ -27,7 +27,7 @@ static int ldbus_timeout_handle(lua_State *L) {
 	if (timeout == NULL) {
 		lua_pushnil(L);
 	} else {
-		lua_pushboolean(L, dbus_timeout_handle(timeout));
+		lua_pushboolean(L, ldbus_wrap(L, dbus_timeout_handle(timeout)));
 	}
 	return 1;
 }
@@ -46,8 +46,8 @@ LDBUS_INTERNAL void push_DBusTimeout(lua_State *L, DBusTimeout *timeout) {
 }
 
 LDBUS_INTERNAL dbus_bool_t ldbus_timeout_add_function(DBusTimeout *timeout, void *data) {
-	lua_State *L = ((ldbus_timeout_udata*)data)->L;
-	int ref = ((ldbus_timeout_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 
 	if (!lua_checkstack(L, 4)) return FALSE;
 
@@ -63,9 +63,8 @@ LDBUS_INTERNAL dbus_bool_t ldbus_timeout_add_function(DBusTimeout *timeout, void
 }
 
 LDBUS_INTERNAL void ldbus_timeout_remove_function(DBusTimeout *timeout, void *data) {
-	lua_State *L = ((ldbus_timeout_udata*)data)->L;
-	int ref = ((ldbus_timeout_udata*)data)->ref;
-
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 	DBusTimeout **udata;
 
 	/* Lookup remove callback from ref in `data` */
@@ -94,13 +93,11 @@ LDBUS_INTERNAL void ldbus_timeout_remove_function(DBusTimeout *timeout, void *da
 	if (udata != NULL) {
 		*udata = NULL;
 	}
-
-	return;
 }
 
 LDBUS_INTERNAL void ldbus_timeout_toggled_function(DBusTimeout *timeout, void *data) {
-	lua_State *L = ((ldbus_timeout_udata*)data)->L;
-	int ref = ((ldbus_timeout_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 
 	/* Lookup remove callback from ref in `data` */
 	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
@@ -114,15 +111,6 @@ LDBUS_INTERNAL void ldbus_timeout_toggled_function(DBusTimeout *timeout, void *d
 
 	/* Call the callback, with looked-up timeout as argument */
 	lua_pcall(L, 1, 0, 0);
-	return;
-}
-
-LDBUS_INTERNAL void ldbus_timeout_free_data_function(void *data) {
-	lua_State *L = ((ldbus_timeout_udata*)data)->L;
-	int ref = ((ldbus_timeout_udata*)data)->ref;
-
-	luaL_unref(L, LUA_REGISTRYINDEX, ref);
-	free(data);
 }
 
 int lua_open_ldbus_timeout(lua_State *L) {
diff --git a/src/timeout.h b/src/timeout.h
index 5794563..4eab6e6 100644
--- a/src/timeout.h
+++ b/src/timeout.h
@@ -12,13 +12,11 @@
 
 #define check_DBusTimeout(L, arg) (*(DBusTimeout **)luaL_checkudata((L), (arg), DBUS_TIMEOUT_METATABLE))
 
-typedef ldbus_callback_udata ldbus_timeout_udata;
-
 LDBUS_INTERNAL void push_DBusTimeout(lua_State *L, DBusTimeout *timeout);
 LDBUS_INTERNAL dbus_bool_t ldbus_timeout_add_function(DBusTimeout *timeout, void *data);
 LDBUS_INTERNAL void ldbus_timeout_remove_function(DBusTimeout *timeout, void *data);
 LDBUS_INTERNAL void ldbus_timeout_toggled_function(DBusTimeout *timeout, void *data);
-LDBUS_INTERNAL void ldbus_timeout_free_data_function(void *data);
+#define ldbus_timeout_free_data_function ldbus_free_ref
 
 int lua_open_ldbus_timeout(lua_State *L);
 
diff --git a/src/watch.c b/src/watch.c
index 2773212..173bc96 100644
--- a/src/watch.c
+++ b/src/watch.c
@@ -42,11 +42,9 @@ static int ldbus_watch_get_flags(lua_State *L) {
 static int ldbus_watch_handle(lua_State *L) {
 	DBusWatch *watch = check_DBusWatch(L, 1);
 	int flags;
-	dbus_bool_t ok;
 	luaL_argcheck(L, watch != NULL, 1, "watch invalid");
 	flags = luaL_checkinteger(L, 2);
-	ok = dbus_watch_handle(watch, flags);
-	lua_pushboolean(L, ok);
+	lua_pushboolean(L, ldbus_wrap(L, dbus_watch_handle(watch, flags)));
 	return 1;
 }
 
@@ -64,8 +62,8 @@ LDBUS_INTERNAL void push_DBusWatch(lua_State *L, DBusWatch *watch) {
 }
 
 LDBUS_INTERNAL dbus_bool_t ldbus_watch_add_function(DBusWatch *watch, void *data) {
-	lua_State *L = ((ldbus_watch_udata*)data)->L;
-	int ref = ((ldbus_watch_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 
 	if (!lua_checkstack(L, 4)) return FALSE;
 
@@ -81,9 +79,8 @@ LDBUS_INTERNAL dbus_bool_t ldbus_watch_add_function(DBusWatch *watch, void *data
 }
 
 LDBUS_INTERNAL void ldbus_watch_remove_function(DBusWatch *watch, void *data) {
-	lua_State *L = ((ldbus_watch_udata*)data)->L;
-	int ref = ((ldbus_watch_udata*)data)->ref;
-
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 	DBusWatch **udata;
 
 	/* Lookup remove callback from ref in `data` */
@@ -112,13 +109,11 @@ LDBUS_INTERNAL void ldbus_watch_remove_function(DBusWatch *watch, void *data) {
 	if (udata != NULL) {
 		*udata = NULL;
 	}
-
-	return;
 }
 
 LDBUS_INTERNAL void ldbus_watch_toggled_function(DBusWatch *watch, void *data) {
-	lua_State *L = ((ldbus_watch_udata*)data)->L;
-	int ref = ((ldbus_watch_udata*)data)->ref;
+	lua_State *L = ldbus_get_state();
+	intptr_t ref = (intptr_t) data;
 
 	/* Lookup remove callback from ref in `data` */
 	lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
@@ -132,15 +127,6 @@ LDBUS_INTERNAL void ldbus_watch_toggled_function(DBusWatch *watch, void *data) {
 
 	/* Call the callback, with looked-up watch as argument */
 	lua_pcall(L, 1, 0, 0);
-	return;
-}
-
-LDBUS_INTERNAL void ldbus_watch_free_data_function(void *data) {
-	lua_State *L = ((ldbus_watch_udata*)data)->L;
-	int ref = ((ldbus_watch_udata*)data)->ref;
-
-	luaL_unref(L, LUA_REGISTRYINDEX, ref);
-	free(data);
 }
 
 int luaopen_ldbus_watch(lua_State *L) {
diff --git a/src/watch.h b/src/watch.h
index f37f877..d3abf08 100644
--- a/src/watch.h
+++ b/src/watch.h
@@ -12,13 +12,11 @@
 
 #define check_DBusWatch(L, arg) (*(DBusWatch **)luaL_checkudata((L), (arg), DBUS_WATCH_METATABLE))
 
-typedef ldbus_callback_udata ldbus_watch_udata;
-
 LDBUS_INTERNAL void push_DBusWatch(lua_State *L, DBusWatch *watch);
 LDBUS_INTERNAL dbus_bool_t ldbus_watch_add_function(DBusWatch *watch, void *data);
 LDBUS_INTERNAL void ldbus_watch_remove_function(DBusWatch *watch, void *data);
 LDBUS_INTERNAL void ldbus_watch_toggled_function(DBusWatch *watch, void *data);
-LDBUS_INTERNAL void ldbus_watch_free_data_function(void *data);
+#define ldbus_watch_free_data_function ldbus_free_ref
 
 int luaopen_ldbus_watch(lua_State *L);
 
-- 
2.6.1