sql_map.cc
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:3k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
  6.    (at your option) any later version.
  7.    
  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU General Public License
  14.    along with this program; if not, write to the Free Software
  15.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  16. #ifdef __GNUC__
  17. #pragma implementation // gcc: Class implementation
  18. #endif
  19. #include "mysql_priv.h"
  20. #ifdef HAVE_MMAP
  21. #include <sys/mman.h>
  22. #include <sys/stat.h>
  23. #endif
  24. #ifndef MAP_NORESERVE
  25. #define MAP_NORESERVE 0 // For IRIX
  26. #endif
  27. mapped_files::mapped_files(const my_string filename,byte *magic,uint magic_length)
  28. {
  29. #ifdef HAVE_MMAP
  30.   name=my_strdup(filename,MYF(0));
  31.   use_count=1;
  32.   error=0;
  33.   map=0;
  34.   size=0;
  35.   if ((file=my_open(name,O_RDONLY,MYF(MY_WME))) >= 0)
  36.   {
  37.     struct stat stat_buf;
  38.     if (!fstat(file,&stat_buf))
  39.     {
  40.       if (!(map=(byte*) mmap(0,(size=(ulong) stat_buf.st_size),PROT_READ,
  41.      MAP_SHARED | MAP_NORESERVE,file,
  42.      0L)))
  43.       {
  44. error=errno;
  45. my_printf_error(0,"Can't map file: %s, errno: %d",MYF(0),
  46. (my_string) name,error);
  47.       }
  48.     }
  49.     if (map && memcmp(map,magic,magic_length))
  50.     {
  51.       my_printf_error(0,"Wrong magic in %s",MYF(0),name);
  52.       VOID(munmap(map,size));
  53.       map=0;
  54.     }
  55.     if (!map)
  56.     {
  57.       VOID(my_close(file,MYF(0)));
  58.       file= -1;
  59.     }
  60.   }
  61. #endif
  62. }
  63. mapped_files::~mapped_files()
  64. {
  65. #ifdef HAVE_MMAP
  66.   if (file >= 0)
  67.   {
  68.     VOID(munmap((caddr_t) map,size));
  69.     VOID(my_close(file,MYF(0)));
  70.     file= -1; map=0;
  71.   }
  72.   my_free(name,MYF(0));
  73. #endif
  74. }
  75. static I_List<mapped_files> maps_in_use;
  76. /*
  77. **  Check if a file is mapped. If it is, then return pointer to old map,
  78. **  else alloc new object
  79. */
  80. mapped_files *map_file(const my_string name,byte *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.   while ((map=list++))
  90.   {
  91.     if (!strcmp(path,map->name))
  92.       break;
  93.   }
  94.   if (!map)
  95.   {
  96.     map=new mapped_files(path,magic,magic_length);
  97.     maps_in_use.append(map);
  98.   }
  99.   else
  100.   {
  101.     map->use_count++;
  102.     if (!map->map)
  103.       my_printf_error(0,"Can't map file: %s, error: %d",MYF(0),path,
  104.       map->error);
  105.   }
  106.   VOID(pthread_mutex_unlock(&LOCK_mapped_file));
  107.   return map;
  108. #else
  109.   return NULL;
  110. #endif
  111. }
  112. /*
  113. ** free the map if there are no more users for it
  114. */
  115. void unmap_file(mapped_files *map)
  116. {
  117. #ifdef HAVE_MMAP
  118.   VOID(pthread_mutex_lock(&LOCK_mapped_file));
  119.   if (!map->use_count--)
  120.     delete map;
  121.   VOID(pthread_mutex_unlock(&LOCK_mapped_file));
  122. #endif
  123. }
  124. /*****************************************************************************
  125. ** Instansiate templates
  126. *****************************************************************************/
  127. #ifdef __GNUC__
  128. /* Used templates */
  129. template class I_List<mapped_files>;
  130. template class I_List_iterator<mapped_files>;
  131. #endif