Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/realloc.c @ 9928:9a02133ad731
Add tentative support for Linux libc5.
author | Bruno Haible <bruno@clisp.org> |
---|---|
date | Thu, 17 Apr 2008 02:01:23 +0200 |
parents | 19e60ffc2be6 |
children | e8d2c6fc33ad |
rev | line source |
---|---|
5462
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
1 /* realloc() function that is glibc compatible. |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
2 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
3 Copyright (C) 1997, 2003, 2004, 2006, 2007 Free Software Foundation, Inc. |
1027 | 4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9203
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
1027 | 6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9203
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9203
diff
changeset
|
8 (at your option) any later version. |
1027 | 9 |
10 This program is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 GNU General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU General Public License | |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
9203
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
1027 | 17 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
18 /* written by Jim Meyering and Bruno Haible */ |
1027 | 19 |
7302
8a1a9361108c
* _fpending.c: Include <config.h> unconditionally, since we no
Paul Eggert <eggert@cs.ucla.edu>
parents:
6259
diff
changeset
|
20 #include <config.h> |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
21 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
22 /* Only the AC_FUNC_REALLOC macro defines 'realloc' already in config.h. */ |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
23 #ifdef realloc |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
24 # define NEED_REALLOC_GNU 1 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
25 #endif |
1027 | 26 |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
27 /* Infer the properties of the system's malloc function. |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
28 Only the AC_FUNC_MALLOC macro defines 'malloc' already in config.h. */ |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
29 #if GNULIB_MALLOC_GNU && !defined malloc |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
30 # define SYSTEM_MALLOC_GLIBC_COMPATIBLE 1 |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
31 #endif |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
32 |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
33 /* Below we want to call the system's malloc and realloc. |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
34 Undefine the symbols here so that including <stdlib.h> provides a |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
35 declaration of malloc(), not of rpl_malloc(), and likewise for realloc. */ |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
36 #undef malloc |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
37 #undef realloc |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
38 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
39 /* Specification. */ |
4662 | 40 #include <stdlib.h> |
1027 | 41 |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
42 #include <errno.h> |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
43 |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
44 /* Below we want to call the system's malloc and realloc. |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
45 Undefine the symbols, if they were defined by gnulib's <stdlib.h> |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
46 replacement. */ |
9203
b62ad47fce4a
Oops, avoid an infinite recursion.
Bruno Haible <bruno@clisp.org>
parents:
9191
diff
changeset
|
47 #undef malloc |
b62ad47fce4a
Oops, avoid an infinite recursion.
Bruno Haible <bruno@clisp.org>
parents:
9191
diff
changeset
|
48 #undef realloc |
b62ad47fce4a
Oops, avoid an infinite recursion.
Bruno Haible <bruno@clisp.org>
parents:
9191
diff
changeset
|
49 |
1027 | 50 /* Change the size of an allocated block of memory P to N bytes, |
1085 | 51 with error checking. If N is zero, change it to 1. If P is NULL, |
52 use malloc. */ | |
1027 | 53 |
4662 | 54 void * |
55 rpl_realloc (void *p, size_t n) | |
1027 | 56 { |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
57 void *result; |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
58 |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
59 #if NEED_REALLOC_GNU |
1085 | 60 if (n == 0) |
5462
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
61 { |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
62 n = 1; |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
63 |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
64 /* In theory realloc might fail, so don't rely on it to free. */ |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
65 free (p); |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
66 p = NULL; |
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
67 } |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
68 #endif |
5462
5ebd8ce9b57f
(rpl_realloc): Call 'free' if n==0, since realloc might fail.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4662
diff
changeset
|
69 |
9453
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
70 if (p == NULL) |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
71 { |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
72 #if GNULIB_REALLOC_GNU && !NEED_REALLOC_GNU && !SYSTEM_MALLOC_GLIBC_COMPATIBLE |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
73 if (n == 0) |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
74 n = 1; |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
75 #endif |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
76 result = malloc (n); |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
77 } |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
78 else |
19e60ffc2be6
Fix a compilation error of realloc.c on OSF/1 4.0d and similar bugs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
79 result = realloc (p, n); |
9191
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
80 |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
81 #if !HAVE_REALLOC_POSIX |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
82 if (result == NULL) |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
83 errno = ENOMEM; |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
84 #endif |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
85 |
c7875dab3c3e
New module 'realloc-posix'.
Bruno Haible <bruno@clisp.org>
parents:
7302
diff
changeset
|
86 return result; |
1027 | 87 } |