mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-07-23 10:27:51 +02:00
build
debian
docs
gio
glib
gmodule
gobject
gthread
m4macros
po
tests
collate
gobject
refcount
.gitignore
Makefile.am
assert-msg-test.c
asyncqueue-test.c
atomic-test.c
bit-test.c
casefold.txt
casemap.txt
child-test.c
completion-test.c
cxx-test.C
dirname-test.c
env-test.c
errorcheck-mutex-test.c
file-test.c
gen-casefold-txt.pl
gen-casemap-txt.pl
gio-ls.c
gio-test.c
iochannel-test-infile
iochannel-test.c
libmoduletestplugin_a.c
libmoduletestplugin_b.c
mainloop-test.c
makefile.msc.in
mapping-test.c
memchunks.c
module-test.c
onceinit.c
qsort-test.c
relation-test.c
run-assert-msg-test.sh
run-collate-tests.sh
slice-color.c
slice-concurrent.c
slice-test.c
slice-threadinit.c
spawn-test-win32-gui.c
spawn-test.c
testgdate.c
testgdateparser.c
testglib.c
thread-test.c
threadpool-test.c
timeloop-basic.c
timeloop-closure.c
timeloop.c
type-test.c
unicode-caseconv.c
unicode-collate.c
unicode-encoding.c
unicode-normalize.c
utf8.txt
.gitignore
AUTHORS
COPYING
ChangeLog.pre-1-2
ChangeLog.pre-2-0
ChangeLog.pre-2-10
ChangeLog.pre-2-12
ChangeLog.pre-2-14
ChangeLog.pre-2-16
ChangeLog.pre-2-18
ChangeLog.pre-2-2
ChangeLog.pre-2-20
ChangeLog.pre-2-4
ChangeLog.pre-2-6
ChangeLog.pre-2-8
HACKING
INSTALL.in
MAINTAINERS
Makefile.am
Makefile.decl
NEWS
NEWS.pre-1-3
README.commits
README.in
README.win32
acglib.m4
acinclude.m4
autogen.sh
config.h.win32.in
configure.ac
gio-2.0-uninstalled.pc.in
gio-2.0.pc.in
gio-unix-2.0-uninstalled.pc.in
gio-unix-2.0.pc.in
gio-windows-2.0.pc.in
glib-2.0-uninstalled.pc.in
glib-2.0.pc.in
glib-gettextize.in
glib-zip.in
gmodule-2.0-uninstalled.pc.in
gmodule-2.0.pc.in
gmodule-export-2.0.pc.in
gmodule-no-export-2.0-uninstalled.pc.in
gmodule-no-export-2.0.pc.in
gobject-2.0-uninstalled.pc.in
gobject-2.0.pc.in
gthread-2.0-uninstalled.pc.in
gthread-2.0.pc.in
makefile.msc
mkinstalldirs
msvc_recommended_pragmas.h
sanity_check
win32-fixup.pl
167 lines
5.7 KiB
C
167 lines
5.7 KiB
C
/* slice-threadinit.c - test GSlice across g_thread_init
|
|
* Copyright (C) 2007 Tim Janik
|
|
*
|
|
* This work is provided "as is"; redistribution and modification
|
|
* in whole or in part, in any medium, physical or electronic is
|
|
* permitted without restriction.
|
|
*
|
|
* This work is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
*
|
|
* In no event shall the authors or contributors be liable for any
|
|
* direct, indirect, incidental, special, exemplary, or consequential
|
|
* damages (including, but not limited to, procurement of substitute
|
|
* goods or services; loss of use, data, or profits; or business
|
|
* interruption) however caused and on any theory of liability, whether
|
|
* in contract, strict liability, or tort (including negligence or
|
|
* otherwise) arising in any way out of the use of this software, even
|
|
* if advised of the possibility of such damage.
|
|
*/
|
|
#include <glib.h>
|
|
|
|
#define N_PAGES (101) /* number of pages to sample */
|
|
#define SAMPLE_SIZE (7)
|
|
#define PAGE_SIZE (128) /* must be <= minimum GSlice alignment block */
|
|
#define MAGAZINE_PROBES { 81, 265, 347 } /* block sizes hopefully unused by g_thread_init */
|
|
#define MAX_PROBE_TRIALS (1031) /* must be >= maximum magazine size */
|
|
|
|
#define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
|
|
|
|
static struct {
|
|
void *page;
|
|
void *sample;
|
|
} pages[N_PAGES] = { { NULL, }, };
|
|
|
|
static const guint magazine_probes[] = MAGAZINE_PROBES;
|
|
#define N_MAGAZINE_PROBES G_N_ELEMENTS (magazine_probes)
|
|
|
|
static void
|
|
release_trash_list (GSList **trash_list,
|
|
gsize block_size)
|
|
{
|
|
while (*trash_list)
|
|
{
|
|
g_slice_free1 (block_size, (*trash_list)->data);
|
|
*trash_list = g_slist_delete_link (*trash_list, *trash_list);
|
|
}
|
|
}
|
|
|
|
static GSList *free_list = NULL;
|
|
|
|
static gboolean
|
|
allocate_from_known_page (void)
|
|
{
|
|
guint i, j, n_trials = N_PAGES * PAGE_SIZE / SAMPLE_SIZE; /* upper bound */
|
|
for (i = 0; i < n_trials; i++)
|
|
{
|
|
void *b = g_slice_alloc (SAMPLE_SIZE);
|
|
void *p = (void*) (PAGE_SIZE * ((gsize) b / PAGE_SIZE));
|
|
free_list = g_slist_prepend (free_list, b);
|
|
/* find page */
|
|
for (j = 0; j < N_PAGES; j++)
|
|
if (pages[j].page == p)
|
|
return TRUE;
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
int
|
|
main (int argc,
|
|
char *argv[])
|
|
{
|
|
int j, n_pages = 0;
|
|
void *mps[N_MAGAZINE_PROBES];
|
|
|
|
/* probe some magazine sizes */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
mps[j] = g_slice_alloc (magazine_probes[j]);
|
|
/* mps[*] now contains pointers to allocated slices */
|
|
|
|
/* allocate blocks from N_PAGES different pages */
|
|
while (n_pages < N_PAGES)
|
|
{
|
|
void *b = g_slice_alloc (SAMPLE_SIZE);
|
|
void *p = (void*) (PAGE_SIZE * ((gsize) b / PAGE_SIZE));
|
|
for (j = 0; j < N_PAGES; j++)
|
|
if (pages[j].page == p)
|
|
break;
|
|
if (j < N_PAGES) /* known page */
|
|
free_list = g_slist_prepend (free_list, b);
|
|
else /* new page */
|
|
{
|
|
j = n_pages++;
|
|
pages[j].page = p;
|
|
pages[j].sample = b;
|
|
}
|
|
}
|
|
/* release intermediate allocations */
|
|
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
|
|
/* ensure that we can allocate from known pages */
|
|
if (!allocate_from_known_page())
|
|
g_error ("failed to allocate from magazine/page cache (before g_thread_init)");
|
|
/* release intermediate allocations */
|
|
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
|
|
/* release magazine probes to be retained */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
/* mps[*] now contains pointers to releaed slices */
|
|
|
|
/* ensure probes were retained */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
{
|
|
GSList *trash = NULL;
|
|
guint k;
|
|
for (k = 0; k < MAX_PROBE_TRIALS; k++)
|
|
{
|
|
void *mem = g_slice_alloc (magazine_probes[j]);
|
|
if (mem == mps[j])
|
|
break; /* reallocated previously freed slice */
|
|
trash = g_slist_prepend (trash, mem);
|
|
}
|
|
release_trash_list (&trash, magazine_probes[j]);
|
|
if (k >= MAX_PROBE_TRIALS) /* failed to reallocate slice */
|
|
g_error ("failed to reallocate slice from magazine (before g_thread_init): size=%d", magazine_probes[j]);
|
|
}
|
|
/* mps[*] now contains pointers to reallocated slices */
|
|
|
|
/* release magazine probes to be retained across g_thread_init */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
/* mps[*] now contains pointers to released slices */
|
|
|
|
/* initialize threading (should retain allocator state) */
|
|
g_thread_init (NULL);
|
|
|
|
/* ensure probes were retained */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
{
|
|
GSList *trash = NULL;
|
|
guint k;
|
|
for (k = 0; k < MAX_PROBE_TRIALS; k++)
|
|
{
|
|
void *mem = g_slice_alloc (magazine_probes[j]);
|
|
if (mem == mps[j])
|
|
break; /* reallocated previously freed slice */
|
|
trash = g_slist_prepend (trash, mem);
|
|
}
|
|
release_trash_list (&trash, magazine_probes[j]);
|
|
if (k >= MAX_PROBE_TRIALS) /* failed to reallocate slice */
|
|
g_error ("failed to reallocate slice from magazine (after g_thread_init): size=%d", magazine_probes[j]);
|
|
}
|
|
/* mps[*] now contains pointers to reallocated slices */
|
|
|
|
/* ensure that we can allocate from known pages */
|
|
if (!allocate_from_known_page())
|
|
g_error ("failed to allocate from magazine/page cache (after g_thread_init)");
|
|
|
|
/* some cleanups */
|
|
for (j = 0; j < N_MAGAZINE_PROBES; j++)
|
|
g_slice_free1 (magazine_probes[j], mps[j]);
|
|
release_trash_list (&free_list, SAMPLE_SIZE);
|
|
|
|
return 0;
|
|
}
|