[cig-commits] r9347 - cs/avm/trunk
tan2 at geodynamics.org
tan2 at geodynamics.org
Thu Feb 14 14:13:16 PST 2008
Author: tan2
Date: 2008-02-14 14:13:16 -0800 (Thu, 14 Feb 2008)
New Revision: 9347
Modified:
cs/avm/trunk/avm-partition.c
cs/avm/trunk/avm-util.h
cs/avm/trunk/prem-derived-models.c
Log:
Check the status of malloc with a macro
Modified: cs/avm/trunk/avm-partition.c
===================================================================
--- cs/avm/trunk/avm-partition.c 2008-02-14 22:13:01 UTC (rev 9346)
+++ cs/avm/trunk/avm-partition.c 2008-02-14 22:13:16 UTC (rev 9347)
@@ -110,7 +110,7 @@
return 1;
}
}
- fields = malloc(num_columns * sizeof(double));
+ CHECK_MALLOC(fields = malloc(num_columns * sizeof(double)));
/* read content */
while (read_double_vector(in, num_columns, fields)
Modified: cs/avm/trunk/avm-util.h
===================================================================
--- cs/avm/trunk/avm-util.h 2008-02-14 22:13:01 UTC (rev 9346)
+++ cs/avm/trunk/avm-util.h 2008-02-14 22:13:16 UTC (rev 9347)
@@ -26,6 +26,17 @@
*/
+/* check that there is enough memory for malloc
+ * usage: CHECK_MALLOC(p = malloc(...))
+ */
+#define CHECK_MALLOC(alloc) \
+ if ((alloc) == NULL) { \
+ fprintf(stderr, "No memory: malloc failed at line %d, %s!\n", \
+ __LINE__, __FILE__); \
+ fflush(stderr); \
+ exit(1); \
+ }
+
int read_double_vector(FILE *in, int num_columns, double *fields);
void get_prem(double radius, double *vp, double *vs, double *rho);
Modified: cs/avm/trunk/prem-derived-models.c
===================================================================
--- cs/avm/trunk/prem-derived-models.c 2008-02-14 22:13:01 UTC (rev 9346)
+++ cs/avm/trunk/prem-derived-models.c 2008-02-14 22:13:16 UTC (rev 9347)
@@ -113,9 +113,9 @@
FILE *in;
prem_modulus_derv_t *p;
- /* if *p is NULL, we need to set up the context */
- if (!(*context)) return;
- *context = p = malloc(sizeof(prem_modulus_derv_t));
+ /* if *context is NULL, we need to set up the context */
+ if (*context != NULL) return;
+ CHECK_MALLOC(*context = p = malloc(sizeof(prem_modulus_derv_t)));
p->num_compositions = num_compositions;
/* check the # of input fields */
@@ -137,18 +137,18 @@
p->num_layers = num_layers;
/* allocate memory */
- p->layer_radius = malloc(num_layers * sizeof(double));
- p->layer_dksdt = malloc(num_layers * sizeof(double));
- p->layer_dmudt = malloc(num_layers * sizeof(double));
- p->layer_drhodt = malloc(num_layers * sizeof(double));
+ CHECK_MALLOC(p->layer_radius = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_dksdt = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_dmudt = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_drhodt = malloc(num_layers * sizeof(double)));
- p->layer_dksdc = malloc(num_layers * sizeof(double*));
- p->layer_dmudc = malloc(num_layers * sizeof(double*));
- p->layer_drhodc = malloc(num_layers * sizeof(double*));
+ CHECK_MALLOC(p->layer_dksdc = malloc(num_layers * sizeof(double*)));
+ CHECK_MALLOC(p->layer_dmudc = malloc(num_layers * sizeof(double*)));
+ CHECK_MALLOC(p->layer_drhodc = malloc(num_layers * sizeof(double*)));
for (i = 0; i < num_layers; ++i) {
- p->layer_dksdc[i] = malloc(num_compositions * sizeof(double));
- p->layer_dmudc[i] = malloc(num_compositions * sizeof(double));
- p->layer_drhodc[i] = malloc(num_compositions * sizeof(double));
+ CHECK_MALLOC(p->layer_dksdc[i] = malloc(num_compositions * sizeof(double)));
+ CHECK_MALLOC(p->layer_dmudc[i] = malloc(num_compositions * sizeof(double)));
+ CHECK_MALLOC(p->layer_drhodc[i] = malloc(num_compositions * sizeof(double)));
}
@@ -158,7 +158,7 @@
* dks/dc2, dmu/dc2, drho/dc2, ...]
*/
total_columns = 4 + 3 * num_compositions;
- tmp = malloc(total_columns * sizeof(double));
+ CHECK_MALLOC(tmp = malloc(total_columns * sizeof(double)));
for (i = 0; i < num_layers; ++i) {
itmp = read_double_vector(in, total_columns, tmp);
@@ -278,7 +278,7 @@
/* if *context is NULL, we need to set up the context */
if (*context != NULL) return;
- *context = p = malloc(sizeof(prem_velocity_derv_t));
+ CHECK_MALLOC(*context = p = malloc(sizeof(prem_velocity_derv_t)));
p->num_compositions = num_compositions;
/* check the # of input fields */
@@ -300,18 +300,18 @@
p->num_layers = num_layers;
/* allocate memory */
- p->layer_radius = malloc(num_layers * sizeof(double));
- p->layer_dvpdt = malloc(num_layers * sizeof(double));
- p->layer_dvsdt = malloc(num_layers * sizeof(double));
- p->layer_drhodt = malloc(num_layers * sizeof(double));
+ CHECK_MALLOC(p->layer_radius = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_dvpdt = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_dvsdt = malloc(num_layers * sizeof(double)));
+ CHECK_MALLOC(p->layer_drhodt = malloc(num_layers * sizeof(double)));
- p->layer_dvpdc = malloc(num_layers * sizeof(double*));
- p->layer_dvsdc = malloc(num_layers * sizeof(double*));
- p->layer_drhodc = malloc(num_layers * sizeof(double*));
+ CHECK_MALLOC(p->layer_dvpdc = malloc(num_layers * sizeof(double*)));
+ CHECK_MALLOC(p->layer_dvsdc = malloc(num_layers * sizeof(double*)));
+ CHECK_MALLOC(p->layer_drhodc = malloc(num_layers * sizeof(double*)));
for (i = 0; i < num_layers; ++i) {
- p->layer_dvpdc[i] = malloc(num_compositions * sizeof(double));
- p->layer_dvsdc[i] = malloc(num_compositions * sizeof(double));
- p->layer_drhodc[i] = malloc(num_compositions * sizeof(double));
+ CHECK_MALLOC(p->layer_dvpdc[i] = malloc(num_compositions * sizeof(double)));
+ CHECK_MALLOC(p->layer_dvsdc[i] = malloc(num_compositions * sizeof(double)));
+ CHECK_MALLOC(p->layer_drhodc[i] = malloc(num_compositions * sizeof(double)));
}
@@ -321,7 +321,7 @@
* dvp/dc2, dvs/dc2, drho/dc2, ...]
*/
total_columns = 4 + 3 * num_compositions;
- tmp = malloc(total_columns * sizeof(double));
+ CHECK_MALLOC(tmp = malloc(total_columns * sizeof(double)));
for (i = 0; i < num_layers; ++i) {
itmp = read_double_vector(in, total_columns, tmp);
More information about the cig-commits
mailing list