~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_map.cc

  • Committer: Brian Aker
  • Date: 2008-08-12 03:12:57 UTC
  • Revision ID: brian@tangent.org-20080812031257-ln3uk87y1r22byeg
First pass of new sql_db.cc work

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2001, 2004-2005 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 <drizzled/server_includes.h>
 
17
#include <sys/stat.h>
 
18
#ifdef HAVE_SYS_MMAN_H
 
19
#include <sys/mman.h>
 
20
#endif
 
21
 
 
22
mapped_files::mapped_files(const char * filename,uchar *magic,uint magic_length)
 
23
{
 
24
#ifdef HAVE_MMAP
 
25
  name=my_strdup(filename,MYF(0));
 
26
  use_count=1;
 
27
  error=0;
 
28
  map=0;
 
29
  size=0;
 
30
  if ((file=my_open(name,O_RDONLY,MYF(MY_WME))) >= 0)
 
31
  {
 
32
    struct stat stat_buf;
 
33
    if (!fstat(file,&stat_buf))
 
34
    {
 
35
      if (!(map=(uchar*) my_mmap(0,(size_t)(size= stat_buf.st_size),PROT_READ,
 
36
                             MAP_SHARED | MAP_NORESERVE,file,
 
37
                             0L)))
 
38
      {
 
39
        error=errno;
 
40
        my_error(ER_NO_FILE_MAPPING, MYF(0), (char *) name, error);
 
41
      }
 
42
    }
 
43
    if (map && memcmp(map,magic,magic_length))
 
44
    {
 
45
      my_error(ER_WRONG_MAGIC, MYF(0), name);
 
46
      VOID(my_munmap((char*) map,(size_t)size));
 
47
      map=0;
 
48
    }
 
49
    if (!map)
 
50
    {
 
51
      VOID(my_close(file,MYF(0)));
 
52
      file= -1;
 
53
    }
 
54
  }
 
55
#endif
 
56
}
 
57
 
 
58
 
 
59
mapped_files::~mapped_files()
 
60
{
 
61
#ifdef HAVE_MMAP
 
62
  if (file >= 0)
 
63
  {
 
64
    VOID(my_munmap((char*) map,(size_t)size));
 
65
    VOID(my_close(file,MYF(0)));
 
66
    file= -1; map=0;
 
67
  }
 
68
  my_free(name,MYF(0));
 
69
#endif
 
70
}
 
71
 
 
72
 
 
73
static I_List<mapped_files> maps_in_use;
 
74
 
 
75
/*
 
76
**  Check if a file is mapped. If it is, then return pointer to old map,
 
77
**  else alloc new object
 
78
*/
 
79
 
 
80
mapped_files *map_file(const char * name,uchar *magic,uint magic_length)
 
81
{
 
82
#ifdef HAVE_MMAP
 
83
  VOID(pthread_mutex_lock(&LOCK_mapped_file));
 
84
  I_List_iterator<mapped_files> list(maps_in_use);
 
85
  mapped_files *map;
 
86
  char path[FN_REFLEN];
 
87
  sprintf(path,"%s/%s/%s.uniq",mysql_data_home,current_thd->db,name);
 
88
  (void) unpack_filename(path,path);
 
89
 
 
90
  while ((map=list++))
 
91
  {
 
92
    if (!strcmp(path,map->name))
 
93
      break;
 
94
  }
 
95
  if (!map)
 
96
  {
 
97
    map=new mapped_files(path,magic,magic_length);
 
98
    maps_in_use.append(map);
 
99
  }
 
100
  else
 
101
  {
 
102
    map->use_count++;
 
103
    if (!map->map)
 
104
      my_error(ER_NO_FILE_MAPPING, MYF(0), path, map->error);
 
105
  }
 
106
  VOID(pthread_mutex_unlock(&LOCK_mapped_file));
 
107
  return map;
 
108
#else
 
109
  return NULL;
 
110
#endif
 
111
}
 
112
 
 
113
/*
 
114
** free the map if there are no more users for it
 
115
*/
 
116
 
 
117
void unmap_file(mapped_files *map)
 
118
{
 
119
#ifdef HAVE_MMAP
 
120
  VOID(pthread_mutex_lock(&LOCK_mapped_file));
 
121
  if (!map->use_count--)
 
122
    delete map;
 
123
  VOID(pthread_mutex_unlock(&LOCK_mapped_file));
 
124
#endif
 
125
}
 
126
 
 
127
/*****************************************************************************
 
128
** Instansiate templates
 
129
*****************************************************************************/
 
130
 
 
131
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
 
132
/* Used templates */
 
133
template class I_List<mapped_files>;
 
134
template class I_List_iterator<mapped_files>;
 
135
#endif