summaryrefslogtreecommitdiff
path: root/glib/tests/slice-known-pages.c
blob: ee856068bc1a502db0d696f9d58463439ab563cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* slice-known-pages.c - test GSlice across known pages
 * 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 \
  {                     \
    97, 265, 347        \
  }                                       /* block sizes hopefully unused */
#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, 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;
}

static void
test_slice_known_pages (void)
{
  gsize 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 */
  g_assert_true (allocate_from_known_page());

  /* 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 released 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]);
      g_assert_cmpint (k, <, MAX_PROBE_TRIALS); /* failed to reallocate slice */
    }
  /* mps[*] now contains pointers to reallocated slices */

  /* release magazine probes to be retained across known pages */
  for (j = 0; j < N_MAGAZINE_PROBES; j++)
    g_slice_free1 (magazine_probes[j], mps[j]);
  /* mps[*] now contains pointers to released 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]);
      g_assert_cmpint (k, <, MAX_PROBE_TRIALS); /* failed to reallocate slice */
    }
  /* mps[*] now contains pointers to reallocated slices */

  /* ensure that we can allocate from known pages */
  g_assert_true (allocate_from_known_page());

  /* 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);
}

int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/slice/known_pages", test_slice_known_pages);

  return g_test_run ();
}