~ubuntu-branches/ubuntu/hardy/renameutils/hardy

« back to all changes in this revision

Viewing changes to lib/xmalloc.c

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2006-07-21 16:39:15 UTC
  • mfrom: (1.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060721163915-k3kqs080hol8uw1n
Tags: 0.8.1-4
* Enable large file support on 32-bit platforms (closes: #377845)
* Bump Standards-Version up to 3.7.2 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* xmalloc.c -- malloc with out of memory checking
2
2
 
3
3
   Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4
 
   1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
 
4
   1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5
5
 
6
6
   This program is free software; you can redistribute it and/or modify
7
7
   it under the terms of the GNU General Public License as published by
15
15
 
16
16
   You should have received a copy of the GNU General Public License
17
17
   along with this program; if not, write to the Free Software Foundation,
18
 
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
19
19
 
20
20
#if HAVE_CONFIG_H
21
21
# include <config.h>
30
30
# define SIZE_MAX ((size_t) -1)
31
31
#endif
32
32
 
 
33
/* 1 if calloc is known to be compatible with GNU calloc.  This
 
34
   matters if we are not also using the calloc module, which defines
 
35
   HAVE_CALLOC and supports the GNU API even on non-GNU platforms.  */
 
36
#if defined HAVE_CALLOC || defined __GLIBC__
 
37
enum { HAVE_GNU_CALLOC = 1 };
 
38
#else
 
39
enum { HAVE_GNU_CALLOC = 0 };
 
40
#endif
 
41
 
33
42
/* Allocate an array of N objects, each with S bytes of memory,
34
43
   dynamically, with error checking.  S must be nonzero.  */
35
44
 
204
213
{
205
214
  void *p;
206
215
  /* Test for overflow, since some calloc implementations don't have
207
 
     proper overflow checks.  */
208
 
  if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0))
 
216
     proper overflow checks.  But omit overflow and size-zero tests if
 
217
     HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
 
218
     returns NULL if successful.  */
 
219
  if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
 
220
      || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
209
221
    xalloc_die ();
210
222
  return p;
211
223
}
212
224
 
213
225
/* Clone an object P of size S, with error checking.  There's no need
214
 
   for xnclone (P, N, S), since xclone (P, N * S) works without any
 
226
   for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
215
227
   need for an arithmetic overflow check.  */
216
228
 
217
229
void *
218
 
xclone (void const *p, size_t s)
 
230
xmemdup (void const *p, size_t s)
219
231
{
220
232
  return memcpy (xmalloc (s), p, s);
221
233
}
 
234
 
 
235
/* Clone STRING.  */
 
236
 
 
237
char *
 
238
xstrdup (char const *string)
 
239
{
 
240
  return xmemdup (string, strlen (string) + 1);
 
241
}