summaryrefslogtreecommitdiff
path: root/mono/metadata/test-gc-memfuncs.c
blob: a8ccf8833c506601cb196bd0b9b0913a059cc3df (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
/*
 * test-sgen-qsort.c: Unit test for our own bzero/memmove.
 *
 * Copyright (C) 2013 Xamarin Inc
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License 2.0 as published by the Free Software Foundation;
 *
 * This 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License 2.0 along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "config.h"

#include "metadata/gc-internal.h"

#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <assert.h>

#define POOL_SIZE	2048
#define START_OFFSET	128

#define BZERO_OFFSETS	64
#define BZERO_SIZES	256

#define MEMMOVE_SRC_OFFSETS		32
#define MEMMOVE_DEST_OFFSETS		32
#define MEMMOVE_SIZES			256
#define MEMMOVE_NONOVERLAP_START	1024

int
main (void)
{
	unsigned char *random_mem = malloc (POOL_SIZE);
	unsigned char *reference = malloc (POOL_SIZE);
	unsigned char *playground = malloc (POOL_SIZE);
	long *long_random_mem;
	int i, offset, size, src_offset, dest_offset;

	srandom (time (NULL));

	/* init random memory */
	long_random_mem = (long*)random_mem;
	for (i = 0; i < POOL_SIZE / sizeof (long); ++i)
		long_random_mem [i] = random ();

	/* test bzero */
	for (offset = 0; offset <= BZERO_OFFSETS; ++offset) {
		for (size = 0; size <= BZERO_SIZES; ++size) {
			memcpy (reference, random_mem, POOL_SIZE);
			memcpy (playground, random_mem, POOL_SIZE);

			bzero (reference + START_OFFSET + offset, size);
			mono_gc_bzero_atomic (playground + START_OFFSET + offset, size);

			assert (!memcmp (reference, playground, POOL_SIZE));
		}
	}

	/* test memmove */
	for (src_offset = -MEMMOVE_SRC_OFFSETS; src_offset <= MEMMOVE_SRC_OFFSETS; ++src_offset) {
		for (dest_offset = -MEMMOVE_DEST_OFFSETS; dest_offset <= MEMMOVE_DEST_OFFSETS; ++dest_offset) {
			for (size = 0; size <= MEMMOVE_SIZES; ++size) {
				/* overlapping */
				memcpy (reference, random_mem, POOL_SIZE);
				memcpy (playground, random_mem, POOL_SIZE);

				memmove (reference + START_OFFSET + dest_offset, reference + START_OFFSET + src_offset, size);
				mono_gc_memmove_atomic (playground + START_OFFSET + dest_offset, playground + START_OFFSET + src_offset, size);

				assert (!memcmp (reference, playground, POOL_SIZE));

				/* non-overlapping with dest < src */
				memcpy (reference, random_mem, POOL_SIZE);
				memcpy (playground, random_mem, POOL_SIZE);

				memmove (reference + START_OFFSET + dest_offset, reference + MEMMOVE_NONOVERLAP_START + src_offset, size);
				mono_gc_memmove_atomic (playground + START_OFFSET + dest_offset, playground + MEMMOVE_NONOVERLAP_START + src_offset, size);

				assert (!memcmp (reference, playground, POOL_SIZE));

				/* non-overlapping with dest > src */
				memcpy (reference, random_mem, POOL_SIZE);
				memcpy (playground, random_mem, POOL_SIZE);

				memmove (reference + MEMMOVE_NONOVERLAP_START + dest_offset, reference + START_OFFSET + src_offset, size);
				mono_gc_memmove_atomic (playground + MEMMOVE_NONOVERLAP_START + dest_offset, playground + START_OFFSET + src_offset, size);

				assert (!memcmp (reference, playground, POOL_SIZE));
			}
		}
	}

	return 0;
}