home *** CD-ROM | disk | FTP | other *** search
/ Whiteline: Alpha / Whiteline Alpha.iso / linux / atari / source / source.lzh / atari-linux-0.01pl3 / fs / file_table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-05  |  1.8 KB  |  95 lines

  1. /*
  2.  *  linux/fs/file_table.c
  3.  *
  4.  *  Copyright (C) 1991, 1992  Linus Torvalds
  5.  *
  6.  * This file is subject to the terms and conditions of the GNU General Public
  7.  * License.  See the file README.legal in the main directory of this archive
  8.  * for more details.
  9.  */
  10.  
  11. #include <linux/fs.h>
  12. #include <linux/string.h>
  13. #include <linux/sched.h>
  14. #include <linux/mm.h>
  15.  
  16. struct file * first_file;
  17. int nr_files = 0;
  18.  
  19. static void insert_file_free(struct file *file)
  20. {
  21.     file->f_next = first_file;
  22.     file->f_prev = first_file->f_prev;
  23.     file->f_next->f_prev = file;
  24.     file->f_prev->f_next = file;
  25.     first_file = file;
  26. }
  27.  
  28. static void remove_file_free(struct file *file)
  29. {
  30.     if (first_file == file)
  31.         first_file = first_file->f_next;
  32.     if (file->f_next)
  33.         file->f_next->f_prev = file->f_prev;
  34.     if (file->f_prev)
  35.         file->f_prev->f_next = file->f_next;
  36.     file->f_next = file->f_prev = NULL;
  37. }
  38.  
  39. static void put_last_free(struct file *file)
  40. {
  41.     remove_file_free(file);
  42.     file->f_prev = first_file->f_prev;
  43.     file->f_prev->f_next = file;
  44.     file->f_next = first_file;
  45.     file->f_next->f_prev = file;
  46. }
  47.  
  48. void grow_files(void)
  49. {
  50.     struct file * file;
  51.     unsigned int i;
  52.  
  53.     file = (struct file *) get_free_page(GFP_KERNEL);
  54.  
  55.     if (!file)
  56.         return;
  57.  
  58.     nr_files+=i= PAGE_SIZE/sizeof(struct file);
  59.  
  60.     if (!first_file)
  61.         file->f_next = file->f_prev = first_file = file++, i--;
  62.     
  63.     for (; i ; i--)
  64.         insert_file_free(file++);
  65. }
  66.  
  67. unsigned long file_table_init(unsigned long start, unsigned long end)
  68. {
  69.     first_file = NULL;
  70.     return start;
  71. }
  72.  
  73. struct file * get_empty_filp(void)
  74. {
  75.     int i;
  76.     struct file * f;
  77.  
  78.     if (!first_file)
  79.         grow_files();
  80. repeat:
  81.     for (f = first_file, i=0; i < nr_files; i++, f = f->f_next)
  82.         if (!f->f_count) {
  83.             remove_file_free(f);
  84.             memset(f,0,sizeof(*f));
  85.             put_last_free(f);
  86.             f->f_count = 1;
  87.             return f;
  88.         }
  89.     if (nr_files < NR_FILE) {
  90.         grow_files();
  91.         goto repeat;
  92.     }
  93.     return NULL;
  94. }
  95.