| 1 |
/* |
| 2 |
* sysprof-allocator.c |
| 3 |
* |
| 4 |
* Copysysprofht 2024 Christian Hergert <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify |
| 7 |
* it under the terms of the GNU General Public License as published by |
| 8 |
* the Free Software Foundation, either version 3 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This program 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 |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include "sysprof-allocator-private.h" |
| 25 |
|
| 26 |
#define SYSPROF_ALLOCATOR_MIN_PAGE_SIZE (4*4096) |
| 27 |
|
| 28 |
typedef struct _SysprofPage |
| 29 |
{ |
| 30 |
struct _SysprofPage *next; |
| 31 |
guint8 *data; |
| 32 |
gsize avail; |
| 33 |
gsize len; |
| 34 |
} SysprofPage; |
| 35 |
|
| 36 |
struct _SysprofAllocator |
| 37 |
{ |
| 38 |
SysprofPage *pages; |
| 39 |
SysprofPage *current; |
| 40 |
GHashTable *strings; |
| 41 |
char empty[1]; |
| 42 |
}; |
| 43 |
|
| 44 |
static inline SysprofPage * |
| 45 |
sysprof_page_new (gsize size) |
| 46 |
{ |
| 47 |
SysprofPage *page = g_new0 (SysprofPage, 1); |
| 48 |
|
| 49 |
page->len = size; |
| 50 |
page->data = g_malloc (size); |
| 51 |
page->avail = page->len; |
| 52 |
page->next = NULL; |
| 53 |
|
| 54 |
return page; |
| 55 |
} |
| 56 |
|
| 57 |
SysprofAllocator * |
| 58 |
sysprof_allocator_new (void) |
| 59 |
{ |
| 60 |
SysprofAllocator *self; |
| 61 |
|
| 62 |
self = g_atomic_rc_box_new0 (SysprofAllocator); |
| 63 |
self->pages = sysprof_page_new (SYSPROF_ALLOCATOR_MIN_PAGE_SIZE); |
| 64 |
self->current = self->pages; |
| 65 |
self->strings = g_hash_table_new (g_str_hash, g_str_equal); |
| 66 |
|
| 67 |
return self; |
| 68 |
} |
| 69 |
|
| 70 |
SysprofAllocator * |
| 71 |
sysprof_allocator_ref (SysprofAllocator *self) |
| 72 |
{ |
| 73 |
return g_atomic_rc_box_acquire (self); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
sysprof_allocator_finalize (gpointer data) |
| 78 |
{ |
| 79 |
SysprofAllocator *self = data; |
| 80 |
SysprofPage *page; |
| 81 |
|
| 82 |
self->current = NULL; |
| 83 |
|
| 84 |
g_clear_pointer (&self->strings, g_hash_table_unref); |
| 85 |
|
| 86 |
while ((page = self->pages)) |
| 87 |
{ |
| 88 |
self->pages = page->next; |
| 89 |
|
| 90 |
g_free (page->data); |
| 91 |
g_free (page); |
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
void |
| 97 |
sysprof_allocator_unref (SysprofAllocator *self) |
| 98 |
{ |
| 99 |
g_atomic_rc_box_release_full (self, sysprof_allocator_finalize); |
| 100 |
} |
| 101 |
|
| 102 |
static inline gboolean |
| 103 |
sysprof_allocator_ispow2 (gsize x) |
| 104 |
{ |
| 105 |
return ((x != 0) && !(x & (x - 1))); |
| 106 |
} |
| 107 |
|
| 108 |
static inline gsize |
| 109 |
sysprof_allocator_nextpow2 (gsize x) |
| 110 |
{ |
| 111 |
return x == 1 ? 1 : 1 << (64 - __builtin_clzl (x - 1)); |
| 112 |
} |
| 113 |
|
| 114 |
#define LOWER_MASK ((GLIB_SIZEOF_VOID_P*2)-1) |
| 115 |
|
| 116 |
gpointer |
| 117 |
sysprof_allocator_alloc (SysprofAllocator *allocator, |
| 118 |
gsize size) |
| 119 |
{ |
| 120 |
gpointer ret; |
| 121 |
|
| 122 |
if (size == 0) |
| 123 |
return NULL; |
| 124 |
|
| 125 |
if (size < 16) |
| 126 |
size = 16; |
| 127 |
else if (size & LOWER_MASK) |
| 128 |
size += (LOWER_MASK + 1) - (size & LOWER_MASK); |
| 129 |
|
| 130 |
g_assert ((size & LOWER_MASK) == 0); |
| 131 |
|
| 132 |
if G_UNLIKELY (size > allocator->current->avail) |
| 133 |
{ |
| 134 |
SysprofPage *page; |
| 135 |
|
| 136 |
if (size > SYSPROF_ALLOCATOR_MIN_PAGE_SIZE) |
| 137 |
{ |
| 138 |
gsize newsize = size; |
| 139 |
|
| 140 |
if (!sysprof_allocator_ispow2 (size)) |
| 141 |
newsize = sysprof_allocator_nextpow2 (size); |
| 142 |
|
| 143 |
page = sysprof_page_new (newsize); |
| 144 |
page->avail = newsize - size; |
| 145 |
|
| 146 |
if (page->avail < allocator->current->avail) |
| 147 |
{ |
| 148 |
page->next = allocator->pages; |
| 149 |
allocator->pages = page; |
| 150 |
} |
| 151 |
else |
| 152 |
{ |
| 153 |
allocator->current->next = page; |
| 154 |
allocator->current = page; |
| 155 |
} |
| 156 |
|
| 157 |
return page->data; |
| 158 |
} |
| 159 |
else |
| 160 |
{ |
| 161 |
page = sysprof_page_new (allocator->current->len * 2); |
| 162 |
allocator->current->next = page; |
| 163 |
allocator->current = page; |
| 164 |
} |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
g_assert (allocator->current->len >= allocator->current->avail); |
| 169 |
g_assert (size <= allocator->current->avail); |
| 170 |
|
| 171 |
ret = &allocator->current->data[allocator->current->len - allocator->current->avail]; |
| 172 |
allocator->current->avail -= size; |
| 173 |
return ret; |
| 174 |
} |
| 175 |
|
| 176 |
gconstpointer |
| 177 |
sysprof_allocator_cstring (SysprofAllocator *allocator, |
| 178 |
const char *str, |
| 179 |
gssize len) |
| 180 |
{ |
| 181 |
char *dst; |
| 182 |
|
| 183 |
if (len == 0 || str[0] == 0) |
| 184 |
return (&allocator->empty); |
| 185 |
|
| 186 |
if (len < 0) |
| 187 |
{ |
| 188 |
if ((dst = g_hash_table_lookup (allocator->strings, str))) |
| 189 |
return dst; |
| 190 |
len = strlen (str); |
| 191 |
} |
| 192 |
else |
| 193 |
{ |
| 194 |
char stack[128]; |
| 195 |
|
| 196 |
if (len < G_N_ELEMENTS (stack)) |
| 197 |
{ |
| 198 |
memcpy (stack, str, len); |
| 199 |
stack[len] = 0; |
| 200 |
dst = g_hash_table_lookup (allocator->strings, stack); |
| 201 |
} |
| 202 |
else |
| 203 |
{ |
| 204 |
char *freeme = g_strndup (str, len); |
| 205 |
dst = g_hash_table_lookup (allocator->strings, freeme); |
| 206 |
g_free (freeme); |
| 207 |
} |
| 208 |
|
| 209 |
if (dst != NULL) |
| 210 |
return dst; |
| 211 |
} |
| 212 |
|
| 213 |
dst = sysprof_allocator_alloc (allocator, len + 1); |
| 214 |
memcpy (dst, str, len); |
| 215 |
dst[len] = 0; |
| 216 |
|
| 217 |
|
| 218 |
g_hash_table_replace (allocator->strings, dst, dst); |
| 219 |
|
| 220 |
return dst; |
| 221 |
} |