diff options
author | Yorhel <git@yorhel.nl> | 2013-04-13 09:48:44 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2013-04-13 09:55:14 +0200 |
commit | b88b6f719d9f15f3d8bbe5559d040bc8fe6f1dbb (patch) | |
tree | e27aecf0274a32a3a50b3b4614dfac99c794f935 | |
parent | a774ee6f7fa0b7ac2c72d46d57d6820248c8729f (diff) |
dbo: Make the list of exported interfaces for an object static
I'm not quite convinced yet that this actually simplifies things. It
does make object creation a *tiny* bit more efficient.
This also changes the type of the dbo_<name>_interface variables,
because the pointer indirection didn't allow for using the pointer to
the interface struct as a compile-time constant.
m--------- | deps | 0 | ||||
-rw-r--r-- | src/app.c | 43 | ||||
-rw-r--r-- | src/dbo.c | 54 | ||||
-rw-r--r-- | src/dbo.h | 28 | ||||
-rw-r--r-- | src/global.h | 1 | ||||
-rw-r--r-- | src/hub/chat.c | 4 | ||||
-rw-r--r-- | src/hub/chat.h | 3 | ||||
-rw-r--r-- | src/hub/connection.c | 6 | ||||
-rw-r--r-- | src/hub/connection.h | 2 | ||||
-rw-r--r-- | src/hub/hub.c | 8 | ||||
-rw-r--r-- | src/hub/hub.h | 3 | ||||
-rw-r--r-- | src/hub/manager.c | 16 | ||||
-rw-r--r-- | src/hub/users.c | 8 | ||||
-rw-r--r-- | src/hub/users.h | 3 | ||||
-rwxr-xr-x | src/itfgen.pl | 9 |
15 files changed, 103 insertions, 85 deletions
diff --git a/deps b/deps -Subproject 486855fccada3c1906861e9eb62c80b16e3a28b +Subproject 79d43d183d03d1ad8ef546e291f13b40d54047b @@ -32,8 +32,12 @@ static const char *version = ; -static dbo_app_properties_t props; -static dbo_t obj; +typedef struct { + dbo_t obj; + dbo_app_properties_t props; +} app_t; + +static app_t app_obj; const char *app_version_long() { @@ -157,8 +161,8 @@ static void app_cert_init() { -const char *app_tiger_cid() { return props.TigerCID; } -const char *app_tiger_pid() { return props.TigerPID; } +const char *app_tiger_cid() { return app_obj.props.TigerCID; } +const char *app_tiger_pid() { return app_obj.props.TigerPID; } static void app_pid_save(const char *pid) { @@ -202,52 +206,53 @@ static char *app_cid_create(const char *pid, char *res) { } -static void set_TigerPID(dbo_t *o, DBusMessage *msg, const char *newpid) { +static void set_TigerPID(app_t *o, DBusMessage *msg, const char *newpid) { if(!isbase32_24(newpid)) dbo_sendfree(dbus_message_new_error_printf(msg, DBUS_ERROR_INVALID_ARGS, "Invalid PID")); else { char cid[40]; - dbo_app_TigerCID_set(o, &props, app_cid_create(newpid, cid)); - dbo_app_TigerPID_setreply(o, &props, msg, newpid); + dbo_app_TigerCID_set(o, &o->props, app_cid_create(newpid, cid)); + dbo_app_TigerPID_setreply(o, &o->props, msg, newpid); app_pid_save(newpid); - yinfo("PID/CID changed to %s/%s", props.TigerPID, props.TigerCID); + yinfo("PID/CID changed to %s/%s", o->props.TigerPID, o->props.TigerCID); } } -static void set_LogLevel(dbo_t *o, DBusMessage *msg, const char *newlvl) { +static void set_LogLevel(app_t *o, DBusMessage *msg, const char *newlvl) { ylog_set_level(YLOG_DEFAULT, newlvl); - dbo_app_LogLevel_setreply(o, &props, msg, newlvl); + dbo_app_LogLevel_setreply(o, &o->props, msg, newlvl); yinfo("Log level changed to: %s", newlvl); } -static void Shutdown(dbo_t *obj, DBusMessage *msg) { +static void Shutdown(app_t *obj, DBusMessage *msg) { dbo_app_Shutdown_reply(msg); main_shutdown(); } void app_init(const char *loglvl) { - static dbo_app_vtable_t(dbo_t) vt = dbo_app_funcs(); + static const dbo_app_vtable_t(app_t) vt = dbo_app_funcs(); + static const dbo_reg_t reg = { &dbo_app_interface, (void**)&vt, offsetof(app_t, props) }; + char pid[40]; char cid[40]; - props.VersionString = (char *)app_version_long(); /* No need to strdup(), won't get free()'d */ - props.VersionNumber = app_version_num(); + app_obj.props.VersionString = (char *)app_version_long(); /* No need to strdup(), won't get free()'d */ + app_obj.props.VersionNumber = app_version_num(); - props.LogLevel = strdup(loglvl ? loglvl : ""); + app_obj.props.LogLevel = strdup(loglvl ? loglvl : ""); app_pid_init(pid); - props.TigerPID = strdup(pid); - props.TigerCID = strdup(app_cid_create(pid, cid)); + app_obj.props.TigerPID = strdup(pid); + app_obj.props.TigerCID = strdup(app_cid_create(pid, cid)); app_cert_init(); - dbo_register(&obj, APP_PATH); - dbo_add_itf(&obj, dbo_app_interface, &vt, &props); + dbo_register(&app_obj.obj, APP_PATH, ®, 1); } /* vim: set noet sw=4 ts=4: */ @@ -189,9 +189,9 @@ static void handle_introspect(DBusMessage *msg, dbo_t *obj) { * behaviour. */ kputs(XML_INTERFACE_INTROSPECTABLE XML_INTERFACE_PEER, &s); - size_t i; - for(i=0; i<kv_size(obj->regs); i++) - introspect_itf(kv_A(obj->regs, i).itf, &s); + int i; + for(i=0; i<obj->numregs; i++) + introspect_itf(obj->regs[i].itf, &s); } introspect_childs(msg, &s); @@ -208,11 +208,10 @@ static void handle_introspect(DBusMessage *msg, dbo_t *obj) { * index of the integer on success, -1 on failure. Sets *res to point to the * property object and *idx to the offset for the vtable. */ static int prop_find(dbo_t *obj, const char *interface, const char *prop, const dbo_property_t **res, int *idx) { - size_t i; - int j; + int i, j; - for(i=0; i<kv_size(obj->regs); i++) { - const dbo_interface_t *itf = kv_A(obj->regs, i).itf; + for(i=0; i<obj->numregs; i++) { + const dbo_interface_t *itf = obj->regs[i].itf; if(*interface && strcmp(itf->name, interface) != 0) continue; if(idx) @@ -247,7 +246,7 @@ static void prop_get(dbo_t *obj, DBusMessage *msg) { DBusMessage *reply = dbus_message_new_method_return(msg); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, p->type, &sub); - p->getter(kv_A(obj->regs, i).ptable, &sub); + p->getter(((char *)obj) + obj->regs[i].poffset, &sub); dbus_message_iter_close_container(&iter, &sub); dbo_sendfree(reply); } @@ -262,10 +261,9 @@ static void prop_getall(dbo_t *obj, DBusMessage *msg) { DBusMessage *reply = dbus_message_new_method_return(msg); dbus_message_iter_init_append(reply, &iter); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub); - size_t i; - int j; - for(i=0; i<kv_size(obj->regs); i++) { - const dbo_interface_t *itf = kv_A(obj->regs, i).itf; + int i, j; + for(i=0; i<obj->numregs; i++) { + const dbo_interface_t *itf = obj->regs[i].itf; if(*interface && strcmp(itf->name, interface) != 0) continue; for(j=0; j<itf->n_properties; j++) { @@ -274,7 +272,7 @@ static void prop_getall(dbo_t *obj, DBusMessage *msg) { dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, NULL, &sub2); dbus_message_iter_append_basic(&sub2, DBUS_TYPE_STRING, &(itf->properties[j].name)); dbus_message_iter_open_container(&sub2, DBUS_TYPE_VARIANT, itf->properties[j].type, &sub3); - itf->properties[j].getter(kv_A(obj->regs, i).ptable, &sub3); + itf->properties[j].getter(((char *)obj) + obj->regs[i].poffset, &sub3); dbus_message_iter_close_container(&sub2, &sub3); dbus_message_iter_close_container(&sub, &sub2); } @@ -306,18 +304,17 @@ static void prop_set(dbo_t *obj, DBusMessage *msg) { return; } - p->setter(obj, msg, &sub, kv_A(obj->regs, i).vtable[idx + kv_A(obj->regs, i).itf->n_methods]); + p->setter(obj, msg, &sub, obj->regs[i].vtable[idx + obj->regs[i].itf->n_methods]); } static bool route_method(dbo_t *obj, DBusMessage *msg) { const char *interface = dbus_message_get_interface(msg); const char *method = dbus_message_get_member(msg); - size_t i; - int j; + int i, j; - for(i=0; i<kv_size(obj->regs); i++) { - const dbo_interface_t *itf = kv_A(obj->regs, i).itf; + for(i=0; i<obj->numregs; i++) { + const dbo_interface_t *itf = obj->regs[i].itf; if(interface && strcmp(itf->name, interface) != 0) continue; @@ -330,7 +327,7 @@ static bool route_method(dbo_t *obj, DBusMessage *msg) { ydebug("method(%s, %s, %s, %s)", obj->path, itf->name, method, method_signature(m)); DBusMessageIter iter; dbus_message_iter_init(msg, &iter); - m->wrapper(obj, msg, &iter, kv_A(obj->regs, i).vtable[j]); + m->wrapper(obj, msg, &iter, obj->regs[i].vtable[j]); return true; } } @@ -381,11 +378,8 @@ unhandled: static void handle_unregister(DBusConnection *con, void *data) { - if(data) { - dbo_t *o = data; - free(o->path); - kv_destroy(o->regs); - } + if(data) + free(((dbo_t *)data)->path); } @@ -398,10 +392,18 @@ void dbo_init() { /* Export a new object at the given path. */ -void dbo_register(dbo_t *o, const char *path) { +void dbo_register(dbo_t *o, const char *path, const dbo_reg_t *regs, int numregs) { + int i; + o->path = strdup(path); - kv_init(o->regs); + o->regs = regs; + o->numregs = numregs; + o->hasprops = false; + for(i=0; !o->hasprops && i<numregs; i++) + if(regs[i].itf->n_properties > 0) + o->hasprops = true; + dbus_connection_register_object_path(dbuscon, o->path, &vtable, o); } @@ -77,13 +77,14 @@ typedef struct { typedef struct { const dbo_interface_t *itf; void **vtable; - void *ptable; + size_t poffset; /* Offset from the dbo_t struct to the properties struct */ } dbo_reg_t; typedef struct { char *path; - kvec_t(dbo_reg_t) regs; + const dbo_reg_t *regs; + int numregs; bool hasprops; } dbo_t; @@ -93,21 +94,16 @@ typedef struct { void dbo_init(); -/* Export a new object at the given path. */ -void dbo_register(dbo_t *o, const char *path); - - -/* Export an interface for the given object. - * 3rd argument should be a pointer to an initialized dbo_<interface>_vtable_t - * structure. Last argument should point to a dbo_<interface>_properties_t - * structure, or NULL if the interface has no properties. - * It is an error to add the same interface multiple times to the same object. +/* Export a new object at the given path. The *regs argument should point to a + * list of interfaces exported on this object. E.g. + * + * static dbo_reg_t regs[] = { + * { itf, vtable, offsetof(my_struct, name_of_itf_properties_member) }, + * .. + * }; + * dbo_register(myobj, mypath, regs, sizeof(regs)/sizeof(*regs)); */ -static inline void dbo_add_itf(dbo_t *o, const dbo_interface_t *itf, void *vtable, void *ptable) { - dbo_reg_t r = { itf, vtable, ptable }; - o->hasprops = o->hasprops || itf->n_properties > 0; - kv_push(dbo_reg_t, o->regs, r); -} +void dbo_register(dbo_t *o, const char *path, const dbo_reg_t *regs, int numregs); /* Remove the object. The same object can be re-used again with _register(). */ diff --git a/src/global.h b/src/global.h index 4a9580b..4bbdf36 100644 --- a/src/global.h +++ b/src/global.h @@ -65,7 +65,6 @@ #include <evtp.h> #include <khash.h> #include <kstring.h> -#include <kvec.h> #include <sqlasync.h> #include <ylog.h> #include <yuri.h> diff --git a/src/hub/chat.c b/src/hub/chat.c index cfdd2c6..a0e1ee8 100644 --- a/src/hub/chat.c +++ b/src/hub/chat.c @@ -166,10 +166,8 @@ void hub_chat_message(hub_t *h, int64_t group, int64_t from, const char *message void hub_chat_init(hub_t *h) { - static dbo_hubch_vtable_t(hub_t) vt = dbo_hubch_funcs(); h->ch.props.ChatLogSize = 0; h->ch.groups = kh_init(chatgroups); - dbo_add_itf(&h->dbo, dbo_hubch_interface, &vt, &h->ch.props); } @@ -181,4 +179,6 @@ void hub_chat_destroy(hub_t *h) { kh_destroy(chatgroups, h->ch.groups); } +const hub_chat_vt_t hub_chat_vt = dbo_hubch_funcs(); + /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/chat.h b/src/hub/chat.h index 43825bb..aa4d8df 100644 --- a/src/hub/chat.h +++ b/src/hub/chat.h @@ -46,5 +46,8 @@ void hub_chat_message(hub_t *h, int64_t group, int64_t from, const char *message void hub_chat_init(hub_t *h); void hub_chat_destroy(hub_t *h); +typedef dbo_hubch_vtable_t(hub_t) hub_chat_vt_t; +extern const hub_chat_vt_t hub_chat_vt; + #endif /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/connection.c b/src/hub/connection.c index 833f3ed..c3bfcdf 100644 --- a/src/hub/connection.c +++ b/src/hub/connection.c @@ -391,7 +391,6 @@ void hub_conn_autoconnect(hub_t *h) { void hub_conn_init(hub_t *h, const char *addr, bool autoconnect, uint16_t timeout) { - static dbo_hubc_vtable_t(hub_t) vt = dbo_hubc_funcs(); h->c.props.AutoConnect = autoconnect; h->c.props.ConnectState = HUBC_IDLE; h->c.props.ConnectAddress = strdup(addr); @@ -401,8 +400,6 @@ void hub_conn_init(hub_t *h, const char *addr, bool autoconnect, uint16_t timeou ev_timer_init(&h->c.timer, hub_conn_timer, h->c.props.ReconnectTimeout, 0); h->c.timer.data = h; - - dbo_add_itf(&h->dbo, dbo_hubc_interface, &vt, &h->c.props); } @@ -411,4 +408,7 @@ void hub_conn_destroy(hub_t *h) { dbo_hubc_properties_free(&h->c.props); } + +const hub_conn_vt_t hub_conn_vt = dbo_hubc_funcs(); + /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/connection.h b/src/hub/connection.h index 4e3fbb7..ffe7122 100644 --- a/src/hub/connection.h +++ b/src/hub/connection.h @@ -62,6 +62,8 @@ void hub_conn_autoconnect(hub_t *h); void hub_conn_init(hub_t *h, const char *addr, bool autoconnect, uint16_t timeout); void hub_conn_destroy(hub_t *h); +typedef dbo_hubc_vtable_t(hub_t) hub_conn_vt_t; +extern const hub_conn_vt_t hub_conn_vt; #endif /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/hub.c b/src/hub/hub.c index ec7b158..2a206e5 100644 --- a/src/hub/hub.c +++ b/src/hub/hub.c @@ -199,8 +199,6 @@ void hub_hub_disconnected(hub_t *h) { void hub_hub_init(hub_t *h, const char *nick, const char *desc, const char *mail, uint32_t conn, const char *encoding) { - static dbo_hub_vtable_t(hub_t) vt = dbo_hub_funcs(); - if(nick) h->h.props.MyNick = strdup(nick); else { @@ -223,8 +221,6 @@ void hub_hub_init(hub_t *h, const char *nick, const char *desc, const char *mail h->h.props.HubError = strdup(""); h->h.props.HubErrorMessage = strdup(""); h->h.props.HubRedirect = strdup(""); - - dbo_add_itf(&h->dbo, dbo_hub_interface, &vt, &h->h.props); } @@ -232,4 +228,8 @@ void hub_hub_destroy(hub_t *h) { dbo_hub_properties_free(&h->h.props); } + +const hub_hub_vt_t hub_hub_vt = dbo_hub_funcs(); + + /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/hub.h b/src/hub/hub.h index f5fe798..7425ccd 100644 --- a/src/hub/hub.h +++ b/src/hub/hub.h @@ -80,5 +80,8 @@ void hub_hub_disconnected(hub_t *h); void hub_hub_init(hub_t *h, const char *nick, const char *desc, const char *mail, uint32_t conn, const char *encoding); void hub_hub_destroy(hub_t *h); +typedef dbo_hub_vtable_t(hub_t) hub_hub_vt_t; +extern const hub_hub_vt_t hub_hub_vt; + #endif /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/manager.c b/src/hub/manager.c index 8490b8f..f18c9ba 100644 --- a/src/hub/manager.c +++ b/src/hub/manager.c @@ -92,6 +92,13 @@ static int hub_newid() { /* Create a hub object and register it with dbo */ static void hub_create(int id, sqlasync_result_t *r) { + static const dbo_reg_t regs[] = { + { &dbo_hub_interface, (void**)&hub_hub_vt, offsetof(hub_t, h.props) }, + { &dbo_hubc_interface, (void**)&hub_conn_vt, offsetof(hub_t, c.props) }, + { &dbo_hubu_interface, (void**)&hub_users_vt, offsetof(hub_t, u.props) }, + { &dbo_hubch_interface, (void**)&hub_chat_vt, offsetof(hub_t, ch.props) } + }; + char path[50]; assert(snprintf(path, sizeof(path), HUB_BASE_PATH"/%d", id) < (int)sizeof(path)); yinfo("Creating hub object: %d", id); @@ -105,7 +112,7 @@ static void hub_create(int id, sqlasync_result_t *r) { h->id = id; h->type = HUBM_HNONE; h->proto = hub_adc; - dbo_register(&h->dbo, path); + dbo_register(&h->dbo, path, regs, sizeof(regs)/sizeof(*regs)); #define INTVAL(v, min, max, def) (v.type == SQLITE_INTEGER && v.val.i64 >= min && v.val.i64 <= max ? v.val.i64 : def) @@ -254,13 +261,12 @@ static void hub_manager_load() { void hub_manager_create(bool autoconnect) { - static dbo_hubm_vtable_t(obj_t) vt = dbo_hubm_funcs(); + static const dbo_hubm_vtable_t(obj_t) vt = dbo_hubm_funcs(); + static const dbo_reg_t reg = { &dbo_hubm_interface, (void**)&vt, offsetof(obj_t, props) }; manager.props.NormalHubs = manager.props.RegHubs = manager.props.OpHubs = 0; - dbo_register(&manager.dbo, MANAGER_PATH); - dbo_add_itf(&manager.dbo, dbo_hubm_interface, &vt, &manager.props); - + dbo_register(&manager.dbo, MANAGER_PATH, ®, 1); hub_manager_load(); int i; diff --git a/src/hub/users.c b/src/hub/users.c index 940d410..9415136 100644 --- a/src/hub/users.c +++ b/src/hub/users.c @@ -175,7 +175,7 @@ static void hub_users_emitcounts(hub_t *h) { char *strval; DBusMessage *msg = dbus_message_new_signal(h->dbo.path, DBUS_INTERFACE_PROPERTIES, "PropertiesChanged"); dbus_message_iter_init_append(msg, &iter); - dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &dbo_hubu_interface->name); + dbus_message_iter_append_basic(&iter, DBUS_TYPE_STRING, &dbo_hubu_interface.name); dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub); strval = "UserCount"; @@ -505,8 +505,6 @@ void hub_users_disconnected(hub_t *h) { void hub_users_init(hub_t *h, const char *salt) { - static dbo_hubu_vtable_t(hub_t) vt = dbo_hubu_funcs(); - h->u.delmark = true; h->u.needsweep = false; h->u.list = kh_init(userlist); @@ -528,7 +526,6 @@ void hub_users_init(hub_t *h, const char *salt) { h->u.props.UserListComplete = false; h->u.props.UserShareSize = 0; h->u.props.UserCount = 0; - dbo_add_itf(&h->dbo, dbo_hubu_interface, &vt, &h->u.props); } @@ -548,4 +545,7 @@ void hub_users_destroy(hub_t *h) { kh_destroy(ulistsid, h->u.sids); } +const hub_users_vt_t hub_users_vt = dbo_hubu_funcs(); + + /* vim: set noet sw=4 ts=4: */ diff --git a/src/hub/users.h b/src/hub/users.h index e41c582..44e8aee 100644 --- a/src/hub/users.h +++ b/src/hub/users.h @@ -165,5 +165,8 @@ void hub_users_init(hub_t *h, const char *salt); void hub_users_shutdown(hub_t *h); void hub_users_destroy(hub_t *h); +typedef dbo_hubu_vtable_t(hub_t) hub_users_vt_t; +extern const hub_users_vt_t hub_users_vt; + #endif /* vim: set noet sw=4 ts=4: */ diff --git a/src/itfgen.pl b/src/itfgen.pl index 602cd29..681d35b 100755 --- a/src/itfgen.pl +++ b/src/itfgen.pl @@ -304,8 +304,8 @@ sub prop_setter { $r .= "\tbool changed = dbo_${itf}_$_->{name}_set_noemit(p, val);\n"; $r .= "\tif(changed)\n"; $r .= $_->{flags} =~ /emitchange/ - ? "\t\tdbo_prop_invalidate(((dbo_t *)obj)->path, dbo_${itf}_interface, $i);\n" - : "\t\tdbo_prop_changed(((dbo_t *)obj)->path, dbo_${itf}_interface, $i, p);\n"; + ? "\t\tdbo_prop_invalidate(((dbo_t *)obj)->path, &dbo_${itf}_interface, $i);\n" + : "\t\tdbo_prop_changed(((dbo_t *)obj)->path, &dbo_${itf}_interface, $i, p);\n"; $r .= "\treturn changed;\n"; } $r .= "}\n"; @@ -389,10 +389,9 @@ sub write_c_itf { } # The interface object - printf $F 'static const dbo_interface_t %s_interface = {"%s", %d, %d, %d, %1$s_methods, %1$s_signals, %s};'."\n", + printf $F 'const dbo_interface_t dbo_%s_interface = {"%s", %d, %d, %d, %1$s_methods, %1$s_signals, %s};'."\n", $itf, $itf{$itf}{name}, map((scalar @{$itf{$itf}{$_}}), qw|methods signals properties|), @{$itf{$itf}{properties}} ? "${itf}_properties" : 'NULL'; - printf $F "const dbo_interface_t *dbo_%s_interface = &%1\$s_interface;\n\n\n", $itf; } @@ -422,7 +421,7 @@ sub write_h_itf { my($F, $itf) = @_; # Interface object - printf $F "extern const dbo_interface_t *dbo_${itf}_interface;\n"; + printf $F "extern const dbo_interface_t dbo_${itf}_interface;\n"; # vtable print $F "#define dbo_${itf}_vtable_t(_obj_type)\\\n"; |