| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 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 |