File: yrdwr.c

package info (click to toggle)
yorick 1.4-14
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,948 kB
  • ctags: 6,609
  • sloc: ansic: 63,898; yacc: 889; makefile: 605; sh: 65; lisp: 60; fortran: 19
file content (342 lines) | stat: -rw-r--r-- 10,289 bytes parent folder | download | duplicates (3)
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
148
149
150
151
152
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
    YRDWR.C
    Implement YRead and YWrite functions, plus general gather/scatter
    routines with StructDef format Converter.

    $Id: yrdwr.c,v 1.1 1993/08/27 18:32:09 munro Exp $
 */
/*    Copyright (c) 1994.  The Regents of the University of California.
                    All rights reserved.  */

#include "bcast.h"
#include "defmem.h"

/* Things to note:
   (1) If base->file!=0, then base->model!=0 (since the in-memory
       version of the StructDef can't refer to any particular file).
   (2) The Convert operator will be 0 for non-pointer,
       native-format disk files, since no conversion operation
       is required.
 */

extern void ReadGather(void *dst, void *srcM, long srcD, StructDef *base,
		       long number, const Strider *strider);
extern void WriteScatter(void *src, void *dstM, long dstD, StructDef *base,
			 long number, const Strider *strider);

static void *WriteRecurse(void *src, StructDef *base, long number);
extern void SetSequentialWrite(IOStream *file, long last);

static void ClearTmp(Array **tmpArray);
static StructDef *NeedsConversion(StructDef *base);

/*--------------------------------------------------------------------------*/

/* YRead and YWrite assume that the buffer passed to them is to hold
   the data in its FINAL format, that is, as base->model->...->model.  */

void YRead(void *dst, long src, StructDef *base, long number,
	   const Strider *strider)
{
  if (base->file && base->addressType==2 && strider)
    YError("cannot use a strider reading sequential object from file");
  ReadGather(dst, (void *)0, src, base, number, strider);
}

void YWrite(void *src, long dst, StructDef *base, long number,
	    const Strider *strider)
{
  if (base->file && base->addressType==2) {
    if (strider)
      YError("cannot use a strider writing sequential object to file");
    SetSequentialWrite(base->file, dst+base->size*number);
  }
  WriteScatter(src, (void *)0, dst, base, number, strider);
}

void SetSequentialWrite(IOStream *file, long last)
{
  if (last<file->nextAddress)
    YError("sequential object must be written at end of binary file");
  /* Converter for sequential object may need to know address at
     end of object being written, since the corresponding write
     occurs AFTER the Converter is called.  */
  file->seqAddress= last;
}

/*--------------------------------------------------------------------------*/

static Array *tmp1Array= 0, *tmp2Array= 0;

void ClearTmpArray(void)
{
  ClearTmp(&tmp1Array);
  ClearTmp(&tmp2Array);
}

Array *NewTmpArray(StructDef *base, Dimension *dims)
{
  Array *array= NewArray(base, dims);
  if (tmp1Array) {
    if (tmp2Array) {   /* both tmp1 and tmp2 exist -- deallocate tmp1 */
      Array *tmp= tmp2Array;
      ClearTmp(&tmp1Array);
      tmp2Array= array;
      tmp1Array= tmp;
    } else {           /* only tmp1 exists, leave it alone */
      tmp2Array= array;
    }
  } else {             /* neither tmp1 nor tmp2 exist, use tmp1 */
    tmp1Array= array;
  }
  return array;
}

static void ClearTmp(Array **tmpArray)
{
  Array *array= *tmpArray;
  *tmpArray= 0;
  Unref(array);
}

static StructDef *NeedsConversion(StructDef *base)
{
  while (base) {
    if (base->Convert) return base;
    base= base->model;
  }
  return 0;
}

/*--------------------------------------------------------------------------*/

/* In a read/gather operation, base describes the data type of the
   source (which is either in memory or on disk).  The destination is
   assumed to have been allocated prior to this call, and to have the
   data type base->model->...->model.
   If base->file, then srcM==0 will cause a subsequent call to
   ReadPointees, while if srcM!=0, the call will not be made
   (this is intended to prevent recursion).  */
void ReadGather(void *dst, void *srcM, long srcD, StructDef *base,
		long number, const Strider *strider)
{
  StructDef *preModel= NeedsConversion(base);
  void *dstM;
  int doPointees= (srcM==0);

  if (preModel) {
    Array *array;
    Dimension *dims;
    ClearTmpArray();
    dims= NewDimension(number, 1L, (Dimension *)0);
    array= NewTmpArray(preModel, dims);
    dims->references--;
    dstM= array->value.c;
  } else {
    dstM= dst;
  }

  if (base->file) CastRead(dstM, srcD, base, number, strider);
  else Gather(dstM, srcM, base, number, strider);

  if (preModel) {
    StructDef *model= preModel->model;
    Converter *Convert= preModel->Convert;
    Converter *NextConvert= 0;

    while (model) {
      while (model->model && !model->Convert) model= model->model;

      srcM= dstM;
      if ((NextConvert= model->Convert)) {
	/* There are two temporary arrays; NewTmpArray automatically
	   frees the array it allocated two calls before.  Both
	   arrays are deallocated by ClearTmpArray, which is also called
	   from YError.  */
	Dimension *dims= NewDimension(number, 1L, (Dimension *)0);
	Array *array= NewTmpArray(model, dims);
	dims->references--;
	dstM= array->value.c;
      } else {
	/* last pass always takes this branch */
	dstM= dst;
      }

      Convert(preModel, srcM, dstM, number, 0);
      preModel= model;
      model= model->model;
      Convert= NextConvert;
    }

    ClearTmpArray();
  }

  if (doPointees && base->file) ReadPointees(base->file);
}

