| [bd85400] | 1 | //                               -*- Mode: C -*- | 
|---|
|  | 2 | // | 
|---|
|  | 3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 4 | // | 
|---|
|  | 5 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 6 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 7 | // | 
|---|
|  | 8 | // alloc.c -- | 
|---|
|  | 9 | // | 
|---|
|  | 10 | // Author           : Peter A. Buhr | 
|---|
|  | 11 | // Created On       : Wed Feb  3 07:56:22 2016 | 
|---|
|  | 12 | // Last Modified By : Peter A. Buhr | 
|---|
| [7937abf] | 13 | // Last Modified On : Fri Mar 11 17:42:08 2016 | 
|---|
|  | 14 | // Update Count     : 59 | 
|---|
| [bd85400] | 15 | // | 
|---|
| [097e2b0] | 16 |  | 
|---|
| [7937abf] | 17 | forall( otype T ) T * malloc( char fill ); | 
|---|
|  | 18 | forall( dtype T ) T *?=?( T **, void * ); | 
|---|
|  | 19 | void *malloc( unsigned long int ); | 
|---|
|  | 20 | #if 0 | 
|---|
| [bd85400] | 21 | #include <fstream> | 
|---|
|  | 22 | #include <stdlib> | 
|---|
|  | 23 | extern "C" { | 
|---|
|  | 24 | #include <stdlib.h>                                                                             // access C malloc, realloc | 
|---|
|  | 25 | #include <stdio.h> | 
|---|
|  | 26 | } // exten "C" | 
|---|
| [097e2b0] | 27 |  | 
|---|
| [bd85400] | 28 | int * foo( int * p, int c ) { return p; } | 
|---|
|  | 29 | int * bar( int * p, int c ) { return p; } | 
|---|
|  | 30 | int * baz( int * p, int c ) { return p; } | 
|---|
| [7937abf] | 31 | #endif | 
|---|
| [097e2b0] | 32 |  | 
|---|
| [784deab] | 33 | int main( void ) { | 
|---|
| [7937abf] | 34 | #if 0 | 
|---|
| [bd85400] | 35 | size_t size = 10; | 
|---|
|  | 36 | int * p; | 
|---|
|  | 37 | struct S { int x; double y; } * s; | 
|---|
| [7937abf] | 38 | #endif | 
|---|
| [bd85400] | 39 |  | 
|---|
| [7937abf] | 40 | #if 0 | 
|---|
| [bd85400] | 41 | p = malloc( sizeof(*p) );                                                   // C malloc, type unsafe | 
|---|
|  | 42 | printf( "here1\n" ); | 
|---|
|  | 43 | free( p ); | 
|---|
|  | 44 | p = malloc();                                                                               // CFA malloc, type safe | 
|---|
|  | 45 | printf( "here2\n" ); | 
|---|
|  | 46 | free( p ); | 
|---|
| [7937abf] | 47 | #endif | 
|---|
|  | 48 | //    int * p; | 
|---|
|  | 49 | //    p = malloc( (char)'\0' );                                                                 // CFA malloc, type safe | 
|---|
|  | 50 | (int *)malloc( (char)'\0' );                                                                        // CFA malloc, type safe | 
|---|
|  | 51 | (void *)malloc( (char)'\0' );                                                                       // CFA malloc, type safe | 
|---|
|  | 52 | #if 0 | 
|---|
| [bd85400] | 53 | printf( "here3\n" ); | 
|---|
|  | 54 | p = malloc( p, 1000 );                                                              // CFA remalloc, type safe | 
|---|
|  | 55 | printf( "here4\n" ); | 
|---|
|  | 56 | free( p ); | 
|---|
|  | 57 | p = calloc( size, sizeof(*p) );                                             // C calloc, type unsafe | 
|---|
|  | 58 | printf( "here5\n" ); | 
|---|
|  | 59 | free( p ); | 
|---|
|  | 60 | p = calloc( size );                                                                 // CFA calloc, type safe | 
|---|
|  | 61 | printf( "here6\n" ); | 
|---|
|  | 62 | free( p ); | 
|---|
|  | 63 | p = calloc( size );                                                                 // CFA calloc, type safe | 
|---|
|  | 64 | p = realloc( p, 1000 );                                                             // C realloc, type unsafe | 
|---|
|  | 65 | p = realloc( p, 1000, '\0' );                                               // CFA realloc, type unsafe | 
|---|
|  | 66 | p = memset( p );                                                                    // CFA memset, type unsafe | 
|---|
|  | 67 | printf( "here7\n" ); | 
|---|
|  | 68 | free( p ); | 
|---|
|  | 69 | p = memalign( 16 ); | 
|---|
|  | 70 | printf( "here8\n" ); | 
|---|
|  | 71 | free( p ); | 
|---|
|  | 72 | posix_memalign( &p, 16 ); | 
|---|
|  | 73 | printf( "here9\n" ); | 
|---|
|  | 74 | free( p ); | 
|---|
| [7937abf] | 75 |  | 
|---|
| [bd85400] | 76 | float * fp = malloc() + 1; | 
|---|
|  | 77 | fprintf( stderr, "%p %p\n", fp, fp - 1 ); | 
|---|
|  | 78 | free( fp - 1 ); | 
|---|
|  | 79 | p = realloc( st1, size, '\0' );                                             // C realloc, type unsafe | 
|---|
|  | 80 |  | 
|---|
|  | 81 | double *y; | 
|---|
|  | 82 | x = memset( st1, '\0' );                                                    // SHOULD FAIL!! | 
|---|
|  | 83 |  | 
|---|
| [097e2b0] | 84 | int *p; | 
|---|
|  | 85 | p = foo( bar( baz( malloc(), 0 ), 0 ), 0 ); | 
|---|
|  | 86 | free( p ); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | struct St2 { int x; double y; }; | 
|---|
|  | 89 | struct St2 * st2; | 
|---|
|  | 90 |  | 
|---|
|  | 91 | y = malloc(); | 
|---|
|  | 92 | st1 = malloc(); | 
|---|
|  | 93 | //    st1 = realloc( st2, 10, st1 ); | 
|---|
|  | 94 |  | 
|---|
|  | 95 | *y = 1.0; | 
|---|
|  | 96 | printf("%f\n", *y); | 
|---|
|  | 97 |  | 
|---|
|  | 98 | st1->x = *x + 1; | 
|---|
|  | 99 | st1->y = *y *1.5; | 
|---|
|  | 100 | printf("{ %d, %f }\n", st1->x, st1->y); | 
|---|
|  | 101 |  | 
|---|
|  | 102 | free( y ); | 
|---|
|  | 103 |  | 
|---|
|  | 104 | x = malloc( 10 ); | 
|---|
|  | 105 | for ( int i = 0; i < 10; i += 1 ) { | 
|---|
|  | 106 | x[i] = i * 10; | 
|---|
|  | 107 | } | 
|---|
|  | 108 | for ( int j = 0; j < 10; j += 1 ) { | 
|---|
|  | 109 | printf( "x[%d] = %d\n", j, x[j] ); | 
|---|
|  | 110 | } | 
|---|
|  | 111 | free( x ); | 
|---|
|  | 112 | #endif | 
|---|
|  | 113 | } | 
|---|
| [bd85400] | 114 |  | 
|---|
|  | 115 | // Local Variables: // | 
|---|
|  | 116 | // tab-width: 4 // | 
|---|
|  | 117 | // compile-command: "cfa alloc.c" // | 
|---|
|  | 118 | // End: // | 
|---|