| 1 | /* vmem.h
|
|---|
| 2 | *
|
|---|
| 3 | * (c) 1999 Microsoft Corporation. All rights reserved.
|
|---|
| 4 | * Portions (c) 1999 ActiveState Tool Corp, http://www.ActiveState.com/
|
|---|
| 5 | *
|
|---|
| 6 | * You may distribute under the terms of either the GNU General Public
|
|---|
| 7 | * License or the Artistic License, as specified in the README file.
|
|---|
| 8 | *
|
|---|
| 9 | * Options:
|
|---|
| 10 | *
|
|---|
| 11 | * Defining _USE_MSVCRT_MEM_ALLOC will cause all memory allocations
|
|---|
| 12 | * to be forwarded to MSVCRT.DLL. Defining _USE_LINKED_LIST as well will
|
|---|
| 13 | * track all allocations in a doubly linked list, so that the host can
|
|---|
| 14 | * free all memory allocated when it goes away.
|
|---|
| 15 | * If _USE_MSVCRT_MEM_ALLOC is not defined then Knuth's boundary tag algorithm
|
|---|
| 16 | * is used; defining _USE_BUDDY_BLOCKS will use Knuth's algorithm R
|
|---|
| 17 | * (Buddy system reservation)
|
|---|
| 18 | *
|
|---|
| 19 | */
|
|---|
| 20 |
|
|---|
| 21 | #ifndef ___VMEM_H_INC___
|
|---|
| 22 | #define ___VMEM_H_INC___
|
|---|
| 23 |
|
|---|
| 24 | #define _USE_MSVCRT_MEM_ALLOC
|
|---|
| 25 | #define _USE_LINKED_LIST
|
|---|
| 26 |
|
|---|
| 27 | // #define _USE_BUDDY_BLOCKS
|
|---|
| 28 |
|
|---|
| 29 | // #define _DEBUG_MEM
|
|---|
| 30 | #ifdef _DEBUG_MEM
|
|---|
| 31 | #define ASSERT(f) if(!(f)) DebugBreak();
|
|---|
| 32 |
|
|---|
| 33 | inline void MEMODS(char *str)
|
|---|
| 34 | {
|
|---|
| 35 | OutputDebugString(str);
|
|---|
| 36 | OutputDebugString("\n");
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | inline void MEMODSlx(char *str, long x)
|
|---|
| 40 | {
|
|---|
| 41 | char szBuffer[512];
|
|---|
| 42 | sprintf(szBuffer, "%s %lx\n", str, x);
|
|---|
| 43 | OutputDebugString(szBuffer);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | #define WALKHEAP() WalkHeap(0)
|
|---|
| 47 | #define WALKHEAPTRACE() WalkHeap(1)
|
|---|
| 48 |
|
|---|
| 49 | #else
|
|---|
| 50 |
|
|---|
| 51 | #define ASSERT(f)
|
|---|
| 52 | #define MEMODS(x)
|
|---|
| 53 | #define MEMODSlx(x, y)
|
|---|
| 54 | #define WALKHEAP()
|
|---|
| 55 | #define WALKHEAPTRACE()
|
|---|
| 56 |
|
|---|
| 57 | #endif
|
|---|
| 58 |
|
|---|
| 59 | #ifdef _USE_MSVCRT_MEM_ALLOC
|
|---|
| 60 |
|
|---|
| 61 | #ifndef _USE_LINKED_LIST
|
|---|
| 62 | // #define _USE_LINKED_LIST
|
|---|
| 63 | #endif
|
|---|
| 64 |
|
|---|
| 65 | /*
|
|---|
| 66 | * Pass all memory requests throught to msvcrt.dll
|
|---|
| 67 | * optionaly track by using a doubly linked header
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | typedef void (*LPFREE)(void *block);
|
|---|
| 71 | typedef void* (*LPMALLOC)(size_t size);
|
|---|
| 72 | typedef void* (*LPREALLOC)(void *block, size_t size);
|
|---|
| 73 | #ifdef _USE_LINKED_LIST
|
|---|
| 74 | class VMem;
|
|---|
| 75 | typedef struct _MemoryBlockHeader* PMEMORY_BLOCK_HEADER;
|
|---|
| 76 | typedef struct _MemoryBlockHeader {
|
|---|
| 77 | PMEMORY_BLOCK_HEADER pNext;
|
|---|
| 78 | PMEMORY_BLOCK_HEADER pPrev;
|
|---|
| 79 | VMem *owner;
|
|---|
| 80 | } MEMORY_BLOCK_HEADER, *PMEMORY_BLOCK_HEADER;
|
|---|
| 81 | #endif
|
|---|
| 82 |
|
|---|
| 83 | class VMem
|
|---|
| 84 | {
|
|---|
| 85 | public:
|
|---|
| 86 | VMem();
|
|---|
| 87 | ~VMem();
|
|---|
| 88 | virtual void* Malloc(size_t size);
|
|---|
| 89 | virtual void* Realloc(void* pMem, size_t size);
|
|---|
| 90 | virtual void Free(void* pMem);
|
|---|
| 91 | virtual void GetLock(void);
|
|---|
| 92 | virtual void FreeLock(void);
|
|---|
| 93 | virtual int IsLocked(void);
|
|---|
| 94 | virtual long Release(void);
|
|---|
| 95 | virtual long AddRef(void);
|
|---|
| 96 |
|
|---|
| 97 | inline BOOL CreateOk(void)
|
|---|
| 98 | {
|
|---|
| 99 | return TRUE;
|
|---|
| 100 | };
|
|---|
| 101 |
|
|---|
| 102 | protected:
|
|---|
| 103 | #ifdef _USE_LINKED_LIST
|
|---|
| 104 | void LinkBlock(PMEMORY_BLOCK_HEADER ptr)
|
|---|
| 105 | {
|
|---|
| 106 | PMEMORY_BLOCK_HEADER next = m_Dummy.pNext;
|
|---|
| 107 | m_Dummy.pNext = ptr;
|
|---|
| 108 | ptr->pPrev = &m_Dummy;
|
|---|
| 109 | ptr->pNext = next;
|
|---|
| 110 | ptr->owner = this;
|
|---|
| 111 | next->pPrev = ptr;
|
|---|
| 112 | }
|
|---|
| 113 | void UnlinkBlock(PMEMORY_BLOCK_HEADER ptr)
|
|---|
| 114 | {
|
|---|
| 115 | PMEMORY_BLOCK_HEADER next = ptr->pNext;
|
|---|
| 116 | PMEMORY_BLOCK_HEADER prev = ptr->pPrev;
|
|---|
| 117 | prev->pNext = next;
|
|---|
| 118 | next->pPrev = prev;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | MEMORY_BLOCK_HEADER m_Dummy;
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | long m_lRefCount; // number of current users
|
|---|
| 125 | CRITICAL_SECTION m_cs; // access lock
|
|---|
| 126 | HINSTANCE m_hLib;
|
|---|
| 127 | LPFREE m_pfree;
|
|---|
| 128 | LPMALLOC m_pmalloc;
|
|---|
| 129 | LPREALLOC m_prealloc;
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | VMem::VMem()
|
|---|
| 133 | {
|
|---|
| 134 | m_lRefCount = 1;
|
|---|
| 135 | InitializeCriticalSection(&m_cs);
|
|---|
| 136 | #ifdef _USE_LINKED_LIST
|
|---|
| 137 | m_Dummy.pNext = m_Dummy.pPrev = &m_Dummy;
|
|---|
| 138 | m_Dummy.owner = this;
|
|---|
| 139 | #endif
|
|---|
| 140 | m_hLib = LoadLibrary("msvcrt.dll");
|
|---|
| 141 | if (m_hLib) {
|
|---|
| 142 | m_pfree = (LPFREE)GetProcAddress(m_hLib, "free");
|
|---|
| 143 | m_pmalloc = (LPMALLOC)GetProcAddress(m_hLib, "malloc");
|
|---|
| 144 | m_prealloc = (LPREALLOC)GetProcAddress(m_hLib, "realloc");
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | VMem::~VMem(void)
|
|---|
| 149 | {
|
|---|
| 150 | #ifdef _USE_LINKED_LIST
|
|---|
| 151 | while (m_Dummy.pNext != &m_Dummy) {
|
|---|
| 152 | Free(m_Dummy.pNext+1);
|
|---|
| 153 | }
|
|---|
| 154 | #endif
|
|---|
| 155 | if (m_hLib)
|
|---|
| 156 | FreeLibrary(m_hLib);
|
|---|
| 157 | DeleteCriticalSection(&m_cs);
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | void* VMem::Malloc(size_t size)
|
|---|
| 161 | {
|
|---|
| 162 | #ifdef _USE_LINKED_LIST
|
|---|
| 163 | GetLock();
|
|---|
| 164 | PMEMORY_BLOCK_HEADER ptr = (PMEMORY_BLOCK_HEADER)m_pmalloc(size+sizeof(MEMORY_BLOCK_HEADER));
|
|---|
| 165 | LinkBlock(ptr);
|
|---|
| 166 | FreeLock();
|
|---|
| 167 | return (ptr+1);
|
|---|
| 168 | #else
|
|---|
| 169 | return m_pmalloc(size);
|
|---|
| 170 | #endif
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | void* VMem::Realloc(void* pMem, size_t size)
|
|---|
| 174 | {
|
|---|
| 175 | #ifdef _USE_LINKED_LIST
|
|---|
| 176 | if (!pMem)
|
|---|
| 177 | return Malloc(size);
|
|---|
| 178 |
|
|---|
| 179 | if (!size) {
|
|---|
| 180 | Free(pMem);
|
|---|
| 181 | return NULL;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | GetLock();
|
|---|
| 185 | PMEMORY_BLOCK_HEADER ptr = (PMEMORY_BLOCK_HEADER)(((char*)pMem)-sizeof(MEMORY_BLOCK_HEADER));
|
|---|
| 186 | UnlinkBlock(ptr);
|
|---|
| 187 | ptr = (PMEMORY_BLOCK_HEADER)m_prealloc(ptr, size+sizeof(MEMORY_BLOCK_HEADER));
|
|---|
| 188 | LinkBlock(ptr);
|
|---|
| 189 | FreeLock();
|
|---|
| 190 |
|
|---|
| 191 | return (ptr+1);
|
|---|
| 192 | #else
|
|---|
| 193 | return m_prealloc(pMem, size);
|
|---|
| 194 | #endif
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | void VMem::Free(void* pMem)
|
|---|
| 198 | {
|
|---|
| 199 | #ifdef _USE_LINKED_LIST
|
|---|
| 200 | if (pMem) {
|
|---|
| 201 | PMEMORY_BLOCK_HEADER ptr = (PMEMORY_BLOCK_HEADER)(((char*)pMem)-sizeof(MEMORY_BLOCK_HEADER));
|
|---|
| 202 | if (ptr->owner != this) {
|
|---|
| 203 | if (ptr->owner) {
|
|---|
| 204 | #if 1
|
|---|
| 205 | dTHX;
|
|---|
| 206 | int *nowhere = NULL;
|
|---|
| 207 | Perl_warn(aTHX_ "Free to wrong pool %p not %p",this,ptr->owner);
|
|---|
| 208 | *nowhere = 0;
|
|---|
| 209 | #else
|
|---|
| 210 | ptr->owner->Free(pMem);
|
|---|
| 211 | #endif
|
|---|
| 212 | }
|
|---|
| 213 | return;
|
|---|
| 214 | }
|
|---|
| 215 | GetLock();
|
|---|
| 216 | UnlinkBlock(ptr);
|
|---|
| 217 | ptr->owner = NULL;
|
|---|
| 218 | m_pfree(ptr);
|
|---|
| 219 | FreeLock();
|
|---|
| 220 | }
|
|---|
| 221 | #else
|
|---|
| 222 | m_pfree(pMem);
|
|---|
| 223 | #endif
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | void VMem::GetLock(void)
|
|---|
| 227 | {
|
|---|
| 228 | EnterCriticalSection(&m_cs);
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | void VMem::FreeLock(void)
|
|---|
| 232 | {
|
|---|
| 233 | LeaveCriticalSection(&m_cs);
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | int VMem::IsLocked(void)
|
|---|
| 237 | {
|
|---|
| 238 | #if 0
|
|---|
| 239 | /* XXX TryEnterCriticalSection() is not available in some versions
|
|---|
| 240 | * of Windows 95. Since this code is not used anywhere yet, we
|
|---|
| 241 | * skirt the issue for now. */
|
|---|
| 242 | BOOL bAccessed = TryEnterCriticalSection(&m_cs);
|
|---|
| 243 | if(bAccessed) {
|
|---|
| 244 | LeaveCriticalSection(&m_cs);
|
|---|
| 245 | }
|
|---|
| 246 | return !bAccessed;
|
|---|
| 247 | #else
|
|---|
| 248 | ASSERT(0); /* alarm bells for when somebody calls this */
|
|---|
| 249 | return 0;
|
|---|
| 250 | #endif
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | long VMem::Release(void)
|
|---|
| 254 | {
|
|---|
| 255 | long lCount = InterlockedDecrement(&m_lRefCount);
|
|---|
| 256 | if(!lCount)
|
|---|
| 257 | delete this;
|
|---|
| 258 | return lCount;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 | long VMem::AddRef(void)
|
|---|
| 262 | {
|
|---|
| 263 | long lCount = InterlockedIncrement(&m_lRefCount);
|
|---|
| 264 | return lCount;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | #else /* _USE_MSVCRT_MEM_ALLOC */
|
|---|
| 268 |
|
|---|
| 269 | /*
|
|---|
| 270 | * Knuth's boundary tag algorithm Vol #1, Page 440.
|
|---|
| 271 | *
|
|---|
| 272 | * Each block in the heap has tag words before and after it,
|
|---|
| 273 | * TAG
|
|---|
| 274 | * block
|
|---|
| 275 | * TAG
|
|---|
| 276 | * The size is stored in these tags as a long word, and includes the 8 bytes
|
|---|
| 277 | * of overhead that the boundary tags consume. Blocks are allocated on long
|
|---|
| 278 | * word boundaries, so the size is always multiples of long words. When the
|
|---|
| 279 | * block is allocated, bit 0, (the tag bit), of the size is set to 1. When
|
|---|
| 280 | * a block is freed, it is merged with adjacent free blocks, and the tag bit
|
|---|
| 281 | * is set to 0.
|
|---|
| 282 | *
|
|---|
| 283 | * A linked list is used to manage the free list. The first two long words of
|
|---|
| 284 | * the block contain double links. These links are only valid when the block
|
|---|
| 285 | * is freed, therefore space needs to be reserved for them. Thus, the minimum
|
|---|
| 286 | * block size (not counting the tags) is 8 bytes.
|
|---|
| 287 | *
|
|---|
| 288 | * Since memory allocation may occur on a single threaded, explict locks are not
|
|---|
| 289 | * provided.
|
|---|
| 290 | *
|
|---|
| 291 | */
|
|---|
| 292 |
|
|---|
| 293 | const long lAllocStart = 0x00020000; /* start at 128K */
|
|---|
| 294 | const long minBlockSize = sizeof(void*)*2;
|
|---|
| 295 | const long sizeofTag = sizeof(long);
|
|---|
| 296 | const long blockOverhead = sizeofTag*2;
|
|---|
| 297 | const long minAllocSize = minBlockSize+blockOverhead;
|
|---|
| 298 | #ifdef _USE_BUDDY_BLOCKS
|
|---|
| 299 | const long lSmallBlockSize = 1024;
|
|---|
| 300 | const size_t nListEntries = ((lSmallBlockSize-minAllocSize)/sizeof(long));
|
|---|
| 301 |
|
|---|
| 302 | inline size_t CalcEntry(size_t size)
|
|---|
| 303 | {
|
|---|
| 304 | ASSERT((size&(sizeof(long)-1)) == 0);
|
|---|
| 305 | return ((size - minAllocSize) / sizeof(long));
|
|---|
| 306 | }
|
|---|
| 307 | #endif
|
|---|
| 308 |
|
|---|
| 309 | typedef BYTE* PBLOCK; /* pointer to a memory block */
|
|---|
| 310 |
|
|---|
| 311 | /*
|
|---|
| 312 | * Macros for accessing hidden fields in a memory block:
|
|---|
| 313 | *
|
|---|
| 314 | * SIZE size of this block (tag bit 0 is 1 if block is allocated)
|
|---|
| 315 | * PSIZE size of previous physical block
|
|---|
| 316 | */
|
|---|
| 317 |
|
|---|
| 318 | #define SIZE(block) (*(ULONG*)(((PBLOCK)(block))-sizeofTag))
|
|---|
| 319 | #define PSIZE(block) (*(ULONG*)(((PBLOCK)(block))-(blockOverhead)))
|
|---|
| 320 | inline void SetTags(PBLOCK block, long size)
|
|---|
| 321 | {
|
|---|
| 322 | SIZE(block) = size;
|
|---|
| 323 | PSIZE(block+(size&~1)) = size;
|
|---|
| 324 | }
|
|---|
| 325 |
|
|---|
| 326 | /*
|
|---|
| 327 | * Free list pointers
|
|---|
| 328 | * PREV pointer to previous block
|
|---|
| 329 | * NEXT pointer to next block
|
|---|
| 330 | */
|
|---|
| 331 |
|
|---|
| 332 | #define PREV(block) (*(PBLOCK*)(block))
|
|---|
| 333 | #define NEXT(block) (*(PBLOCK*)((block)+sizeof(PBLOCK)))
|
|---|
| 334 | inline void SetLink(PBLOCK block, PBLOCK prev, PBLOCK next)
|
|---|
| 335 | {
|
|---|
| 336 | PREV(block) = prev;
|
|---|
| 337 | NEXT(block) = next;
|
|---|
| 338 | }
|
|---|
| 339 | inline void Unlink(PBLOCK p)
|
|---|
| 340 | {
|
|---|
| 341 | PBLOCK next = NEXT(p);
|
|---|
| 342 | PBLOCK prev = PREV(p);
|
|---|
| 343 | NEXT(prev) = next;
|
|---|
| 344 | PREV(next) = prev;
|
|---|
| 345 | }
|
|---|
| 346 | #ifndef _USE_BUDDY_BLOCKS
|
|---|
| 347 | inline void AddToFreeList(PBLOCK block, PBLOCK pInList)
|
|---|
| 348 | {
|
|---|
| 349 | PBLOCK next = NEXT(pInList);
|
|---|
| 350 | NEXT(pInList) = block;
|
|---|
| 351 | SetLink(block, pInList, next);
|
|---|
| 352 | PREV(next) = block;
|
|---|
| 353 | }
|
|---|
| 354 | #endif
|
|---|
| 355 |
|
|---|
| 356 | /* Macro for rounding up to the next sizeof(long) */
|
|---|
| 357 | #define ROUND_UP(n) (((ULONG)(n)+sizeof(long)-1)&~(sizeof(long)-1))
|
|---|
| 358 | #define ROUND_UP64K(n) (((ULONG)(n)+0x10000-1)&~(0x10000-1))
|
|---|
| 359 | #define ROUND_DOWN(n) ((ULONG)(n)&~(sizeof(long)-1))
|
|---|
| 360 |
|
|---|
| 361 | /*
|
|---|
| 362 | * HeapRec - a list of all non-contiguous heap areas
|
|---|
| 363 | *
|
|---|
| 364 | * Each record in this array contains information about a non-contiguous heap area.
|
|---|
| 365 | */
|
|---|
| 366 |
|
|---|
| 367 | const int maxHeaps = 32; /* 64 was overkill */
|
|---|
| 368 | const long lAllocMax = 0x80000000; /* max size of allocation */
|
|---|
| 369 |
|
|---|
| 370 | #ifdef _USE_BUDDY_BLOCKS
|
|---|
| 371 | typedef struct _FreeListEntry
|
|---|
| 372 | {
|
|---|
| 373 | BYTE Dummy[minAllocSize]; // dummy free block
|
|---|
| 374 | } FREE_LIST_ENTRY, *PFREE_LIST_ENTRY;
|
|---|
| 375 | #endif
|
|---|
| 376 |
|
|---|
| 377 | #ifndef _USE_BUDDY_BLOCKS
|
|---|
| 378 | #define USE_BIGBLOCK_ALLOC
|
|---|
| 379 | #endif
|
|---|
| 380 | /*
|
|---|
| 381 | * performance tuning
|
|---|
| 382 | * Use VirtualAlloc() for blocks bigger than nMaxHeapAllocSize since
|
|---|
| 383 | * Windows 95/98/Me have heap managers that are designed for memory
|
|---|
| 384 | * blocks smaller than four megabytes.
|
|---|
| 385 | */
|
|---|
| 386 |
|
|---|
| 387 | #ifdef USE_BIGBLOCK_ALLOC
|
|---|
| 388 | const int nMaxHeapAllocSize = (1024*512); /* don't allocate anything larger than this from the heap */
|
|---|
| 389 | #endif
|
|---|
| 390 |
|
|---|
| 391 | typedef struct _HeapRec
|
|---|
| 392 | {
|
|---|
| 393 | PBLOCK base; /* base of heap area */
|
|---|
| 394 | ULONG len; /* size of heap area */
|
|---|
| 395 | #ifdef USE_BIGBLOCK_ALLOC
|
|---|
| 396 | BOOL bBigBlock; /* was allocate using VirtualAlloc */
|
|---|
| 397 | #endif
|
|---|
| 398 | } HeapRec;
|
|---|
| 399 |
|
|---|
| 400 | class VMem
|
|---|
| 401 | {
|
|---|
| 402 | public:
|
|---|
| 403 | VMem();
|
|---|
| 404 | ~VMem();
|
|---|
| 405 | virtual void* Malloc(size_t size);
|
|---|
|
|---|