/* In a write/scatter operation, base describes the data type of the
   destination (which is either in memory or on disk).  The source is
   assumed to have been allocated prior to this call, and to have the
   data type base->model->...->model.
   If base->file, then dstM==0 will cause a subsequent call to
   WritePointees, while if dstM!=0, the call will not be made
   (this is intended to prevent recursion).  */
void WriteScatter(void *src, void *dstM, long dstD, StructDef *base,
		  long number, const Strider *strider)
{
  StructDef *preModel= NeedsConversion(base);
  void *sbuffer;

  if (preModel) {
    ClearTmpArray();
    /* Note that SetSequentialWrite has set seqAddress properly BEFORE
       the call to WriteScatter -- DO NOT do anything like this:
       if (base->file && base->addressType==2) base->file->seqAddress= dstD;
     */
    sbuffer= WriteRecurse(src, preModel, number);

  } else {
    sbuffer= src;
  }

  if (base->file) {
    IOStream *file= base->file;
    long nextAddress= file->nextAddress;
    CastWrite(sbuffer, dstD, base, number, strider);
    if (!dstM) {
      WritePointees(file);
      if (nextAddress<file->nextAddress) FlushFile(file, 0);
    }
  } else {
    Scatter(sbuffer, dstM, base, number, strider);
  }

  if (preModel) ClearTmpArray();
}

static void *WriteRecurse(void *src, StructDef *base, long number)
{
  StructDef *model= base->model;
  Converter *Convert= base->Convert;  /* guaranteed non-zero */
  Array *darray;
  void *dst;
  Dimension *dims;

  while (model->model && !model->Convert) model= model->model;

  if (model->Convert) src= WriteRecurse(src, model, number);

  /* There are two temporary arrays; NewTmpArray automatically
     frees the array it allocated two calls before.  Both
     arrays are deallocated by ClearTmpArray, which is also called
     from YError.  */
  dims= NewDimension(number, 1L, (Dimension *)0);
  darray= NewTmpArray(base, dims);
  dims->references--;
  dst= darray->value.c;

  Convert(base, dst, src, number, 1);
  return dst;
}

/*--------------------------------------------------------------------------*/

void ReadPointees(IOStream *file)
{
  long nValid= file->pointeeList.nValid;
  long n= file->pointeeList.table.nItems - nValid;
  MDinfo *mdInfo= &file->pointeeList.mdInfo[nValid];
  Array *array;
  int noRecursion= 1;

  if (file->pointeeList.writing!=0) return;

  /* mark that read is in progress */
  file->pointeeList.writing= 2;

  /* The Convert routine has already read the header, storing
     the addresses of the header, the data, and the memory address
     of a newly created Array into pointeeList.mdInfo.  */

  while (n>0) {
    if (mdInfo->m) {
      array= Pointee(mdInfo->m);
      ReadGather(array->value.c, &noRecursion, mdInfo->data,
		 mdInfo->base, array->type.number, (Strider *)0);
    }

    /* note that pointeeList.table.nItems may have increased
       during the reading of this pointee */
    file->pointeeList.nValid= (++nValid);
    mdInfo= &file->pointeeList.mdInfo[nValid];
    n= file->pointeeList.table.nItems - nValid;
  }

  /* successful completion of read */
  file->pointeeList.writing= 0;
}

void WritePointees(IOStream *file)
{
  long nValid= file->pointeeList.nValid;
  long n= file->pointeeList.table.nItems - nValid;
  MDinfo *mdInfo= &file->pointeeList.mdInfo[nValid];
  Array *array;
  int noRecursion= 1;

  if (file->pointeeList.writing!=1) return;

  /* mark that write is in progress */
  file->pointeeList.writing= 3;

  /* The Convert routine has already written the header, storing
     the addresses of the header, the data, and the memory data
     into pointeeList.mdInfo.  */

  while (n>0) {
    if (mdInfo->m) {
      array= Pointee(mdInfo->m);
      WriteScatter(array->value.c, &noRecursion, mdInfo->data,
		   mdInfo->base, array->type.number, (Strider *)0);
    }

    /* note that pointeeList.table.nItems may have increased
       during the writing of this pointee */
    file->pointeeList.nValid= (++nValid);
    mdInfo= &file->pointeeList.mdInfo[nValid];
    n= file->pointeeList.table.nItems - nValid;
  }

  /* successful completion of write */
  file->pointeeList.writing= 1;
}

void ClearPointees(IOStream *file, int writing)
{
  long i, nItems= file->pointeeList.table.nItems;
  MDinfo *mdInfo;
  Array *array;

  if (nItems && !(file->pointeeList.writing&2)) {
    /* Note that this is skipped if failed at previous attempt to
       call WritePointees or ReadPointees.  */
    if (file->pointeeList.writing) WritePointees(file);
    else ReadPointees(file);
    /* tracking down pointers may have changed nItems, mdInfo */
    nItems= file->pointeeList.table.nItems;
  }

  mdInfo= file->pointeeList.mdInfo;
  HashXClear(&file->pointeeList.table);
  file->pointeeList.nValid= 0;
  file->pointeeList.mdInfo= 0;
  file->pointeeList.writing= writing;

  for (i=0 ; i<nItems ; i++) {
    array= Pointee(mdInfo[i].m);
    Unref(array);
  }
  Yfree(mdInfo);
}

/*--------------------------------------------------------------------------*/