certtool is able to set certificate policies via a template
[gnutls.git] / lib / gnutls_mem.c
blobccdc5e12f52e3b82d50e51af1f5dfc3ed91781b9
1 /*
2 * Copyright (C) 2001-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
25 #include <gnutls_num.h>
26 #include <xsize.h>
28 gnutls_alloc_function gnutls_secure_malloc = malloc;
29 gnutls_alloc_function gnutls_malloc = malloc;
30 gnutls_free_function gnutls_free = free;
31 gnutls_realloc_function gnutls_realloc = realloc;
33 void *(*gnutls_calloc) (size_t, size_t) = calloc;
34 char *(*gnutls_strdup) (const char *) = _gnutls_strdup;
36 void *
37 _gnutls_calloc (size_t nmemb, size_t size)
39 void *ret;
40 size_t n = xtimes (nmemb, size);
41 ret = (size_in_bounds_p (n) ? gnutls_malloc (n) : NULL);
42 if (ret != NULL)
43 memset (ret, 0, size);
44 return ret;
47 svoid *
48 gnutls_secure_calloc (size_t nmemb, size_t size)
50 svoid *ret;
51 size_t n = xtimes (nmemb, size);
52 ret = (size_in_bounds_p (n) ? gnutls_secure_malloc (n) : NULL);
53 if (ret != NULL)
54 memset (ret, 0, size);
55 return ret;
58 /* This realloc will free ptr in case realloc
59 * fails.
61 void *
62 gnutls_realloc_fast (void *ptr, size_t size)
64 void *ret;
66 if (size == 0)
67 return ptr;
69 ret = gnutls_realloc (ptr, size);
70 if (ret == NULL)
72 gnutls_free (ptr);
75 return ret;
78 char *
79 _gnutls_strdup (const char *str)
81 size_t siz = strlen (str) + 1;
82 char *ret;
84 ret = gnutls_malloc (siz);
85 if (ret != NULL)
86 memcpy (ret, str, siz);
87 return ret;
91 #if 0
92 /* don't use them. They are included for documentation.
95 /**
96 * gnutls_malloc:
97 * @s: size to allocate in bytes
99 * This function will allocate 's' bytes data, and
100 * return a pointer to memory. This function is supposed
101 * to be used by callbacks.
103 * The allocation function used is the one set by
104 * gnutls_global_set_mem_functions().
106 void *
107 gnutls_malloc (size_t s)
112 * gnutls_free:
113 * @ptr: pointer to memory
115 * This function will free data pointed by ptr.
117 * The deallocation function used is the one set by
118 * gnutls_global_set_mem_functions().
121 void
122 gnutls_free (void *ptr)
126 #endif