diff options
author | Martin Baulig <baulig@suse.de> | 2000-11-20 22:37:15 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 2000-11-20 22:37:15 +0000 |
commit | 752d5b47d2134cd6e76465943aadcdcb0ea5e777 (patch) | |
tree | 062defcfdaa4a83273e1c0112025f725a74f4332 /lib | |
parent | c15ff3f342e7f5eb74a5f5867ebe87c89d9a5cb0 (diff) | |
download | libgtop-752d5b47d2134cd6e76465943aadcdcb0ea5e777.tar.gz |
Started with the big rewrite for GNOME 2.0:
2000-11-20 Martin Baulig <baulig@suse.de>
Started with the big rewrite for GNOME 2.0:
- split the `glibtop' structure into `glibtop_server' and
`glibtop_client' and make `glibtop_client' a GObject.
- cleanly separate the library and the backends.
This stuff currently does not compile.
* glibtop.h (glibtop_server_private, glibtop_server_info):
Removed typedefs.
(struct _glibtop, struct _glibtop_server_info): Removed.
(glibtop_global_server, glibtop_server_features): Removed.
(glibtop_server_ref, glibtop_server_unref): Removed.
* glibtop.h (glibtop_init_s): First argument is now a
`glibtop_server *'.
* include/glibtop/*.h: Removed all #defines with the
glibtop_global_server.
(glibtop_get_*_l): First argument is now a `glibtop_client *'.
(glibtop_get_*_s, glibtop_init_*_s): First argument is now a
`glibtop_server *'.
* lib/glibtop-client.c: New file.
* sysdeps/common/xmalloc.c: Moved to lib/.
* sysdeps/common/error.c: Moved to lib/.
* lib/xmalloc.c: Moved here from sysdeps/common/.
* lib/error.c: Moved here from sysdeps/common/.
* include/glibtop/backend.h
(glibtop_backend_open_func_t, glibtop_backend_close_func_t):
First argument is now a `glibtop_server *'.
(glibtop_backend_info): Added `glibtop_server *server'.
(glibtop_open_backend_l): Returns `glibtop_backend *', first
argument is `glibtop_client *' and added `GError **'.
* include/glibtop/close.h (glibtop_close_s, glibtop_close_p):
First argument is now a `glibtop_server *'.
* include/glibtop/error.h (*):
First argument is now a `glibtop_server *'.
* include/glibtop/errors.h: Switched this to use GError.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 13 | ||||
-rw-r--r-- | lib/error.c | 170 | ||||
-rw-r--r-- | lib/errors.c | 148 | ||||
-rw-r--r-- | lib/glib-arrays.c | 27 | ||||
-rw-r--r-- | lib/glibtop-client.c | 106 | ||||
-rw-r--r-- | lib/init.c | 97 | ||||
-rw-r--r-- | lib/open-backend.c | 110 | ||||
-rw-r--r-- | lib/parameter.c | 6 | ||||
-rw-r--r-- | lib/xmalloc.c | 98 |
9 files changed, 553 insertions, 222 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index e09b2cf3..9f7a2318 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -2,12 +2,15 @@ LINK = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -o $@ INCLUDES = @INCLUDES@ -lib_LTLIBRARIES = libgtop.la +lib_LTLIBRARIES = libgtop.la libgtop_server.la -libgtop_la_SOURCES = init.c open.c close.c lib.c parameter.c \ - sysdeps.c errors.c glib-arrays.c \ - backend.c init-backends.c open-backend.c \ - sysdeps-init.c +libgtop_la_SOURCES = errors.c glib-arrays.c backend.c \ + init-backends.c open-backend.c \ + glibtop-client.c + +libgtop_server_la_SOURCES = error.c xmalloc.c + +libgtop_server_la_LDFLAGS = $(LT_VERSION_INFO) libgtop_la_LDFLAGS = $(LT_VERSION_INFO) libgtop_la_LIBADD = $(LIBGTOP_XML_LIB) diff --git a/lib/error.c b/lib/error.c new file mode 100644 index 00000000..9b2abe7e --- /dev/null +++ b/lib/error.c @@ -0,0 +1,170 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <glibtop/error.h> + +#define DEFAULT_NAME "LibGTop-Server" + +/* Prints error message and exits. */ + +static void +print_server_name (glibtop_server *server) +{ + fprintf (stderr, "%s: ", server ? + (server->name ? server->name : DEFAULT_NAME) + : DEFAULT_NAME); +} + +void +glibtop_error_vr (glibtop_server *server, char *format, va_list args) +{ + print_server_name (server); + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); + +#ifdef LIBGTOP_ENABLE_DEBUG + abort (); +#else + exit (1); +#endif +} + +void +glibtop_error_io_vr (glibtop_server *server, char *format, int error, va_list args) +{ + print_server_name (server); + vfprintf (stderr, format, args); + fprintf (stderr, ": %s\n", strerror (error)); + +#ifdef LIBGTOP_ENABLE_DEBUG + abort (); +#else + exit (1); +#endif +} + +void +glibtop_warn_vr (glibtop_server *server, char *format, va_list args) +{ + print_server_name (server); + vfprintf (stderr, format, args); + fprintf (stderr, "\n"); + +#ifdef LIBGTOP_FATAL_WARNINGS + abort (); +#endif +} + +void +glibtop_warn_io_vr (glibtop_server *server, char *format, int error, va_list args) +{ + print_server_name (server); + vfprintf (stderr, format, args); + fprintf (stderr, ": %s\n", strerror (error)); + +#ifdef LIBGTOP_FATAL_WARNINGS + abort (); +#endif +} + +void +glibtop_error_r (glibtop_server *server, char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_error_vr (server, format, args); + va_end (args); +} + +void +glibtop_warn_r (glibtop_server *server, char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_warn_vr (server, format, args); + va_end (args); +} + +void +glibtop_error_io_r (glibtop_server *server, char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_error_io_vr (server, format, errno, args); + va_end (args); +} + +void +glibtop_warn_io_r (glibtop_server *server, char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_warn_io_vr (server, format, errno, args); + va_end (args); +} + +#ifndef __GNUC__ + +static void +glibtop_error (char *format, ...) +{ + va_list args; + va_start (args, format); + glibtop_error_vr (glibtop_global_server, format, args); + va_end (args); +} + +static void +glibtop_warn (char *format, ...) +{ + va_list args; + va_start (args, format); + glibtop_warn_vr (glibtop_global_server, format, args); + va_end (args); +} + +static void +glibtop_error_io (char *format, ...) +{ + va_list args; + va_start (args, format); + glibtop_error_io_vr (glibtop_global_server, format, errno, args); + va_end (args); +} + +static void +glibtop_warn_io (char *format, ...) +{ + va_list args; + va_start (args, format); + glibtop_warn_io_vr (glibtop_global_server, format, errno, args); + va_end (args); +} + +#endif /* no __GNUC__ */ diff --git a/lib/errors.c b/lib/errors.c index 56205153..f4c892a7 100644 --- a/lib/errors.c +++ b/lib/errors.c @@ -25,42 +25,132 @@ #include <glibtop.h> #include <glibtop/global.h> -#include <glibtop/xmalloc.h> - -const char *glibtop_error_strings[GLIBTOP_MAX_ERROR] = { - N_("No error"), - N_("Unknown error"), - N_("Invalid argument"), - N_("No such parameter"), - N_("Attempted to modify a read-only value"), - N_("Parameter size mismatch"), - N_("Communication with LibGTop server failed"), - N_("No such process"), - N_("No kernel support"), - N_("Incompatible kernel version") -}; - -char * -glibtop_get_error_string_l (glibtop *server, unsigned error_number) +#include <glibtop/errors.h> + +/* Error quark */ +GQuark +glibtop_error_quark (void) +{ + static GQuark q = 0; + if (q == 0) + q = g_quark_from_static_string ("glibtop-error-quark"); + + return q; +} + +void +glibtop_error_vl (glibtop_client *client, glibtop_error code, + const char *format, va_list args) +{ + gchar *message; + GError *error; + + g_return_if_fail (GLIBTOP_IS_CLIENT (client)); + message = g_strdup_vprintf (format, args); + + error = g_error_new_literal (GLIBTOP_ERROR, code, message); + glibtop_client_propagate_error (client, error); + + g_error_free (error); + g_free (message); +} + +void +glibtop_error_io_vl (glibtop_client *client, glibtop_error code, + int io_errno, const char *format, va_list args) { - if (error_number >= GLIBTOP_MAX_ERROR) - error_number = GLIBTOP_ERROR_UNKNOWN; + gchar *message, *fullmessage; + GError *error; + + g_return_if_fail (GLIBTOP_IS_CLIENT (client)); + message = g_strdup_vprintf (format, args); + fullmessage = g_strdup_printf ("%s: %s", message, strerror (io_errno)); - return glibtop_strdup_r (server, _(glibtop_error_strings [error_number])); + error = g_error_new_literal (GLIBTOP_ERROR, code, message); + glibtop_client_propagate_error (client, error); + + g_error_free (error); + g_free (fullmessage); + g_free (message); } -unsigned -glibtop_get_errno_l (glibtop *server) +void +glibtop_warn_vl (glibtop_client *client, glibtop_error code, + const char *format, va_list args) { - return server->glibtop_errno; + gchar *message; + GError *error; + + g_return_if_fail (GLIBTOP_IS_CLIENT (client)); + message = g_strdup_vprintf (format, args); + + error = g_error_new_literal (GLIBTOP_ERROR, code, message); + glibtop_client_propagate_warning (client, error); + + g_error_free (error); + g_free (message); } -unsigned -glibtop_clear_errno_l (glibtop *server) +void +glibtop_warn_io_vl (glibtop_client *client, glibtop_error code, + int io_errno, const char *format, va_list args) { - unsigned old_errno; + gchar *message, *fullmessage; + GError *error; + + g_return_if_fail (GLIBTOP_IS_CLIENT (client)); + message = g_strdup_vprintf (format, args); + fullmessage = g_strdup_printf ("%s: %s", message, strerror (io_errno)); + + error = g_error_new_literal (GLIBTOP_ERROR, code, message); + glibtop_client_propagate_warning (client, error); - old_errno = server->glibtop_errno; - server->glibtop_errno = 0; - return old_errno; + g_error_free (error); + g_free (fullmessage); + g_free (message); } + +void +glibtop_error_l (glibtop_client *client, glibtop_error code, + char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_error_vl (client, code, format, args); + va_end (args); +} + +void +glibtop_warn_l (glibtop_client *client, glibtop_error code, + char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_warn_vl (client, code, format, args); + va_end (args); +} + +void +glibtop_error_io_l (glibtop_client *client, glibtop_error code, + char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_error_io_vl (client, code, errno, format, args); + va_end (args); +} + +void +glibtop_warn_io_l (glibtop_client *client, glibtop_error code, + char *format, ...) +{ + va_list args; + + va_start (args, format); + glibtop_warn_io_vl (client, code, errno, format, args); + va_end (args); +} + diff --git a/lib/glib-arrays.c b/lib/glib-arrays.c index d0310eb6..13c80422 100644 --- a/lib/glib-arrays.c +++ b/lib/glib-arrays.c @@ -29,14 +29,15 @@ #include <glibtop/glib-arrays.h> GArray * -glibtop_get_proclist_as_array_l (glibtop *server, int64_t which, int64_t arg) +glibtop_get_proclist_as_array_l (glibtop_client *client, int64_t which, + int64_t arg) { glibtop_array array; GArray *retval; unsigned *ptr; int i; - ptr = glibtop_get_proclist_l (server, &array, which, arg); + ptr = glibtop_get_proclist_l (client, &array, which, arg); if (!ptr) return NULL; retval = g_array_new (FALSE, TRUE, sizeof (guint)); @@ -45,20 +46,20 @@ glibtop_get_proclist_as_array_l (glibtop *server, int64_t which, int64_t arg) for (i = 0; i < array.number; i++) g_array_index (retval, guint, i) = ptr [i]; - glibtop_free_r (server, ptr); + g_free (ptr); return retval; } GPtrArray * -glibtop_get_proc_args_as_array_l (glibtop *server, pid_t pid) +glibtop_get_proc_args_as_array_l (glibtop_client *client, pid_t pid) { glibtop_array array; GPtrArray *retval; char **ptr; int i; - ptr = glibtop_get_proc_args_l (server, &array, pid); + ptr = glibtop_get_proc_args_l (client, &array, pid); if (!ptr) return NULL; retval = g_ptr_array_new (); @@ -66,23 +67,23 @@ glibtop_get_proc_args_as_array_l (glibtop *server, pid_t pid) for (i = 0; i < array.number; i++) { retval->pdata [i] = g_strdup (ptr [i]); - glibtop_free_r (server, ptr [i]); + g_free (ptr [i]); } - glibtop_free_r (server, ptr); + g_free (ptr); return retval; } GPtrArray * -glibtop_get_proc_map_as_array_l (glibtop *server, pid_t pid) +glibtop_get_proc_map_as_array_l (glibtop_client *client, pid_t pid) { glibtop_array array; GPtrArray *retval; glibtop_map_entry *ptr; int i; - ptr = glibtop_get_proc_map_l (server, &array, pid); + ptr = glibtop_get_proc_map_l (client, &array, pid); if (!ptr) return NULL; retval = g_ptr_array_new (); @@ -91,20 +92,20 @@ glibtop_get_proc_map_as_array_l (glibtop *server, pid_t pid) for (i = 0; i < array.number; i++) retval->pdata [i] = g_memdup (ptr+i, sizeof (glibtop_map_entry)); - glibtop_free_r (server, ptr); + g_free (ptr); return retval; } GPtrArray * -glibtop_get_mountlist_as_array_l (glibtop *server, int all_fs) +glibtop_get_mountlist_as_array_l (glibtop_client *client, int all_fs) { glibtop_array array; GPtrArray *retval; glibtop_mountentry *ptr; int i; - ptr = glibtop_get_mountlist_l (server, &array, all_fs); + ptr = glibtop_get_mountlist_l (client, &array, all_fs); if (!ptr) return NULL; retval = g_ptr_array_new (); @@ -113,7 +114,7 @@ glibtop_get_mountlist_as_array_l (glibtop *server, int all_fs) for (i = 0; i < array.number; i++) retval->pdata [i] = g_memdup (ptr+i, sizeof (glibtop_mountentry)); - glibtop_free_r (server, ptr); + g_free (ptr); return retval; } diff --git a/lib/glibtop-client.c b/lib/glibtop-client.c new file mode 100644 index 00000000..f4f98024 --- /dev/null +++ b/lib/glibtop-client.c @@ -0,0 +1,106 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 2.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <glibtop.h> + +static void glibtop_client_class_init (glibtop_client_class *klass); +static void glibtop_client_init (glibtop_client *client); +static void glibtop_client_finalize (GObject *object); + +static gpointer parent_class = NULL; + + +/* Internal data */ + +struct _glibtop_client_private +{ +}; + +GType +glibtop_client_get_type (void) +{ + static GType glibtop_client_type = 0; + + if (!glibtop_client_type) { + static const GTypeInfo glibtop_client_info = { + sizeof (glibtop_client_class), + NULL, /* base_class_init */ + NULL, /* base_class_finalize */ + (GClassInitFunc) glibtop_client_class_init, + NULL, /* class_finalize */ + NULL, /* class_data */ + sizeof (glibtop_client), + 16, /* n_preallocs */ + (GInstanceInitFunc) glibtop_client_init, + }; + + glibtop_client_type = g_type_register_static + (G_TYPE_OBJECT, "glibtop_client", &glibtop_client_info, 0); + } + + return glibtop_client_type; +} + +static void +glibtop_client_class_init (glibtop_client_class *class) +{ + GObjectClass *gobject_class; + + gobject_class = (GObjectClass *) class; + + parent_class = g_type_class_ref (G_TYPE_OBJECT); + + gobject_class->finalize = glibtop_client_finalize; +} + +static void +glibtop_client_init (glibtop_client *glibtop) +{ + glibtop_client_private *priv; + + priv = g_new0 (glibtop_client_private, 1); + glibtop->_priv = priv; +} + +static void +glibtop_client_finalize (GObject *object) +{ + glibtop_client *glibtop; + glibtop_client_private *priv = NULL; + + glibtop = GLIBTOP_CLIENT (object); + priv = glibtop->_priv; + + g_free (priv); + + if (G_OBJECT_CLASS (parent_class)->finalize) + G_OBJECT_CLASS (parent_class)->finalize (object); +} + +glibtop_client * +glibtop_client_new (void) +{ + return g_object_new (GLIBTOP_TYPE_CLIENT, NULL); +} @@ -28,104 +28,9 @@ #include <glibtop/sysdeps.h> #include <glibtop/parameter.h> -#ifndef DEFAULT_PORT -#define DEFAULT_PORT 42800 -#endif - -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = &_glibtop_global_server; - void -glibtop_server_ref (glibtop *server) +glibtop_init_s (glibtop_server *server, unsigned long features, unsigned flags) { - if (server == NULL) - return; - - server->refcount++; -} - -void -glibtop_server_unref (glibtop *server) -{ - if (server == NULL) - return; - - if (!server->refcount) { - glibtop_warn_r (server, "Attempted to unref server " - "which refcount == 0"); - return; - } - - server->refcount--; -} - -glibtop * -glibtop_init_r (glibtop **server_ptr, unsigned long features, unsigned flags) -{ - glibtop *server; - - if (server_ptr == NULL) - return NULL; - - if (*server_ptr == NULL) - *server_ptr = glibtop_global_server; - - server = *server_ptr; - - /* Should we do the initialization? */ - - if (flags & GLIBTOP_INIT_NO_INIT) - return server; - - if (!server->info) { - server->info = g_new0 (glibtop_server_info, 1); - } - - /* Do the initialization, but only if not already initialized. */ - - if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) { - if (flags & GLIBTOP_FEATURES_EXCEPT) - features = ~features & GLIBTOP_SYSDEPS_ALL; - - if (features == 0) - features = GLIBTOP_SYSDEPS_ALL; - - if (flags & GLIBTOP_FEATURES_NO_SERVER) { - features = 0; - } - - server->info->features = features; - - server->flags |= _GLIBTOP_INIT_STATE_INIT; - } - - /* Should we open the server? */ - - if (flags & GLIBTOP_INIT_NO_OPEN) - return server; - - /* Open server, but only if not already opened. */ - - if ((server->flags & _GLIBTOP_INIT_STATE_OPEN) == 0) - glibtop_open_l (glibtop_global_server, "glibtop", - features, flags); - - return server; -} - -glibtop * -glibtop_init_s (glibtop **server_ptr, unsigned long features, unsigned flags) -{ - glibtop *server; - - if (server_ptr == NULL) - return NULL; - - if (*server_ptr == NULL) - *server_ptr = glibtop_global_server; - - server = *server_ptr; - /* Should we do the initialization? */ if (flags & GLIBTOP_INIT_NO_INIT) diff --git a/lib/open-backend.c b/lib/open-backend.c index 5dda16bb..9caae1cc 100644 --- a/lib/open-backend.c +++ b/lib/open-backend.c @@ -25,12 +25,9 @@ #include <glibtop.h> #include <glibtop/global.h> -#include <glibtop/xmalloc.h> #include <glibtop/backend.h> -#if LIBGTOP_USE_GMODULE - #include <gmodule.h> static void @@ -40,7 +37,8 @@ unload_module (gpointer data, gpointer user_data) } static int -load_extra_libs (glibtop *server, glibtop_backend_entry *entry) +load_extra_libs (glibtop_client *client, glibtop_backend_entry *entry, + GError **error) { GSList *list; GSList *loaded_here = NULL; @@ -51,9 +49,10 @@ load_extra_libs (glibtop *server, glibtop_backend_entry *entry) module = g_module_open (filename, G_MODULE_BIND_LAZY); if (!module) { - glibtop_warn_r (server, "Cannot open extra shared library `%s' " - "for backend `%s' (%s)", filename, entry->name, - g_module_error ()); + g_set_error (error, GLIBTOP_ERROR, GLIBTOP_ERROR_NO_SUCH_BACKEND, + "Cannot open extra shared library `%s' " + "for backend `%s' (%s)", filename, entry->name, + g_module_error ()); g_slist_foreach (loaded_here, unload_module, NULL); return -GLIBTOP_ERROR_NO_SUCH_BACKEND; } @@ -67,119 +66,78 @@ load_extra_libs (glibtop *server, glibtop_backend_entry *entry) return 0; } -#else /* not LIBGTOP_USE_GMODULE */ - -extern glibtop_backend_info LibGTopBackendInfo_Sysdeps; -extern glibtop_backend_info LibGTopBackendInfo_Common; -extern glibtop_backend_info LibGTopBackendInfo_Server; - -typedef struct { - const char *name; - glibtop_backend_info *backend_info; -} backend_init_table_entry; - -static backend_init_table_entry backend_init_table [] = { -#ifdef LIBGTOP_HAVE_SYSDEPS - { "glibtop-backend-sysdeps", &LibGTopBackendInfo_Sysdeps }, -#endif - { "glibtop-backend-common", &LibGTopBackendInfo_Common }, -#ifdef LIBGTOP_NEED_SERVER - { "glibtop-backend-server", &LibGTopBackendInfo_Server }, -#endif - { NULL, NULL } -}; - -static glibtop_backend_info * -find_backend_by_name (const char *backend_name) -{ - backend_init_table_entry *table; - - for (table = backend_init_table; table->name; table++) - if (!strcmp (backend_name, table->name)) - return table->backend_info; - - return NULL; -} - -#endif /* LIBGTOP_USE_GMODULE */ - -int -glibtop_open_backend_l (glibtop *server, const char *backend_name, - u_int64_t features, const char **backend_args) +glibtop_backend * +glibtop_open_backend_l (glibtop_client *client, const char *backend_name, + u_int64_t features, const char **backend_args, + GError **error) { const glibtop_backend_info *info; glibtop_backend_entry *entry; glibtop_backend *backend; + g_return_val_if_fail (GLIBTOP_IS_CLIENT (client), NULL); + entry = glibtop_backend_by_name (backend_name); - if (!entry) return -GLIBTOP_ERROR_NO_SUCH_BACKEND; + if (!entry) return NULL; if (!entry->_priv) { entry->_priv = g_new0 (glibtop_backend_module, 1); -#if LIBGTOP_USE_GMODULE if (entry->extra_libs) { int retval; - retval = load_extra_libs (server, entry); - if (retval < 0) return retval; + retval = load_extra_libs (client, entry, error); + if (retval < 0) return NULL; } entry->_priv->module = g_module_open (entry->shlib_name, G_MODULE_BIND_LAZY); if (!entry->_priv->module) { - glibtop_warn_r (server, "Cannot open shared library `%s' " - "for backend `%s' (%s)", entry->shlib_name, - entry->name, g_module_error ()); - return -GLIBTOP_ERROR_NO_SUCH_BACKEND; + g_set_error (error, GLIBTOP_ERROR, GLIBTOP_ERROR_NO_SUCH_BACKEND, + "Cannot open shared library `%s' " + "for backend `%s' (%s)", entry->shlib_name, + entry->name, g_module_error ()); + return NULL; } if (!g_module_symbol (entry->_priv->module, "LibGTopBackendInfo", (gpointer*) &entry->info)) { - glibtop_warn_r (server, "Library `%s' is not a valid " - "LibGTop Backend library (start symbol not found)", - entry->shlib_name); + g_set_error (error, GLIBTOP_ERROR, GLIBTOP_ERROR_NO_SUCH_BACKEND, + "Library `%s' is not a valid " + "LibGTop Backend library (start symbol not found)", + entry->shlib_name); g_module_close (entry->_priv->module); g_free (entry->_priv); entry->_priv = NULL; - return -GLIBTOP_ERROR_NO_SUCH_BACKEND; + return NULL; } -#else /* not LIBGTOP_USE_GMODULE */ - entry->info = find_backend_by_name (backend_name); - if (!entry->info) - return -GLIBTOP_ERROR_NO_SUCH_BACKEND; -#endif /* not LIBGTOP_USE_GMODULE */ } entry->_priv->refcount++; info = entry->info; - if (!info) return -GLIBTOP_ERROR_NO_SUCH_BACKEND; + if (!info) return NULL; - backend = glibtop_calloc_r (server, 1, sizeof (glibtop_backend)); + backend = g_new0 (glibtop_backend, 1); backend->_priv_module = entry->_priv; backend->info = info; - if (!server->_priv) - server->_priv = glibtop_calloc_r - (server, 1, sizeof (glibtop_server_private)); + backend->server = glibtop_server_new (); if (info->open) { int retval; - retval = info->open (server, backend, features, backend_args); + retval = info->open (backend->server, backend, features, backend_args); if (retval) { - glibtop_free_r (server, backend->_priv); - glibtop_free_r (server, backend); - return retval; + glibtop_server_unref (backend->server); + g_free (backend->_priv); + g_free (backend); + return NULL; } } - server->_priv->backend_list = g_slist_append - (server->_priv->backend_list, backend); - - return 0; + return backend; } diff --git a/lib/parameter.c b/lib/parameter.c index 4ec58c0b..745fb227 100644 --- a/lib/parameter.c +++ b/lib/parameter.c @@ -45,7 +45,7 @@ #define _strlen(ptr) (ptr ? strlen (ptr) : 0) ssize_t -glibtop_get_parameter_l (glibtop *server, const unsigned parameter, +glibtop_get_parameter_l (glibtop_client *client, const unsigned parameter, void *data_ptr, size_t data_size) { switch (parameter) { @@ -70,7 +70,7 @@ glibtop_get_parameter_l (glibtop *server, const unsigned parameter, } int -glibtop_get_parameter_size_l (glibtop *server, const unsigned parameter) +glibtop_get_parameter_size_l (glibtop_client *client, const unsigned parameter) { switch (parameter) { case GLIBTOP_PARAM_ERROR_METHOD: @@ -89,7 +89,7 @@ glibtop_get_parameter_size_l (glibtop *server, const unsigned parameter) } int -glibtop_set_parameter_l (glibtop *server, const unsigned parameter, +glibtop_set_parameter_l (glibtop_client *client, const unsigned parameter, const void *data_ptr, size_t data_size) { switch (parameter) { diff --git a/lib/xmalloc.c b/lib/xmalloc.c new file mode 100644 index 00000000..f46ea55b --- /dev/null +++ b/lib/xmalloc.c @@ -0,0 +1,98 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */ + +/* $Id$ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <glibtop/xmalloc.h> + +#ifdef LIBGTOP_USE_GLIB_MALLOC +#include <glib.h> +#endif + +/* Wrappers to malloc, calloc, realloc ... */ + +void * +glibtop_malloc_r (glibtop_server *server, size_t size) +{ +#ifdef LIBGTOP_USE_GLIB_MALLOC + return g_malloc0 (size); +#else + void *buf = malloc (size); + + if (!buf) + glibtop_error_io_r (server, "malloc %d bytes", size); + + return buf; +#endif +} + +void * +glibtop_calloc_r (glibtop_server *server, size_t nmemb, size_t size) +{ +#ifdef LIBGTOP_USE_GLIB_MALLOC + return g_malloc0 (size * nmemb); +#else + void *buf = calloc (nmemb, size); + + if (!buf) + glibtop_error_io_r (server, "calloc %d blocks (%d bytes each)", + nmemb, size); + + return buf; +#endif +} + +void * +glibtop_realloc_r (glibtop_server *server, void *ptr, size_t size) +{ +#ifdef LIBGTOP_USE_GLIB_MALLOC + return g_realloc (ptr, size); +#else + void *buf = realloc (ptr, size); + + if (!buf) + glibtop_error_io_r (server, "realloc %d bytes", size); + + return buf; +#endif +} + +char * +glibtop_strdup_r (glibtop_server *server, const char *string) +{ +#ifdef LIBGTOP_USE_GLIB_MALLOC + return g_strdup (string); +#else + return strcpy (glibtop_malloc_r (server, strlen (string) + 1), string); +#endif +} + +void +glibtop_free_r (glibtop_server *server, const void *ptr) +{ +#ifdef LIBGTOP_USE_GLIB_MALLOC + g_free (ptr); +#else + if (ptr) free ((void *) ptr); +#endif +} |