-
Notifications
You must be signed in to change notification settings - Fork 6
/
hydroalloc_mem.c
201 lines (117 loc) · 3.25 KB
/
hydroalloc_mem.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*-------------------------------------------------------------------------------------------
* hydroalloc_mem.c
*
* Author: Albert Kettner, March 2006
*
* Allocate memory for 1D, 2D, 3D for any variable: int, fload, double, struct, etc!
* Function definitions are in hydroalloc_mem.h
*
* Variable Def.Location Type Units Usage
* -------- ------------ ---- ----- -----
*
*-------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#ifdef WITH_UNISTD
# include <unistd.h>
#endif
#include "hydroalloc_mem.h"
/* FUNCTION ALLOCATE_1D FILE */
FILE ** allocate_1d_F (int nrows)
{
FILE ** i;
i = (FILE **) malloc (nrows * sizeof (FILE *));
if (!i)
{
perror ("allocate_1d_F");
exit (-1);
}
return i;
}
/*------------------------------------------------------
* ...for allocating a 1D "matrix" of whatever!
*
* note that sizeof(void*) is the same as sizeof(double*)
* is the same as sizeof(float*), etc. A pointer always
* has the same size (the size of the memory address.)
*------------------------------------------------------*/
void *
matrixalloc1D (int max_width, long size)
{
void *atemp;
atemp = (void *) malloc (max_width * size);
if (!atemp)
{
perror ("matrixalloc");
exit (1);
}
return atemp;
}
/*------------------------------------------------------
* ...for allocating a 2D matrix of whatever!
*
* note that sizeof(void*) is the same as sizeof(double*)
* is the same as sizeof(float*), etc. A pointer always
* has the same size (the size of the memory address.)
*------------------------------------------------------*/
void **
matrixalloc2D (int max_width, int max_length, long size)
{
int i;
void **atemp;
atemp = (void **) malloc (max_width * sizeof (void *));
if (!atemp)
{
perror ("matrixalloc");
exit (1);
}
for (i = 0; i < max_width; ++i)
{
atemp[i] = (void *) malloc (max_length * size);
if (!atemp[i])
{
perror ("matrixalloc");
exit (1);
}
}
return atemp;
}
/*------------------------------------------------------
* ...for allocating a 3D matrix of whatever!
*
* note that sizeof(void*) is the same as sizeof(double*)
* is the same as sizeof(float*), etc. A pointer always
* has the same size (the size of the memory address.)
*------------------------------------------------------*/
void ***
matrixalloc3D (int max_width, int max_length, int max_height, long size)
{
int i, j;
void ***atemp;
atemp = (void ***) malloc (max_width * sizeof (void *));
if (!atemp)
{
perror ("matrixalloc");
exit (1);
}
for (i = 0; i < max_width; ++i)
{
atemp[i] = (void **) malloc (max_length * sizeof (void *));
if (!atemp[i])
{
perror ("matrixalloc");
exit (1);
}
}
for (i = 0; i < max_width; ++i)
for (j = 0; j < max_length; ++j)
{
atemp[i][j] = (void *) malloc (max_height * size);
if (!atemp[i][j])
{
perror ("matrixalloc");
exit (1);
}
}
return atemp;
}