Newer
Older
* fitsbody.c
*
* Handle memory allocation for FITS bodies.
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
* This file part of: AstrOmatic FITS/LDAC library
* Copyright: (C) 1995-2010 IAP/CNRS/SorbonneU
* License: GNU General Public License
* AstrOmatic software is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
* AstrOmatic software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with AstrOmatic software.
* If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 11/02/2020
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
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
148
149
150
151
152
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include "fitscat_defs.h"
#include "fitscat.h"
size_t body_maxram = BODY_DEFRAM,
body_maxvram = BODY_DEFVRAM,
body_ramleft, body_vramleft, body_ramflag;
int body_vmnumber;
char body_swapdirname[MAXCHARS] = BODY_DEFSWAPDIR;
/******* alloc_body ***********************************************************
PROTO PIXTYPE *alloc_body(tabstruct *tab,
void (*func)(PIXTYPE *ptr, int npix))
PURPOSE Allocate memory for and read a FITS data body (read-only). If not
enough RAM is available, a swap file is created.
INPUT Table (tab) structure.
OUTPUT Pointer to the mapped data if OK, or NULL otherwise.
NOTES The file pointer must be positioned at the beginning of the data.
AUTHOR E. Bertin (IAP)
VERSION 05/05/2001
***/
PIXTYPE *alloc_body(tabstruct *tab, void (*func)(PIXTYPE *ptr, int npix))
{
FILE *file;
PIXTYPE *buffer;
size_t npix, size, sizeleft, spoonful;
if (!body_ramflag)
{
body_ramleft = body_maxram;
body_vramleft = body_maxvram;
body_ramflag = 1;
}
/* Return a NULL pointer if size is zero */
if (!tab->tabsize)
return (PIXTYPE *)NULL;
/* Check that there is a cat parent structure and that the file is open */
if (tab->cat && !tab->cat->file)
error(EXIT_FAILURE, "*Internal Error*: Cannot access table: ",
tab->extname);
/* Decide if the data will go in physical memory or on swap-space */
npix = tab->tabsize/tab->bytepix;
size = npix*sizeof(PIXTYPE);
if (size < body_ramleft)
{
/*-- There should be enough RAM left: try to do a malloc() */
if ((tab->bodybuf = malloc(size)))
{
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
read_body(tab, (PIXTYPE *)tab->bodybuf, npix);
/*---- Apply pixel processing */
if (func)
(*func)((PIXTYPE *)tab->bodybuf, npix);
body_ramleft -= size;
return (PIXTYPE *)tab->bodybuf;
}
else
tab->bodybuf = NULL;
}
if (size < body_vramleft)
{
/*-- Convert and copy the data to a swap file, and mmap() it */
if (!(buffer = malloc(DATA_BUFSIZE)))
return NULL;
sprintf(tab->swapname, "%s/vm%05ld_%05x.tmp",
body_swapdirname, (long)getpid(),
(unsigned int)++body_vmnumber) ;
if (!(file=fopen(tab->swapname, "wb+")))
error(EXIT_FAILURE, "*Error*: cannot create swap-file ", tab->swapname);
add_cleanupfilename(tab->swapname);
spoonful = (size%DATA_BUFSIZE);
if (!spoonful)
spoonful = DATA_BUFSIZE;
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
read_body(tab, buffer, spoonful/sizeof(PIXTYPE));
/*-- Apply pixel processing */
if (func)
(*func)(buffer, spoonful/sizeof(PIXTYPE));
QFWRITE(buffer, spoonful, file, tab->swapname);
for (sizeleft = size; sizeleft -= spoonful;)
{
read_body(tab, buffer, (spoonful=DATA_BUFSIZE)/sizeof(PIXTYPE));
/*--- Apply pixel processing */
if (func)
(*func)(buffer, spoonful/sizeof(PIXTYPE));
QFWRITE(buffer, spoonful, file, tab->swapname);
}
free(buffer);
tab->bodybuf = mmap(NULL,size,PROT_READ,MAP_SHARED,fileno(file),(off_t)0);
fclose(file);
tab->swapflag = 1;
body_vramleft -= size;
/*-- Memory mapping problem */
if (tab->bodybuf == (void *)-1)
return NULL;
return (PIXTYPE *)tab->bodybuf;
}
/* If no memory left at all: forget it! */
return NULL;
}
Emmanuel Bertin
committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/******* alloc_ibody ***********************************************************
PROTO FLAGTYPE *alloc_ibody(tabstruct *tab,
void (*func)(FLAGTYPE *ptr, int npix))
PURPOSE Allocate memory for and read a FITS integer data body (read-only).
If not enough RAM is available, a swap file is created.
INPUT Table (tab) structure.
OUTPUT Pointer to the mapped data if OK, or NULL otherwise.
NOTES The file pointer must be positioned at the beginning of the data.
AUTHOR E. Bertin (IAP)
VERSION 30/01/2012
***/
FLAGTYPE *alloc_ibody(tabstruct *tab,
void (*func)(FLAGTYPE *ptr, int npix))
{
FILE *file;
FLAGTYPE *buffer;
size_t npix, size, sizeleft, spoonful;
if (!body_ramflag)
{
body_ramleft = body_maxram;
body_vramleft = body_maxvram;
body_ramflag = 1;
}
/* Return a NULL pointer if size is zero */
if (!tab->tabsize)
return (FLAGTYPE *)NULL;
/* Check that there is a cat parent structure and that the file is open */
if (tab->cat && !tab->cat->file)
error(EXIT_FAILURE, "*Internal Error*: Cannot access table: ",
tab->extname);
/* Decide if the data will go in physical memory or on swap-space */
npix = tab->tabsize/tab->bytepix;
size = npix*sizeof(FLAGTYPE);
if (size < body_ramleft)
{
/*-- There should be enough RAM left: try to do a malloc() */
if ((tab->bodybuf = malloc(size)))
{
QFSEEK(tab->cat->file, tab->bodypos, SEEK_SET, tab->cat->filename);
read_ibody(tab, (FLAGTYPE *)tab->bodybuf, npix);
/*---- Apply pixel processing */
if (func)
(*func)((FLAGTYPE *)tab->bodybuf, npix);
body_ramleft -= size;
Loading
Loading full blame…