~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/my_lockmem.c

  • Committer: Jay Pipes
  • Date: 2008-07-18 20:20:47 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080718202047-1tnd4i9z3k3cvg9v
DBUG entirely removed from server and client

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
/* Alloc a block of locked memory */
 
17
 
 
18
#include "mysys_priv.h"
 
19
#include "mysys_err.h"
 
20
#include <my_list.h>
 
21
 
 
22
#ifdef HAVE_MLOCK
 
23
#include <sys/mman.h>
 
24
 
 
25
struct st_mem_list
 
26
{
 
27
  LIST list;
 
28
  uchar *page;
 
29
  uint size;
 
30
};
 
31
 
 
32
LIST *mem_list;
 
33
 
 
34
uchar *my_malloc_lock(uint size,myf MyFlags)
 
35
{
 
36
  int success;
 
37
  uint pagesize=sysconf(_SC_PAGESIZE);
 
38
  uchar *ptr;
 
39
  struct st_mem_list *element;
 
40
 
 
41
  size=((size-1) & ~(pagesize-1))+pagesize;
 
42
  if (!(ptr=memalign(pagesize,size)))
 
43
  {
 
44
    if (MyFlags & (MY_FAE+MY_WME))
 
45
      my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size);
 
46
    return(0);
 
47
  }
 
48
  success = mlock((uchar*) ptr,size);
 
49
  if (success != 0 && geteuid() == 0)
 
50
  {
 
51
    fprintf(stderr, "Warning: Failed to lock memory. errno %d\n",
 
52
            errno);
 
53
  }
 
54
  else
 
55
  {
 
56
    /* Add block in a list for munlock */
 
57
    if (!(element=(struct st_mem_list*) my_malloc(sizeof(*element),MyFlags)))
 
58
    {
 
59
      VOID(munlock((uchar*) ptr,size));
 
60
      free(ptr);
 
61
      return(0);
 
62
    }
 
63
    element->list.data=(uchar*) element;
 
64
    element->page=ptr;
 
65
    element->size=size;
 
66
    pthread_mutex_lock(&THR_LOCK_malloc);
 
67
    mem_list=list_add(mem_list,&element->list);
 
68
    pthread_mutex_unlock(&THR_LOCK_malloc);
 
69
  }
 
70
  return(ptr);
 
71
}
 
72
 
 
73
 
 
74
void my_free_lock(uchar *ptr,myf Myflags __attribute__((unused)))
 
75
{
 
76
  LIST *list;
 
77
  struct st_mem_list *element=0;
 
78
 
 
79
  pthread_mutex_lock(&THR_LOCK_malloc);
 
80
  for (list=mem_list ; list ; list=list->next)
 
81
  {
 
82
    element=(struct st_mem_list*) list->data;
 
83
    if (ptr == element->page)
 
84
    {                                           /* Found locked mem */
 
85
      VOID(munlock((uchar*) ptr,element->size));
 
86
      mem_list=list_delete(mem_list,list);
 
87
      break;
 
88
    }
 
89
  }
 
90
  pthread_mutex_unlock(&THR_LOCK_malloc);
 
91
  if (element)
 
92
    my_free((uchar*) element,MYF(0));
 
93
  free(ptr);                                    /* Free even if not locked */
 
94
}
 
95
 
 
96
#endif /* HAVE_MLOCK */