Clean up/renaming etc

This commit is contained in:
Shahram Najm 2017-10-27 11:53:46 +01:00
commit bf6c55e2f4
6 changed files with 181 additions and 27 deletions

View File

@ -954,6 +954,49 @@ subroutine codes_new_from_file (ifile, msgid , product_kind, status)
end if
end subroutine codes_new_from_file
!> Load in memory all messages from a file without decoding.
!>
!> @param ifile id of the file opened with @ref codes_open_file
!> @param nmessages number of messages loaded
!> @param status CODES_SUCCESS if OK, CODES_END_OF_FILE at the end of file, or error code
subroutine codes_any_load_all_from_file ( ifile, nmessages , status)
integer(kind=kindOfInt),intent(in) :: ifile
integer(kind=kindOfInt),intent(out) :: nmessages
integer(kind=kindOfInt) :: iret
integer(kind=kindOfInt),optional,intent(out) :: status
iret=any_f_load_all_from_file ( ifile, nmessages)
if (present(status)) then
status = iret
else
call grib_check(iret,'any_f_load_all_from_file','')
endif
end subroutine codes_any_load_all_from_file
!> Decode message from loaded.
!>
!> The message can be accessed through its msgid and it will be available\n
!> until @ref codes_release is called.\n
!>
!> @param imsg id of the binary message
!> @param msgid id of the message loaded in memory
!> @param status CODES_SUCCESS if OK, CODES_END_OF_FILE at the end of file, or error code
subroutine codes_any_new_from_loaded ( imsg, msgid , status)
integer(kind=kindOfInt),intent(in) :: imsg
integer(kind=kindOfInt),intent(out) :: msgid
integer(kind=kindOfInt) :: iret
integer(kind=kindOfInt),optional,intent(out) :: status
iret=any_f_new_from_loaded( imsg, msgid )
if (present(status)) then
status = iret
else
call grib_check(iret,'any_f_new_from_loaded','')
endif
end subroutine codes_any_new_from_loaded
!> Load in memory a message from a file.
!>
!> The message can be accessed through its msgid and it will be available\n

View File

@ -25,6 +25,9 @@ integer, external :: grib_f_new_from_message, &
codes_bufr_f_new_from_samples, &
grib_f_read_any_from_file, &
any_f_new_from_file, &
any_f_load_all_from_file, &
any_f_new_from_loaded, &
codes_f_clear_loaded_from_file, &
grib_f_new_from_file, &
bufr_f_new_from_file, &
grib_f_headers_only_new_from_file

View File

