source: src/tests/alloc.c @ f2b12406

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 f2b12406 was a4683611, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

third attempt at memory-allocation routines

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