[MBEDTLS] Update mbedtls to v2.1.1. By Ismael Ferreras Morezuelas with a fix by Roy...
[reactos.git] / reactos / dll / 3rdparty / mbedtls / memory_buffer_alloc.c
index 6cde16a..b2c775a 100644 (file)
@@ -1,56 +1,49 @@
 /*
  *  Buffer-based memory allocator
  *
- *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
+ *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
+ *  SPDX-License-Identifier: Apache-2.0
  *
- *  This file is part of mbed TLS (https://polarssl.org)
+ *  Licensed under the Apache License, Version 2.0 (the "License"); you may
+ *  not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
  *
- *  This program is free software; you can redistribute it and/or modify
- *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
+ *  http://www.apache.org/licenses/LICENSE-2.0
  *
- *  This program is distributed in the hope that it will be useful,
- *  but WITHOUT ANY WARRANTY; without even the implied warranty of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *  GNU General Public License for more details.
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
  *
- *  You should have received a copy of the GNU General Public License along
- *  with this program; if not, write to the Free Software Foundation, Inc.,
- *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *  This file is part of mbed TLS (https://tls.mbed.org)
  */
 
-#if !defined(POLARSSL_CONFIG_FILE)
-#include "polarssl/config.h"
+#if !defined(MBEDTLS_CONFIG_FILE)
+#include "mbedtls/config.h"
 #else
-#include POLARSSL_CONFIG_FILE
+#include MBEDTLS_CONFIG_FILE
 #endif
 
-#if defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
+#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
+#include "mbedtls/memory_buffer_alloc.h"
 
-#include "polarssl/memory_buffer_alloc.h"
+/* No need for the header guard as MBEDTLS_MEMORY_BUFFER_ALLOC_C
+   is dependent upon MBEDTLS_PLATFORM_C */
+#include "mbedtls/platform.h"
 
 #include <string.h>
 
-#if defined(POLARSSL_MEMORY_DEBUG)
-#include <stdio.h>
-#endif
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
 #include <execinfo.h>
 #endif
 
-#if defined(POLARSSL_THREADING_C)
-#include "polarssl/threading.h"
-#endif
-
-#if defined(POLARSSL_PLATFORM_C)
-#include "polarssl/platform.h"
-#else
-#define polarssl_fprintf fprintf
+#if defined(MBEDTLS_THREADING_C)
+#include "mbedtls/threading.h"
 #endif
 
 /* Implementation that should never be optimized out by the compiler */
