diff options
author | Tim Janik <timj@gtk.org> | 2000-03-01 09:44:10 +0000 |
---|---|---|
committer | Tim Janik <timj@src.gnome.org> | 2000-03-01 09:44:10 +0000 |
commit | db8baf697889c80c0632d7cc91343c18a2647429 (patch) | |
tree | 0c33983550e73a88b4aa2d7f09bc905cf3e4c0b5 /glib | |
parent | d568a76ae4976e0aa01e26e868121a07b4e91338 (diff) | |
download | glib-db8baf697889c80c0632d7cc91343c18a2647429.tar.gz |
minor optimization.
Wed Mar 1 10:39:39 2000 Tim Janik <timj@gtk.org>
* gslist.c (g_slist_reverse): minor optimization.
* testglib.c (g_node_test): added a couple of tests for
g_node_copy().
* glib.h:
* gnode.c (g_node_copy): new function to copy subtrees,
supplied by dbsears@ix.netcom.com.
changed iterator to walk the children list backwards, so
we get down from O(n^2) to O(n).
* gnode.c (g_node_first_sibling): applied patch from
dbsears@ix.netcom.com to optimize access if node->parent
is present.
* gutils.c (g_get_any_init): backed out HAVE_PW_GECOS check around
assignment of g_real_name, sicne HAVE_PW_GECOS is never defined and
thus breaks the original code.
* merged changes from 1.2.7.
Sat Feb 19 19:43:29 2000 Tim Janik <timj@gtk.org>
* testgmodule.c (main): added test to check that not yet bound symbols
in shared libraries of the main module are retrievable, from David Gero.
Fri Jan 28 11:37:41 2000 Owen Taylor <otaylor@redhat.com>
Bug #4156 - Changes vaguely modelled after Scott Gifford's patch
* gtimer.c (g_timer_elapsed): Never report negative times -
clip times to 0.
* gmain.c (g_timeout_prepare): Guard against unexpected
clock shifts by never setting a timeout of more than
data->interval msecs.
Diffstat (limited to 'glib')
-rw-r--r-- | glib/glib.h | 23 | ||||
-rw-r--r-- | glib/gmain.c | 86 | ||||
-rw-r--r-- | glib/gnode.c | 21 | ||||
-rw-r--r-- | glib/gslist.c | 26 | ||||
-rw-r--r-- | glib/gstrfuncs.c | 4 | ||||
-rw-r--r-- | glib/gtimer.c | 9 | ||||
-rw-r--r-- | glib/gutils.c | 8 |
7 files changed, 101 insertions, 76 deletions
diff --git a/glib/glib.h b/glib/glib.h index bcbb53da2..463cd9a05 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -1159,6 +1159,7 @@ void g_node_pop_allocator (void); GNode* g_node_new (gpointer data); void g_node_destroy (GNode *root); void g_node_unlink (GNode *node); +GNode* g_node_copy (GNode *node); GNode* g_node_insert (GNode *parent, gint position, GNode *node); @@ -1598,24 +1599,22 @@ gchar* g_strjoin (const gchar *separator, */ gchar* g_strcompress (const gchar *source); -/* Copy a string escaping nonprintable characters like in C strings. - * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points - * to a string containing characters that are not to be escaped. - */ -gchar* g_strescape (const gchar *source, - const gchar *exceptions); -/* - * Convert between the operating system (or C runtime) +/* Convert between the operating system (or C runtime) * representation of file names and UTF-8. */ gchar* g_filename_to_utf8 (const gchar *opsysstring); gchar* g_filename_from_utf8 (const gchar *utf8string); -/* Deprecated API: - * gchar* g_strescape (const gchar *source); - * Luckily this function wasn't much used. - * Add a second NULL parameter in calls for mostly identical semantics. +/* Copy a string escaping nonprintable characters like in C strings. + * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points + * to a string containing characters that are not to be escaped. + * + * Deprecated API: gchar* g_strescape (const gchar *source); + * Luckily this function wasn't used much, using NULL as second parameter + * provides mostly identical semantics. */ +gchar* g_strescape (const gchar *source, + const gchar *exceptions); gpointer g_memdup (gconstpointer mem, guint byte_size); diff --git a/glib/gmain.c b/glib/gmain.c index a16021b3b..a3b18340c 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -1236,21 +1236,49 @@ g_main_wakeup (void) /* Timeouts */ -static gboolean -g_timeout_prepare (gpointer source_data, +static void +g_timeout_set_expiration (GTimeoutData *data, + GTimeVal *current_time) +{ + guint seconds = data->interval / 1000; + guint msecs = data->interval - seconds * 1000; + + data->expiration.tv_sec = current_time->tv_sec + seconds; + data->expiration.tv_usec = current_time->tv_usec + msecs * 1000; + if (data->expiration.tv_usec >= 1000000) + { + data->expiration.tv_usec -= 1000000; + data->expiration.tv_sec++; + } +} + +static gboolean +g_timeout_prepare (gpointer source_data, GTimeVal *current_time, gint *timeout, gpointer user_data) { glong msec; GTimeoutData *data = source_data; - - msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 + - (data->expiration.tv_usec - current_time->tv_usec) / 1000; - - *timeout = (msec <= 0) ? 0 : msec; - - return (msec <= 0); + + msec = ((data->expiration.tv_sec - current_time->tv_sec) * 1000 + + (data->expiration.tv_usec - current_time->tv_usec) / 1000); + + if (msec < 0) + msec = 0; + else if (msec > data->interval) + { + /* The system time has been set backwards, so we + * reset the expiration time to now + data->interval; + * this at least avoids hanging for long periods of time. + */ + g_timeout_set_expiration (data, current_time); + msec = data->interval; + } + + *timeout = msec; + + return msec == 0; } static gboolean @@ -1259,14 +1287,14 @@ g_timeout_check (gpointer source_data, gpointer user_data) { GTimeoutData *data = source_data; - - return (data->expiration.tv_sec < current_time->tv_sec) || - ((data->expiration.tv_sec == current_time->tv_sec) && - (data->expiration.tv_usec <= current_time->tv_usec)); + + return ((data->expiration.tv_sec < current_time->tv_sec) || + ((data->expiration.tv_sec == current_time->tv_sec) && + (data->expiration.tv_usec <= current_time->tv_usec))); } static gboolean -g_timeout_dispatch (gpointer source_data, +g_timeout_dispatch (gpointer source_data, GTimeVal *dispatch_time, gpointer user_data) { @@ -1274,47 +1302,29 @@ g_timeout_dispatch (gpointer source_data, if (data->callback (user_data)) { - guint seconds = data->interval / 1000; - guint msecs = data->interval - seconds * 1000; + g_timeout_set_expiration (data, dispatch_time); - data->expiration.tv_sec = dispatch_time->tv_sec + seconds; - data->expiration.tv_usec = dispatch_time->tv_usec + msecs * 1000; - if (data->expiration.tv_usec >= 1000000) - { - data->expiration.tv_usec -= 1000000; - data->expiration.tv_sec++; - } return TRUE; } else return FALSE; } -guint +guint g_timeout_add_full (gint priority, - guint interval, + guint interval, GSourceFunc function, gpointer data, GDestroyNotify notify) { - guint seconds; - guint msecs; GTimeoutData *timeout_data = g_new (GTimeoutData, 1); + GTimeVal current_time; timeout_data->interval = interval; timeout_data->callback = function; - g_get_current_time (&timeout_data->expiration); - - seconds = timeout_data->interval / 1000; - msecs = timeout_data->interval - seconds * 1000; + g_get_current_time (¤t_time); - timeout_data->expiration.tv_sec += seconds; - timeout_data->expiration.tv_usec += msecs * 1000; - if (timeout_data->expiration.tv_usec >= 1000000) - { - timeout_data->expiration.tv_usec -= 1000000; - timeout_data->expiration.tv_sec++; - } + g_timeout_set_expiration (timeout_data, ¤t_time); return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify); } diff --git a/glib/gnode.c b/glib/gnode.c index 5145c6bd6..5db03c639 100644 --- a/glib/gnode.c +++ b/glib/gnode.c @@ -190,6 +190,24 @@ g_node_unlink (GNode *node) } GNode* +g_node_copy (GNode *node) +{ + GNode *new_node = NULL; + + if (node) + { + GNode *child; + + new_node = g_node_new (node->data); + + for (child = g_node_last_child (node); child; child = child->prev) + g_node_prepend (new_node, g_node_copy (child)); + } + + return new_node; +} + +GNode* g_node_insert (GNode *parent, gint position, GNode *node) @@ -917,6 +935,9 @@ g_node_first_sibling (GNode *node) { g_return_val_if_fail (node != NULL, NULL); + if (node->parent) + return node->parent->children; + while (node->prev) node = node->prev; diff --git a/glib/gslist.c b/glib/gslist.c index 9e7c88f34..2f01673b0 100644 --- a/glib/gslist.c +++ b/glib/gslist.c @@ -377,32 +377,26 @@ g_slist_copy (GSList *list) GSList* g_slist_reverse (GSList *list) { - GSList *tmp; - GSList *prev; - GSList *last; - - last = NULL; - prev = NULL; - + GSList *prev = NULL; + GSList *next = NULL; + while (list) { - last = list; - - tmp = list->next; + next = list->next; list->next = prev; - + prev = list; - list = tmp; + list = next; } - - return last; + + return prev; } GSList* g_slist_nth (GSList *list, guint n) { - while ((n-- > 0) && list) + while (n-- > 0 && list) list = list->next; return list; @@ -412,7 +406,7 @@ gpointer g_slist_nth_data (GSList *list, guint n) { - while ((n-- > 0) && list) + while (n-- > 0 && list) list = list->next; return list ? list->data : NULL; diff --git a/glib/gstrfuncs.c b/glib/gstrfuncs.c index 6df775774..927aca58e 100644 --- a/glib/gstrfuncs.c +++ b/glib/gstrfuncs.c @@ -1291,8 +1291,6 @@ g_filename_from_utf8 (const gchar *utf8string) #endif } - -/* blame Elliot for these next five routines */ gchar* g_strchug (gchar *string) { @@ -1303,7 +1301,7 @@ g_strchug (gchar *string) for (start = string; *start && isspace (*start); start++) ; - g_memmove(string, start, strlen(start) + 1); + g_memmove (string, start, strlen( start) + 1); return string; } diff --git a/glib/gtimer.c b/glib/gtimer.c index 6850e585a..d613a5094 100644 --- a/glib/gtimer.c +++ b/glib/gtimer.c @@ -184,9 +184,16 @@ g_timer_elapsed (GTimer *timer, elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec; total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6); + if (total < 0) + { + total = 0; - if (microseconds) + if (microseconds) + *microseconds = 0; + } + else if (microseconds) *microseconds = elapsed.tv_usec; + #endif /* !G_OS_WIN32 */ return total; diff --git a/glib/gutils.c b/glib/gutils.c index 0ec887a7d..62b4e54ef 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -515,8 +515,8 @@ g_get_any_init (void) */ if (error == 0 || error == ENOENT) { - g_warning ("getpwuid_r(): failed due to: " - "No such user: %lu.", (unsigned long)getuid ()); + g_warning ("getpwuid_r(): failed due to unknown user id (%lu)", + (gulong) getuid ()); break; } if (bufsize > 32 * 1024) @@ -541,11 +541,7 @@ g_get_any_init (void) if (pw) { g_user_name = g_strdup (pw->pw_name); -#ifdef HAVE_PW_GECOS g_real_name = g_strdup (pw->pw_gecos); -#else - g_real_name = g_strdup (g_user_name); -#endif if (!g_home_dir) g_home_dir = g_strdup (pw->pw_dir); } |