~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_file.cc

  • Committer: Lee
  • Date: 2009-01-07 22:45:50 UTC
  • mfrom: (772 drizzle)
  • mto: This revision was merged to the branch mainline in revision 777.
  • Revision ID: lbieber@lbieber-desktop-20090107224550-tzjtc8klmv84ppyg
finish enabling tests, remove timezone2, timezone3

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
#include <stdlib.h>
 
20
#include <string.h>
 
21
 
 
22
/*
 
23
  set how many open files we want to be able to handle
 
24
 
 
25
  SYNOPSIS
 
26
    set_maximum_open_files()
 
27
    max_file_limit              Files to open
 
28
 
 
29
  NOTES
 
30
    The request may not fulfilled becasue of system limitations
 
31
 
 
32
  RETURN
 
33
    Files available to open.
 
34
    May be more or less than max_file_limit!
 
35
*/
 
36
 
 
37
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
 
38
 
 
39
#ifndef RLIM_INFINITY
 
40
#define RLIM_INFINITY ((uint) 0xffffffff)
 
41
#endif
 
42
 
 
43
static uint32_t set_max_open_files(uint32_t max_file_limit)
 
44
{
 
45
  struct rlimit rlimit;
 
46
  rlim_t old_cur;
 
47
 
 
48
  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
 
49
  {
 
50
    old_cur= rlimit.rlim_cur;
 
51
    if (rlimit.rlim_cur == RLIM_INFINITY)
 
52
      rlimit.rlim_cur = max_file_limit;
 
53
    if (rlimit.rlim_cur >= max_file_limit)
 
54
    {
 
55
      if (rlimit.rlim_cur > UINT32_MAX)
 
56
        return UINT32_MAX;
 
57
      else
 
58
        return((uint32_t)rlimit.rlim_cur);
 
59
    }
 
60
    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
 
61
    if (setrlimit(RLIMIT_NOFILE, &rlimit))
 
62
      max_file_limit= (old_cur < UINT32_MAX) ? (uint32_t)old_cur : UINT32_MAX;
 
63
    else
 
64
    {
 
65
      rlimit.rlim_cur= 0;                       /* Safety if next call fails */
 
66
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
 
67
      if (rlimit.rlim_cur)                      /* If call didn't fail */
 
68
        max_file_limit= (uint32_t) rlimit.rlim_cur;
 
69
    }
 
70
  }
 
71
  return(max_file_limit);
 
72
}
 
73
 
 
74
#else
 
75
static int set_max_open_files(uint32_t max_file_limit)
 
76
{
 
77
  /* We don't know the limit. Return best guess */
 
78
  return cmin(max_file_limit, OS_FILE_LIMIT);
 
79
}
 
80
#endif
 
81
 
 
82
 
 
83
/*
 
84
  Change number of open files
 
85
 
 
86
  SYNOPSIS:
 
87
    my_set_max_open_files()
 
88
    files               Number of requested files
 
89
 
 
90
  RETURN
 
91
    number of files available for open
 
92
*/
 
93
 
 
94
uint32_t my_set_max_open_files(uint32_t files)
 
95
{
 
96
  struct st_my_file_info *tmp;
 
97
 
 
98
  files= set_max_open_files(cmin(files, OS_FILE_LIMIT));
 
99
  if (files <= MY_NFILE)
 
100
    return(files);
 
101
 
 
102
  if (!(tmp= (st_my_file_info*) malloc(sizeof(st_my_file_info) * files)))
 
103
    return(MY_NFILE);
 
104
 
 
105
  /* Copy any initialized files */
 
106
  memcpy(tmp, my_file_info, sizeof(*tmp) * cmin(my_file_limit, files));
 
107
  /*
 
108
    The int cast is necessary since 'my_file_limits' might be greater
 
109
    than 'files'.
 
110
  */
 
111
  memset(tmp + my_file_limit, 0,
 
112
         cmax((int) (files - my_file_limit), 0)*sizeof(*tmp));
 
113
  my_free_open_file_info();                     /* Free if already allocated */
 
114
  my_file_info= tmp;
 
115
  my_file_limit= files;
 
116
  return(files);
 
117
}
 
118
 
 
119
 
 
120
void my_free_open_file_info()
 
121
{
 
122
  if (my_file_info != my_file_info_default)
 
123
  {
 
124
    /* Copy data back for my_print_open_files */
 
125
    memcpy(my_file_info_default, my_file_info,
 
126
           sizeof(*my_file_info_default)* MY_NFILE);
 
127
    free((char*) my_file_info);
 
128
    my_file_info= my_file_info_default;
 
129
    my_file_limit= MY_NFILE;
 
130
  }
 
131
  return;
 
132
}