-static void polarssl_zeroize( void *v, size_t n ) {
+static void mbedtls_zeroize( void *v, size_t n ) {
     volatile unsigned char *p = v; while( n-- ) *p++ = 0;
 }
 
@@ -68,7 +61,7 @@ struct _memory_header
     memory_header   *next;
     memory_header   *prev_free;
     memory_header   *next_free;
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     char            **trace;
     size_t          trace_count;
 #endif
@@ -81,43 +74,42 @@ typedef struct
     size_t          len;
     memory_header   *first;
     memory_header   *first_free;
-    size_t          current_alloc_size;
     int             verify;
-#if defined(POLARSSL_MEMORY_DEBUG)
-    size_t          malloc_count;
+#if defined(MBEDTLS_MEMORY_DEBUG)
+    size_t          alloc_count;
     size_t          free_count;
     size_t          total_used;
     size_t          maximum_used;
     size_t          header_count;
     size_t          maximum_header_count;
 #endif
-#if defined(POLARSSL_THREADING_C)
-    threading_mutex_t   mutex;
+#if defined(MBEDTLS_THREADING_C)
+    mbedtls_threading_mutex_t   mutex;
 #endif
 }
 buffer_alloc_ctx;
 
 static buffer_alloc_ctx heap;
 
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
 static void debug_header( memory_header *hdr )
 {
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     size_t i;
 #endif
 
-    polarssl_fprintf( stderr, "HDR:  PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
+    mbedtls_fprintf( stderr, "HDR:  PTR(%10zu), PREV(%10zu), NEXT(%10zu), "
                               "ALLOC(%zu), SIZE(%10zu)\n",
                       (size_t) hdr, (size_t) hdr->prev, (size_t) hdr->next,
                       hdr->alloc, hdr->size );
-    polarssl_fprintf( stderr, "      FPREV(%10zu), FNEXT(%10zu)\n",
+    mbedtls_fprintf( stderr, "      FPREV(%10zu), FNEXT(%10zu)\n",
                       (size_t) hdr->prev_free, (size_t) hdr->next_free );
 
-#if defined(POLARSSL_MEMORY_BACKTRACE)
-    polarssl_fprintf( stderr, "TRACE: \n" );
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
+    mbedtls_fprintf( stderr, "TRACE: \n" );
     for( i = 0; i < hdr->trace_count; i++ )
-        polarssl_fprintf( stderr, "%s\n", hdr->trace[i] );
-    polarssl_fprintf( stderr, "\n" );
+        mbedtls_fprintf( stderr, "%s\n", hdr->trace[i] );
+    mbedtls_fprintf( stderr, "\n" );
 #endif
 }
 
@@ -125,14 +117,14 @@ static void debug_chain()
 {
     memory_header *cur = heap.first;
 
-    polarssl_fprintf( stderr, "\nBlock list\n" );
+    mbedtls_fprintf( stderr, "\nBlock list\n" );
     while( cur != NULL )
     {
         debug_header( cur );
         cur = cur->next;
     }
 
-    polarssl_fprintf( stderr, "Free list\n" );
+    mbedtls_fprintf( stderr, "Free list\n" );
     cur = heap.first_free;
 
     while( cur != NULL )
@@ -141,46 +133,46 @@ static void debug_chain()
         cur = cur->next_free;
     }
 }
-#endif /* POLARSSL_MEMORY_DEBUG */
+#endif /* MBEDTLS_MEMORY_DEBUG */
 
 static int verify_header( memory_header *hdr )
 {
     if( hdr->magic1 != MAGIC1 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: MAGIC1 mismatch\n" );
 #endif
         return( 1 );
     }
 
     if( hdr->magic2 != MAGIC2 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: MAGIC2 mismatch\n" );
 #endif
         return( 1 );
     }
 
     if( hdr->alloc > 1 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: alloc has illegal value\n" );
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: alloc has illegal value\n" );
 #endif
         return( 1 );
     }
 
     if( hdr->prev != NULL && hdr->prev == hdr->next )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: prev == next\n" );
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: prev == next\n" );
 #endif
         return( 1 );
     }
 
     if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: prev_free == next_free\n" );
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: prev_free == next_free\n" );
 #endif
         return( 1 );
     }
@@ -194,8 +186,8 @@ static int verify_chain()
 
     if( verify_header( heap.first ) != 0 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: verification of first header "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: verification of first header "
                                   "failed\n" );
 #endif
         return( 1 );
@@ -203,8 +195,8 @@ static int verify_chain()
 
     if( heap.first->prev != NULL )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: verification failed: "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: verification failed: "
                                   "first->prev != NULL\n" );
 #endif
         return( 1 );
