source: stable/2.4/ADOL-C/src/adalloc.c @ 412

Last change on this file since 412 was 364, checked in by kulshres, 7 years ago

change alloc parameters to size_t

to avoid inadventent wrap arounds and not artificially limit the memory sizes. also adjust print format for error messages accordingly

From: Jean Utke <utke@…>

  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 KB
Line 
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File:     adalloc.c
4 Revision: $Id: adalloc.c 364 2012-11-15 11:51:15Z kulshres $
5 Contents: C allocation of arrays of doubles in several dimensions
6 
7 Copyright (c) Andrea Walther, Andreas Griewank, Andreas Kowarz,
8               Hristo Mitev, Sebastian Schlenkrich, Jean Utke, Olaf Vogel
9 
10 This file is part of ADOL-C. This software is provided as open source.
11 Any use, reproduction, or distribution of the software constitutes
12 recipient's acceptance of the terms of the accompanying license file.
13 
14----------------------------------------------------------------------------*/
15
16#include <adolc/adalloc.h>
17
18#if defined(ADOLC_USE_CALLOC)
19#  if defined(HAVE_MALLOC)
20#     define ADOLC_MALLOC(n,m) calloc(n,m)
21#  else
22#     define ADOLC_MALLOC(n,m) rpl_calloc(n,m)
23#  endif
24#else
25#  if defined(HAVE_MALLOC)
26#     define ADOLC_MALLOC(n,m) malloc(n*m)
27#  else
28#     define ADOLC_MALLOC(n,m) rpl_malloc(n,m)
29#  endif
30#endif
31
32BEGIN_C_DECLS
33
34/****************************************************************************/
35/*                                              MEMORY MANAGEMENT UTILITIES */
36
37/*--------------------------------------------------------------------------*/
38double* myalloc1(size_t m) {
39    double* A = (double*)ADOLC_MALLOC(m,sizeof(double));
40    if (A == NULL) {
41        fprintf(DIAG_OUT,"ADOL-C error: myalloc1 cannot allocate %zd bytes\n",
42                (size_t)(m*sizeof(double)));
43        exit (-1);
44    }
45    return A;
46}
47
48/*--------------------------------------------------------------------------*/
49double** myalloc2(size_t m, size_t n) {
50    double *Adum = (double*)ADOLC_MALLOC(m*n,sizeof(double));
51    double   **A = (double**)malloc(m*sizeof(double*));
52    int i;
53    if (Adum == NULL) {
54        fprintf(DIAG_OUT,"ADOL-C error: myalloc2 cannot allocate %zd bytes\n",
55                (size_t)(m*n*sizeof(double)));
56        exit (-1);
57    }
58    if (A == NULL) {
59        fprintf(DIAG_OUT,"ADOL-C error: myalloc2 cannot allocate %zd bytes\n",
60                (size_t)(m*sizeof(double*)));
61        exit (-1);
62    }
63    for (i=0; i<m; i++) {
64        A[i] = Adum;
65        Adum += n;
66    }
67    return A;
68}
69
70/*--------------------------------------------------------------------------*/
71double*** myalloc3(size_t m, size_t n, size_t p) { /* This function allocates 3-tensors contiguously */
72    double *Adum = (double*) ADOLC_MALLOC(m*n*p,sizeof(double));
73    double **Apt = (double**)malloc(m*n*sizeof(double*));
74    double  ***A = (double***)malloc(m*sizeof(double**));
75    int i,j;
76    if (Adum == NULL) {
77        fprintf(DIAG_OUT,"ADOL-C error: myalloc3 cannot allocate %zd bytes\n",
78                (size_t)(m*n*p*sizeof(double)));
79        exit (-1);
80    }
81    if (Apt == NULL) {
82        fprintf(DIAG_OUT,"ADOL-C error: myalloc3 cannot allocate %zd bytes\n",
83                (size_t)(m*n*sizeof(double*)));
84        exit (-1);
85    }
86    if (A == NULL) {
87        fprintf(DIAG_OUT,"ADOL-C error: myalloc3 cannot allocate %zd bytes\n",
88                (size_t)(m*sizeof(double**)));
89        exit (-1);
90    }
91    for (i=0; i<m; i++) {
92        A[i] = Apt;
93        for (j=0; j<n; j++) {
94            *Apt++ =  Adum;
95            Adum += p;
96        }
97    }
98    return A;
99}
100
101/*--------------------------------------------------------------------------*/
102void myfree1(double   *A) {
103    free((char*) A);
104}
105
106/*--------------------------------------------------------------------------*/
107void myfree2(double  **A) {
108    free((char*)*A);
109    free((char*) A);
110}
111
112/*--------------------------------------------------------------------------*/
113void myfree3(double ***A) {
114    free((char*)**A);
115    free((char*)*A);
116    free((char*) A);
117}
118
119
120/****************************************************************************/
121/*                                          SPECIAL IDENTITY REPRESENTATION */
122
123/*--------------------------------------------------------------------------*/
124double   **myallocI2(int n) {
125    double *Idum = (double*)ADOLC_MALLOC((2*n-1),sizeof(double));
126    double   **I = (double**)malloc(n*sizeof(double*));
127    int i;
128    if (Idum == NULL) {
129        fprintf(DIAG_OUT,"ADOL-C error: myallocI2 cannot allocate %i bytes\n",
130                (int)((2*n-1)*sizeof(double)));
131        exit (-1);
132    }
133    if (I == NULL) {
134        fprintf(DIAG_OUT,"ADOL-C error: myallocI2 cannot allocate %i bytes\n",
135                (int)(n*sizeof(double*)));
136        exit (-1);
137    }
138    Idum += (n - 1);
139    I[0] = Idum;
140    *Idum = 1.0;
141    /* 20020628 olvo n3l: Initialization to 0 */
142    for (i=1; i<n; i++)
143        *(++Idum)= 0.0;
144    Idum-=(n-1);
145    for (i=1; i<n; i++) {
146        I[i] = --Idum;
147        *Idum = 0.0;
148    }
149    return I;
150}
151
152/*--------------------------------------------------------------------------*/
153void myfreeI2(int n, double** I) {
154    free((char*)I[n-1]);
155    free((char*) I);
156}
157
158/****************************************************************************/
159/*                              INTEGER VARIANT FOR BIT PATTERN PROPAGATION */
160
161/* ------------------------------------------------------------------------- */
162unsigned int *myalloc1_uint(int m) {
163    unsigned int *A = (unsigned int*)malloc(m*sizeof(unsigned int));
164    if (A == NULL) {
165        fprintf(DIAG_OUT, "ADOL-C error, "__FILE__
166                ":%i : \nmyalloc1_ushort cannot allocate %i bytes\n",
167                __LINE__, (int)(m*sizeof(unsigned int)));
168        exit (-1);
169    } /* endif */
170    return A;
171}
172
173
174/* ------------------------------------------------------------------------- */
175unsigned long int *myalloc1_ulong(int m) {
176    unsigned long int *A = (unsigned long int*)  calloc(m,sizeof(unsigned long int));
177    if (A == NULL) {
178        fprintf(DIAG_OUT, "ADOL-C error, "__FILE__
179                ":%i : \nmyalloc1_ulong cannot allocate %i bytes\n",
180                __LINE__, (int)(m*sizeof(unsigned long int)));
181        exit (-1);
182    } /* endif */
183    return A;
184}
185
186
187/* ------------------------------------------------------------------------- */
188unsigned long int **myalloc2_ulong(int m,int n) {
189    unsigned long int *Adum = (unsigned long int*)  calloc(m*n,sizeof(unsigned long int));
190    unsigned long int **A   = (unsigned long int**) calloc(m,sizeof(unsigned long int*));
191    int i;
192    if (Adum == NULL) {
193        fprintf(DIAG_OUT, "ADOL-C error, "__FILE__
194                ":%i : \nmyalloc2_ulong cannot allocate %i bytes\n",
195                __LINE__, (int)(m*n*sizeof(unsigned long int)));
196        exit (-1);
197    } /* endif */
198    if (A == NULL) {
199        fprintf(DIAG_OUT, "ADOL-C error, "__FILE__
200                ":%i : \nmyalloc2_ulong cannot allocate %i bytes\n",
201                __LINE__, (int)(m*sizeof(unsigned long int*)));
202        exit (-1);
203    } /* endif */
204    for(i=0;i<m;i++) {
205        A[i] = Adum;
206        Adum += n;
207    }
208    return A;
209
210    /* To deallocate an array set up by   A = myalloc2_ulong(m,n)   */
211    /*    use  free((char*)*A); free((char*)A);  in that order      */
212
213}
214
215
216/* ------------------------------------------------------------------------ */
217
218void myfree1_uint(unsigned int *A) {
219    free((char *)A);
220}
221
222/* ------------------------------------------------------------------------ */
223
224void myfree1_ulong(unsigned long int *A) {
225    free((char *)A);
226}
227
228/* ------------------------------------------------------------------------ */
229
230void myfree2_ulong(unsigned long int **A) {
231    free((char *)*A);
232    free((char *)A);
233}
234
235
236END_C_DECLS
237
Note: See TracBrowser for help on using the repository browser.