Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Buffer-based memory allocator
- *
- * Copyright (C) 2006-2013, Brainspark B.V.
- *
- * This file is part of PolarSSL (http://www.polarssl.org)
- * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
- *
- * All rights reserved.
- *
- * 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.
- *
- * 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.
- *
- * 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.
- */
- #include "config.h"
- #if defined(POLARSSL_MEMORY_C) && defined(POLARSSL_MEMORY_BUFFER_ALLOC_C)
- #include "memory.h"
- #include <linux/string.h>
- //#include <linux/smp.h>
- #include <linux/crypto.h>
- #if defined(POLARSSL_MEMORY_DEBUG)
- //#include <stdio.h>
- #define fprintf
- #if defined(POLARSSL_MEMORY_BACKTRACE)
- #include <execinfo.h>
- #endif
- #endif
- #if defined(POLARSSL_THREADING_C)
- #include "threading.h"
- #endif
- buffer_alloc_ctx heap[MAX_CPUS_TSX];
- //unsigned char malloc_buf[MAX_CPUS_TSX][MALLOC_SIZE] __attribute__((aligned(64)));
- static int verify_header( memory_header *hdr )
- {
- if( hdr->magic1 != MAGIC1 )
- {
- return( 1 );
- }
- if( hdr->magic2 != MAGIC2 )
- {
- return( 1 );
- }
- if( hdr->alloc > 1 )
- {
- return( 1 );
- }
- if( hdr->prev != NULL && hdr->prev == hdr->next )
- {
- return( 1 );
- }
- if( hdr->prev_free != NULL && hdr->prev_free == hdr->next_free )
- {
- return( 1 );
- }
- return( 0 );
- }
- static int verify_chain(void)
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- memory_header *prv = heap[id].first, *cur = heap[id].first->next;
- if( verify_header( heap[id].first ) != 0 )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: verification of first header failed\n" );
- #endif
- return( 1 );
- }
- if( heap[id].first->prev != NULL )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: verification failed: first->prev != NULL\n" );
- #endif
- return( 1 );
- }
- while( cur != NULL )
- {
- if( verify_header( cur ) != 0 )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: verification of header failed\n" );
- #endif
- return( 1 );
- }
- if( cur->prev != prv )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: verification failed: cur->prev != prv\n" );
- #endif
- return( 1 );
- }
- prv = cur;
- cur = cur->next;
- }
- return( 0 );
- }
- void *buffer_alloc_malloc( size_t len )
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- memory_header *new, *cur = heap[id].first_free;
- unsigned char *p;
- if( heap[id].buf == NULL || heap[id].first == NULL )
- return( NULL );
- if( len % POLARSSL_MEMORY_ALIGN_MULTIPLE )
- {
- len -= len % POLARSSL_MEMORY_ALIGN_MULTIPLE;
- len += POLARSSL_MEMORY_ALIGN_MULTIPLE;
- }
- // Find block that fits
- //
- while( cur != NULL )
- {
- if( cur->size >= len )
- break;
- cur = cur->next_free;
- }
- if( cur == NULL )
- return( NULL );
- if( cur->alloc != 0 )
- {
- //exit( 1 ); //GL/////////////
- return( NULL );
- }
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].malloc_count++;
- #endif
- // Found location, split block if > memory_header + 4 room left
- //
- if( cur->size - len < sizeof(memory_header) + POLARSSL_MEMORY_ALIGN_MULTIPLE )
- {
- cur->alloc = 1;
- // Remove from free_list
- //
- if( cur->prev_free != NULL )
- cur->prev_free->next_free = cur->next_free;
- else
- heap[id].first_free = cur->next_free;
- if( cur->next_free != NULL )
- cur->next_free->prev_free = cur->prev_free;
- cur->prev_free = NULL;
- cur->next_free = NULL;
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].total_used += cur->size;
- if( heap[id].total_used > heap[id].maximum_used)
- heap[id].maximum_used = heap[id].total_used;
- #endif
- #if defined(POLARSSL_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[id].verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ){
- //exit( 1 ); //GL/////////////
- return( NULL );
- }
- return ( (unsigned char *) cur ) + sizeof(memory_header);
- }
- p = ( (unsigned char *) cur ) + sizeof(memory_header) + len;
- new = (memory_header *) p;
- new->size = cur->size - len - sizeof(memory_header);
- new->alloc = 0;
- new->prev = cur;
- new->next = cur->next;
- #if defined(POLARSSL_MEMORY_BACKTRACE)
- new->trace = NULL;
- new->trace_count = 0;
- #endif
- new->magic1 = MAGIC1;
- new->magic2 = MAGIC2;
- if( new->next != NULL )
- new->next->prev = new;
- // Replace cur with new in free_list
- //
- new->prev_free = cur->prev_free;
- new->next_free = cur->next_free;
- if( new->prev_free != NULL )
- new->prev_free->next_free = new;
- else
- heap[id].first_free = new;
- if( new->next_free != NULL )
- new->next_free->prev_free = new;
- cur->alloc = 1;
- cur->size = len;
- cur->next = new;
- cur->prev_free = NULL;
- cur->next_free = NULL;
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].header_count++;
- heap[id].total_used += cur->size;
- if( heap[id].total_used > heap[id].maximum_used)
- heap[id].maximum_used = heap[id].total_used;
- #endif
- #if defined(POLARSSL_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[id].verify & MEMORY_VERIFY_ALLOC ) && verify_chain() != 0 ){
- //exit( 1 ); //GL/////////////
- return( NULL );
- }
- return ( (unsigned char *) cur ) + sizeof(memory_header);
- }
- void buffer_alloc_free( void *ptr )
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- memory_header *hdr, *old = NULL;
- unsigned char *p = (unsigned char *) ptr;
- if( ptr == NULL || heap[id].buf == NULL || heap[id].first == NULL )
- return;
- if( p < heap[id].buf || p > heap[id].buf + heap[id].len )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: polarssl_free() outside of managed space\n" );
- #endif
- //exit( 1 ); //GL/////////////
- return;
- }
- p -= sizeof(memory_header);
- hdr = (memory_header *) p;
- if( verify_header( hdr ) != 0 ){
- //exit( 1 ); //GL/////////////
- return;
- }
- if( hdr->alloc != 1 )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- //fprintf( stderr, "FATAL: polarssl_free() on unallocated data\n" );
- #endif
- //exit( 1 ); //GL/////////////
- return;
- }
- hdr->alloc = 0;
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].free_count++;
- heap[id].total_used -= hdr->size;
- #endif
- // Regroup with block before
- //
- if( hdr->prev != NULL && hdr->prev->alloc == 0 )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].header_count--;
- #endif
- hdr->prev->size += sizeof(memory_header) + hdr->size;
- hdr->prev->next = hdr->next;
- old = hdr;
- hdr = hdr->prev;
- if( hdr->next != NULL )
- hdr->next->prev = hdr;
- #if defined(POLARSSL_MEMORY_BACKTRACE)
- free( old->trace );
- #endif
- memset1( old, 0, sizeof(memory_header) );
- }
- // Regroup with block after
- //
- if( hdr->next != NULL && hdr->next->alloc == 0 )
- {
- #if defined(POLARSSL_MEMORY_DEBUG)
- heap[id].header_count--;
- #endif
- hdr->size += sizeof(memory_header) + hdr->next->size;
- old = hdr->next;
- hdr->next = hdr->next->next;
- if( hdr->prev_free != NULL || hdr->next_free != NULL )
- {
- if( hdr->prev_free != NULL )
- hdr->prev_free->next_free = hdr->next_free;
- else
- heap[id].first_free = hdr->next_free;
- if( hdr->next_free != NULL )
- hdr->next_free->prev_free = hdr->prev_free;
- }
- hdr->prev_free = old->prev_free;
- hdr->next_free = old->next_free;
- if( hdr->prev_free != NULL )
- hdr->prev_free->next_free = hdr;
- else
- heap[id].first_free = hdr;
- if( hdr->next_free != NULL )
- hdr->next_free->prev_free = hdr;
- if( hdr->next != NULL )
- hdr->next->prev = hdr;
- #if defined(POLARSSL_MEMORY_BACKTRACE)
- free( old->trace );
- #endif
- memset1( old, 0, sizeof(memory_header) );
- }
- // Prepend to free_list if we have not merged
- // (Does not have to stay in same order as prev / next list)
- //
- if( old == NULL )
- {
- hdr->next_free = heap[id].first_free;
- heap[id].first_free->prev_free = hdr;
- heap[id].first_free = hdr;
- }
- #if defined(POLARSSL_MEMORY_BACKTRACE)
- hdr->trace = NULL;
- hdr->trace_count = 0;
- #endif
- if( ( heap[id].verify & MEMORY_VERIFY_FREE ) && verify_chain() != 0 ){
- //exit( 1 ); //GL/////////////
- return;
- }
- }
- void memory_buffer_set_verify( int verify )
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- heap[id].verify = verify;
- }
- int memory_buffer_alloc_verify()
- {
- return verify_chain();
- }
- #if defined(POLARSSL_MEMORY_DEBUG)
- void memory_buffer_alloc_status()
- {
- /*
- int id = smp_processor_id();
- //fprintf( stderr,
- "Current use: %u blocks / %u bytes, max: %u bytes, malloc / free: %u / %u\n",
- heap[id].header_count, heap[id].total_used, heap[id].maximum_used,
- heap[id].malloc_count, heap[id].free_count );
- if( heap[id].first->next == NULL )
- //fprintf( stderr, "All memory de-allocated in stack buffer\n" );
- else
- {
- //fprintf( stderr, "Memory currently allocated:\n" );
- //debug_chain();
- }
- */
- }
- #endif /* POLARSSL_MEMORY_BUFFER_ALLOC_DEBUG */
- int memory_buffer_alloc_init( )
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- unsigned char *buf;
- buf = heap[id].malloc_buf;
- #ifdef USE_STACK
- memset1( heap+id, 0, heap[id].stack- (unsigned char *)&(heap[id]) );
- #else
- //memset( &heap[id], 0, sizeof(heap[id]) );
- memset1( heap+ id, 0, sizeof(buffer_alloc_ctx) );
- #endif
- //P_DEBUG("id: %d, buffer_alloc_ctx: %x, id: %d, buffer_alloc_ctx: %x, id: %d, buffer_alloc_ctx: %x\n",0,&(heap[0]),1,&(heap[1]),smp_processor_id(),&(heap[smp_processor_id()]));
- #if defined(POLARSSL_THREADING_C)
- polarssl_mutex_init( heap+id.mutex );
- polarssl_malloc = buffer_alloc_malloc_mutexed;
- polarssl_free = buffer_alloc_free_mutexed;
- #else
- //polarssl_malloc = buffer_alloc_malloc;
- //polarssl_free = buffer_alloc_free;
- #endif
- heap[id].buf = buf;
- heap[id].len = MALLOC_SIZE;
- heap[id].first = (memory_header *) buf;
- heap[id].first->size = MALLOC_SIZE - sizeof(memory_header);
- heap[id].first->magic1 = MAGIC1;
- heap[id].first->magic2 = MAGIC2;
- heap[id].first_free = heap[id].first;
- return( 0 );
- }
- void memory_buffer_alloc_free()
- {
- int id = smp_processor_id();
- #ifdef HYPER
- if(id == 1 || id == 3 || id == 5 || id == 7)
- id--;
- #endif
- #if defined(POLARSSL_THREADING_C)
- polarssl_mutex_free( &heap[id].mutex );
- #endif
- memset1( heap+id, 0, sizeof(buffer_alloc_ctx) );
- }
- #endif /* POLARSSL_MEMORY_C && POLARSSL_MEMORY_BUFFER_ALLOC_C */
Add Comment
Please, Sign In to add comment