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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/**
* Copyright 1981-2016 ECMWF.
*
* This software is licensed under the terms of the Apache Licence
* Version 2.0 which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
*
* In applying this licence, ECMWF does not waive the privileges and immunities
* granted to it by virtue of its status as an intergovernmental organisation
* nor does it submit to any jurisdiction.
*/
#include <stdio.h>
#include <stdlib.h>
#include "common/JPointer.h"
#include "common/fortint.h"
#include "common/fortreal.h"
/*
* memory containers and handlers (not to be used directly)
*/
static struct generic_userspace_t {
JPointer array;
int nbytes;
int isstatic;
}
generic_userspace[11] = {0,};
int generic_userspace_static(struct generic_userspace_t *curr, fortfloat *array_, fortint *nbytes_)
{
if (curr->array)
free(curr->array);
curr->array = (JPointer) array_;
curr->nbytes = *nbytes_;
curr->isstatic = 1;
return 0;
}
JPointer generic_userspace_get(struct generic_userspace_t *curr, fortint *nbytes_)
{
JPointer temp_array = NULL;
/* if current user-space memory can contain request, return */
if ( ((curr->array!=NULL) && (curr->nbytes>=*nbytes_))
|| (nbytes_<=0) )
return curr->array;
if (curr->isstatic==1) {
/* static memory management (ProdGen-style) */
fprintf(stderr,
"ERROR: userspace static allocation too small "
"(current: %db, required: %db)\n", curr->nbytes, *nbytes_);
return 0;
}
else if (curr->nbytes<*nbytes_) {
/* dynamic memory management: reallocate as necessary */
if (curr->array!=NULL)
free(curr->array);
/*
fprintf(stdout,
"INFO: userspace dynamic allocation requested: "
"%ldb\n", *nbytes_);
*/
temp_array = (JPointer) malloc(*nbytes_);
if (temp_array==NULL) {
fprintf(stderr,
"ERROR: userspace dynamic allocation failure "
"(requested: %db)\n", *nbytes_);
return 0;
}
curr->nbytes = *nbytes_;
curr->array = temp_array;
}
return curr->array;
}
int generic_userspace_free(struct generic_userspace_t *curr)
{
if (curr->array)
free(curr->array);
curr->nbytes = 0;
curr->isstatic = 0;
return 0;
}
/*
* HIRLAM/HIRLAMW/HIRLSM memory management interfaces
*/
int hirlam_userspace_1_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 0],array_,nbytes_); }
int hirlam_userspace_2_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 1],array_,nbytes_); }
int hirlam_userspace_3_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 2],array_,nbytes_); }
int hirlamw_userspace_1_static_(fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 3],array_,nbytes_); }
int hirlamw_userspace_2_static_(fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 4],array_,nbytes_); }
int hirlamw_userspace_3_static_(fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 5],array_,nbytes_); }
int hirlsm_userspace_1_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 6],array_,nbytes_); }
int hirlsm_userspace_2_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 7],array_,nbytes_); }
int hirlsm_userspace_3_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 8],array_,nbytes_); }
int hirlsm_userspace_4_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[ 9],array_,nbytes_); }
int hirlsm_userspace_5_static_ (fortfloat *array_, fortint *nbytes_) { return generic_userspace_static(&generic_userspace[10],array_,nbytes_); }
JPointer hirlam_userspace_1_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 0],nbytes_); }
JPointer hirlam_userspace_2_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 1],nbytes_); }
JPointer hirlam_userspace_3_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 2],nbytes_); }
JPointer hirlamw_userspace_1_get_(fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 3],nbytes_); }
JPointer hirlamw_userspace_2_get_(fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 4],nbytes_); }
JPointer hirlamw_userspace_3_get_(fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 5],nbytes_); }
JPointer hirlsm_userspace_1_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 6],nbytes_); }
JPointer hirlsm_userspace_2_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 7],nbytes_); }
JPointer hirlsm_userspace_3_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 8],nbytes_); }
JPointer hirlsm_userspace_4_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[ 9],nbytes_); }
JPointer hirlsm_userspace_5_get_ (fortint *nbytes_) { return generic_userspace_get(&generic_userspace[10],nbytes_); }
int hirlam_userspace_free_ () {
generic_userspace_free(&generic_userspace[ 0]);
generic_userspace_free(&generic_userspace[ 1]);
generic_userspace_free(&generic_userspace[ 2]);
return 0;
}
int hirlamw_userspace_free_() {
generic_userspace_free(&generic_userspace[ 3]);
generic_userspace_free(&generic_userspace[ 4]);
generic_userspace_free(&generic_userspace[ 5]);
return 0;
}
int hirlsm_userspace_free_ () {
generic_userspace_free(&generic_userspace[ 6]);
generic_userspace_free(&generic_userspace[ 7]);
generic_userspace_free(&generic_userspace[ 8]);
generic_userspace_free(&generic_userspace[ 9]);
generic_userspace_free(&generic_userspace[10]);
return 0;
}
|