Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

796 lines
21 KiB

  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. * Knuth's boundary tag algorithm Vol #1, Page 440.
  10. *
  11. * Each block in the heap has tag words before and after it,
  12. * TAG
  13. * block
  14. * TAG
  15. * The size is stored in these tags as a long word, and includes the 8 bytes
  16. * of overhead that the boundary tags consume. Blocks are allocated on long
  17. * word boundaries, so the size is always multiples of long words. When the
  18. * block is allocated, bit 0, (the tag bit), of the size is set to 1. When
  19. * a block is freed, it is merged with adjacent free blocks, and the tag bit
  20. * is set to 0.
  21. *
  22. * A linked list is used to manage the free list. The first two long words of
  23. * the block contain double links. These links are only valid when the block
  24. * is freed, therefore space needs to be reserved for them. Thus, the minimum
  25. * block size (not counting the tags) is 8 bytes.
  26. *
  27. * Since memory allocation may occur on a single threaded, explict locks are
  28. * provided.
  29. *
  30. */
  31. #ifndef ___VMEM_H_INC___
  32. #define ___VMEM_H_INC___
  33. const long lAllocStart = 0x00010000; /* start at 64K */
  34. const long minBlockSize = sizeof(void*)*2;
  35. const long sizeofTag = sizeof(long);
  36. const long blockOverhead = sizeofTag*2;
  37. const long minAllocSize = minBlockSize+blockOverhead;
  38. typedef BYTE* PBLOCK; /* pointer to a memory block */
  39. /*
  40. * Macros for accessing hidden fields in a memory block:
  41. *
  42. * SIZE size of this block (tag bit 0 is 1 if block is allocated)
  43. * PSIZE size of previous physical block
  44. */
  45. #define SIZE(block) (*(ULONG*)(((PBLOCK)(block))-sizeofTag))
  46. #define PSIZE(block) (*(ULONG*)(((PBLOCK)(block))-(sizeofTag*2)))
  47. inline void SetTags(PBLOCK block, long size)
  48. {
  49. SIZE(block) = size;
  50. PSIZE(block+(size&~1)) = size;
  51. }
  52. /*
  53. * Free list pointers
  54. * PREV pointer to previous block
  55. * NEXT pointer to next block
  56. */
  57. #define PREV(block) (*(PBLOCK*)(block))
  58. #define NEXT(block) (*(PBLOCK*)((block)+sizeof(PBLOCK)))
  59. inline void SetLink(PBLOCK block, PBLOCK prev, PBLOCK next)
  60. {
  61. PREV(block) = prev;
  62. NEXT(block) = next;
  63. }
  64. inline void Unlink(PBLOCK p)
  65. {
  66. PBLOCK next = NEXT(p);
  67. PBLOCK prev = PREV(p);
  68. NEXT(prev) = next;
  69. PREV(next) = prev;
  70. }
  71. inline void AddToFreeList(PBLOCK block, PBLOCK pInList)
  72. {
  73. PBLOCK next = NEXT(pInList);
  74. NEXT(pInList) = block;
  75. SetLink(block, pInList, next);
  76. PREV(next) = block;
  77. }
  78. /* Macro for rounding up to the next sizeof(long) */
  79. #define ROUND_UP(n) (((ULONG)(n)+sizeof(long)-1)&~(sizeof(long)-1))
  80. #define ROUND_UP64K(n) (((ULONG)(n)+0x10000-1)&~(0x10000-1))
  81. #define ROUND_DOWN(n) ((ULONG)(n)&~(sizeof(long)-1))
  82. /*
  83. * HeapRec - a list of all non-contiguous heap areas
  84. *
  85. * Each record in this array contains information about a non-contiguous heap area.
  86. */
  87. const int maxHeaps = 32; /* 64 was overkill */
  88. const long lAllocMax = 0x80000000; /* max size of allocation */
  89. #define USE_BIGBLOCK_ALLOC
  90. /*
  91. * performance tuning
  92. * Use VirtualAlloc() for blocks bigger than nMaxHeapAllocSize since
  93. * Windows 95/98/Me have heap managers that are designed for memory
  94. * blocks smaller than four megabytes.
  95. */
  96. #ifdef USE_BIGBLOCK_ALLOC
  97. const int nMaxHeapAllocSize = (1024*512); /* don't allocate anything larger than this from the heap */
  98. #endif
  99. typedef struct _HeapRec
  100. {
  101. PBLOCK base; /* base of heap area */
  102. ULONG len; /* size of heap area */
  103. #ifdef USE_BIGBLOCK_ALLOC
  104. BOOL bBigBlock; /* was allocate using VirtualAlloc */
  105. #endif
  106. } HeapRec;
  107. class VMem
  108. {
  109. public:
  110. VMem();
  111. ~VMem();
  112. virtual void* Malloc(size_t size);
  113. virtual void* Realloc(void* pMem, size_t size);
  114. virtual void Free(void* pMem);
  115. virtual void GetLock(void);
  116. virtual void FreeLock(void);
  117. virtual int IsLocked(void);
  118. virtual long Release(void);
  119. virtual long AddRef(void);
  120. inline BOOL CreateOk(void)
  121. {
  122. return m_hHeap != NULL;
  123. };
  124. void ReInit(void);
  125. protected:
  126. void Init(void);
  127. int Getmem(size_t size);
  128. #ifdef USE_BIGBLOCK_ALLOC
  129. int HeapAdd(void* ptr, size_t size, BOOL bBigBlock);
  130. #else
  131. int HeapAdd(void* ptr, size_t size);
  132. #endif
  133. void* Expand(void* block, size_t size);
  134. void WalkHeap(void);
  135. HANDLE m_hHeap; // memory heap for this script
  136. char m_FreeDummy[minAllocSize]; // dummy free block
  137. PBLOCK m_pFreeList; // pointer to first block on free list
  138. PBLOCK m_pRover; // roving pointer into the free list
  139. HeapRec m_heaps[maxHeaps]; // list of all non-contiguous heap areas
  140. int m_nHeaps; // no. of heaps in m_heaps
  141. long m_lAllocSize; // current alloc size
  142. long m_lRefCount; // number of current users
  143. CRITICAL_SECTION m_cs; // access lock
  144. #ifdef _DEBUG_MEM
  145. FILE* m_pLog;
  146. #endif
  147. };
  148. // #define _DEBUG_MEM
  149. #ifdef _DEBUG_MEM
  150. #define ASSERT(f) if(!(f)) DebugBreak();
  151. inline void MEMODS(char *str)
  152. {
  153. OutputDebugString(str);
  154. OutputDebugString("\n");
  155. }
  156. inline void MEMODSlx(char *str, long x)
  157. {
  158. char szBuffer[512];
  159. sprintf(szBuffer, "%s %lx\n", str, x);
  160. OutputDebugString(szBuffer);
  161. }
  162. #define WALKHEAP() WalkHeap()
  163. #define WALKHEAPTRACE() m_pRover = NULL; WalkHeap()
  164. #else
  165. #define ASSERT(f)
  166. #define MEMODS(x)
  167. #define MEMODSlx(x, y)
  168. #define WALKHEAP()
  169. #define WALKHEAPTRACE()
  170. #endif
  171. VMem::VMem()
  172. {
  173. m_lRefCount = 1;
  174. BOOL bRet = (NULL != (m_hHeap = HeapCreate(HEAP_NO_SERIALIZE,
  175. lAllocStart, /* initial size of heap */
  176. 0))); /* no upper limit on size of heap */
  177. ASSERT(bRet);
  178. InitializeCriticalSection(&m_cs);
  179. #ifdef _DEBUG_MEM
  180. m_pLog = 0;
  181. #endif
  182. Init();
  183. }
  184. VMem::~VMem(void)
  185. {
  186. ASSERT(HeapValidate(m_hHeap, HEAP_NO_SERIALIZE, NULL));
  187. WALKHEAPTRACE();
  188. #ifdef _DEBUG_MEM
  189. MemoryUsageMessage(NULL, 0, 0, 0);
  190. #endif
  191. DeleteCriticalSection(&m_cs);
  192. #ifdef USE_BIGBLOCK_ALLOC
  193. for(int index = 0; index < m_nHeaps; ++index) {
  194. if (m_heaps[index].bBigBlock) {
  195. VirtualFree(m_heaps[index].base, 0, MEM_RELEASE);
  196. }
  197. }
  198. #endif
  199. BOOL bRet = HeapDestroy(m_hHeap);
  200. ASSERT(bRet);
  201. }
  202. void VMem::ReInit(void)
  203. {
  204. for(int index = 0; index < m_nHeaps; ++index) {
  205. #ifdef USE_BIGBLOCK_ALLOC
  206. if (m_heaps[index].bBigBlock) {
  207. VirtualFree(m_heaps[index].base, 0, MEM_RELEASE);
  208. }
  209. else
  210. #endif
  211. HeapFree(m_hHeap, HEAP_NO_SERIALIZE, m_heaps[index].base);
  212. }
  213. Init();
  214. }
  215. void VMem::Init(void)
  216. { /*
  217. * Initialize the free list by placing a dummy zero-length block on it.
  218. * Set the number of non-contiguous heaps to zero.
  219. */
  220. m_pFreeList = m_pRover = (PBLOCK)(&m_FreeDummy[minBlockSize]);
  221. PSIZE(m_pFreeList) = SIZE(m_pFreeList) = 0;
  222. PREV(m_pFreeList) = NEXT(m_pFreeList) = m_pFreeList;
  223. m_nHeaps = 0;
  224. m_lAllocSize = lAllocStart;
  225. }
  226. void* VMem::Malloc(size_t size)
  227. {
  228. WALKHEAP();
  229. /*
  230. * Adjust the real size of the block to be a multiple of sizeof(long), and add
  231. * the overhead for the boundary tags. Disallow negative or zero sizes.
  232. */
  233. size_t realsize = (size < blockOverhead) ? minAllocSize : (size_t)ROUND_UP(size) + minBlockSize;
  234. if((int)realsize < minAllocSize || size == 0)
  235. return NULL;
  236. /*
  237. * Start searching the free list at the rover. If we arrive back at rover without
  238. * finding anything, allocate some memory from the heap and try again.
  239. */
  240. PBLOCK ptr = m_pRover; /* start searching at rover */
  241. int loops = 2; /* allow two times through the loop */
  242. for(;;) {
  243. size_t lsize = SIZE(ptr);
  244. ASSERT((lsize&1)==0);
  245. /* is block big enough? */
  246. if(lsize >= realsize) {
  247. /* if the remainder is too small, don't bother splitting the block. */
  248. size_t rem = lsize - realsize;
  249. if(rem < minAllocSize) {
  250. if(m_pRover == ptr)
  251. m_pRover = NEXT(ptr);
  252. /* Unlink the block from the free list. */
  253. Unlink(ptr);
  254. }
  255. else {
  256. /*
  257. * split the block
  258. * The remainder is big enough to split off into a new block.
  259. * Use the end of the block, resize the beginning of the block
  260. * no need to change the free list.
  261. */
  262. SetTags(ptr, rem);
  263. ptr += SIZE(ptr);
  264. lsize = realsize;
  265. }
  266. /* Set the boundary tags to mark it as allocated. */
  267. SetTags(ptr, lsize | 1);
  268. return ((void *)ptr);
  269. }
  270. /*
  271. * This block was unsuitable. If we've gone through this list once already without
  272. * finding anything, allocate some new memory from the heap and try again.
  273. */
  274. ptr = NEXT(ptr);
  275. if(ptr == m_pRover) {
  276. if(!(loops-- && Getmem(realsize))) {
  277. return NULL;
  278. }
  279. ptr = m_pRover;
  280. }
  281. }
  282. }
  283. void* VMem::Realloc(void* block, size_t size)
  284. {
  285. WALKHEAP();
  286. /* if size is zero, free the block. */
  287. if(size == 0) {
  288. Free(block);
  289. return (NULL);
  290. }
  291. /* if block pointer is NULL, do a Malloc(). */
  292. if(block == NULL)
  293. return Malloc(size);
  294. /*
  295. * Grow or shrink the block in place.
  296. * if the block grows then the next block will be used if free
  297. */
  298. if(Expand(block, size) != NULL)
  299. return block;
  300. /*
  301. * adjust the real size of the block to be a multiple of sizeof(long), and add the
  302. * overhead for the boundary tags. Disallow negative or zero sizes.
  303. */
  304. size_t realsize = (size < blockOverhead) ? minAllocSize : (size_t)ROUND_UP(size) + minBlockSize;
  305. if((int)realsize < minAllocSize)
  306. return NULL;
  307. /*
  308. * see if the previous block is free, and is it big enough to cover the new size
  309. * if merged with the current block.
  310. */
  311. PBLOCK ptr = (PBLOCK)block;
  312. size_t cursize = SIZE(ptr) & ~1;
  313. size_t psize = PSIZE(ptr);
  314. if((psize&1) == 0 && (psize + cursize) >= realsize) {
  315. PBLOCK prev = ptr - psize;
  316. if(m_pRover == prev)
  317. m_pRover = NEXT(prev);
  318. /* Unlink the next block from the free list. */
  319. Unlink(prev);
  320. /* Copy contents of old block to new location, make it the current block. */
  321. memmove(prev, ptr, cursize);
  322. cursize += psize; /* combine sizes */
  323. ptr = prev;
  324. size_t rem = cursize - realsize;
  325. if(rem >= minAllocSize) {
  326. /*
  327. * The remainder is big enough to be a new block. Set boundary
  328. * tags for the resized block and the new block.
  329. */
  330. prev = ptr + realsize;
  331. /*
  332. * add the new block to the free list.
  333. * next block cannot be free
  334. */
  335. SetTags(prev, rem);
  336. AddToFreeList(prev, m_pFreeList);
  337. cursize = realsize;
  338. }
  339. /* Set the boundary tags to mark it as allocated. */
  340. SetTags(ptr, cursize | 1);
  341. return ((void *)ptr);
  342. }
  343. /* Allocate a new block, copy the old to the new, and free the old. */
  344. if((ptr = (PBLOCK)Malloc(size)) != NULL) {
  345. memmove(ptr, block, cursize-minBlockSize);
  346. Free(block);
  347. }
  348. return ((void *)ptr);
  349. }
  350. void VMem::Free(void* p)
  351. {
  352. WALKHEAP();
  353. /* Ignore null pointer. */
  354. if(p == NULL)
  355. return;
  356. PBLOCK ptr = (PBLOCK)p;
  357. /* Check for attempt to free a block that's already free. */
  358. size_t size = SIZE(ptr);
  359. if((size&1) == 0) {
  360. MEMODSlx("Attempt to free previously freed block", (long)p);
  361. return;
  362. }
  363. size &= ~1; /* remove allocated tag */
  364. /* if previous block is free, add this block to it. */
  365. int linked = FALSE;
  366. size_t psize = PSIZE(ptr);
  367. if((psize&1) == 0) {
  368. ptr -= psize; /* point to previous block */
  369. size += psize; /* merge the sizes of the two blocks */
  370. linked = TRUE; /* it's already on the free list */
  371. }
  372. /* if the next physical block is free, merge it with this block. */
  373. PBLOCK next = ptr + size; /* point to next physical block */
  374. size_t nsize = SIZE(next);
  375. if((nsize&1) == 0) {
  376. /* block is free move rover if needed */
  377. if(m_pRover == next)
  378. m_pRover = NEXT(next);
  379. /* unlink the next block from the free list. */
  380. Unlink(next);
  381. /* merge the sizes of this block and the next block. */
  382. size += nsize;
  383. }
  384. /* Set the boundary tags for the block; */
  385. SetTags(ptr, size);
  386. /* Link the block to the head of the free list. */
  387. if(!linked) {
  388. AddToFreeList(ptr, m_pFreeList);
  389. }
  390. }
  391. void VMem::GetLock(void)
  392. {
  393. EnterCriticalSection(&m_cs);
  394. }
  395. void VMem::FreeLock(void)
  396. {
  397. LeaveCriticalSection(&m_cs);
  398. }
  399. int VMem::IsLocked(void)
  400. {
  401. #if 0
  402. /* XXX TryEnterCriticalSection() is not available in some versions
  403. * of Windows 95. Since this code is not used anywhere yet, we
  404. * skirt the issue for now. */
  405. BOOL bAccessed = TryEnterCriticalSection(&m_cs);
  406. if(bAccessed) {
  407. LeaveCriticalSection(&m_cs);
  408. }
  409. return !bAccessed;
  410. #else
  411. ASSERT(0); /* alarm bells for when somebody calls this */
  412. return 0;
  413. #endif
  414. }
  415. long VMem::Release(void)
  416. {
  417. long lCount = InterlockedDecrement(&m_lRefCount);
  418. if(!lCount)
  419. delete this;
  420. return lCount;
  421. }
  422. long VMem::AddRef(void)
  423. {
  424. long lCount = InterlockedIncrement(&m_lRefCount);
  425. return lCount;
  426. }
  427. int VMem::Getmem(size_t requestSize)
  428. { /* returns -1 is successful 0 if not */
  429. #ifdef USE_BIGBLOCK_ALLOC
  430. BOOL bBigBlock;
  431. #endif
  432. void *ptr;
  433. /* Round up size to next multiple of 64K. */
  434. size_t size = (size_t)ROUND_UP64K(requestSize);
  435. /*
  436. * if the size requested is smaller than our current allocation size
  437. * adjust up
  438. */
  439. if(size < (unsigned long)m_lAllocSize)
  440. size = m_lAllocSize;
  441. /* Update the size to allocate on the next request */
  442. if(m_lAllocSize != lAllocMax)
  443. m_lAllocSize <<= 1;
  444. if(m_nHeaps != 0
  445. #ifdef USE_BIGBLOCK_ALLOC
  446. && !m_heaps[m_nHeaps-1].bBigBlock
  447. #endif
  448. ) {
  449. /* Expand the last allocated heap */
  450. ptr = HeapReAlloc(m_hHeap, HEAP_REALLOC_IN_PLACE_ONLY|HEAP_NO_SERIALIZE,
  451. m_heaps[m_nHeaps-1].base,
  452. m_heaps[m_nHeaps-1].len + size);
  453. if(ptr != 0) {
  454. HeapAdd(((char*)ptr) + m_heaps[m_nHeaps-1].len, size
  455. #ifdef USE_BIGBLOCK_ALLOC
  456. , FALSE
  457. #endif
  458. );
  459. return -1;
  460. }
  461. }
  462. /*
  463. * if we didn't expand a block to cover the requested size
  464. * allocate a new Heap
  465. * the size of this block must include the additional dummy tags at either end
  466. * the above ROUND_UP64K may not have added any memory to include this.
  467. */
  468. if(size == requestSize)
  469. size = (size_t)ROUND_UP64K(requestSize+(sizeofTag*2));
  470. #ifdef USE_BIGBLOCK_ALLOC
  471. bBigBlock = FALSE;
  472. if (size >= nMaxHeapAllocSize) {
  473. bBigBlock = TRUE;
  474. ptr = VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE);
  475. }
  476. else
  477. #endif
  478. ptr = HeapAlloc(m_hHeap, HEAP_NO_SERIALIZE, size);
  479. if(ptr == 0) {
  480. MEMODSlx("HeapAlloc failed on size!!!", size);
  481. return 0;
  482. }
  483. #ifdef USE_BIGBLOCK_ALLOC
  484. if (HeapAdd(ptr, size, bBigBlock)) {
  485. if (bBigBlock) {
  486. VirtualFree(ptr, 0, MEM_RELEASE);
  487. }
  488. }
  489. #else
  490. HeapAdd(ptr, size);
  491. #endif
  492. return -1;
  493. }
  494. #ifdef USE_BIGBLOCK_ALLOC
  495. int VMem::HeapAdd(void* p, size_t size, BOOL bBigBlock)
  496. #else
  497. int VMem::HeapAdd(void* p, size_t size)
  498. #endif
  499. { /* if the block can be succesfully added to the heap, returns 0; otherwise -1. */
  500. int index;
  501. /* Check size, then round size down to next long word boundary. */
  502. if(size < minAllocSize)
  503. return -1;
  504. size = (size_t)ROUND_DOWN(size);
  505. PBLOCK ptr = (PBLOCK)p;
  506. #ifdef USE_BIGBLOCK_ALLOC
  507. if (!bBigBlock) {
  508. #endif
  509. /*
  510. * Search for another heap area that's contiguous with the bottom of this new area.
  511. * (It should be extremely unusual to find one that's contiguous with the top).
  512. */
  513. for(index = 0; index < m_nHeaps; ++index) {
  514. if(ptr == m_heaps[index].base + (int)m_heaps[index].len) {
  515. /*
  516. * The new block is contiguous with a previously allocated heap area. Add its
  517. * length to that of the previous heap. Merge it with the the dummy end-of-heap
  518. * area marker of the previous heap.
  519. */
  520. m_heaps[index].len += size;
  521. break;
  522. }
  523. }
  524. #ifdef USE_BIGBLOCK_ALLOC
  525. }
  526. else {
  527. index = m_nHeaps;
  528. }
  529. #endif
  530. if(index == m_nHeaps) {
  531. /* The new block is not contiguous, or is BigBlock. Add it to the heap list. */
  532. if(m_nHeaps == maxHeaps) {
  533. return -1; /* too many non-contiguous heaps */
  534. }
  535. m_heaps[m_nHeaps].base = ptr;
  536. m_heaps[m_nHeaps].len = size;
  537. #ifdef USE_BIGBLOCK_ALLOC
  538. m_heaps[m_nHeaps].bBigBlock = bBigBlock;
  539. #endif
  540. m_nHeaps++;
  541. /*
  542. * Reserve the first LONG in the block for the ending boundary tag of a dummy
  543. * block at the start of the heap area.
  544. */
  545. size -= minBlockSize;
  546. ptr += minBlockSize;
  547. PSIZE(ptr) = 1; /* mark the dummy previous block as allocated */
  548. }
  549. /*
  550. * Convert the heap to one large block. Set up its boundary tags, and those of
  551. * marker block after it. The marker block before the heap will already have
  552. * been set up if this heap is not contiguous with the end of another heap.
  553. */
  554. SetTags(ptr, size | 1);
  555. PBLOCK next = ptr + size; /* point to dummy end block */
  556. SIZE(next) = 1; /* mark the dummy end block as allocated */
  557. /*
  558. * Link the block to the start of the free list by calling free().
  559. * This will merge the block with any adjacent free blocks.
  560. */
  561. Free(ptr);
  562. return 0;
  563. }
  564. void* VMem::Expand(void* block, size_t size)
  565. {
  566. /*
  567. * Adjust the size of the block to be a multiple of sizeof(long), and add the
  568. * overhead for the boundary tags. Disallow negative or zero sizes.
  569. */
  570. size_t realsize = (size < blockOverhead) ? minAllocSize : (size_t)ROUND_UP(size) + minBlockSize;
  571. if((int)realsize < minAllocSize || size == 0)
  572. return NULL;
  573. PBLOCK ptr = (PBLOCK)block;
  574. /* if the current size is the same as requested, do nothing. */
  575. size_t cursize = SIZE(ptr) & ~1;
  576. if(cursize == realsize) {
  577. return block;
  578. }
  579. /* if the block is being shrunk, convert the remainder of the block into a new free block. */
  580. if(realsize <= cursize) {
  581. size_t nextsize = cursize - realsize; /* size of new remainder block */
  582. if(nextsize >= minAllocSize) {
  583. /*
  584. * Split the block
  585. * Set boundary tags for the resized block and the new block.
  586. */
  587. SetTags(ptr, realsize | 1);
  588. ptr += realsize;
  589. /*
  590. * add the new block to the free list.
  591. * call Free to merge this block with next block if free
  592. */
  593. SetTags(ptr, nextsize | 1);
  594. Free(ptr);
  595. }
  596. return block;
  597. }
  598. PBLOCK next = ptr + cursize;
  599. size_t nextsize = SIZE(next);
  600. /* Check the next block for consistency.*/
  601. if((nextsize&1) == 0 && (nextsize + cursize) >= realsize) {
  602. /*
  603. * The next block is free and big enough. Add the part that's needed
  604. * to our block, and split the remainder off into a new block.
  605. */
  606. if(m_pRover == next)
  607. m_pRover = NEXT(next);
  608. /* Unlink the next block from the free list. */
  609. Unlink(next);
  610. cursize += nextsize; /* combine sizes */
  611. size_t rem = cursize - realsize; /* size of remainder */
  612. if(rem >= minAllocSize) {
  613. /*
  614. * The remainder is big enough to be a new block.
  615. * Set boundary tags for the resized block and the new block.
  616. */
  617. next = ptr + realsize;
  618. /*
  619. * add the new block to the free list.
  620. * next block cannot be free
  621. */
  622. SetTags(next, rem);
  623. AddToFreeList(next, m_pFreeList);
  624. cursize = realsize;
  625. }
  626. /* Set the boundary tags to mark it as allocated. */
  627. SetTags(ptr, cursize | 1);
  628. return ((void *)ptr);
  629. }
  630. return NULL;
  631. }
  632. #ifdef _DEBUG_MEM
  633. #define LOG_FILENAME ".\\MemLog.txt"
  634. void MemoryUsageMessage(char *str, long x, long y, int c)
  635. {
  636. char szBuffer[512];
  637. if(str) {
  638. if(!m_pLog)
  639. m_pLog = fopen(LOG_FILENAME, "w");
  640. sprintf(szBuffer, str, x, y, c);
  641. fputs(szBuffer, m_pLog);
  642. }
  643. else {
  644. fflush(m_pLog);
  645. fclose(m_pLog);
  646. m_pLog = 0;
  647. }
  648. }
  649. void VMem::WalkHeap(void)
  650. {
  651. if(!m_pRover) {
  652. MemoryUsageMessage("VMem heaps used %d\n", m_nHeaps, 0, 0);
  653. }
  654. /* Walk all the heaps - verify structures */
  655. for(int index = 0; index < m_nHeaps; ++index) {
  656. PBLOCK ptr = m_heaps[index].base;
  657. size_t size = m_heaps[index].len;
  658. ASSERT(HeapValidate(m_hHeap, HEAP_NO_SERIALIZE, p));
  659. /* set over reserved header block */
  660. size -= minBlockSize;
  661. ptr += minBlockSize;
  662. PBLOCK pLast = ptr + size;
  663. ASSERT(PSIZE(ptr) == 1); /* dummy previous block is allocated */
  664. ASSERT(SIZE(pLast) == 1); /* dummy next block is allocated */
  665. while(ptr < pLast) {
  666. ASSERT(ptr > m_heaps[index].base);
  667. size_t cursize = SIZE(ptr) & ~1;
  668. ASSERT((PSIZE(ptr+cursize) & ~1) == cursize);
  669. if(!m_pRover) {
  670. MemoryUsageMessage("Memory Block %08x: Size %08x %c\n", (long)ptr, cursize, (SIZE(p)&1) ? 'x' : ' ');
  671. }
  672. if(!(SIZE(ptr)&1)) {
  673. /* this block is on the free list */
  674. PBLOCK tmp = NEXT(ptr);
  675. while(tmp != ptr) {
  676. ASSERT((SIZE(tmp)&1)==0);
  677. if(tmp == m_pFreeList)
  678. break;
  679. ASSERT(NEXT(tmp));
  680. tmp = NEXT(tmp);
  681. }
  682. if(tmp == ptr) {
  683. MemoryUsageMessage("Memory Block %08x: Size %08x free but not in free list\n", (long)ptr, cursize, 0);
  684. }
  685. }
  686. ptr += cursize;
  687. }
  688. }
  689. if(!m_pRover) {
  690. MemoryUsageMessage(NULL, 0, 0, 0);
  691. }
  692. }
  693. #endif
  694. #endif /* ___VMEM_H_INC___ */