1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
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.
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.
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,
18
/* Alloc a block of locked memory */
20
#include "mysys_priv.h"
21
#include "mysys_err.h"
36
byte *my_malloc_lock(uint size,myf MyFlags)
39
uint pagesize=sysconf(_SC_PAGESIZE);
41
struct st_mem_list *element;
42
DBUG_ENTER("my_malloc_lock");
44
size=((size-1) & ~(pagesize-1))+pagesize;
45
if (!(ptr=memalign(pagesize,size)))
47
if (MyFlags & (MY_FAE+MY_WME))
48
my_error(EE_OUTOFMEMORY, MYF(ME_BELL+ME_WAITTANG),size);
51
success = mlock((byte*) ptr,size);
52
if (success != 0 && geteuid() == 0)
54
DBUG_PRINT("warning",("Failed to lock memory. errno %d\n",
56
fprintf(stderr, "Warning: Failed to lock memory. errno %d\n",
61
/* Add block in a list for munlock */
62
if (!(element=(struct st_mem_list*) my_malloc(sizeof(*element),MyFlags)))
64
VOID(munlock((byte*) ptr,size));
68
element->list.data=(byte*) element;
71
pthread_mutex_lock(&THR_LOCK_malloc);
72
mem_list=list_add(mem_list,&element->list);
73
pthread_mutex_unlock(&THR_LOCK_malloc);
79
void my_free_lock(byte *ptr,myf Myflags __attribute__((unused)))
82
struct st_mem_list *element=0;
84
pthread_mutex_lock(&THR_LOCK_malloc);
85
for (list=mem_list ; list ; list=list->next)
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);
95
pthread_mutex_unlock(&THR_LOCK_malloc);
97
my_free((gptr) element,MYF(0));
98
free(ptr); /* Free even if not locked */
101
#endif /* HAVE_MLOCK */