Debian Codegraph @

src/dex-stack-private.h

raw symbols SCIP
1 /* dex-stack-private.h
2 *
3 * Copyright 2022 Christian Hergert <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2.1 of the
8 * License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: LGPL-2.1-or-later
19 */
20
21 #pragma once
22
23 #include <glib.h>
24
25 G_BEGIN_DECLS
26
27 typedef struct _DexStack DexStack;
28 typedef struct _DexStackPool DexStackPool;
29
30 struct _DexStack
31 {
32 GList link;
33 gsize size;
34 #ifdef G_OS_UNIX
35 gpointer base;
36 gpointer guard;
37 gpointer ptr;
38 #endif
39 };
40
41 struct _DexStackPool
42 {
43 GMutex mutex;
44 GQueue stacks;
45 gsize stack_size;
46 guint min_pool_size;
47 guint max_pool_size;
48 guint mark_unused : 1;
49 };
50
51 DexStackPool *dex_stack_pool_new (gsize stack_size,
52 int min_pool_size,
53 int max_pool_size);
54 void dex_stack_pool_free (DexStackPool *stack_pool);
55 DexStack *dex_stack_new (gsize size);
56 void dex_stack_free (DexStack *stack);
57 void dex_stack_mark_unused (DexStack *stack);
58
59 static inline DexStack *
60 dex_stack_pool_acquire (DexStackPool *stack_pool)
61 {
62 DexStack *ret;
63
64 g_assert (stack_pool != NULL);
65
66 g_mutex_lock (&stack_pool->mutex);
67 if (stack_pool->stacks.length > 0)
68 {
69 ret = g_queue_pop_head_link (&stack_pool->stacks)->data;
70 g_mutex_unlock (&stack_pool->mutex);
71 }
72 else
73 {
74 g_mutex_unlock (&stack_pool->mutex);
75 ret = dex_stack_new (stack_pool->stack_size);
76 }
77
78 return ret;
79 }
80
81 static inline void
82 dex_stack_pool_release (DexStackPool *stack_pool,
83 DexStack *stack)
84 {
85 g_assert (stack_pool != NULL);
86 g_assert (stack->link.data == stack);
87 g_assert (stack->link.prev == NULL);
88 g_assert (stack->link.next == NULL);
89
90 g_mutex_lock (&stack_pool->mutex);
91 if (stack_pool->stacks.length > stack_pool->max_pool_size)
92 {
93 g_mutex_unlock (&stack_pool->mutex);
94 dex_stack_free (stack);
95 }
96 else
97 {
98 if (stack_pool->mark_unused)
99 dex_stack_mark_unused (stack);
100 g_queue_push_head_link (&stack_pool->stacks, &stack->link);
101 g_mutex_unlock (&stack_pool->mutex);
102 }
103 }
104
105 G_END_DECLS