Imported Upstream version 2.74.3
[platform/upstream/glib.git] / glib / gtrashstack.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 /*
21  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GLib Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 /* we know we are deprecated here, no need for warnings */
30 #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
31 #define GLIB_DISABLE_DEPRECATION_WARNINGS
32 #endif
33
34 #include "gtrashstack.h"
35
36 /**
37  * SECTION:trash_stack
38  * @title: Trash Stacks
39  * @short_description: maintain a stack of unused allocated memory chunks
40  *
41  * A #GTrashStack is an efficient way to keep a stack of unused allocated
42  * memory chunks. Each memory chunk is required to be large enough to hold
43  * a #gpointer. This allows the stack to be maintained without any space
44  * overhead, since the stack pointers can be stored inside the memory chunks.
45  *
46  * There is no function to create a #GTrashStack. A %NULL #GTrashStack*
47  * is a perfectly valid empty stack.
48  *
49  * There is no longer any good reason to use #GTrashStack.  If you have
50  * extra pieces of memory, free() them and allocate them again later.
51  *
52  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
53  */
54
55 /**
56  * GTrashStack:
57  * @next: pointer to the previous element of the stack,
58  *     gets stored in the first `sizeof (gpointer)`
59  *     bytes of the element
60  *
61  * Each piece of memory that is pushed onto the stack
62  * is cast to a GTrashStack*.
63  *
64  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
65  */
66
67 /**
68  * g_trash_stack_push:
69  * @stack_p: a #GTrashStack
70  * @data_p: (not nullable): the piece of memory to push on the stack
71  *
72  * Pushes a piece of memory onto a #GTrashStack.
73  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
74  */
75 void
76 g_trash_stack_push (GTrashStack **stack_p,
77                     gpointer      data_p)
78 {
79   GTrashStack *data = (GTrashStack *) data_p;
80
81   data->next = *stack_p;
82   *stack_p = data;
83 }
84
85 /**
86  * g_trash_stack_pop:
87  * @stack_p: a #GTrashStack
88  *
89  * Pops a piece of memory off a #GTrashStack.
90  *
91  * Returns: the element at the top of the stack
92  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
93  */
94 gpointer
95 g_trash_stack_pop (GTrashStack **stack_p)
96 {
97   GTrashStack *data;
98
99   data = *stack_p;
100   if (data)
101     {
102       *stack_p = data->next;
103       /* NULLify private pointer here, most platforms store NULL as
104        * subsequent 0 bytes
105        */
106       data->next = NULL;
107     }
108
109   return data;
110 }
111
112 /**
113  * g_trash_stack_peek:
114  * @stack_p: a #GTrashStack
115  *
116  * Returns the element at the top of a #GTrashStack
117  * which may be %NULL.
118  *
119  * Returns: the element at the top of the stack
120  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
121  */
122 gpointer
123 g_trash_stack_peek (GTrashStack **stack_p)
124 {
125   GTrashStack *data;
126
127   data = *stack_p;
128
129   return data;
130 }
131
132 /**
133  * g_trash_stack_height:
134  * @stack_p: a #GTrashStack
135  *
136  * Returns the height of a #GTrashStack.
137  *
138  * Note that execution of this function is of O(N) complexity
139  * where N denotes the number of items on the stack.
140  *
141  * Returns: the height of the stack
142  * Deprecated: 2.48: #GTrashStack is deprecated without replacement
143  */
144 guint
145 g_trash_stack_height (GTrashStack **stack_p)
146 {
147   GTrashStack *data;
148   guint i = 0;
149
150   for (data = *stack_p; data; data = data->next)
151     i++;
152
153   return i;
154 }