Use libtasn1 v2.4.
[gnutls.git] / lib / gnutls_mem.c
blob1acc4a5d4abd04c6839b000b342cb43b6871ce35
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008, 2010 Free Software
3 * Foundation, Inc.
5 * Author: Nikos Mavrogiannopoulos
7 * This file is part of GNUTLS.
9 * The GNUTLS library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 * USA
26 #include <gnutls_int.h>
27 #include <gnutls_errors.h>
28 #include <gnutls_num.h>
29 #include <xsize.h>
31 gnutls_alloc_function gnutls_secure_malloc = malloc;
32 gnutls_alloc_function gnutls_malloc = malloc;
33 gnutls_free_function gnutls_free = free;
34 gnutls_realloc_function gnutls_realloc = realloc;
36 void *(*gnutls_calloc) (size_t, size_t) = calloc;
37 char *(*gnutls_strdup) (const char *) = _gnutls_strdup;
39 int
40 _gnutls_is_secure_mem_null (const void *ign)
42 return 0;
45 int (*_gnutls_is_secure_memory) (const void *) = _gnutls_is_secure_mem_null;
48 void *
49 _gnutls_calloc (size_t nmemb, size_t size)
51 void *ret;
52 size_t n = xtimes (nmemb, size);
53 ret = (size_in_bounds_p (n) ? gnutls_malloc (n) : NULL);
54 if (ret != NULL)
55 memset (ret, 0, size);
56 return ret;
59 svoid *
60 gnutls_secure_calloc (size_t nmemb, size_t size)
62 svoid *ret;
63 size_t n = xtimes (nmemb, size);
64 ret = (size_in_bounds_p (n) ? gnutls_secure_malloc (n) : NULL);
65 if (ret != NULL)
66 memset (ret, 0, size);
67 return ret;
70 /* This realloc will free ptr in case realloc
71 * fails.
73 void *
74 gnutls_realloc_fast (void *ptr, size_t size)
76 void *ret;
78 if (size == 0)
79 return ptr;
81 ret = gnutls_realloc (ptr, size);
82 if (ret == NULL)
84 gnutls_free (ptr);
87 return ret;
90 char *
91 _gnutls_strdup (const char *str)
93 size_t siz = strlen (str) + 1;
94 char *ret;
96 ret = gnutls_malloc (siz);
97 if (ret != NULL)
98 memcpy (ret, str, siz);
99 return ret;
103 #if 0
104 /* don't use them. They are included for documentation.
108 * gnutls_malloc - Allocates and returns data
110 * This function will allocate 's' bytes data, and
111 * return a pointer to memory. This function is supposed
112 * to be used by callbacks.
114 * The allocation function used is the one set by gnutls_global_set_mem_functions().
117 void *
118 gnutls_malloc (size_t s)
123 * gnutls_free - Returns a free() like function
124 * @d: pointer to memory
126 * This function will free data pointed by ptr.
128 * The deallocation function used is the one set by gnutls_global_set_mem_functions().
131 void
132 gnutls_free (void *ptr)
136 #endif