home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / KERNEL-S / V1.0 / LINUX-1.0 / LINUX-1 / linux / fs / file_table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-08  |  1.6 KB  |  90 lines

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