about summary refs log tree commit diff
path: root/malloc/tst-memalign-2.c
blob: 2d097f4c0cbf2925caeaf99ab79fbfcfb5dbfb31 (plain) (blame)
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
/* Test for memalign chunk reuse.
   Copyright (C) 2022-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <array_length.h>
#include <libc-pointer-arith.h>
#include <support/check.h>

typedef struct TestCase {
  size_t size;
  size_t alignment;
  void *ptr1;
  void *ptr2;
} TestCase;

static TestCase tcache_allocs[] = {
  { 24, 32, NULL, NULL },
  { 24, 64, NULL, NULL },
  { 128, 128, NULL, NULL },
  { 500, 128, NULL, NULL }
};
#define TN array_length (tcache_allocs)

static TestCase large_allocs[] = {
  { 23450, 64, NULL, NULL },
  { 23450, 64, NULL, NULL },
  { 23550, 64, NULL, NULL },
  { 23550, 64, NULL, NULL },
  { 23650, 64, NULL, NULL },
  { 23650, 64, NULL, NULL },
  { 33650, 64, NULL, NULL },
  { 33650, 64, NULL, NULL }
};
#define LN array_length (large_allocs)

void *p;

/* Sanity checks, ancillary to the actual test.  */
#define CHECK(p,a) \
  if (p == NULL || !PTR_IS_ALIGNED (p, a)) \
    FAIL_EXIT1 ("NULL or misaligned memory detected.\n");

static int
do_test (void)
{
  int i, j;
  int count;
  void *ptr[10];
  void *p;

  /* TCache test.  */

  for (i = 0; i < TN; ++ i)
    {
      size_t sz2;

      tcache_allocs[i].ptr1 = memalign (tcache_allocs[i].alignment, tcache_allocs[i].size);
      CHECK (tcache_allocs[i].ptr1, tcache_allocs[i].alignment);
      sz2 = malloc_usable_size (tcache_allocs[i].ptr1);
      free (tcache_allocs[i].ptr1);

      /* This should return the same chunk as was just free'd.  */
      tcache_allocs[i].ptr2 = memalign (tcache_allocs[i].alignment, sz2);
      CHECK (tcache_allocs[i].ptr2, tcache_allocs[i].alignment);
      free (tcache_allocs[i].ptr2);

      TEST_VERIFY (tcache_allocs[i].ptr1 == tcache_allocs[i].ptr2);
    }

  /* Test for non-head tcache hits.  This exercises the memalign
     scanning code to find matching allocations.  */
  for (i = 0; i < array_length (ptr); ++ i)
    {
      if (i == 4)
	{
	  ptr[i] = memalign (64, 256);
	  CHECK (ptr[i], 64);
	}
      else
	{
	  ptr[i] = malloc (256);
	  CHECK (ptr[i], 4);
	}
    }
  for (i = 0; i < array_length (ptr); ++ i)
    free (ptr[i]);

  p = memalign (64, 256);
  CHECK (p, 64);

  count = 0;
  for (i = 0; i < 10; ++ i)
    if (ptr[i] == p)
      ++ count;
  free (p);
  TEST_VERIFY (count > 0);

  /* Large bins test.  This verifies that the over-allocated parts
     that memalign releases for future allocations can be reused by
     memalign itself at least in some cases.  */

  for (i = 0; i < LN; ++ i)
    {
      large_allocs[i].ptr1 = memalign (large_allocs[i].alignment, large_allocs[i].size);
      CHECK (large_allocs[i].ptr1, large_allocs[i].alignment);
      /* Keep chunks from combining by fragmenting the heap.  */
      p = malloc (512);
      CHECK (p, 4);
    }

  for (i = 0; i < LN; ++ i)
    free (large_allocs[i].ptr1);

  /* Force the unsorted bins to be scanned and moved to small/large
     bins.  */
  p = malloc (60000);

  for (i = 0; i < LN; ++ i)
    {
      large_allocs[i].ptr2 = memalign (large_allocs[i].alignment, large_allocs[i].size);
      CHECK (large_allocs[i].ptr2, large_allocs[i].alignment);
    }

  count = 0;
  for (i = 0; i < LN; ++ i)
    {
      int ok = 0;
      for (j = 0; j < LN; ++ j)
	if (large_allocs[i].ptr1 == large_allocs[j].ptr2)
	  ok = 1;
      if (ok == 1)
	count ++;
    }

  /* The allocation algorithm is complicated outside of the memalign
     logic, so just make sure it's working for most of the
     allocations.  This avoids possible boundary conditions with
     empty/full heaps.  */
  TEST_VERIFY (count > LN / 2);

  return 0;
}

#include <support/test-driver.c>