1 /* stdmem.c - private memory allocator
2 * Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
4 * This file is part of Libgcrypt.
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
11 * Libgcrypt 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 Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
32 #define MAGIC_NOR_BYTE 0x55
33 #define MAGIC_SEC_BYTE 0xcc
34 #define MAGIC_END_BYTE 0xaa
36 #if SIZEOF_UNSIGNED_LONG == 8
43 static int use_m_guard = 0;
46 * Warning: Never use this function after any of the functions
47 * here have been used.
50 _gcry_private_enable_m_guard(void)
56 * Allocate memory of size n.
57 * Return NULL if we are out of memory.
60 _gcry_private_malloc( size_t n)
63 return NULL; /* allocating 0 bytes is undefined - better return
68 if( !(p = malloc( n + EXTRA_ALIGN+5 )) )
70 ((byte*)p)[EXTRA_ALIGN+0] = n;
71 ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ;
72 ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ;
73 ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_NOR_BYTE;
74 p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE;
75 return p+EXTRA_ALIGN+4;
83 * Allocate memory of size n from the secure memory pool.
84 * Return NULL if we are out of memory.
87 _gcry_private_malloc_secure( size_t n)
90 return NULL; /* allocating 0 bytes is undefined - better return
95 if( !(p = _gcry_secmem_malloc( n +EXTRA_ALIGN+ 5 )) )
97 ((byte*)p)[EXTRA_ALIGN+0] = n;
98 ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ;
99 ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ;
100 ((byte*)p)[EXTRA_ALIGN+3] = MAGIC_SEC_BYTE;
101 p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE;
102 return p+EXTRA_ALIGN+4;
105 return _gcry_secmem_malloc( n );
111 * realloc and clear the old space
112 * Return NULL if there is not enoug memory.
115 _gcry_private_realloc( void *a, size_t n )
118 unsigned char *p = a;
123 return _gcry_private_malloc(n);
125 _gcry_private_check_heap(p);
129 if( len >= n ) /* we don't shrink for now */
131 if( p[-1] == MAGIC_SEC_BYTE )
132 b = _gcry_private_malloc_secure(n);
134 b = _gcry_private_malloc(n);
138 memset(b+len, 0, n-len );
139 _gcry_private_free( p );
142 else if( _gcry_private_is_secure(a) ) {
143 return _gcry_secmem_realloc( a, n );
146 return realloc( a, n );
152 _gcry_private_check_heap( const void *a )
161 if( !(p[-1] == MAGIC_NOR_BYTE || p[-1] == MAGIC_SEC_BYTE) )
162 _gcry_log_fatal("memory at %p corrupted (underflow=%02x)\n", p, p[-1] );
166 if( p[len] != MAGIC_END_BYTE )
167 _gcry_log_fatal("memory at %p corrupted (overflow=%02x)\n", p, p[-1] );
172 * Free a memory block allocated by this opr the secmem module
175 _gcry_private_free( void *a )
182 _gcry_private_check_heap(p);
183 if( _gcry_private_is_secure(a) )
184 _gcry_secmem_free(p-EXTRA_ALIGN-4);
186 free(p-EXTRA_ALIGN-4);
189 else if( _gcry_private_is_secure(a) )
190 _gcry_secmem_free(p);