1 /* no-libgcrypt.c - Replacement functions for libgcrypt.
2  *	Copyright (C) 2003 Free Software Foundation, Inc.
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20 
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 
27 #include "../jnlib/logging.h"
28 
29 #include "i18n.h"
30 #include "no-libgcrypt.h"
31 
32 
33 /* Replace libgcrypt's malloc functions which are used by
34    ../jnlib/libjnlib.a .  util.h defines macros to map them to xmalloc
35    etc. */
36 static void
out_of_core(void)37 out_of_core (void)
38 {
39   log_fatal (_("error allocating enough memory: %s\n"), strerror (errno));
40 }
41 
42 
43 void *
gcry_malloc(size_t n)44 gcry_malloc (size_t n)
45 {
46   return malloc (n);
47 }
48 
49 void *
gcry_xmalloc(size_t n)50 gcry_xmalloc (size_t n)
51 {
52   void *p = malloc (n);
53   if (!p)
54     out_of_core ();
55   return p;
56 }
57 
58 
59 void *
gcry_realloc(void * a,size_t n)60 gcry_realloc (void *a, size_t n)
61 {
62   return realloc (a, n);
63 }
64 
65 void *
gcry_xrealloc(void * a,size_t n)66 gcry_xrealloc (void *a, size_t n)
67 {
68   void *p = realloc (a, n);
69   if (!p)
70     out_of_core ();
71   return p;
72 }
73 
74 
75 
76 void *
gcry_calloc(size_t n,size_t m)77 gcry_calloc (size_t n, size_t m)
78 {
79   return calloc (n, m);
80 }
81 
82 void *
gcry_xcalloc(size_t n,size_t m)83 gcry_xcalloc (size_t n, size_t m)
84 {
85   void *p = calloc (n, m);
86   if (!p)
87     out_of_core ();
88   return p;
89 }
90 
91 
92 char *
gcry_strdup(const char * string)93 gcry_strdup (const char *string)
94 {
95   return strdup (string);
96 }
97 
98 char *
gcry_xstrdup(const char * string)99 gcry_xstrdup (const char *string)
100 {
101   void *p = malloc (strlen (string)+1);
102   if (!p)
103     out_of_core ();
104   strcpy( p, string );
105   return p;
106 }
107 
108 void
gcry_free(void * a)109 gcry_free (void *a)
110 {
111   if (a)
112     free (a);
113 }
114