|
Packit |
ae235b |
/* slice-threadinit.c - test GSlice across g_thread_init
|
|
Packit |
ae235b |
* Copyright (C) 2007 Tim Janik
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* This work is provided "as is"; redistribution and modification
|
|
Packit |
ae235b |
* in whole or in part, in any medium, physical or electronic is
|
|
Packit |
ae235b |
* permitted without restriction.
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* This work is distributed in the hope that it will be useful,
|
|
Packit |
ae235b |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
ae235b |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
Packit |
ae235b |
*
|
|
Packit |
ae235b |
* In no event shall the authors or contributors be liable for any
|
|
Packit |
ae235b |
* direct, indirect, incidental, special, exemplary, or consequential
|
|
Packit |
ae235b |
* damages (including, but not limited to, procurement of substitute
|
|
Packit |
ae235b |
* goods or services; loss of use, data, or profits; or business
|
|
Packit |
ae235b |
* interruption) however caused and on any theory of liability, whether
|
|
Packit |
ae235b |
* in contract, strict liability, or tort (including negligence or
|
|
Packit |
ae235b |
* otherwise) arising in any way out of the use of this software, even
|
|
Packit |
ae235b |
* if advised of the possibility of such damage.
|
|
Packit |
ae235b |
*/
|
|
Packit |
ae235b |
#include <glib.h>
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
#define N_PAGES (101) /* number of pages to sample */
|
|
Packit |
ae235b |
#define SAMPLE_SIZE (7)
|
|
Packit |
ae235b |
#define PAGE_SIZE (128) /* must be <= minimum GSlice alignment block */
|
|
Packit |
ae235b |
#define MAGAZINE_PROBES { 97, 265, 347 } /* block sizes hopefully unused by g_thread_init */
|
|
Packit |
ae235b |
#define MAX_PROBE_TRIALS (1031) /* must be >= maximum magazine size */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
#define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static struct {
|
|
Packit |
ae235b |
void *page;
|
|
Packit |
ae235b |
void *sample;
|
|
Packit |
ae235b |
} pages[N_PAGES] = { { NULL, }, };
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static const guint magazine_probes[] = MAGAZINE_PROBES;
|
|
Packit |
ae235b |
#define N_MAGAZINE_PROBES G_N_ELEMENTS (magazine_probes)
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static void
|
|
Packit |
ae235b |
release_trash_list (GSList **trash_list,
|
|
Packit |
ae235b |
gsize block_size)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
while (*trash_list)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
g_slice_free1 (block_size, (*trash_list)->data);
|
|
Packit |
ae235b |
*trash_list = g_slist_delete_link (*trash_list, *trash_list);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static GSList *free_list = NULL;
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
static gboolean
|
|
Packit |
ae235b |
allocate_from_known_page (void)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
guint i, j, n_trials = N_PAGES * PAGE_SIZE / SAMPLE_SIZE; /* upper bound */
|
|
Packit |
ae235b |
for (i = 0; i < n_trials; i++)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
void *b = g_slice_alloc (SAMPLE_SIZE);
|
|
Packit |
ae235b |
void *p = (void*) (PAGE_SIZE * ((gsize) b / PAGE_SIZE));
|
|
Packit |
ae235b |
free_list = g_slist_prepend (free_list, b);
|
|
Packit |
ae235b |
/* find page */
|
|
Packit |
ae235b |
for (j = 0; j < N_PAGES; j++)
|
|
Packit |
ae235b |
if (pages[j].page == p)
|
|
Packit |
ae235b |
return TRUE;
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
return FALSE;
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
int
|
|
Packit |
ae235b |
main (int argc,
|
|
Packit |
ae235b |
char *argv[])
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
int j, n_pages = 0;
|
|
Packit |
ae235b |
void *mps[N_MAGAZINE_PROBES];
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* probe some magazine sizes */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
mps[j] = g_slice_alloc (magazine_probes[j]);
|
|
Packit |
ae235b |
/* mps[*] now contains pointers to allocated slices */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* allocate blocks from N_PAGES different pages */
|
|
Packit |
ae235b |
while (n_pages < N_PAGES)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
void *b = g_slice_alloc (SAMPLE_SIZE);
|
|
Packit |
ae235b |
void *p = (void*) (PAGE_SIZE * ((gsize) b / PAGE_SIZE));
|
|
Packit |
ae235b |
for (j = 0; j < N_PAGES; j++)
|
|
Packit |
ae235b |
if (pages[j].page == p)
|
|
Packit |
ae235b |
break;
|
|
Packit |
ae235b |
if (j < N_PAGES) /* known page */
|
|
Packit |
ae235b |
free_list = g_slist_prepend (free_list, b);
|
|
Packit |
ae235b |
else /* new page */
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
j = n_pages++;
|
|
Packit |
ae235b |
pages[j].page = p;
|
|
Packit |
ae235b |
pages[j].sample = b;
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
/* release intermediate allocations */
|
|
Packit |
ae235b |
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* ensure that we can allocate from known pages */
|
|
Packit |
ae235b |
if (!allocate_from_known_page())
|
|
Packit |
ae235b |
g_error ("failed to allocate from magazine/page cache (before g_thread_init)");
|
|
Packit |
ae235b |
/* release intermediate allocations */
|
|
Packit |
ae235b |
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* release magazine probes to be retained */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
Packit |
ae235b |
/* mps[*] now contains pointers to releaed slices */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* ensure probes were retained */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
GSList *trash = NULL;
|
|
Packit |
ae235b |
guint k;
|
|
Packit |
ae235b |
for (k = 0; k < MAX_PROBE_TRIALS; k++)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
void *mem = g_slice_alloc (magazine_probes[j]);
|
|
Packit |
ae235b |
if (mem == mps[j])
|
|
Packit |
ae235b |
break; /* reallocated previously freed slice */
|
|
Packit |
ae235b |
trash = g_slist_prepend (trash, mem);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
release_trash_list (&trash, magazine_probes[j]);
|
|
Packit |
ae235b |
if (k >= MAX_PROBE_TRIALS) /* failed to reallocate slice */
|
|
Packit |
ae235b |
g_error ("failed to reallocate slice from magazine (before g_thread_init): size=%d", magazine_probes[j]);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
/* mps[*] now contains pointers to reallocated slices */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* release magazine probes to be retained across g_thread_init */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
Packit |
ae235b |
/* mps[*] now contains pointers to released slices */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* initialize threading (should retain allocator state) */
|
|
Packit |
ae235b |
g_thread_init (NULL);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* ensure probes were retained */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
GSList *trash = NULL;
|
|
Packit |
ae235b |
guint k;
|
|
Packit |
ae235b |
for (k = 0; k < MAX_PROBE_TRIALS; k++)
|
|
Packit |
ae235b |
{
|
|
Packit |
ae235b |
void *mem = g_slice_alloc (magazine_probes[j]);
|
|
Packit |
ae235b |
if (mem == mps[j])
|
|
Packit |
ae235b |
break; /* reallocated previously freed slice */
|
|
Packit |
ae235b |
trash = g_slist_prepend (trash, mem);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
release_trash_list (&trash, magazine_probes[j]);
|
|
Packit |
ae235b |
if (k >= MAX_PROBE_TRIALS) /* failed to reallocate slice */
|
|
Packit |
ae235b |
g_error ("failed to reallocate slice from magazine (after g_thread_init): size=%d", magazine_probes[j]);
|
|
Packit |
ae235b |
}
|
|
Packit |
ae235b |
/* mps[*] now contains pointers to reallocated slices */
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* ensure that we can allocate from known pages */
|
|
Packit |
ae235b |
if (!allocate_from_known_page())
|
|
Packit |
ae235b |
g_error ("failed to allocate from magazine/page cache (after g_thread_init)");
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
/* some cleanups */
|
|
Packit |
ae235b |
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
Packit |
ae235b |
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
Packit |
ae235b |
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
Packit |
ae235b |
|
|
Packit |
ae235b |
return 0;
|
|
Packit |
ae235b |
}
|