Main Page | Class Hierarchy | Compound List | File List | Compound Members | File Members

bigblock.C

Go to the documentation of this file.
00001 
00015 #include <assert.h>
00016 #include <stdio.h>
00017 #include <stdlib.h>
00018 #include <string.h>
00019 #include "stdtypes.h"
00020 #include "blockpool.h"
00021 
00022 /*===============================<o>=====================================
00023 
00024 Copyright 1996, 1997, 2004 Ian Kaplan, Bear Products International,
00025 www.bearcave.com.
00026 
00027 All Rights Reserved
00028 
00029 You may use this software in software components for which you do
00030 not collect money (e.g., non-commercial software).  All commercial
00031 use is reserved.
00032 
00033 ===============================<o>=====================================*/
00034 
00035 
00036 #ifdef _WIN32
00037 
00038 #include <windows.h>  // for VirtualAlloc and GetSysInfo
00039 
00040 #else
00041 
00042 #define _UNIX_
00043 extern "C" int getpagesize(void);
00044 
00045 #endif
00046 
00047 
00048 
00049 void big_block_alloc::alloc_error(void)
00050 {
00051   printf("big_block_alloc: memory allocation error\n");
00052   exit( 1 );
00053 } /* big_block_alloc::alloc_error */
00054 
00055 
00056 
00062 void *big_block_alloc::MemoryAlloc( unsigned int num_bytes )
00063 {
00064   void *mem;
00065 
00066   assert( num_bytes >= alloc_gran );  
00067   assert( num_bytes % page_size == 0 );  // should be an integral number of pages
00068 
00069 #ifdef _UNIX_
00073   mem = (void *)malloc( num_bytes );
00074   if (mem != NULL) { // zero out the memory
00075     memset(mem, 0, num_bytes);
00076   }
00077 
00078 #else
00082 
00083   // Call the Win32 virtual memory allocater.  Virtual blocks are always
00084   // zeroed out (see Microsoft documentation on VirtualAlloc).
00085   mem = VirtualAlloc(NULL, num_bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
00086 #endif
00087 
00088   if (mem == NULL)
00089     alloc_error();
00090 
00091   return mem;
00092 } // big_block_alloc::MemoryAlloc
00093 
00094 
00095 
00099 void big_block_alloc::MemoryFree( void *address )
00100 {
00101 
00102 #ifdef _UNIX_
00106 
00107   free( address );
00108 
00109 #else
00113 
00114   if (! VirtualFree(address, 0, MEM_RELEASE))
00115     alloc_error();
00116 
00117 #endif
00118 
00119 } // big_block_alloc::MemoryFree
00120 
00121 
00122 
00132 void big_block_alloc::GetSysInfo(void)
00133 {
00134 
00135 #ifdef _UNIX_
00139   const int page_multiple = 4;
00140 
00141   page_size = getpagesize();
00142   // on HP-UX 9.0 use page_size = (unsigned int)sysconf( _SC_PAGE_SIZE );
00143   alloc_gran = page_size * page_multiple;
00144 
00145 #else
00149   
00150   SYSTEM_INFO sys_info;  // system info structure, defined in windows.h
00151 
00152   GetSystemInfo( &sys_info );
00153   page_size = sys_info.dwPageSize;
00154   alloc_gran = sys_info.dwAllocationGranularity;
00155 
00156 #endif
00157   assert( page_size != 0 );
00158   assert( alloc_gran != 0 );
00159 } // big_block_alloc::GetSysInfo
00160 

Generated on Wed Mar 31 21:15:55 2004 for Data Structures for a VHDL Compiler by doxygen 1.3.3