00001 /* 00002 This product contains certain software code or other information 00003 ("AT&T Software") proprietary to AT&T Corp. ("AT&T"). The AT&T 00004 Software is provided to you "AS IS". YOU ASSUME TOTAL RESPONSIBILITY 00005 AND RISK FOR USE OF THE AT&T SOFTWARE. AT&T DOES NOT MAKE, AND 00006 EXPRESSLY DISCLAIMS, ANY EXPRESS OR IMPLIED WARRANTIES OF ANY KIND 00007 WHATSOEVER, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 00008 MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, WARRANTIES OF 00009 TITLE OR NON-INFRINGEMENT OF ANY INTELLECTUAL PROPERTY RIGHTS, ANY 00010 WARRANTIES ARISING BY USAGE OF TRADE, COURSE OF DEALING OR COURSE OF 00011 PERFORMANCE, OR ANY WARRANTY THAT THE AT&T SOFTWARE IS "ERROR FREE" OR 00012 WILL MEET YOUR REQUIREMENTS. 00013 00014 Unless you accept a license to use the AT&T Software, you shall not 00015 reverse compile, disassemble or otherwise reverse engineer this 00016 product to ascertain the source code for any AT&T Software. 00017 00018 (c) AT&T Corp. All rights reserved. AT&T is a registered trademark of AT&T Corp. 00019 00020 *********************************************************************** 00021 00022 History: 00023 00024 24/11/99 - initial release by Hartmut Liefke, liefke@seas.upenn.edu 00025 Dan Suciu, suciu@research.att.com 00026 */ 00027 00028 //************************************************************************** 00029 //************************************************************************** 00030 00031 // This module contains the memory manager for XMill 00032 // The memory manager can handle four different block sizes (see below). 00033 // and the blocks are hierarchically organized. 00034 00035 #include <string.h> 00036 00037 #define BLOCKSIZE_NUM 4 00038 00039 #define LARGEBLOCK_SIZE 65536 00040 #define MEDIUMBLOCK_SIZE 8192 00041 #define SMALLBLOCK_SIZE 1024 00042 #define TINYBLOCK_SIZE 256 00043 00044 extern unsigned long blocksizes[]; // For each possible block size, 00045 // we store the size 00046 00047 extern char *freeblocklists[]; // For each possible block size, 00048 // we store the list of free blocks 00049 00050 00051 #include "Error.hpp" 00052 00053 //********************************************************************** 00054 //********************************************************************** 00055 00056 extern unsigned long allocatedmemory; 00057 00058 // The memory management for blocks 00059 00060 char *AllocateBlockRecurs(unsigned char blocksizeidx); 00061 // Allocates a block of size blocksizes[blocksizeidx] 00062 00063 inline char *AllocateBlock(unsigned char blocksizeidx) 00064 // Allocates a new memory block 00065 // and increases the allocated memory count 00066 { 00067 allocatedmemory+=blocksizes[blocksizeidx]; 00068 return AllocateBlockRecurs(blocksizeidx); 00069 } 00070 00071 inline void FreeBlock(char *ptr,unsigned char blocksizeidx) 00072 // Frees a memory block 00073 { 00074 *(char **)ptr=freeblocklists[blocksizeidx]; 00075 freeblocklists[blocksizeidx]=ptr; 00076 00077 #ifdef RELEASEMEM_SAFE 00078 memset(ptr+4,0xcd,blocksizes[blocksizeidx]-4); 00079 #endif 00080 00081 allocatedmemory-=blocksizes[blocksizeidx]; 00082 } 00083 00084 inline unsigned long GetBlockSize(unsigned char blocksizeidx) 00085 // Returns the block size for a specific index 00086 { 00087 return blocksizes[blocksizeidx]; 00088 } 00089 00090 //********************************************************************** 00091 //********************************************************************** 00092 00093 // For the decompressor, we implement an additional memory management 00094 // A single memory block is allocated (and can be reallocated, if it is 00095 // is too small) and data is stored within that block 00096 // from the start to the end 00097 00098 #ifdef XDEMILL 00099 00100 //extern MemStreamer blockmem; 00101 00102 #define MEMBLOCK_THRESHOLD 8000 00103 00104 extern unsigned char *memoryalloc_buf; 00105 extern unsigned char *memoryalloc_curptr; 00106 extern unsigned long memoryalloc_bufsize; 00107 00108 inline void SetMemoryAllocationSize(unsigned long allocsize) 00109 // Sets the amount of memory needed. If the current block is too small, 00110 // the current block is reallocated. 00111 { 00112 if(memoryalloc_bufsize<allocsize) 00113 // Current block is too small? 00114 { 00115 // Let's reallocate 00116 if(memoryalloc_buf!=NULL) 00117 free(memoryalloc_buf); 00118 00119 memoryalloc_buf=(unsigned char *)malloc(allocsize); 00120 if(memoryalloc_buf==NULL) 00121 ExitNoMem(); 00122 00123 memoryalloc_curptr =memoryalloc_buf; 00124 memoryalloc_bufsize =allocsize; 00125 } 00126 else 00127 // If we have enough space, we just use the existing block 00128 memoryalloc_curptr=memoryalloc_buf; 00129 } 00130 00131 inline void WordAlignMemBlock() 00132 // We align the current pointer to an address divisible by 4 00133 { 00134 memoryalloc_curptr= 00135 (unsigned char *) 00136 (((unsigned long)memoryalloc_curptr+3)&0xFFFFFFFCL); 00137 } 00138 00139 inline unsigned char *AllocateMemBlock(unsigned long size) 00140 // We allocate a new piece of data 00141 { 00142 memoryalloc_curptr+=size; 00143 if(memoryalloc_curptr>memoryalloc_buf+memoryalloc_bufsize) 00144 { 00145 Error("Fatal Error!"); 00146 Exit(); 00147 } 00148 return memoryalloc_curptr-size; 00149 } 00150 00151 inline void FreeMemBlock(void *ptr,unsigned long size) 00152 // We forget about freeing the block, sine we will use the entire block 00153 // later again 00154 { 00155 } 00156 00157 #endif