~mysql/mysql-server/mysql-6.0

« back to all changes in this revision

Viewing changes to mysys/my_lockmem.c

  • Committer: bk at mysql
  • Date: 2000-07-31 19:29:14 UTC
  • Revision ID: sp1r-bk@work.mysql.com-20000731192914-08846
Import changeset

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
   
 
3
   This library is free software; you can redistribute it and/or
 
4
   modify it under the terms of the GNU Library General Public
 
5
   License as published by the Free Software Foundation; either
 
6
   version 2 of the License, or (at your option) any later version.
 
7
   
 
8
   This library 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 GNU
 
11
   Library General Public License for more details.
 
12
   
 
13
   You should have received a copy of the GNU Library General Public
 
14
   License along with this library; if not, write to the Free
 
15
   Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 
16
   MA 02111-1307, USA */
 
17
 
 
18
/* Alloc a block of locked memory */
 
19
 
 
20
#include "mysys_priv.h"
 
21
#include "mysys_err.h"
 
22
#include <my_list.h>
 
23
 
 
24
#ifdef HAVE_MLOCK
 
25
#include <sys/mman.h>
 
26
 
 
27
struct st_mem_list
 
28
{
 
29
  LIST list;
 
30
  byte *page;
 
31
  uint size;
 
32
};
 
33
 
 
34
LIST *mem_list;
 
35
 
 
36
byte *my_malloc_lock(uint size,myf MyFlags)
 
37
{
 
38
  int success;
 
39
  uint pagesize=sysconf(_SC_PAGESIZE);
 
40
  byte *ptr;
 
41
  struct st_mem_list *element;
 
42
  DBUG_ENTER("my_malloc_lock");
 
43
 
 
44
  size=((size-1) & ~(pagesize-1))+pagesize;
 
45
  if (!(ptr=memalign(pagesize,size)))
 
46
  {
 
47
    if (MyFlags & (MY_FAE+MY_WME))
 
48
      my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size);
 
49
    DBUG_RETURN(0);
 
50
  }
 
51
  success = mlock((byte*) ptr,size);
 
52
  if (success != 0 && geteuid() == 0)
 
53
  {
 
54
    DBUG_PRINT("warning",("Failed to lock memory. errno %d\n",
 
55
                          errno));
 
56
    fprintf(stderr, "Warning: Failed to lock memory. errno %d\n",
 
57
            errno);
 
58
  }
 
59
  else
 
60
  {
 
61
    /* Add block in a list for munlock */
 
62
    if (!(element=(struct st_mem_list*) my_malloc(sizeof(*element),MyFlags)))
 
63
    {
 
64
      VOID(munlock((byte*) ptr,size));
 
65
      free(ptr);
 
66
      DBUG_RETURN(0);
 
67
    }
 
68
    element->list.data=(byte*) element;
 
69
    element->page=ptr;
 
70
    element->size=size;
 
71
    pthread_mutex_lock(&THR_LOCK_malloc);
 
72
    mem_list=list_add(mem_list,&element->list);
 
73
    pthread_mutex_unlock(&THR_LOCK_malloc);
 
74
  }
 
75
  DBUG_RETURN(ptr);
 
76
}
 
77
 
 
78
 
 
79
void my_free_lock(byte *ptr,myf Myflags __attribute__((unused)))
 
80
{
 
81
  LIST *list;
 
82
  struct st_mem_list *element=0;
 
83
 
 
84
  pthread_mutex_lock(&THR_LOCK_malloc);
 
85
  for (list=mem_list ; list ; list=list->next)
 
86
  {
 
87
    element=(struct st_mem_list*) list->data;
 
88
    if (ptr == element->page)
 
89
    {                                           /* Found locked mem */
 
90
      VOID(munlock((byte*) ptr,element->size));
 
91
      mem_list=list_delete(mem_list,list);
 
92
      break;
 
93
    }
 
94
  }
 
95
  pthread_mutex_unlock(&THR_LOCK_malloc);
 
96
  if (element)
 
97
    my_free((gptr) element,MYF(0));
 
98
  free(ptr);                                    /* Free even if not locked */
 
99
}
 
100
 
 
101
#endif /* HAVE_MLOCK */