source: rtems/c/src/exec/libfs/src/imfs/memfile.c @ efb1b830

4.104.114.84.9
Last change on this file since efb1b830 was efb1b830, checked in by Joel Sherrill <joel.sherrill@…>, on Feb 10, 1999 at 5:04:54 PM

Corrected multiple places the file size was not being properly updated.

  • Property mode set to 100644
File size: 21.3 KB
Line 
1/*
2 *  IMFS Device Node Handlers
3 *
4 *  This file contains the set of handlers used to process operations on
5 *  IMFS memory file nodes.  The memory files are created in memory using
6 *  malloc'ed memory.  Thus any data stored in one of these files is lost
7 *  at system shutdown unless special arrangements to copy the data to
8 *  some type of non-volailte storage are made by the application.
9 *
10 *  COPYRIGHT (c) 1989-1998.
11 *  On-Line Applications Research Corporation (OAR).
12 *  Copyright assigned to U.S. Government, 1994.
13 *
14 *  The license and distribution terms for this file may be
15 *  found in the file LICENSE in this distribution or at
16 *  http://www.OARcorp.com/rtems/license.html.
17 *
18 *  $Id$
19 */
20
21#include <stdlib.h>
22#include <assert.h>
23#include <errno.h>
24
25#include <rtems.h>
26#include <rtems/libio.h>
27#include "imfs.h"
28#include "libio_.h"
29
30#define MEMFILE_STATIC
31
32/*
33 *  Prototypes of private routines
34 */
35
36MEMFILE_STATIC int IMFS_memfile_extend(
37   IMFS_jnode_t  *the_jnode,
38   off_t          new_length
39);
40
41MEMFILE_STATIC int IMFS_memfile_addblock(
42   IMFS_jnode_t  *the_jnode,
43   unsigned int   block
44);
45
46MEMFILE_STATIC int IMFS_memfile_remove_block(
47   IMFS_jnode_t  *the_jnode,
48   unsigned int   block
49);
50
51MEMFILE_STATIC block_p *IMFS_memfile_get_block_pointer(
52   IMFS_jnode_t   *the_jnode,
53   unsigned int    block,
54   int             malloc_it
55);
56
57MEMFILE_STATIC int IMFS_memfile_read(
58   IMFS_jnode_t    *the_jnode,
59   off_t            start,
60   unsigned char   *destination,
61   unsigned int     length
62);
63
64MEMFILE_STATIC int IMFS_memfile_write(
65   IMFS_jnode_t          *the_jnode,
66   off_t                  start,
67   const unsigned char   *source,
68   unsigned int           length
69);
70
71void *memfile_alloc_block(void);
72
73void memfile_free_block(
74  void *memory
75);
76
77/*
78 *  memfile_open
79 *
80 *  This routine processes the open() system call.  Note that there is
81 *  nothing special to be done at open() time.
82 */
83
84int memfile_open(
85  rtems_libio_t *iop,
86  const char    *pathname,
87  unsigned32     flag,
88  unsigned32     mode
89)
90{
91  IMFS_jnode_t  *the_jnode;
92
93  the_jnode = iop->file_info;
94
95  return 0;
96}
97
98/*
99 *  memfile_close
100 *
101 *  This routine processes the close() system call.  Note that there is
102 *  nothing to flush or memory to free at this point.
103 */
104
105int memfile_close(
106  rtems_libio_t *iop
107)
108{
109  IMFS_jnode_t   *the_jnode;
110
111  the_jnode = iop->file_info;
112
113  return 0;
114}
115
116/*
117 *  memfile_read
118 *
119 *  This routine processes the read() system call.
120 */
121
122int memfile_read(
123  rtems_libio_t *iop,
124  void          *buffer,
125  unsigned32     count
126)
127{
128  IMFS_jnode_t   *the_jnode;
129
130  the_jnode = iop->file_info;
131
132  return IMFS_memfile_read( the_jnode, iop->offset, buffer, count );
133}
134
135/*
136 *  memfile_write
137 *
138 *  This routine processes the write() system call.
139 */
140
141int memfile_write(
142  rtems_libio_t *iop,
143  const void    *buffer,
144  unsigned32     count
145)
146{
147  IMFS_jnode_t   *the_jnode;
148  int             status;
149
150  the_jnode = iop->file_info;
151
152  status = IMFS_memfile_write( the_jnode, iop->offset, buffer, count );
153  iop->size = the_jnode->info.file.size;
154
155  return status;
156}
157
158/*
159 *  memfile_ioctl
160 *
161 *  This routine processes the ioctl() system call.
162 *
163 *  NOTE:  No ioctl()'s are supported for in-memory files.
164 */
165
166int memfile_ioctl(
167  rtems_libio_t *iop,
168  unsigned32     command,
169  void          *buffer
170)
171{
172  IMFS_jnode_t   *the_jnode;
173
174  the_jnode = iop->file_info;
175
176  return 0;
177}
178
179/*
180 *  memfile_lseek
181 *
182 *  This routine processes the lseek() system call.
183 */
184
185int memfile_lseek(
186  rtems_libio_t   *iop,
187  off_t            offset,
188  int              whence
189)
190{
191  IMFS_jnode_t   *the_jnode;
192
193  the_jnode = iop->file_info;
194
195  if (IMFS_memfile_extend( the_jnode, iop->offset ))
196    set_errno_and_return_minus_one( ENOSPC );
197
198  iop->size = the_jnode->info.file.size;
199  return iop->offset;
200}
201
202/*
203 *  memfile_stat
204 *
205 *  This IMFS_stat() can be used.
206 */
207
208/*
209 *  memfile_ftruncate
210 *
211 *  This routine processes the ftruncate() system call.
212 */
213
214int memfile_ftruncate(
215  rtems_libio_t        *iop,
216  off_t                 length
217)
218{
219  IMFS_jnode_t   *the_jnode;
220
221  the_jnode = iop->file_info;
222
223  /*
224   *  POSIX 1003.1b does not specify what happens if you truncate a file
225   *  and the new length is greater than the current size.  We treat this
226   *  as an extend operation.
227   */
228
229  if ( length > the_jnode->info.file.size )
230    return IMFS_memfile_extend( the_jnode, length );
231
232  /*
233   *  The in-memory files do not currently reclaim memory until the file is
234   *  deleted.  So we leave the previously allocated blocks in place for
235   *  future use and just set the length.
236   */
237
238  the_jnode->info.file.size = length;
239  iop->size = the_jnode->info.file.size;
240
241  IMFS_update_atime( the_jnode );
242
243  return 0;
244}
245
246/*
247 *  IMFS_memfile_extend
248 *
249 *  This routine insures that the in-memory file is of the length
250 *  specified.  If necessary, it will allocate memory blocks to
251 *  extend the file.
252 */
253
254MEMFILE_STATIC int IMFS_memfile_extend(
255   IMFS_jnode_t  *the_jnode,
256   off_t          new_length
257)
258{
259  unsigned int   block;
260  unsigned int   new_blocks;
261  unsigned int   old_blocks;
262
263  /*
264   *  Perform internal consistency checks
265   */
266
267  assert( the_jnode );
268  if ( !the_jnode )
269    set_errno_and_return_minus_one( EIO );
270
271  assert( the_jnode->type == IMFS_MEMORY_FILE );
272  if ( the_jnode->type != IMFS_MEMORY_FILE )
273    set_errno_and_return_minus_one( EIO );
274
275  if ( new_length >= IMFS_MEMFILE_MAXIMUM_SIZE )
276    set_errno_and_return_minus_one( EINVAL );
277
278  if ( new_length <= the_jnode->info.file.size )
279    return 0;
280
281  /*
282   *  Calculate the number of range of blocks to allocate
283   */
284
285  new_blocks = new_length / IMFS_MEMFILE_BYTES_PER_BLOCK;
286  old_blocks = the_jnode->info.file.size / IMFS_MEMFILE_BYTES_PER_BLOCK;
287
288  /*
289   *  Now allocate each of those blocks.
290   */
291
292  for ( block=old_blocks ; block<=new_blocks ; block++ ) {
293    if ( IMFS_memfile_addblock( the_jnode, block ) ) {
294       for ( ; block>=old_blocks ; block-- ) {
295          IMFS_memfile_remove_block( the_jnode, block );
296       }
297       set_errno_and_return_minus_one( ENOSPC );
298    }
299  }
300
301  /*
302   *  Set the new length of the file.
303   */
304
305  the_jnode->info.file.size = new_length;
306  return 0;
307}
308
309/*
310 *  IMFS_memfile_addblock
311 *
312 *  This routine adds a single block to the specified in-memory file.
313 */
314
315MEMFILE_STATIC int IMFS_memfile_addblock(
316   IMFS_jnode_t  *the_jnode,
317   unsigned int   block
318)
319{
320  block_p  memory;
321  block_p *block_entry_ptr;
322
323  assert( the_jnode );
324  if ( !the_jnode )
325    set_errno_and_return_minus_one( EIO );
326
327  assert( the_jnode->type == IMFS_MEMORY_FILE );
328  if ( the_jnode->type != IMFS_MEMORY_FILE )
329    set_errno_and_return_minus_one( EIO );
330
331  block_entry_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 1 );
332  if ( *block_entry_ptr )
333    return 0;
334
335#if 0
336  printf( "%d %p", block, block_entry_ptr );
337    fflush(stdout);
338#endif
339
340  memory = memfile_alloc_block();
341  assert( memory );
342  if ( !memory )
343    return 1;
344  *block_entry_ptr = memory;
345
346  return 0;
347}
348
349/*
350 *  IMFS_memfile_remove_block
351 *
352 *  This routine removes the specified block from the in-memory file.
353 *
354 *  NOTE:  This is a support routine and is called only to remove
355 *         the last block or set of blocks in a file.  Removing a
356 *         block from the middle of a file would be exceptionally
357 *         dangerous and the results unpredictable.
358 */
359
360MEMFILE_STATIC int IMFS_memfile_remove_block(
361   IMFS_jnode_t  *the_jnode,
362   unsigned int   block
363)
364{
365  block_p *block_entry_ptr;
366  block_p  ptr;
367
368  block_entry_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
369  ptr = *block_entry_ptr;
370  *block_entry_ptr = 0;
371
372  memfile_free_block( ptr );
373
374  return 1;
375}
376
377/*
378 *  memfile_free_blocks_in_table
379 *
380 *  This is a support routine for IMFS_memfile_remove.  It frees all the
381 *  blocks in one of the indirection tables.
382 */
383
384void memfile_free_blocks_in_table(
385  block_p **block_table,
386  int       entries
387)
388{
389  int      i;
390  block_p *b;
391
392  /*
393   *  Perform internal consistency checks
394   */
395
396  assert( block_table );
397  if ( !block_table )
398    return;
399
400  /*
401   *  Now go through all the slots in the table and free the memory.
402   */
403
404  b = *block_table;
405
406  for ( i=0 ; i<entries ; i++ ) {
407    if ( b[i] ) {
408      memfile_free_block( b[i] );
409      b[i] = 0;
410    }
411  }
412
413  /*
414   *  Now that all the blocks in the block table are free, we can
415   *  free the block table itself.
416   */
417
418  memfile_free_block( *block_table );
419  *block_table = 0;
420}
421
422/*
423 *  IMFS_memfile_remove
424 *
425 *  This routine frees all memory associated with an in memory file.
426 *
427 *  NOTE:  This is an exceptionally conservative implementation. 
428 *         It will check EVERY pointer which is non-NULL and insure
429 *         any child non-NULL pointers are freed.  Optimistically, all that
430 *         is necessary is to scan until a NULL pointer is found.  There
431 *         should be no allocated data past that point.
432 *
433 *         In experimentation on the powerpc simulator, it was noted
434 *         that using blocks which held 128 slots versus 16 slots made
435 *         a significant difference in the performance of this routine.
436 *
437 *         Regardless until the IMFS implementation is proven, it
438 *         is better to stick to simple, easy to understand algorithms.
439 */
440
441int IMFS_memfile_remove(
442 IMFS_jnode_t  *the_jnode
443)
444{
445  IMFS_memfile_t  *info;
446  int              i;
447  int              j;
448  unsigned int     to_free;
449  block_p         *p;
450
451  /*
452   *  Perform internal consistency checks
453   */
454
455  assert( the_jnode );
456  if ( !the_jnode )
457    set_errno_and_return_minus_one( EIO );
458
459  assert( the_jnode->type == IMFS_MEMORY_FILE );
460  if ( the_jnode->type != IMFS_MEMORY_FILE )
461    set_errno_and_return_minus_one( EIO );
462
463  /*
464   *  Eventually this could be set smarter at each call to
465   *  memfile_free_blocks_in_table to greatly speed this up.
466   */
467
468  to_free = IMFS_MEMFILE_BLOCK_SLOTS;
469
470  /*
471   *  Now start freeing blocks in this order:
472   *    + indirect
473   *    + doubly indirect
474   *    + triply indirect
475   */
476
477  info = &the_jnode->info.file;
478
479  if ( info->indirect ) {
480    memfile_free_blocks_in_table( &info->indirect, to_free );
481  }
482
483  if ( info->doubly_indirect ) {
484
485    for ( i=0 ; i<IMFS_MEMFILE_BLOCK_SLOTS ; i++ ) {
486      if ( info->doubly_indirect[i] ) {
487        memfile_free_blocks_in_table(
488         (block_p **)&info->doubly_indirect[i], to_free );
489      }
490    }
491    memfile_free_blocks_in_table( &info->doubly_indirect, to_free );
492
493  }
494 
495  if ( info->triply_indirect ) {
496    for ( i=0 ; i<IMFS_MEMFILE_BLOCK_SLOTS ; i++ ) {
497      p = (block_p *) info->triply_indirect[i];
498      for ( j=0 ; j<IMFS_MEMFILE_BLOCK_SLOTS ; j++ ) {
499        if ( p[j] ) {
500          memfile_free_blocks_in_table( (block_p **)&p[j], to_free);
501        }
502      }
503      memfile_free_blocks_in_table(
504        (block_p **)&info->triply_indirect[i], to_free );
505    }
506    memfile_free_blocks_in_table(
507        (block_p **)&info->triply_indirect, to_free );
508  }
509
510  return 0;
511}
512
513/*
514 *  IMFS_memfile_read
515 *
516 *  This routine read from memory file pointed to by the_jnode into
517 *  the specified data buffer specified by destination.  The file
518 *  is NOT extended.  An offset greater than the length of the file
519 *  is considered an error.  Read from an offset for more bytes than
520 *  are between the offset and the end of the file will result in
521 *  reading the data between offset and the end of the file (truncated
522 *  read).
523 */
524
525MEMFILE_STATIC int IMFS_memfile_read(
526   IMFS_jnode_t    *the_jnode,
527   off_t            start,
528   unsigned char   *destination,
529   unsigned int     length
530)
531{
532  block_p             *block_ptr;
533  unsigned int         block;
534  unsigned int         my_length;
535  unsigned int         to_copy = 0;
536  unsigned int         last_byte;
537  unsigned int         copied;
538  unsigned int         start_offset;
539  unsigned char       *dest;
540
541  dest = destination;
542
543  /*
544   *  Perform internal consistency checks
545   */
546
547  assert( the_jnode );
548  if ( !the_jnode )
549    set_errno_and_return_minus_one( EIO );
550
551  assert( the_jnode->type == IMFS_MEMORY_FILE );
552  if ( the_jnode->type != IMFS_MEMORY_FILE )
553    set_errno_and_return_minus_one( EIO );
554
555  /*
556   *  Error checks on arguments
557   */
558
559  assert( dest );
560  if ( !dest )
561    set_errno_and_return_minus_one( EINVAL );
562
563  /*
564   *  If there is nothing to read, then quick exit.
565   */
566
567  my_length = length;
568  if ( !my_length )
569    set_errno_and_return_minus_one( EINVAL );
570
571  /*
572   *  If the last byte we are supposed to read is past the end of this
573   *  in memory file, then shorten the length to read.
574   */
575
576  last_byte = start + length;
577  if ( last_byte > the_jnode->info.file.size )
578    my_length = the_jnode->info.file.size - start;
579
580  copied = 0;
581
582  /*
583   *  Three phases to the read:
584   *    + possibly the last part of one block
585   *    + all of zero of more blocks
586   *    + possibly the first part of one block
587   */
588
589  /*
590   *  Phase 1: possibly the last part of one block
591   */
592
593  start_offset = start % IMFS_MEMFILE_BYTES_PER_BLOCK;
594  block = start / IMFS_MEMFILE_BYTES_PER_BLOCK;
595  if ( start_offset )  {
596    to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK - start_offset;
597    if ( to_copy > my_length )
598      to_copy = my_length;
599    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
600    assert( block_ptr );
601    if ( !block_ptr )
602      return copied;
603    memcpy( dest, &(*block_ptr)[ start_offset ], to_copy );
604    dest += to_copy;
605    block++;
606    my_length -= to_copy;
607    copied += to_copy;
608  }
609
610  /*
611   *  Phase 2: all of zero of more blocks
612   */
613
614  to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK;
615  while ( my_length >= IMFS_MEMFILE_BYTES_PER_BLOCK ) {
616    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
617    assert( block_ptr );
618    if ( !block_ptr )
619      return copied;
620    memcpy( dest, &(*block_ptr)[ 0 ], to_copy );
621    dest += to_copy;
622    block++;
623    my_length -= to_copy;
624    copied += to_copy;
625  }
626
627  /*
628   *  Phase 3: possibly the first part of one block
629   */
630
631  assert( my_length < IMFS_MEMFILE_BYTES_PER_BLOCK );
632
633  if ( my_length ) {
634    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
635    assert( block_ptr );
636    if ( !block_ptr )
637      return copied;
638    memcpy( dest, &(*block_ptr)[ 0 ], my_length );
639    copied += my_length;
640  }
641
642  IMFS_update_atime( the_jnode );
643
644  return copied;
645}
646
647/*
648 *  IMFS_memfile_write
649 *
650 *  This routine writes the specified data buffer into the in memory
651 *  file pointed to by the_jnode.  The file is extended as needed.
652 */
653
654MEMFILE_STATIC int IMFS_memfile_write(
655   IMFS_jnode_t          *the_jnode,
656   off_t                  start,
657   const unsigned char   *source,
658   unsigned int           length
659)
660{
661  block_p             *block_ptr;
662  unsigned int         block;
663  int                  status;
664  unsigned int         my_length;
665  unsigned int         to_copy = 0;
666  unsigned int         last_byte;
667  unsigned int         start_offset;
668  int                  copied;
669  const unsigned char *src;
670
671  src = source;
672
673  /*
674   *  Perform internal consistency checks
675   */
676
677  assert( the_jnode );
678  if ( !the_jnode )
679    set_errno_and_return_minus_one( EIO );
680
681  assert( the_jnode->type == IMFS_MEMORY_FILE );
682  if ( the_jnode->type != IMFS_MEMORY_FILE )
683    set_errno_and_return_minus_one( EIO );
684
685  /*
686   *  Error check arguments
687   */
688
689  assert( source );
690  if ( !source )
691    set_errno_and_return_minus_one( EINVAL );
692
693
694  /*
695   *  If there is nothing to write, then quick exit.
696   */
697
698  my_length = length;
699  if ( !my_length )
700    set_errno_and_return_minus_one( EINVAL );
701
702  /*
703   *  If the last byte we are supposed to write is past the end of this
704   *  in memory file, then extend the length.
705   */
706
707  last_byte = start + length;
708  if ( last_byte > the_jnode->info.file.size ) {
709    status = IMFS_memfile_extend( the_jnode, last_byte );
710    if ( status )
711      set_errno_and_return_minus_one( ENOSPC );
712  }
713
714  copied = 0;
715
716  /*
717   *  Three phases to the write:
718   *    + possibly the last part of one block
719   *    + all of zero of more blocks
720   *    + possibly the first part of one block
721   */
722
723  /*
724   *  Phase 1: possibly the last part of one block
725   */
726
727  start_offset = start % IMFS_MEMFILE_BYTES_PER_BLOCK;
728  block = start / IMFS_MEMFILE_BYTES_PER_BLOCK;
729  if ( start_offset )  {
730    to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK - start_offset;
731    if ( to_copy > my_length )
732      to_copy = my_length;
733    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
734    assert( block_ptr );
735    if ( !block_ptr )
736      return copied;
737    memcpy( &(*block_ptr)[ start_offset ], src, to_copy );
738    src += to_copy;
739    block++;
740    my_length -= to_copy;
741    copied += to_copy;
742  }
743
744  /*
745   *  Phase 2: all of zero of more blocks
746   */
747
748  to_copy = IMFS_MEMFILE_BYTES_PER_BLOCK;
749  while ( my_length >= IMFS_MEMFILE_BYTES_PER_BLOCK ) {
750    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
751    assert( block_ptr );
752    if ( !block_ptr )
753      return copied;
754    memcpy( &(*block_ptr)[ 0 ], src, to_copy );
755    src += to_copy;
756    block++;
757    my_length -= to_copy;
758    copied += to_copy;
759  }
760
761  /*
762   *  Phase 3: possibly the first part of one block
763   */
764
765  assert( my_length < IMFS_MEMFILE_BYTES_PER_BLOCK );
766
767  to_copy = my_length;
768  if ( my_length ) {
769    block_ptr = IMFS_memfile_get_block_pointer( the_jnode, block, 0 );
770    assert( block_ptr );
771    if ( !block_ptr )
772      return copied;
773    memcpy( &(*block_ptr)[ 0 ], src, my_length );
774    my_length = 0;
775    copied += to_copy;
776  }
777
778  IMFS_atime_mtime_update( the_jnode );
779
780  return copied;
781}
782
783/*
784 *  IMFS_memfile_get_block_pointer
785 *
786 *  This routine looks up the block pointer associated with the given block
787 *  number.  If that block has not been allocated and "malloc_it" is
788 *  TRUE, then the block is allocated.  Otherwise, it is an error.
789 */
790
791block_p *IMFS_memfile_get_block_pointer(
792   IMFS_jnode_t   *the_jnode,
793   unsigned int    block,
794   int             malloc_it
795)
796{
797  unsigned int    my_block;
798  IMFS_memfile_t *info;
799  unsigned int    singly;
800  unsigned int    doubly;
801  unsigned int    triply;
802  block_p        *p;
803  block_p        *p1;
804  block_p        *p2;
805
806  /*
807   *  Perform internal consistency checks
808   */
809
810  assert( the_jnode );
811  if ( !the_jnode )
812    return NULL;
813
814  assert( the_jnode->type == IMFS_MEMORY_FILE );
815  if ( the_jnode->type != IMFS_MEMORY_FILE )
816    return NULL;
817
818  info = &the_jnode->info.file;
819
820  my_block = block;
821
822  /*
823   *  Is the block number in the simple indirect portion?
824   */
825
826  if ( my_block <= LAST_INDIRECT ) {
827#if 0
828printf( "(s %d) ", block );
829fflush(stdout);
830#endif
831    p = info->indirect;
832
833    if ( malloc_it ) {
834
835      if ( !p ) {
836        p = memfile_alloc_block();
837        if ( !p )
838           return 0;
839        info->indirect = p;
840      }
841      return &info->indirect[ my_block ];
842    }
843
844    if ( !p )
845      return 0;
846
847    return &info->indirect[ my_block ];
848  }
849
850  /*
851   *  Is the block number in the doubly indirect portion?
852   */
853
854  if ( my_block <= LAST_DOUBLY_INDIRECT ) {
855#if 0
856printf( "(d %d) ", block );
857fflush(stdout);
858#endif
859
860    my_block -= FIRST_DOUBLY_INDIRECT;
861
862    singly = my_block % IMFS_MEMFILE_BLOCK_SLOTS;
863    doubly = my_block / IMFS_MEMFILE_BLOCK_SLOTS;
864
865    p = info->doubly_indirect;
866    if ( malloc_it ) {
867
868      if ( !p ) {
869        p = memfile_alloc_block();
870        if ( !p )
871           return 0;
872        info->doubly_indirect = p;
873      }
874
875      p1 = (block_p *)p[ doubly ];
876      if ( !p1 ) {
877        p1 = memfile_alloc_block();
878        if ( !p1 )
879           return 0;
880        p[ doubly ] = (block_p) p1;
881      }
882
883      return (block_p *)&p[ singly ];
884    }
885
886    if ( !p )
887      return 0;
888
889    p = (block_p *)p[ doubly ];
890    if ( !p ) 
891      return 0;
892
893#if 0
894printf( "(d %d %d %d %d %p %p) ", block, my_block, doubly,
895                                       singly, p, &p[singly] );
896fflush(stdout);
897#endif
898    return (block_p *)&p[ singly ];
899  }
900
901#if 0
902printf( "(t %d) ", block );
903fflush(stdout);
904#endif
905  /*
906   *  Is the block number in the triply indirect portion?
907   */
908
909  if ( my_block <= LAST_TRIPLY_INDIRECT ) {
910    my_block -= FIRST_TRIPLY_INDIRECT;
911
912    singly = my_block % IMFS_MEMFILE_BLOCK_SLOTS;
913    doubly = my_block / IMFS_MEMFILE_BLOCK_SLOTS;
914    triply = doubly / IMFS_MEMFILE_BLOCK_SLOTS;
915    doubly %= IMFS_MEMFILE_BLOCK_SLOTS;
916
917    p = info->triply_indirect;
918
919    if ( malloc_it ) {
920      if ( !p ) {
921        p = memfile_alloc_block();
922        if ( !p )
923           return 0;
924        info->triply_indirect = p;
925      }
926
927      p1 = (block_p *) p[ triply ];
928      if ( !p1 ) {
929        p1 = memfile_alloc_block();
930        if ( !p1 )
931           return 0;
932        p[ triply ] = (block_p) p1;
933      }
934
935      p2 = (block_p *)p1[ doubly ];
936      if ( !p2 ) {
937        p2 = memfile_alloc_block();
938        if ( !p2 )
939           return 0;
940        p1[ doubly ] = (block_p) p2;
941      }
942      return (block_p *)&p2[ singly ];
943    }
944
945    if ( !p )
946      return 0;
947
948#if 0
949printf( "(t %d %d %d %d %d) ", block, my_block, triply, doubly, singly );
950fflush(stdout);
951#endif
952    p1 = (block_p *) p[ triply ];
953    if ( !p1 )
954      return 0;
955
956    p2 = (block_p *)p1[ doubly ];
957    if ( !p )
958      return 0;
959
960    return (block_p *)&p2[ singly ];
961  }
962
963  /*
964   *  This means the requested block number is out of range.
965   */
966
967  return 0;
968}
969
970/*
971 *  memfile_alloc_block
972 *
973 *  Allocate a block for an in-memory file.
974 */
975
976int memfile_blocks_allocated = 0;
977
978void *memfile_alloc_block(void)
979{
980  void *memory;
981
982  memory = (void *)calloc(1, IMFS_MEMFILE_BYTES_PER_BLOCK);
983  if ( memory )
984    memfile_blocks_allocated++;
985
986  return memory;
987}
988
989/*
990 *  memfile_free_blocK
991 *
992 *  Free a block from an in-memory file.
993 */
994
995void memfile_free_block(
996  void *memory
997)
998{
999#if 0
1000printf( "(d %p) ", memory );
1001fflush(stdout);
1002#endif
1003  free(memory);
1004  memfile_blocks_allocated--;
1005}
1006
Note: See TracBrowser for help on using the repository browser.