twe(4): Add two missing error checks.
[dragonfly.git] / contrib / gcc-5.0 / libgcc / config / nvptx / realloc.c
1 /* Implement realloc with the help of the malloc and free wrappers.
2
3    Copyright (C) 2014-2015 Free Software Foundation, Inc.
4
5    This file is free software; you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by the
7    Free Software Foundation; either version 3, or (at your option) any
8    later version.
9
10    This file is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    Under Section 7 of GPL version 3, you are granted additional
16    permissions described in the GCC Runtime Library Exception, version
17    3.1, as published by the Free Software Foundation.
18
19    You should have received a copy of the GNU General Public License and
20    a copy of the GCC Runtime Library Exception along with this program;
21    see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
22    <http://www.gnu.org/licenses/>.  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "nvptx-malloc.h"
27
28 void *
29 __nvptx_realloc (void *ptr, size_t newsz)
30 {
31   if (newsz == 0)
32     {
33       __nvptx_free (ptr);
34       return NULL;
35     }
36   void *newptr = __nvptx_malloc (newsz);
37
38   size_t oldsz;
39   if (ptr == NULL)
40     oldsz = 0;
41   else
42     {
43       size_t *sp = __extension__ (size_t *)(ptr - 8);
44       oldsz = *sp;
45     }
46   if (oldsz != 0)
47     memcpy (newptr, ptr, oldsz > newsz ? newsz : oldsz);
48
49   __nvptx_free (ptr);
50   return newptr;
51 }