1 |
/* |
2 |
* Copyright (C) 1998,1999 Nikos Mavroyanopoulos |
3 |
* |
4 |
* This library is free software; you can redistribute it and/or |
5 |
* modify it under the terms of the GNU Library General Public |
6 |
* License as published by the Free Software Foundation; either |
7 |
* version 2 of the License, or (at your option) any later version. |
8 |
* |
9 |
* This library is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
* Library General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU Library General Public |
15 |
* License along with this library; if not, write to the |
16 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 |
* Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
/* $Id: xmemory.c,v 1.2 2001/01/23 11:51:19 nmav Exp $ */ |
21 |
|
22 |
#ifndef LIBDEFS_H |
23 |
# define LIBDEFS_H |
24 |
# include <libdefs.h> |
25 |
#endif |
26 |
#include <bzero.h> |
27 |
|
28 |
#ifdef HAVE_MLOCK |
29 |
# ifdef HAVE_SYS_MMAN_H |
30 |
# include <sys/mman.h> |
31 |
# endif |
32 |
#endif |
33 |
|
34 |
#ifdef HAVE_MLOCK |
35 |
void LOCKMEM(void *x, size_t size) |
36 |
{ |
37 |
mlock(x, size); |
38 |
} |
39 |
|
40 |
# define UNLOCKMEM(x,y) munlock(x,y) |
41 |
#else |
42 |
# define LOCKMEM(x,y) |
43 |
# define UNLOCKMEM(x,y) |
44 |
#endif |
45 |
|
46 |
#include <bzero.h> |
47 |
|
48 |
/* memory allocation */ |
49 |
|
50 |
void *mxmalloc(size_t size) |
51 |
{ |
52 |
char *x; |
53 |
|
54 |
x = malloc(size); |
55 |
|
56 |
if (x != NULL) { |
57 |
LOCKMEM(x, size); |
58 |
return x; |
59 |
} |
60 |
return NULL; |
61 |
} |
62 |
|
63 |
void *mxcalloc(size_t nmemb, size_t size) |
64 |
{ |
65 |
char *x; |
66 |
|
67 |
x = calloc(nmemb, size); |
68 |
if (x != NULL) { |
69 |
LOCKMEM(x, size); |
70 |
return x; |
71 |
} |
72 |
return NULL; |
73 |
} |
74 |
|
75 |
void *mxrealloc(void *ptr, size_t size) |
76 |
{ |
77 |
char *x; |
78 |
|
79 |
x = realloc(ptr, size); |
80 |
if (x != NULL) { |
81 |
LOCKMEM(x, size); |
82 |
return x; |
83 |
} |
84 |
return NULL; |
85 |
} |
86 |
|
87 |
|
88 |
void mxfree(void *ptr, size_t size) |
89 |
{ |
90 |
|
91 |
Bzero(ptr, size); |
92 |
UNLOCKMEM(ptr, size); |
93 |
free(ptr); |
94 |
|
95 |
} |