1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
|
---|
3 | //
|
---|
4 | // The contents of this file are covered under the licence agreement in the
|
---|
5 | // file "LICENCE" distributed with Cforall.
|
---|
6 | //
|
---|
7 | // alloc.c --
|
---|
8 | //
|
---|
9 | // Author : Peter A. Buhr
|
---|
10 | // Created On : Wed Feb 3 07:56:22 2016
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Mon May 29 11:33:15 2017
|
---|
13 | // Update Count : 228
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <assert>
|
---|
17 | extern "C" {
|
---|
18 | #include <malloc.h> // malloc_usable_size
|
---|
19 | #include <stdint.h> // uintptr_t
|
---|
20 | } // extern
|
---|
21 | #include <fstream>
|
---|
22 | #include <stdlib> // access C malloc, realloc
|
---|
23 |
|
---|
24 | int * foo( int * p, int c ) { return p; }
|
---|
25 | int * bar( int * p, int c ) { return p; }
|
---|
26 | int * baz( int * p, int c ) { return p; }
|
---|
27 |
|
---|
28 | int main( void ) {
|
---|
29 | size_t dim = 10;
|
---|
30 | struct S { int x; double y; } * s;
|
---|
31 | int * p;
|
---|
32 |
|
---|
33 | // allocation, non-array types
|
---|
34 |
|
---|
35 | p = (void *)malloc( sizeof(*p) ); // C malloc, type unsafe
|
---|
36 | *p = 0xdeadbeef;
|
---|
37 | printf( "C malloc %x\n", *p );
|
---|
38 | free( p );
|
---|
39 |
|
---|
40 | p = malloc(); // CFA malloc, type safe
|
---|
41 | printf( "CFA malloc %d\n", *p );
|
---|
42 | free( p );
|
---|
43 |
|
---|
44 | p = malloc( '\1' ); // CFA malloc, fill
|
---|
45 | printf( "CFA malloc, fill %08x\n", *p );
|
---|
46 |
|
---|
47 |
|
---|
48 | // allocation, array types
|
---|
49 | printf( "\n" );
|
---|
50 |
|
---|
51 | p = calloc( dim, sizeof( *p ) ); // C array calloc, type unsafe
|
---|
52 | printf( "C calloc\n" );
|
---|
53 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
54 | printf( "\n" );
|
---|
55 | free( p );
|
---|
56 |
|
---|
57 | p = calloc( dim ); // CFA array calloc, type safe
|
---|
58 | printf( "CFA calloc\n" );
|
---|
59 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
60 | printf( "\n" );
|
---|
61 | free( p );
|
---|
62 |
|
---|
63 | p = amalloc( dim ); // CFA array malloc, type safe
|
---|
64 | printf( "CFA array malloc\n" );
|
---|
65 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
66 | printf( "\n" );
|
---|
67 | free( p );
|
---|
68 |
|
---|
69 | p = amalloc( 2 * dim, '\1' ); // CFA array malloc, fill
|
---|
70 | printf( "CFA array malloc\n" );
|
---|
71 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
72 | printf( "\n" );
|
---|
73 | // do not free
|
---|
74 |
|
---|
75 |
|
---|
76 | // resize, non-array types
|
---|
77 | printf( "\n" );
|
---|
78 |
|
---|
79 | p = (void *)realloc( p, dim * sizeof(*p) ); // CFA realloc
|
---|
80 | printf( "C realloc\n" );
|
---|
81 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
82 | printf( "\n" );
|
---|
83 |
|
---|
84 | p = realloc( p, 2 * dim * sizeof(*p) ); // CFA realloc
|
---|
85 | printf( "CFA realloc\n" );
|
---|
86 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
87 | printf( "\n" );
|
---|
88 |
|
---|
89 | p = realloc( p, dim * sizeof(*p), '\1' ); // CFA realloc
|
---|
90 | printf( "CFA realloc\n" );
|
---|
91 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
92 | printf( "\n" );
|
---|
93 |
|
---|
94 | p = malloc( p, dim * sizeof(*p) ); // CFA malloc
|
---|
95 | printf( "CFA resize malloc\n" );
|
---|
96 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
97 | printf( "\n" );
|
---|
98 |
|
---|
99 | p = malloc( p, 2 * dim * sizeof(*p), '\1' ); // CFA malloc, fill
|
---|
100 | printf( "CFA resize malloc, fill\n" );
|
---|
101 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
102 | printf( "\n" );
|
---|
103 |
|
---|
104 | p = malloc( p, dim * sizeof(*p), '\1' ); // CFA malloc, fill
|
---|
105 | printf( "CFA resize malloc, fill\n" );
|
---|
106 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
107 | printf( "\n" );
|
---|
108 | // do not free
|
---|
109 |
|
---|
110 |
|
---|
111 | // resize, array types
|
---|
112 | printf( "\n" );
|
---|
113 |
|
---|
114 | p = amalloc( p, 2 * dim ); // CFA array malloc
|
---|
115 | printf( "CFA resize array malloc\n" );
|
---|
116 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
117 | printf( "\n" );
|
---|
118 |
|
---|
119 | p = amalloc( p, dim ); // CFA array malloc
|
---|
120 | printf( "CFA resize array malloc\n" );
|
---|
121 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
122 | printf( "\n" );
|
---|
123 |
|
---|
124 | p = amalloc( p, 2 * dim, '\1' ); // CFA array malloc, fill
|
---|
125 | printf( "CFA resize array malloc, fill\n" );
|
---|
126 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
127 | printf( "\n" );
|
---|
128 |
|
---|
129 | p = amalloc( p, dim, '\1' ); // CFA array malloc, fill
|
---|
130 | printf( "CFA resize array malloc, fill\n" );
|
---|
131 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x ", p[i] ); p[i] = 0Xdeadbeef; }
|
---|
132 | free( p );
|
---|
133 | printf( "\n" );
|
---|
134 |
|
---|
135 | struct Struct { int x; double y; };
|
---|
136 | Struct st, st1, sta[dim], sta1[dim], * stp;
|
---|
137 |
|
---|
138 |
|
---|
139 | // alignment, non-array types
|
---|
140 | printf( "\n" );
|
---|
141 |
|
---|
142 | enum { Alignment = 128 };
|
---|
143 | stp = aligned_alloc( Alignment ); // CFA aligned_alloc
|
---|
144 | *stp = (Struct){ 42, 42.5 };
|
---|
145 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
146 | printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y );
|
---|
147 | free( stp );
|
---|
148 |
|
---|
149 | stp = memalign( Alignment ); // CFA memalign
|
---|
150 | *stp = (Struct){ 42, 42.5 };
|
---|
151 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
152 | printf( "CFA memalign %d %g\n", stp->x, stp->y );
|
---|
153 | free( stp );
|
---|
154 |
|
---|
155 | posix_memalign( &stp, Alignment ); // CFA posix_memalign
|
---|
156 | *stp = (Struct){ 42, 42.5 };
|
---|
157 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
158 | printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
|
---|
159 | free( stp );
|
---|
160 |
|
---|
161 | stp = memalign( Alignment, '\1' ); // CFA memalign, fill
|
---|
162 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
163 | printf( "CFA memalign fill %d %g\n", stp->x, stp->y );
|
---|
164 | free( stp );
|
---|
165 |
|
---|
166 |
|
---|
167 | // alignment, array types
|
---|
168 | printf( "\n" );
|
---|
169 |
|
---|
170 | stp = amemalign( Alignment, 2 * dim ); // CFA array memalign
|
---|
171 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
172 | printf( "CFA memalign array\n" );
|
---|
173 | for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%x %g, ", stp[i].x, stp[i].y ); stp[i].x = 0Xdeadbeef, stp[i].y = -17.2; }
|
---|
174 | printf( "\n" );
|
---|
175 | free( stp );
|
---|
176 |
|
---|
177 | stp = amemalign( Alignment, dim, '\1' ); // CFA array memalign, fill
|
---|
178 | assert( (uintptr_t)stp % Alignment == 0 );
|
---|
179 | printf( "CFA memalign array\n" );
|
---|
180 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", stp[i].x, stp[i].y ); stp[i].x = 0Xdeadbeef, stp[i].y = -17.2; }
|
---|
181 | printf( "\n" );
|
---|
182 | free( stp );
|
---|
183 |
|
---|
184 |
|
---|
185 | // data, non-array types
|
---|
186 | printf( "\n" );
|
---|
187 |
|
---|
188 | stp = memset( &st, '\xff' ); // CFA memset, type safe
|
---|
189 | printf( "CFA memset %x %g\n", st.x, st.y );
|
---|
190 | stp = memcpy( &st1, &st ); // CFA memcpy, type safe
|
---|
191 | printf( "CFA memcpy %x %g\n", st1.x, st1.y );
|
---|
192 |
|
---|
193 |
|
---|
194 | // data, array types
|
---|
195 | printf( "\n" );
|
---|
196 |
|
---|
197 | stp = amemset( sta, dim, '\xff' ); // CFA array memset, type safe
|
---|
198 | printf( "CFA array memset\n" );
|
---|
199 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", sta[i].x, sta[i].y ); sta[i].x = 0Xdeadbeef, sta[i].y = -17.2; }
|
---|
200 | printf( "\n" );
|
---|
201 |
|
---|
202 | stp = amemcpy( sta1, sta, dim ); // CFA array memcpy, type safe
|
---|
203 | printf( "CFA memcpy\n" );
|
---|
204 | for ( int i = 0; i < dim; i += 1 ) { printf( "%x %g, ", sta1[i].x, sta1[i].y ); sta1[i].x = 0Xdeadbeef, sta1[i].y = -17.2; }
|
---|
205 | printf( "\n" );
|
---|
206 |
|
---|
207 | float * fp = malloc() + 1;
|
---|
208 | printf( "pointer arithmetic %d\n", fp == fp - 1 );
|
---|
209 | free( fp - 1 );
|
---|
210 |
|
---|
211 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
|
---|
212 | *p = 0xdeadbeef;
|
---|
213 | printf( "CFA deep malloc %x\n", *p );
|
---|
214 | free( p );
|
---|
215 |
|
---|
216 | stp = malloc();
|
---|
217 | printf( "\nSHOULD FAIL\n" );
|
---|
218 | p = malloc( stp, dim * sizeof(*stp) );
|
---|
219 | p = memset( stp, 10 );
|
---|
220 | p = memcpy( &st1, &st );
|
---|
221 | } // main
|
---|
222 |
|
---|
223 | // Local Variables: //
|
---|
224 | // tab-width: 4 //
|
---|
225 | // compile-command: "cfa alloc.c" //
|
---|
226 | // End: //
|
---|