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 2000/02/28 13:20:21 nikos 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 |
#include <xmemory.h> |
48 |
|
49 |
/* memory allocation */ |
50 |
|
51 |
void *mxmalloc(size_t size) |
52 |
{ |
53 |
char *x; |
54 |
|
55 |
x = malloc(size); |
56 |
|
57 |
if (x != NULL) { |
58 |
LOCKMEM(x, size); |
59 |
return x; |
60 |
} else { |
61 |
sleep(1); |
62 |
x = malloc(size); |
63 |
if (x != NULL) { |
64 |
LOCKMEM(x, size); |
65 |
return x; |
66 |
} else { |
67 |
fprintf(stderr, ("Cannot allocate memory\n")); |
68 |
return NULL; |
69 |
} |
70 |
} |
71 |
} |
72 |
|
73 |
void *mxcalloc(size_t nmemb, size_t size) |
74 |
{ |
75 |
char *x; |
76 |
|
77 |
x = calloc(nmemb, size); |
78 |
if (x != NULL) { |
79 |
LOCKMEM(x, size); |
80 |
return x; |
81 |
} else { |
82 |
sleep(1); |
83 |
x = calloc(nmemb, size); |
84 |
if (x != NULL) { |
85 |
LOCKMEM(x, size); |
86 |
return x; |
87 |
} else { |
88 |
fprintf(stderr, ("Cannot allocate memory\n")); |
89 |
return NULL; |
90 |
} |
91 |
} |
92 |
} |
93 |
|
94 |
void *mxrealloc(void *ptr, size_t size) |
95 |
{ |
96 |
char *x; |
97 |
|
98 |
x = realloc(ptr, size); |
99 |
if (x != NULL) { |
100 |
LOCKMEM(x, size); |
101 |
return x; |
102 |
} else { |
103 |
sleep(1); |
104 |
x = realloc(ptr, size); |
105 |
if (x != NULL) { |
106 |
LOCKMEM(x, size); |
107 |
return x; |
108 |
} else { |
109 |
fprintf(stderr, ("Cannot allocate memory\n")); |
110 |
return NULL; |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
|
116 |
void mxfree(void *ptr, size_t size) |
117 |
{ |
118 |
|
119 |
Bzero(ptr, size); |
120 |
UNLOCKMEM(ptr, size); |
121 |
free(ptr); |
122 |
|
123 |
} |