source: src/tests/alloc.c @ f710aca

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f710aca was 91c389a, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change usages of assert to assert.h

  • Property mode set to 100644
File size: 7.9 KB
Line 
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 : Thu Jul 20 16:01:10 2017
13// Update Count     : 318
14//
15
16#include <assert.h>
17#include <malloc.h>                                                                             // malloc_usable_size
18#include <stdint.h>                                                                             // uintptr_t
19#include <stdlib.h>                                                                             // posix_memalign
20#include <fstream>
21#include <stdlib>                                                                               // access C malloc, realloc
22
23int * foo( int * p, int c ) { return p; }
24int * bar( int * p, int c ) { return p; }
25int * baz( int * p, int c ) { return p; }
26
27int main( void ) {
28    size_t dim = 10;
29    int * p;
30        char fill = '\1';
31
32        // allocation, non-array types
33
34    p = (void *)malloc( sizeof(*p) );                                   // C malloc, type unsafe
35        *p = 0xdeadbeef;
36        printf( "C   malloc %#x\n", *p );
37    free( p );
38
39    p = malloc();                                                                               // CFA malloc, type safe
40        *p = 0xdeadbeef;
41        printf( "CFA malloc %#x\n", *p );
42    free( p );
43
44    p = alloc();                                                                                // CFA alloc, type safe
45        *p = 0xdeadbeef;
46        printf( "CFA alloc %#x\n", *p );
47    free( p );
48
49    p = alloc( fill );                                                                  // CFA alloc, fill
50        printf( "CFA alloc, fill %08x\n", *p );
51
52
53        // allocation, array types
54        printf( "\n" );
55
56    p = calloc( dim, sizeof( *p ) );                                    // C array calloc, type unsafe
57        printf( "C   array calloc, fill 0\n" );
58        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
59        printf( "\n" );
60    free( p );
61
62    p = calloc( dim );                                                                  // CFA array calloc, type safe
63        printf( "CFA array calloc, fill 0\n" );
64        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
65        printf( "\n" );
66    free( p );
67
68    p = alloc( dim );                                                                   // CFA array alloc, type safe
69        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
70        printf( "CFA array alloc, no fill\n" );
71        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
72        printf( "\n" );
73    free( p );
74
75    p = alloc( 2 * dim, fill );                                                 // CFA array alloc, fill
76        printf( "CFA array alloc, fill %#x\n", fill );
77        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
78        printf( "\n" );
79        // do not free
80
81
82        // resize, non-array types
83        printf( "\n" );
84
85    p = (void *)realloc( p, dim * sizeof(*p) );                 // C realloc
86        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
87        printf( "C   realloc\n" );
88        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
89        printf( "\n" );
90
91    p = realloc( p, 2 * dim * sizeof(*p) );                             // CFA realloc
92        for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; }
93        printf( "CFA realloc\n" );
94        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
95        printf( "\n" );
96        // do not free
97
98
99        // resize, array types
100        printf( "\n" );
101
102    p = alloc( p, dim );                                                                // CFA resize array alloc
103        for ( int i = 0; i < dim; i += 1 ) { p[i] = 0xdeadbeef; }
104        printf( "CFA resize alloc\n" );
105        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
106        printf( "\n" );
107
108    p = alloc( p, 2 * dim );                                                    // CFA resize array alloc
109        for ( int i = dim; i < 2 * dim; i += 1 ) { p[i] = 0x1010101; }
110        printf( "CFA resize array alloc\n" );
111        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
112        printf( "\n" );
113
114    p = alloc( p, dim );                                                                // CFA array alloc
115        printf( "CFA resize array alloc\n" );
116        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
117        printf( "\n" );
118
119        free( p );
120        p = 0;
121
122    p = alloc( p, dim, fill );                                                  // CFA array alloc, fill
123        printf( "CFA resize array alloc, fill\n" );
124        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] ); }
125        printf( "\n" );
126
127    p = alloc( p, 2 * dim, fill );                                              // CFA array alloc, fill
128        printf( "CFA resize array alloc, fill\n" );
129        for ( int i = 0; i < 2 * dim; i += 1 ) { printf( "%#x ", p[i] ); }
130        printf( "\n" );
131
132    p = alloc( p, dim, fill );                                                  // CFA array alloc, fill
133        printf( "CFA resize array alloc, fill\n" );
134        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x ", p[i] );; }
135        printf( "\n" );
136        free( p );
137
138
139    struct Struct { int x; double y; };
140    Struct st, st1, sta[dim], sta1[dim], * stp, * stp1;
141
142        // alignment, non-array types
143        printf( "\n" );
144        enum { Alignment = 128 };
145
146    stp = (memalign( Alignment, sizeof( *stp ) ) ){ 42, 42.5 }; // C memalign
147        assert( (uintptr_t)stp % Alignment == 0 );
148        printf( "C   memalign %d %g\n", stp->x, stp->y );
149    free( stp );
150
151    stp = (memalign( Alignment )){ 42, 42.5 };                  // CFA memalign
152        assert( (uintptr_t)stp % Alignment == 0 );
153        printf( "CFA memalign %d %g\n", stp->x, stp->y );
154    free( stp );
155
156    posix_memalign( (void **)&stp, Alignment, sizeof( *stp ) ); // C posix_memalign
157        *stp = (Struct){ 42, 42.5 };
158        assert( (uintptr_t)stp % Alignment == 0 );
159        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
160    free( stp );
161
162    posix_memalign( &stp, Alignment );                                  // CFA posix_memalign
163        *stp = (Struct){ 42, 42.5 };
164        assert( (uintptr_t)stp % Alignment == 0 );
165        printf( "CFA posix_memalign %d %g\n", stp->x, stp->y );
166    free( stp );
167
168    stp = (aligned_alloc( Alignment )){ 42, 42.5 };             // CFA aligned_alloc
169        assert( (uintptr_t)stp % Alignment == 0 );
170        printf( "CFA aligned_alloc %d %g\n", stp->x, stp->y );
171    free( stp );
172
173    stp = (align_alloc( Alignment )){ 42, 42.5 };               // CFA align_alloc
174        assert( (uintptr_t)stp % Alignment == 0 );
175        printf( "CFA align_alloc %d %g\n", stp->x, stp->y );
176    free( stp );
177
178    stp = align_alloc( Alignment, fill );                               // CFA memalign, fill
179        assert( (uintptr_t)stp % Alignment == 0 );
180        printf( "CFA align_alloc fill %#x %a\n", stp->x, stp->y );
181    free( stp );
182
183
184        // alignment, array types
185        printf( "\n" );
186
187    stp = align_alloc( Alignment, dim );                                // CFA array memalign
188        assert( (uintptr_t)stp % Alignment == 0 );
189        for ( int i = 0; i < dim; i += 1 ) { stp[i] = (Struct){ 42, 42.5 }; }
190        printf( "CFA array align_alloc\n" );
191        for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
192        printf( "\n" );
193    free( stp );
194
195    stp = align_alloc( Alignment, dim, fill );                  // CFA array memalign, fill
196        assert( (uintptr_t)stp % Alignment == 0 );
197        printf( "CFA array align_alloc, fill\n" );
198        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", stp[i].x, stp[i].y ); }
199        printf( "\n" );
200    free( stp );
201
202
203        // data, non-array types
204        printf( "\n" );
205
206    memset( &st, fill );                                                                // CFA memset, type safe
207        printf( "CFA memset %#x %a\n", st.x, st.y );
208    memcpy( &st1, &st );                                                                // CFA memcpy, type safe
209        printf( "CFA memcpy %#x %a\n", st1.x, st1.y );
210
211
212        // data, array types
213        printf( "\n" );
214
215    memset( sta, dim, fill );                                                   // CFA array memset, type safe
216        printf( "CFA array memset\n" );
217        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta[i].x, sta[i].y ); }
218        printf( "\n" );
219
220    memcpy( sta1, sta, dim );                                                   // CFA array memcpy, type safe
221        printf( "CFA memcpy\n" );
222        for ( int i = 0; i < dim; i += 1 ) { printf( "%#x %a, ", sta1[i].x, sta1[i].y ); }
223        printf( "\n" );
224
225
226        // new, non-array types
227        printf( "\n" );
228
229        stp = new( 42, 42.5 );
230        stp1 = new( 42, 42.5 );
231        printf( "CFA new initialize\n%d %g %d %g\n", stp->x, stp->y, stp1->x, stp1->y );
232        delete( stp, stp1 );
233
234        // new, array types
235        stp = anew( dim, 42, 42.5 );
236        printf( "CFA array new initialize\n" );
237        for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp[i].x, stp[i].y ); }
238        printf( "\n" );
239        stp1 = anew( dim, 42, 42.5 );
240        for ( int i = 0; i < dim; i += 1 ) { printf( "%d %g, ", stp1[i].x, stp1[i].y ); }
241        printf( "\n" );
242        adelete( dim, stp, dim, stp1 );
243
244        // extras
245        printf( "\n" );
246
247    float * fp = malloc() + 1;
248    printf( "pointer arithmetic %d\n", fp == fp - 1 );
249    free( fp - 1 );
250
251    p = foo( bar( baz( malloc(), 0 ), 0 ), 0 );
252        *p = 0xdeadbeef;
253        printf( "CFA deep malloc %#x\n", *p );
254    free( p );
255
256        stp = malloc();
257        printf( "\nSHOULD FAIL\n" );
258    p = alloc( stp, dim * sizeof(*stp) );
259    p = memset( stp, 10 );
260    p = memcpy( &st1, &st );
261} // main
262
263// Local Variables: //
264// tab-width: 4 //
265// compile-command: "cfa alloc.c" //
266// End: //
Note: See TracBrowser for help on using the repository browser.