1 |
/* Copyright (C) 1998,1999 Nikos Mavroyanopoulos |
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 |
15 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
16 |
* Boston, MA 02111-1307, USA. |
17 |
*/ |
18 |
|
19 |
/* $Id: xmemory.c,v 1.1.1.1 2000/05/22 13:09:10 nmav Exp $ */ |
20 |
|
21 |
#ifndef LIBDEFS_H |
22 |
# define LIBDEFS_H |
23 |
# include <libdefs.h> |
24 |
#endif |
25 |
#include <bzero.h> |
26 |
|
27 |
#ifdef WIN32 |
28 |
# define sleep(x) Sleep(1000*x) |
29 |
#endif |
30 |
|
31 |
#ifdef HAVE_SYS_MMAN_H |
32 |
# include <sys/mman.h> |
33 |
#endif |
34 |
|
35 |
#ifdef HAVE_MLOCK |
36 |
void LOCKMEM(void *x, size_t size) |
37 |
{ |
38 |
mlock(x, size); |
39 |
} |
40 |
|
41 |
# define UNLOCKMEM(x,y) munlock(x,y) |
42 |
#else |
43 |
# define LOCKMEM(x,y) |
44 |
# define UNLOCKMEM(x,y) |
45 |
#endif |
46 |
|
47 |
#include <bzero.h> |
48 |
#include <xmemory.h> |
49 |
|
50 |
/* memory allocation */ |
51 |
|
52 |
void *mxmalloc(size_t size) |
53 |
{ |
54 |
char *x; |
55 |
|
56 |
x = malloc(size); |
57 |
|
58 |
if (x != NULL) { |
59 |
LOCKMEM(x, size); |
60 |
return x; |
61 |
} else { |
62 |
sleep(1); |
63 |
x = malloc(size); |
64 |
if (x != NULL) { |
65 |
LOCKMEM(x, size); |
66 |
return x; |
67 |
} else { |
68 |
fprintf(stderr, ("Cannot allocate memory\n")); |
69 |
return NULL; |
70 |
} |
71 |
} |
72 |
} |
73 |
|
74 |
void *mxcalloc(size_t nmemb, size_t size) |
75 |
{ |
76 |
char *x; |
77 |
|
78 |
x = calloc(nmemb, size); |
79 |
if (x != NULL) { |
80 |
LOCKMEM(x, size); |
81 |
return x; |
82 |
} else { |
83 |
sleep(1); |
84 |
x = calloc(nmemb, size); |
85 |
if (x != NULL) { |
86 |
LOCKMEM(x, size); |
87 |
return x; |
88 |
} else { |
89 |
fprintf(stderr, ("Cannot allocate memory\n")); |
90 |
return NULL; |
91 |
} |
92 |
} |
93 |
} |
94 |
|
95 |
void *mxrealloc(void *ptr, size_t size) |
96 |
{ |
97 |
char *x; |
98 |
|
99 |
x = realloc(ptr, size); |
100 |
if (x != NULL) { |
101 |
LOCKMEM(x, size); |
102 |
return x; |
103 |
} else { |
104 |
sleep(1); |
105 |
x = realloc(ptr, size); |
106 |
if (x != NULL) { |
107 |
LOCKMEM(x, size); |
108 |
return x; |
109 |
} else { |
110 |
fprintf(stderr, ("Cannot allocate memory\n")); |
111 |
return NULL; |
112 |
} |
113 |
} |
114 |
} |
115 |
|
116 |
|
117 |
void mxfree(void *ptr, size_t size) |
118 |
{ |
119 |
|
120 |
// Bzero(ptr, size); |
121 |
UNLOCKMEM(ptr, size); |
122 |
// free(ptr); |
123 |
|
124 |
} |