From 45f221c32f7c88e487fe260eefb3be8d1c2443af Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 16 Oct 2011 16:52:24 -0400 Subject: [PATCH] Move GTrashStack out of gutils.[hc] Reducing the mess in gutils, and moving docs inline at the same time. Double win. --- docs/reference/glib/tmpl/.gitignore | 1 + docs/reference/glib/tmpl/trash_stack.sgml | 77 ---------------- glib/Makefile.am | 2 + glib/glib.h | 1 + glib/gslice.c | 1 + glib/gtrashstack.c | 94 ++++++++++++++++++++ glib/gtrashstack.h | 103 ++++++++++++++++++++++ glib/gutils.h | 61 ------------- 8 files changed, 202 insertions(+), 138 deletions(-) delete mode 100644 docs/reference/glib/tmpl/trash_stack.sgml create mode 100644 glib/gtrashstack.c create mode 100644 glib/gtrashstack.h diff --git a/docs/reference/glib/tmpl/.gitignore b/docs/reference/glib/tmpl/.gitignore index 661ce928c..9a533ae2f 100644 --- a/docs/reference/glib/tmpl/.gitignore +++ b/docs/reference/glib/tmpl/.gitignore @@ -53,6 +53,7 @@ threads-deprecated.sgml threads.sgml timers.sgml timezone.sgml +trash_stack.sgml trees-binary.sgml trees-nary.sgml unicode.sgml diff --git a/docs/reference/glib/tmpl/trash_stack.sgml b/docs/reference/glib/tmpl/trash_stack.sgml deleted file mode 100644 index d9c3d7c24..000000000 --- a/docs/reference/glib/tmpl/trash_stack.sgml +++ /dev/null @@ -1,77 +0,0 @@ - -Trash Stacks - - -maintain a stack of unused allocated memory chunks - - - -A #GTrashStack is an efficient way to keep a stack of unused allocated -memory chunks. Each memory chunk is required to be large enough to hold -a #gpointer. This allows the stack to be maintained without any space -overhead, since the stack pointers can be stored inside the memory chunks. - - -There is no function to create a #GTrashStack. A %NULL #GTrashStack* -is a perfectly valid empty stack. - - - - - - - - - - - - - - - -Each piece of memory that is pushed onto the stack -is cast to a GTrashStack*. - - -@next: pointer to the previous element of the stack, -gets stored in the first sizeof (gpointer) -bytes of the element. - - - -Pushes a piece of memory onto a #GTrashStack. - - -@stack_p: a pointer to a #GTrashStack. -@data_p: the piece of memory to push on the stack. - - - - -Pops a piece of memory off a #GTrashStack. - - -@stack_p: a pointer to a #GTrashStack. -@Returns: the element at the top of the stack. - - - - -Returns the element at the top of a #GTrashStack which may be %NULL. - - -@stack_p: a pointer to a #GTrashStack. -@Returns: the element at the top of the stack. - - - - -Returns the height of a #GTrashStack. -Note that execution of this function is of O(N) complexity -where N denotes the number of items on the stack. - - -@stack_p: a pointer to a #GTrashStack. -@Returns: the height of the stack. - - diff --git a/glib/Makefile.am b/glib/Makefile.am index 8c0c24d15..5381f026f 100644 --- a/glib/Makefile.am +++ b/glib/Makefile.am @@ -187,6 +187,7 @@ libglib_2_0_la_SOURCES = \ gthreadpool.c \ gtimer.c \ gtimezone.c \ + gtrashstack.c \ gtree.c \ guniprop.c \ gutf8.c \ @@ -306,6 +307,7 @@ glibsubinclude_HEADERS = \ gthreadpool.h \ gtimer.h \ gtimezone.h \ + gtrashstack.h \ gtree.h \ gtypes.h \ gunicode.h \ diff --git a/glib/glib.h b/glib/glib.h index 546f2d1b8..58e4c0586 100644 --- a/glib/glib.h +++ b/glib/glib.h @@ -83,6 +83,7 @@ #include #include #include +#include #include #include #include diff --git a/glib/gslice.c b/glib/gslice.c index f89a3cdb4..396ce48af 100644 --- a/glib/gslice.c +++ b/glib/gslice.c @@ -48,6 +48,7 @@ #include "gmem.h" /* gslice.h */ #include "gstrfuncs.h" #include "gutils.h" +#include "gtrashstack.h" #include "gtestutils.h" #include "gthread.h" #include "glib_trace.h" diff --git a/glib/gtrashstack.c b/glib/gtrashstack.c new file mode 100644 index 000000000..71eca13e5 --- /dev/null +++ b/glib/gtrashstack.c @@ -0,0 +1,94 @@ +/* GLIB - Library of useful routines for C programming + * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GLib Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GLib Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GLib at ftp://ftp.gtk.org/pub/gtk/. + */ + +#include "config.h" + +/** + * SECTION:trash_stack + * @title: Trash Stacks + * @short_description: maintain a stack of unused allocated memory chunks + * + * A #GTrashStack is an efficient way to keep a stack of unused allocated + * memory chunks. Each memory chunk is required to be large enough to hold + * a #gpointer. This allows the stack to be maintained without any space + * overhead, since the stack pointers can be stored inside the memory chunks. + * + * There is no function to create a #GTrashStack. A %NULL #GTrashStack* + * is a perfectly valid empty stack. + */ + +/** + * GTrashStack: + * @next: pointer to the previous element of the stack, + * gets stored in the first sizeof (gpointer) + * bytes of the element + * + * Each piece of memory that is pushed onto the stack + * is cast to a GTrashStack*. + */ + +/** + * g_trash_stack_push: + * @stack_p: a #GTrashStack + * @data_p: the piece of memory to push on the stack + * + * Pushes a piece of memory onto a #GTrashStack. + */ + +/** + * g_trash_stack_pop: + * @stack_p: a #GTrashStack + * + * Pops a piece of memory off a #GTrashStack. + * + * Returns: the element at the top of the stack + */ + +/** + * g_trash_stack_peek: + * @stack_p: a #GTrashStack + * + * Returns the element at the top of a #GTrashStack + * which may be %NULL. + * + * Returns: the element at the top of the stack + */ + +/** + * g_trash_stack_height: + * @stack_p: a #GTrashStack + * + * Returns the height of a #GTrashStack. + * + * Note that execution of this function is of O(N) complexity + * where N denotes the number of items on the stack. + * + * Returns: the height of the stack + */ + +#define G_IMPLEMENT_INLINES 1 +#define __G_TRASH_STACK_C__ +#include "gtrashstack.h" diff --git a/glib/gtrashstack.h b/glib/gtrashstack.h new file mode 100644 index 000000000..3f226fe4c --- /dev/null +++ b/glib/gtrashstack.h @@ -0,0 +1,103 @@ +/* GLIB - Library of useful routines for C programming + * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* + * Modified by the GLib Team and others 1997-2000. See the AUTHORS + * file for a list of people on the GLib Team. See the ChangeLog + * files for a list of changes. These files are distributed with + * GLib at ftp://ftp.gtk.org/pub/gtk/. + */ + +#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) +#error "Only can be included directly." +#endif + +#ifndef __G_TRASH_STACK_H__ +#define __G_TRASH_STACK_H__ + +#include + +G_BEGIN_DECLS + +typedef struct _GTrashStack GTrashStack; +struct _GTrashStack +{ + GTrashStack *next; +}; + +G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p, + gpointer data_p); +G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p); +G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p); +G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p); + +#if defined (G_CAN_INLINE) || defined (__G_TRASH_STACK_C__) + +G_INLINE_FUNC void +g_trash_stack_push (GTrashStack **stack_p, + gpointer data_p) +{ + GTrashStack *data = (GTrashStack *) data_p; + + data->next = *stack_p; + *stack_p = data; +} +G_INLINE_FUNC gpointer +g_trash_stack_pop (GTrashStack **stack_p) +{ + GTrashStack *data; + + data = *stack_p; + if (data) + { + *stack_p = data->next; + /* NULLify private pointer here, most platforms store NULL as + * subsequent 0 bytes + */ + data->next = NULL; + } + + return data; +} +G_INLINE_FUNC gpointer +g_trash_stack_peek (GTrashStack **stack_p) +{ + GTrashStack *data; + + data = *stack_p; + + return data; +} +G_INLINE_FUNC guint +g_trash_stack_height (GTrashStack **stack_p) +{ + GTrashStack *data; + guint i = 0; + + for (data = *stack_p; data; data = data->next) + i++; + + return i; +} + +#endif /* G_CAN_INLINE || __G_TRASH_STACK_C__ */ + +G_END_DECLS + +#endif /* __G_UTILS_H__ */ diff --git a/glib/gutils.h b/glib/gutils.h index 2e7c76740..f4c038f04 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -304,21 +304,6 @@ G_INLINE_FUNC gint g_bit_nth_msf (gulong mask, gint nth_bit) G_GNUC_CONST; G_INLINE_FUNC guint g_bit_storage (gulong number) G_GNUC_CONST; -/* Trash Stacks - * elements need to be >= sizeof (gpointer) - */ -typedef struct _GTrashStack GTrashStack; -struct _GTrashStack -{ - GTrashStack *next; -}; - -G_INLINE_FUNC void g_trash_stack_push (GTrashStack **stack_p, - gpointer data_p); -G_INLINE_FUNC gpointer g_trash_stack_pop (GTrashStack **stack_p); -G_INLINE_FUNC gpointer g_trash_stack_peek (GTrashStack **stack_p); -G_INLINE_FUNC guint g_trash_stack_height (GTrashStack **stack_p); - /* inline function implementations */ #if defined (G_CAN_INLINE) || defined (__G_UTILS_C__) @@ -368,52 +353,6 @@ g_bit_storage (gulong number) return n_bits; #endif } -G_INLINE_FUNC void -g_trash_stack_push (GTrashStack **stack_p, - gpointer data_p) -{ - GTrashStack *data = (GTrashStack *) data_p; - - data->next = *stack_p; - *stack_p = data; -} -G_INLINE_FUNC gpointer -g_trash_stack_pop (GTrashStack **stack_p) -{ - GTrashStack *data; - - data = *stack_p; - if (data) - { - *stack_p = data->next; - /* NULLify private pointer here, most platforms store NULL as - * subsequent 0 bytes - */ - data->next = NULL; - } - - return data; -} -G_INLINE_FUNC gpointer -g_trash_stack_peek (GTrashStack **stack_p) -{ - GTrashStack *data; - - data = *stack_p; - - return data; -} -G_INLINE_FUNC guint -g_trash_stack_height (GTrashStack **stack_p) -{ - GTrashStack *data; - guint i = 0; - - for (data = *stack_p; data; data = data->next) - i++; - - return i; -} #endif /* G_CAN_INLINE || __G_UTILS_C__ */ /* Glib version.