@ -135,6 +135,12 @@ struct l_bufr_keys_iterator {
l_bufr_keys_iterator* next;
};
typedef struct l_binary_message l_binary_message;
struct l_binary_message {
size_t size;
void* data;
};
static l_grib_handle* handle_set = NULL;
static l_grib_index* index_set = NULL;
static l_grib_multi_handle* multi_handle_set = NULL;
@ -142,6 +148,7 @@ static l_grib_file* file_set = NULL;
static l_grib_iterator* iterator_set = NULL;
static l_grib_keys_iterator* keys_iterator_set = NULL;
static l_bufr_keys_iterator* bufr_keys_iterator_set = NULL;
static grib_oarray* binary_messages = NULL;
static char* cast_char(char* buf, char* fortstr,int len)
{
@ -1621,6 +1628,86 @@ int grib_f_copy_namespace(int* gidsrc,char* name,int* giddest,int len){
return grib_f_copy_namespace_(gidsrc,name,giddest,len);
}
/*****************************************************************************/
int any_f_load_all_from_file(int* fid,int* n) {
int err = 0;
off_t offset=0;
void *data = NULL;
size_t olen = 0;
l_binary_message* msg=0;
FILE* f = get_file(*fid);
grib_context* c=grib_context_get_default();
/* this needs a callback to a destructor*/
/* grib_oarray_delete_content(c,binary_messages); */
grib_oarray_delete(c,binary_messages);
binary_messages=grib_oarray_new(c,1000,1000);
if (f) {
while (err!=GRIB_END_OF_FILE) {
data = wmo_read_any_from_file_malloc ( f, 0,&olen,&offset,&err );
msg=(l_binary_message*)grib_context_malloc_clear(c,sizeof(l_binary_message));
msg->data=data;
msg->size=olen;
if (err==0 && data) grib_oarray_push(c,binary_messages,msg);
}
if (err==GRIB_END_OF_FILE) err=0;
}
*n=binary_messages->n;
return err;
}
int any_f_load_all_from_file_(int* fid,int* n) {
return any_f_load_all_from_file(fid,n);
}
int any_f_load_all_from_file__(int* fid,int* n) {
return any_f_load_all_from_file(fid,n);
}
int any_f_new_from_loaded(int* msgid,int* gid)
{
grib_handle *h = NULL;
grib_context* c=grib_context_get_default();
/* fortran convention of 1 based index*/
const int n=*msgid-1;
l_binary_message* msg=grib_oarray_get(binary_messages,n);
if (msg && msg->data)
h=grib_handle_new_from_message_copy (c,msg->data,msg->size);
if(h){
push_handle(h,gid);
return GRIB_SUCCESS;
} else {
*gid=-1;
return GRIB_END_OF_FILE;
}
*gid=-1;
return GRIB_INVALID_FILE;
}
int any_f_new_from_loaded_(int* msgid,int* gid){
return any_f_new_from_loaded(msgid,gid);
}
int any_f_new_from_loaded__(int* msgid,int* gid){
return any_f_new_from_loaded(msgid,gid);
}
int codes_f_clear_loaded_from_file() {
grib_context* c=grib_context_get_default();
/* grib_oarray_delete_content(c,binary_messages); */
grib_oarray_delete(c,binary_messages);
return GRIB_SUCCESS;
}
int codes_f_clear_loaded_from_file_() {
return codes_f_clear_loaded_from_file();
}
int codes_f_clear_loaded_from_file__() {
return codes_f_clear_loaded_from_file();
}
/*****************************************************************************/
int grib_f_count_in_file(int* fid,int* n) {
int err = 0;
@ -3026,9 +3113,10 @@ int grib_f_set_string_array_(int* gid, char* key, char* val,int* nvals,int* slen
size_t lsize = *nvals;
char** cval=0;
char* p=val;
grib_context* c=h->context;
grib_context* c;
if(!h) return GRIB_INVALID_GRIB;
c=h->context;
cval=(char**)grib_context_malloc_clear(h->context,sizeof(char*)*lsize);
for (i=0;i<lsize;i++) {

View File

@ -141,6 +141,18 @@ int any_f_new_from_file_(int *fid, int *gid);
int any_f_new_from_file__(int *fid, int *gid);
int any_f_new_from_file(int *fid, int *gid);
int any_f_load_all_from_file_(int* fid,int* n);
int any_f_load_all_from_file__(int* fid,int* n);
int any_f_load_all_from_file(int* fid,int* n);
int any_f_new_from_loaded_(int* msgid,int* gid);
int any_f_new_from_loaded__(int* msgid,int* gid);
int any_f_new_from_loaded(int* msgid,int* gid);
int codes_f_clear_loaded_from_file_();
int codes_f_clear_loaded_from_file__();
int codes_f_clear_loaded_from_file();
int grib_f_new_from_file_(int *fid, int *gid);
int grib_f_new_from_file__(int *fid, int *gid);
int grib_f_new_from_file(int *fid, int *gid);

View File

@ -63,25 +63,15 @@ static void init()
#if MANAGE_MEM
#else
static long cnt = 0;
static long cntp = 0;
static void default_long_lasting_free(const grib_context* c, void* p)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
free(p);
GRIB_MUTEX_LOCK(&mutex_mem);
cntp--;
GRIB_MUTEX_UNLOCK(&mutex_mem);
}
static void* default_long_lasting_malloc(const grib_context* c, size_t size)
{
void* ret;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex_mem);
cntp++;
GRIB_MUTEX_UNLOCK(&mutex_mem);
ret=malloc(size);
if (!ret) {
grib_context_log(c,GRIB_LOG_FATAL,"default_long_lasting_malloc: error allocating %lu bytes",(unsigned long)size);
@ -92,20 +82,12 @@ static void* default_long_lasting_malloc(const grib_context* c, size_t size)
static void default_buffer_free(const grib_context* c, void* p)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
free(p);
GRIB_MUTEX_LOCK(&mutex_mem);
cntp--;
GRIB_MUTEX_UNLOCK(&mutex_mem);
}
static void* default_buffer_malloc(const grib_context* c, size_t size)
{
void* ret;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex_mem);
cntp++;
GRIB_MUTEX_UNLOCK(&mutex_mem);
ret=malloc(size);
if (!ret) {
grib_context_log(c,GRIB_LOG_FATAL,"default_buffer_malloc: error allocating %lu bytes",(unsigned long)size);
@ -127,20 +109,12 @@ static void* default_buffer_realloc(const grib_context* c, void* p, size_t size)
static void default_free(const grib_context* c, void* p)
{
GRIB_MUTEX_INIT_ONCE(&once,&init);
free(p);
GRIB_MUTEX_LOCK(&mutex_mem);
cnt--;
GRIB_MUTEX_UNLOCK(&mutex_mem);
}
static void* default_malloc(const grib_context* c, size_t size)
{
void* ret;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex_mem);
cnt++;
GRIB_MUTEX_UNLOCK(&mutex_mem);
ret=malloc(size);
if (!ret) {
grib_context_log(c,GRIB_LOG_FATAL,"default_malloc: error allocating %lu bytes",(unsigned long)size);

View File

@ -10,6 +10,36 @@
#include "grib_api_internal.h"
#if GRIB_PTHREADS
static pthread_once_t once = PTHREAD_ONCE_INIT;
static pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
static void init() {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mutex1,&attr);
pthread_mutex_init(&mutex2,&attr);
pthread_mutexattr_destroy(&attr);
}
#elif GRIB_OMP_THREADS
static int once = 0;
static omp_nest_lock_t mutex1;
static omp_nest_lock_t mutex2;
static void init()
{
GRIB_OMP_CRITICAL(lock_grib_io_c)
{
if (once == 0)
{
omp_init_nest_lock(&mutex1);
omp_init_nest_lock(&mutex2);
once = 1;
}
}
}
#endif
#define GRIB 0x47524942
#define BUDG 0x42554447
@ -1332,7 +1362,11 @@ static void *_wmo_read_any_from_file_malloc(FILE* f,int* err,size_t *size,off_t
r.headers_only = headers_only;
r.offset = 0;
GRIB_MUTEX_INIT_ONCE(&once,&init);
GRIB_MUTEX_LOCK(&mutex1);
*err = read_any(&r, grib_ok, bufr_ok, hdf5_ok, wrap_ok);
GRIB_MUTEX_UNLOCK(&mutex1);
*size = r.message_size;
*offset = r.offset;