@@ -214,8 +206,8 @@ static int verify_chain()
     {
         if( verify_header( cur ) != 0 )
         {
-#if defined(POLARSSL_MEMORY_DEBUG)
-            polarssl_fprintf( stderr, "FATAL: verification of header "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+            mbedtls_fprintf( stderr, "FATAL: verification of header "
                                       "failed\n" );
 #endif
             return( 1 );
@@ -223,8 +215,8 @@ static int verify_chain()
 
         if( cur->prev != prv )
         {
-#if defined(POLARSSL_MEMORY_DEBUG)
-            polarssl_fprintf( stderr, "FATAL: verification failed: "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+            mbedtls_fprintf( stderr, "FATAL: verification failed: "
                                       "cur->prev != prv\n" );
 #endif
             return( 1 );
@@ -237,11 +229,13 @@ static int verify_chain()
     return( 0 );
 }
 
-static void *buffer_alloc_malloc( size_t len )
+static void *buffer_alloc_calloc( size_t n, size_t size )
 {
     memory_header *new, *cur = heap.first_free;
     unsigned char *p;
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+    void *ret;
+    size_t original_len, len;
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     void *trace_buffer[MAX_BT];
     size_t trace_cnt;
 #endif
@@ -249,10 +243,15 @@ static void *buffer_alloc_malloc( size_t len )
     if( heap.buf == NULL || heap.first == NULL )
         return( NULL );
 
-    if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
+    original_len = len = n * size;
+
+    if( n != 0 && len / n != size )
+        return( NULL );
+
+    if( len % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
     {
-        len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
-        len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
+        len -= len % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
+        len += MBEDTLS_MEMORY_ALIGN_MULTIPLE;
     }
 
     // Find block that fits
@@ -270,21 +269,21 @@ static void *buffer_alloc_malloc( size_t len )
 
     if( cur->alloc != 0 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: block in free_list but allocated "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: block in free_list but allocated "
                                   "data\n" );
 #endif
-        exit( 1 );
+        mbedtls_exit( 1 );
     }
 
-#if defined(POLARSSL_MEMORY_DEBUG)
-    heap.malloc_count++;
+#if defined(MBEDTLS_MEMORY_DEBUG)
+    heap.alloc_count++;
 #endif
 
     // Found location, split block if > memory_header + 4 room left
     //
     if( cur->size - len < sizeof(memory_header) +
-                          POLARSSL_MEMORY_ALIGN_MULTIPLE )
+                          MBEDTLS_MEMORY_ALIGN_MULTIPLE )
     {
         cur->alloc = 1;
 
@@ -301,21 +300,24 @@ static void *buffer_alloc_malloc( size_t len )
         cur->prev_free = NULL;
         cur->next_free = NULL;
 
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
         heap.total_used += cur->size;
         if( heap.total_used > heap.maximum_used )
             heap.maximum_used = heap.total_used;
 #endif
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
         trace_cnt = backtrace( trace_buffer, MAX_BT );
         cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
         cur->trace_count = trace_cnt;
 #endif
 
-        if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
-            exit( 1 );
+        if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
+            mbedtls_exit( 1 );
+
+        ret = (unsigned char *) cur + sizeof( memory_header );
+        memset( ret, 0, original_len );
 
-        return( ( (unsigned char *) cur ) + sizeof(memory_header) );
+        return( ret );
     }
 
     p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
@@ -325,7 +327,7 @@ static void *buffer_alloc_malloc( size_t len )
     new->alloc = 0;
     new->prev = cur;
     new->next = cur->next;
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     new->trace = NULL;
     new->trace_count = 0;
 #endif
@@ -353,7 +355,7 @@ static void *buffer_alloc_malloc( size_t len )
     cur->prev_free = NULL;
     cur->next_free = NULL;
 
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
     heap.header_count++;
     if( heap.header_count > heap.maximum_header_count )
         heap.maximum_header_count = heap.header_count;
@@ -361,16 +363,19 @@ static void *buffer_alloc_malloc( size_t len )
     if( heap.total_used > heap.maximum_used )
         heap.maximum_used = heap.total_used;
 #endif
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     trace_cnt = backtrace( trace_buffer, MAX_BT );
     cur->trace = backtrace_symbols( trace_buffer, trace_cnt );
     cur->trace_count = trace_cnt;
 #endif
 
-    if( ( heap.verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
-        exit( 1 );
+    if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 )
+        mbedtls_exit( 1 );
+
+    ret = (unsigned char *) cur + sizeof( memory_header );
+    memset( ret, 0, original_len );
 
-    return( ( (unsigned char *) cur ) + sizeof(memory_header) );
+    return( ret );
 }
 
 static void buffer_alloc_free( void *ptr )
@@ -383,31 +388,31 @@ static void buffer_alloc_free( void *ptr )
 
     if( p < heap.buf || p > heap.buf + heap.len )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: polarssl_free() outside of managed "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: mbedtls_free() outside of managed "
                                   "space\n" );
 #endif
-        exit( 1 );
+        mbedtls_exit( 1 );
     }
 
     p -= sizeof(memory_header);
     hdr = (memory_header *) p;
 
     if( verify_header( hdr ) != 0 )
-        exit( 1 );
+        mbedtls_exit( 1 );
 
     if( hdr->alloc != 1 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
-        polarssl_fprintf( stderr, "FATAL: polarssl_free() on unallocated "
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        mbedtls_fprintf( stderr, "FATAL: mbedtls_free() on unallocated "
                                   "data\n" );
 #endif
-        exit( 1 );
+        mbedtls_exit( 1 );
     }
 
     hdr->alloc = 0;
 
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
     heap.free_count++;
     heap.total_used -= hdr->size;
 #endif
@@ -416,7 +421,7 @@ static void buffer_alloc_free( void *ptr )
     //
     if( hdr->prev != NULL && hdr->prev->alloc == 0 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
         heap.header_count--;
 #endif
         hdr->prev->size += sizeof(memory_header) + hdr->size;
@@ -427,7 +432,7 @@ static void buffer_alloc_free( void *ptr )
         if( hdr->next != NULL )
             hdr->next->prev = hdr;
 
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
         free( old->trace );
 #endif
         memset( old, 0, sizeof(memory_header) );
@@ -437,7 +442,7 @@ static void buffer_alloc_free( void *ptr )
     //
     if( hdr->next != NULL && hdr->next->alloc == 0 )
     {
-#if defined(POLARSSL_MEMORY_DEBUG)
+#if defined(MBEDTLS_MEMORY_DEBUG)
         heap.header_count--;
 #endif
         hdr->size += sizeof(memory_header) + hdr->next->size;
@@ -469,7 +474,7 @@ static void buffer_alloc_free( void *ptr )
         if( hdr->next != NULL )
             hdr->next->prev = hdr;
 
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
         free( old->trace );
 #endif
         memset( old, 0, sizeof(memory_header) );
@@ -486,85 +491,108 @@ static void buffer_alloc_free( void *ptr )
         heap.first_free = hdr;
     }
 
-#if defined(POLARSSL_MEMORY_BACKTRACE)
+#if defined(MBEDTLS_MEMORY_BACKTRACE)
     hdr->trace = NULL;
     hdr->trace_count = 0;
 #endif
 
-    if( ( heap.verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
-        exit( 1 );
+    if( ( heap.verify & MBEDTLS_MEMORY_VERIFY_FREE ) && verify_chain() != 0 )
+        mbedtls_exit( 1 );
 }
 
-void memory_buffer_set_verify( int verify )
+void mbedtls_memory_buffer_set_verify( int verify )
 {
     heap.verify = verify;
 }
 
-int memory_buffer_alloc_verify()
+int mbedtls_memory_buffer_alloc_verify()
 {
     return verify_chain();
 }
 
-#if defined(POLARSSL_MEMORY_DEBUG)
-void memory_buffer_alloc_status()
+#if defined(MBEDTLS_MEMORY_DEBUG)
+void mbedtls_memory_buffer_alloc_status()
 {
-    polarssl_fprintf( stderr,
+    mbedtls_fprintf( stderr,
                       "Current use: %zu blocks / %zu bytes, max: %zu blocks / "
-                      "%zu bytes (total %zu bytes), malloc / free: %zu / %zu\n",
+                      "%zu bytes (total %zu bytes), alloc / free: %zu / %zu\n",
                       heap.header_count, heap.total_used,
                       heap.maximum_header_count, heap.maximum_used,
                       heap.maximum_header_count * sizeof( memory_header )
                       + heap.maximum_used,
-                      heap.malloc_count, heap.free_count );
+                      heap.alloc_count, heap.free_count );
 
     if( heap.first->next == NULL )
-        polarssl_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
+        mbedtls_fprintf( stderr, "All memory de-allocated in stack buffer\n" );
     else
     {
-        polarssl_fprintf( stderr, "Memory currently allocated:\n" );
+        mbedtls_fprintf( stderr, "Memory currently allocated:\n" );
         debug_chain();
     }
 }
-#endif /* POLARSSL_MEMORY_DEBUG */
 
-#if defined(POLARSSL_THREADING_C)
-static void *buffer_alloc_malloc_mutexed( size_t len )
+void mbedtls_memory_buffer_alloc_max_get( size_t *max_used, size_t *max_blocks )
+{
+    *max_used   = heap.maximum_used;
+    *max_blocks = heap.maximum_header_count;
+}
+
+void mbedtls_memory_buffer_alloc_max_reset( void )
+{
+    heap.maximum_used = 0;
+    heap.maximum_header_count = 0;
+}
+
+void mbedtls_memory_buffer_alloc_cur_get( size_t *cur_used, size_t *cur_blocks )
+{
+    *cur_used   = heap.total_used;
+    *cur_blocks = heap.header_count;
+}
+#endif /* MBEDTLS_MEMORY_DEBUG */
+
+#if defined(MBEDTLS_THREADING_C)
+static void *buffer_alloc_calloc_mutexed( size_t n, size_t size )
 {
     void *buf;
-    polarssl_mutex_lock( &heap.mutex );
-    buf = buffer_alloc_malloc( len );
-    polarssl_mutex_unlock( &heap.mutex );
+    if( mbedtls_mutex_lock( &heap.mutex ) != 0 )
+        return( NULL );
+    buf = buffer_alloc_calloc( n, size );
+    if( mbedtls_mutex_unlock( &heap.mutex ) )
+        return( NULL );
     return( buf );
 }
 
 static void buffer_alloc_free_mutexed( void *ptr )
 {
-    polarssl_mutex_lock( &heap.mutex );
+    /* We have to good option here, but corrupting the heap seems
+     * worse than loosing memory. */
+    if( mbedtls_mutex_lock( &heap.mutex ) )
+        return;
     buffer_alloc_free( ptr );
-    polarssl_mutex_unlock( &heap.mutex );
+    (void) mbedtls_mutex_unlock( &heap.mutex );
 }
-#endif /* POLARSSL_THREADING_C */
+#endif /* MBEDTLS_THREADING_C */
 
-int memory_buffer_alloc_init( unsigned char *buf, size_t len )
+void mbedtls_memory_buffer_alloc_init( unsigned char *buf, size_t len )
 {
     memset( &heap, 0, sizeof(buffer_alloc_ctx) );
     memset( buf, 0, len );
 
-#if defined(POLARSSL_THREADING_C)
-    polarssl_mutex_init( &heap.mutex );
-    platform_set_malloc_free( buffer_alloc_malloc_mutexed,
+#if defined(MBEDTLS_THREADING_C)
+    mbedtls_mutex_init( &heap.mutex );
+    mbedtls_platform_set_calloc_free( buffer_alloc_calloc_mutexed,
                               buffer_alloc_free_mutexed );
 #else
-    platform_set_malloc_free( buffer_alloc_malloc, buffer_alloc_free );
+    mbedtls_platform_set_calloc_free( buffer_alloc_calloc, buffer_alloc_free );
 #endif
 
-    if( (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE )
+    if( (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE )
     {
         /* Adjust len first since buf is used in the computation */
-        len -= POLARSSL_MEMORY_ALIGN_MULTIPLE
-             - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
-        buf += POLARSSL_MEMORY_ALIGN_MULTIPLE
-             - (size_t) buf % POLARSSL_MEMORY_ALIGN_MULTIPLE;
+        len -= MBEDTLS_MEMORY_ALIGN_MULTIPLE
+             - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
+        buf += MBEDTLS_MEMORY_ALIGN_MULTIPLE
+             - (size_t) buf % MBEDTLS_MEMORY_ALIGN_MULTIPLE;
     }
 
     heap.buf = buf;
@@ -575,24 +603,23 @@ int memory_buffer_alloc_init( unsigned char *buf, size_t len )
     heap.first->magic1 = MAGIC1;
     heap.first->magic2 = MAGIC2;
     heap.first_free = heap.first;
-    return( 0 );
 }
 
-void memory_buffer_alloc_free()
+void mbedtls_memory_buffer_alloc_free()
 {
-#if defined(POLARSSL_THREADING_C)
-    polarssl_mutex_free( &heap.mutex );
+#if defined(MBEDTLS_THREADING_C)
+    mbedtls_mutex_free( &heap.mutex );
 #endif
-    polarssl_zeroize( &heap, sizeof(buffer_alloc_ctx) );
+    mbedtls_zeroize( &heap, sizeof(buffer_alloc_ctx) );
 }
 
-#if defined(POLARSSL_SELF_TEST)
+#if defined(MBEDTLS_SELF_TEST)
 static int check_pointer( void *p )
 {
     if( p == NULL )
         return( -1 );
 
-    if( (size_t) p % POLARSSL_MEMORY_ALIGN_MULTIPLE != 0 )
+    if( (size_t) p % MBEDTLS_MEMORY_ALIGN_MULTIPLE != 0 )
         return( -1 );
 
     return( 0 );
@@ -600,7 +627,10 @@ static int check_pointer( void *p )
 
 static int check_all_free( )
 {
-    if( heap.current_alloc_size != 0 ||
+    if(
+#if defined(MBEDTLS_MEMORY_DEBUG)
+        heap.total_used != 0 ||
+#endif
         heap.first != heap.first_free ||
         (void *) heap.first != (void *) heap.buf )
     {
@@ -614,107 +644,107 @@ static int check_all_free( )
     if( ! (condition) )                     \
     {                                       \
         if( verbose != 0 )                  \
-            polarssl_printf( "failed\n" );  \
+            mbedtls_printf( "failed\n" );  \
                                             \
         ret = 1;                            \
         goto cleanup;                       \
     }
 
-int memory_buffer_alloc_self_test( int verbose )
+int mbedtls_memory_buffer_alloc_self_test( int verbose )
 {
     unsigned char buf[1024];
     unsigned char *p, *q, *r, *end;
     int ret = 0;
 
     if( verbose != 0 )
-        polarssl_printf( "  MBA test #1 (basic alloc-free cycle): " );
+        mbedtls_printf( "  MBA test #1 (basic alloc-free cycle): " );
 
-    memory_buffer_alloc_init( buf, sizeof( buf ) );
+    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
 
-    p = polarssl_malloc( 1 );
-    q = polarssl_malloc( 128 );
-    r = polarssl_malloc( 16 );
+    p = mbedtls_calloc( 1, 1 );
+    q = mbedtls_calloc( 1, 128 );
+    r = mbedtls_calloc( 1, 16 );
 
     TEST_ASSERT( check_pointer( p ) == 0 &&
                  check_pointer( q ) == 0 &&
                  check_pointer( r ) == 0 );
 
-    polarssl_free( r );
-    polarssl_free( q );
-    polarssl_free( p );
+    mbedtls_free( r );
+    mbedtls_free( q );
+    mbedtls_free( p );
 
     TEST_ASSERT( check_all_free( ) == 0 );
 
     /* Memorize end to compare with the next test */
     end = heap.buf + heap.len;
 
-    memory_buffer_alloc_free( );
+    mbedtls_memory_buffer_alloc_free( );
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     if( verbose != 0 )
-        polarssl_printf( "  MBA test #2 (buf not aligned): " );
+        mbedtls_printf( "  MBA test #2 (buf not aligned): " );
 
-    memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
+    mbedtls_memory_buffer_alloc_init( buf + 1, sizeof( buf ) - 1 );
 
     TEST_ASSERT( heap.buf + heap.len == end );
 
-    p = polarssl_malloc( 1 );
-    q = polarssl_malloc( 128 );
-    r = polarssl_malloc( 16 );
+    p = mbedtls_calloc( 1, 1 );
+    q = mbedtls_calloc( 1, 128 );
+    r = mbedtls_calloc( 1, 16 );
 
     TEST_ASSERT( check_pointer( p ) == 0 &&
                  check_pointer( q ) == 0 &&
                  check_pointer( r ) == 0 );
 
-    polarssl_free( r );
-    polarssl_free( q );
-    polarssl_free( p );
+    mbedtls_free( r );
+    mbedtls_free( q );
+    mbedtls_free( p );
 
     TEST_ASSERT( check_all_free( ) == 0 );
 
-    memory_buffer_alloc_free( );
+    mbedtls_memory_buffer_alloc_free( );
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
     if( verbose != 0 )
-        polarssl_printf( "  MBA test #3 (full): " );
+        mbedtls_printf( "  MBA test #3 (full): " );
 
-    memory_buffer_alloc_init( buf, sizeof( buf ) );
+    mbedtls_memory_buffer_alloc_init( buf, sizeof( buf ) );
 
-    p = polarssl_malloc( sizeof( buf ) - sizeof( memory_header ) );
+    p = mbedtls_calloc( 1, sizeof( buf ) - sizeof( memory_header ) );
 
     TEST_ASSERT( check_pointer( p ) == 0 );
-    TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
+    TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
 
-    polarssl_free( p );
+    mbedtls_free( p );
 
-    p = polarssl_malloc( sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
-    q = polarssl_malloc( 16 );
+    p = mbedtls_calloc( 1, sizeof( buf ) - 2 * sizeof( memory_header ) - 16 );
+    q = mbedtls_calloc( 1, 16 );
 
     TEST_ASSERT( check_pointer( p ) == 0 && check_pointer( q ) == 0 );
-    TEST_ASSERT( polarssl_malloc( 1 ) == NULL );
+    TEST_ASSERT( mbedtls_calloc( 1, 1 ) == NULL );
 
-    polarssl_free( q );
+    mbedtls_free( q );
 
-    TEST_ASSERT( polarssl_malloc( 17 ) == NULL );
+    TEST_ASSERT( mbedtls_calloc( 1, 17 ) == NULL );
 
-    polarssl_free( p );
+    mbedtls_free( p );
 
     TEST_ASSERT( check_all_free( ) == 0 );
 
-    memory_buffer_alloc_free( );
+    mbedtls_memory_buffer_alloc_free( );
 
     if( verbose != 0 )
-        polarssl_printf( "passed\n" );
+        mbedtls_printf( "passed\n" );
 
 cleanup:
-    memory_buffer_alloc_free( );
+    mbedtls_memory_buffer_alloc_free( );
 
     return( ret );
 }
-#endif /* POLARSSL_SELF_TEST */
+#endif /* MBEDTLS_SELF_TEST */
 
-#endif /* POLARSSL_MEMORY_BUFFER_ALLOC_C */
+#endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */