~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_file.c

pandora-build v0.100 - Fixes several bugs found by cb1kenobi. Add several thoughts from folks at LCA.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000 MySQL AB
2
 
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
16
 
#include "mysys_priv.h"
17
 
#include "my_static.h"
18
 
#include <mystrings/m_string.h>
19
 
 
20
 
/*
21
 
  set how many open files we want to be able to handle
22
 
 
23
 
  SYNOPSIS
24
 
    set_maximum_open_files()
25
 
    max_file_limit              Files to open
26
 
 
27
 
  NOTES
28
 
    The request may not fulfilled becasue of system limitations
29
 
 
30
 
  RETURN
31
 
    Files available to open.
32
 
    May be more or less than max_file_limit!
33
 
*/
34
 
 
35
 
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
36
 
 
37
 
#ifndef RLIM_INFINITY
38
 
#define RLIM_INFINITY ((uint) 0xffffffff)
39
 
#endif
40
 
 
41
 
static uint set_max_open_files(uint max_file_limit)
42
 
{
43
 
  struct rlimit rlimit;
44
 
  uint old_cur;
45
 
 
46
 
  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
47
 
  {
48
 
    old_cur= (uint) rlimit.rlim_cur;
49
 
    if (rlimit.rlim_cur == RLIM_INFINITY)
50
 
      rlimit.rlim_cur = max_file_limit;
51
 
    if (rlimit.rlim_cur >= max_file_limit)
52
 
      return(rlimit.rlim_cur);          /* purecov: inspected */
53
 
    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
54
 
    if (setrlimit(RLIMIT_NOFILE, &rlimit))
55
 
      max_file_limit= old_cur;                  /* Use original value */
56
 
    else
57
 
    {
58
 
      rlimit.rlim_cur= 0;                       /* Safety if next call fails */
59
 
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
60
 
      if (rlimit.rlim_cur)                      /* If call didn't fail */
61
 
        max_file_limit= (uint) rlimit.rlim_cur;
62
 
    }
63
 
  }
64
 
  return(max_file_limit);
65
 
}
66
 
 
67
 
#else
68
 
static int set_max_open_files(uint max_file_limit)
69
 
{
70
 
  /* We don't know the limit. Return best guess */
71
 
  return min(max_file_limit, OS_FILE_LIMIT);
72
 
}
73
 
#endif
74
 
 
75
 
 
76
 
/*
77
 
  Change number of open files
78
 
 
79
 
  SYNOPSIS:
80
 
    my_set_max_open_files()
81
 
    files               Number of requested files
82
 
 
83
 
  RETURN
84
 
    number of files available for open
85
 
*/
86
 
 
87
 
uint my_set_max_open_files(uint files)
88
 
{
89
 
  struct st_my_file_info *tmp;
90
 
 
91
 
  files= set_max_open_files(min(files, OS_FILE_LIMIT));
92
 
  if (files <= MY_NFILE)
93
 
    return(files);
94
 
 
95
 
  if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
96
 
                                                 MYF(MY_WME))))
97
 
    return(MY_NFILE);
98
 
 
99
 
  /* Copy any initialized files */
100
 
  memcpy(tmp, my_file_info, sizeof(*tmp) * min(my_file_limit, files));
101
 
  /*
102
 
    The int cast is necessary since 'my_file_limits' might be greater
103
 
    than 'files'.
104
 
  */
105
 
  memset(tmp + my_file_limit, 0,
106
 
         max((int) (files - my_file_limit), 0)*sizeof(*tmp));
107
 
  my_free_open_file_info();                     /* Free if already allocated */
108
 
  my_file_info= tmp;
109
 
  my_file_limit= files;
110
 
  return(files);
111
 
}
112
 
 
113
 
 
114
 
void my_free_open_file_info()
115
 
{
116
 
  if (my_file_info != my_file_info_default)
117
 
  {
118
 
    /* Copy data back for my_print_open_files */
119
 
    memcpy(my_file_info_default, my_file_info,
120
 
           sizeof(*my_file_info_default)* MY_NFILE);
121
 
    my_free((char*) my_file_info, MYF(0));
122
 
    my_file_info= my_file_info_default;
123
 
    my_file_limit= MY_NFILE;
124
 
  }
125
 
  return;
126
 
}