My favorites
▼
|
Sign in
core-java-performance-examples
Core Java Performance Examples
Project Home
Downloads
Wiki
Issues
Source
Checkout
Browse
Changes
Source path:
svn
/
trunk
/
src
/
test
/
cpp
/
MemoryAlignment
/
main.cpp
r88
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
/*
* File: main.cpp
* Author: peter
*
* Created on 10 September 2011, 08:48
*/
#include <cstdlib>
#include <stddef.h>
#include <iostream>
using namespace std;
class OneInt {
int i;
};
class TwoInt {
int i, j;
};
class ThreeInt {
int i, j, k;
};
class FourInt {
int i, j, k, l;
};
class FiveInt {
int i, j, k, l, m;
};
class SixInt {
int i, j, k, l, m, n;
};
class SevenInt {
int i, j, k, l, m, n, o;
};
class EightInt {
int i, j, k, l, m, n, o, p;
};
class OnePtr {
void * i;
};
class TwoPtr {
void * i, *j;
};
class ThreePtr {
void * i, *j, *k;
};
class FourPtr {
void * i, *j, *k, *l;
};
class FivePtr {
void * i, *j, *k, *l, *m;
};
class SixPtr {
void * i, *j, *k, *l, *m, *n;
};
class SevenPtr {
void * i, *j, *k, *l, *m, *n, *o;
};
class EightPtr {
void * i, *j, *k, *l, *m, *n, *o, *p;
};
/*
*
*/
int main(int argc, char** argv) {
for (int i = 0; i <= 64; i++) {
char *p = (char *) malloc(i);
char *q = (char *) malloc(i);
size_t reserved = q - p;
cout << "malloc(" << i << ") reserved " << reserved << " bytes" << endl;
}
{
// int values.
cout << "heap class with one int reserved " << -((char *) new OneInt() - (char *) new OneInt()) << " bytes" << endl;
OneInt p1, q1;
cout << "stack class with one int reserved " << abs((char *) &q1 - (char *) &p1) << " bytes" << endl;
cout << "heap class with one int reserved " << -((char *) new OneInt() - (char *) new OneInt()) << " bytes" << endl;
OneInt p1b, q1b;
cout << "stack class with one int reserved " << abs((char *) &q1b - (char *) &p1b) << " bytes" << endl;
cout << "heap class with two int reserved " << -((char *) new TwoInt() - (char *) new TwoInt()) << " bytes" << endl;
TwoInt p2, q2;
cout << "stack class with two int reserved " << abs((char *) &q2 - (char *) &p2) << " bytes" << endl;
cout << "heap class with three int reserved " << -((char *) new ThreeInt() - (char *) new ThreeInt()) << " bytes" << endl;
ThreeInt p3, q3;
cout << "stack class with three int reserved " << abs((char *) &q3 - (char *) &p3) << " bytes" << endl;
cout << "heap class with four int reserved " << -((char *) new FourInt() - (char *) new FourInt()) << " bytes" << endl;
FourInt p4, q4;
cout << "stack class with four int reserved " << abs((char *) &q4 - (char *) &p4) << " bytes" << endl;
cout << "heap class with five int reserved " << -((char *) new FiveInt() - (char *) new FiveInt()) << " bytes" << endl;
FiveInt p5, q5;
cout << "stack class with five int reserved " << abs((char *) &q5 - (char *) &p5) << " bytes" << endl;
cout << "heap class with six int reserved " << -((char *) new SixInt() - (char *) new SixInt()) << " bytes" << endl;
SixInt p6, q6;
cout << "stack class with six int reserved " << abs((char *) &q6 - (char *) &p6) << " bytes" << endl;
cout << "heap class with seven int reserved " << -((char *) new SevenInt() - (char *) new SevenInt()) << " bytes" << endl;
SevenInt p7, q7;
cout << "stack class with seven int reserved " << abs((char *) &q7 - (char *) &p7) << " bytes" << endl;
cout << "heap class with eight int reserved " << -((char *) new EightInt() - (char *) new EightInt()) << " bytes" << endl;
EightInt p8, q8;
cout << "stack class with eight int reserved " << abs((char *) &q8 - (char *) &p8) << " bytes" << endl;
}
{
// pointers values.
cout << "heap class with one pointer reserved " << -((char *) new OnePtr() - (char *) new OnePtr()) << " bytes" << endl;
OnePtr p1, q1;
cout << "stack class with one pointer reserved " << abs((char *) &q1 - (char *) &p1) << " bytes" << endl;
cout << "heap class with two pointer reserved " << -((char *) new TwoPtr() - (char *) new TwoPtr()) << " bytes" << endl;
TwoPtr p2, q2;
cout << "stack class with two pointer reserved " << abs((char *) &q2 - (char *) &p2) << " bytes" << endl;
cout << "heap class with three pointer reserved " << -((char *) new ThreePtr() - (char *) new ThreePtr()) << " bytes" << endl;
ThreePtr p3, q3;
cout << "stack class with three pointer reserved " << abs((char *) &q3 - (char *) &p3) << " bytes" << endl;
cout << "heap class with four pointer reserved " << -((char *) new FourPtr() - (char *) new FourPtr()) << " bytes" << endl;
FourPtr p4, q4;
cout << "stack class with four pointer reserved " << abs((char *) &q4 - (char *) &p4) << " bytes" << endl;
cout << "heap class with five pointer reserved " << -((char *) new FivePtr() - (char *) new FivePtr()) << " bytes" << endl;
FivePtr p5, q5;
cout << "stack class with five pointer reserved " << abs((char *) &q5 - (char *) &p5) << " bytes" << endl;
cout << "heap class with six pointer reserved " << -((char *) new SixPtr() - (char *) new SixPtr()) << " bytes" << endl;
SixPtr p6, q6;
cout << "stack class with six pointer reserved " << abs((char *) &q6 - (char *) &p6) << " bytes" << endl;
cout << "heap class with seven pointer reserved " << -((char *) new SevenPtr() - (char *) new SevenPtr()) << " bytes" << endl;
SevenPtr p7, q7;
cout << "stack class with seven pointer reserved " << abs((char *) &q7 - (char *) &p7) << " bytes" << endl;
cout << "heap class with eight pointer reserved " << -((char *) new EightPtr() - (char *) new EightPtr()) << " bytes" << endl;
EightPtr p8, q8;
cout << "stack class with eight pointer reserved " << abs((char *) &q8 - (char *) &p8) << " bytes" << endl;
}
return 0;
}
Show details
Hide details
Change log
r62
by peter.lawrey on Sep 10, 2011
Diff
Test for memory alignment.
Go to:
/trunk/src/test/cpp/MemoryAlignment
...est/cpp/MemoryAlignment/.dep.inc
...est/cpp/MemoryAlignment/Makefile
...est/cpp/MemoryAlignment/main.cpp
...st/cpp/MemoryAlignment/nbproject
...ment/nbproject/Makefile-Debug.mk
...nt/nbproject/Makefile-Release.mk
...nment/nbproject/Makefile-impl.mk
.../nbproject/Makefile-variables.mk
...ent/nbproject/Package-Debug.bash
...t/nbproject/Package-Release.bash
...ent/nbproject/configurations.xml
...emoryAlignment/nbproject/private
...ct/private/Makefile-variables.mk
...oject/private/configurations.xml
...oject/private/private.properties
...nt/nbproject/private/private.xml
...ent/nbproject/project.properties
...yAlignment/nbproject/project.xml
...oject/private/configurations.xml
...st/nbproject/private/private.xml
...memorytest/nbproject/project.xml
...com/google/code/java/core/memory
...core/memory/MemoryAlignment.java
Project members,
sign in
to write a code review
Older revisions
All revisions of this file
File info
Size: 5789 bytes, 147 lines
View raw file
Powered by
Google Project Hosting