Ignore:
Timestamp:
Jan 6, 2002, 5:48:47 PM (24 years ago)
Author:
sandervl
Message:

Heap(Re)Alloc changes: allocate in multiples of 8 bytes and allow the memory block to grow to this boundary with HeapReAlloc before returning a new pointer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/os2heap.cpp

    r7360 r7728  
    1 /* $Id: os2heap.cpp,v 1.31 2001-11-16 14:52:55 phaller Exp $ */
     1/* $Id: os2heap.cpp,v 1.32 2002-01-06 16:48:47 sandervl Exp $ */
    22
    33/*
    44 * Heap class for OS/2
    55 *
    6  * Copyright 1998 Sander van Leeuwen
     6 * Copyright 1998-2002 Sander van Leeuwen <sandervl@xs4all.nl>
    77 *
    88 *
     
    1010 *       WINE controls depend on this, even though it apparently
    1111 *       doesn't work like this in Windows.
     12 * NOTE: Round up size to next 8 bytes boundary
     13 *       When reallocating memory block, don't use different
     14 *       memory block unless new size is larger than old size
     15 *       (rounded up to next 8 bytes boundary)
     16 *       (Verified this behaviour in NT4 (Global/Heap(Re)Alloc);
     17 *        fixes crashes in Opera 5.12 which relies on this 'feature')
    1218 *
    1319 * Project Odin Software License can be found in LICENSE.TXT
     
    150156 HEAPELEM *lpHeapObj;
    151157 LPVOID    lpMem;
     158 DWORD     dwAllocBytes;
    152159
    153160//  dprintf(("OS2Heap::Alloc\n"));
    154   lpMem = _umalloc(uheap, dwBytes + HEAP_OVERHEAD);
     161
     162  //size must be multiple of 8 bytes
     163  dwAllocBytes = HEAP_ALIGN(dwBytes);
     164
     165  lpMem = _umalloc(uheap, dwAllocBytes + HEAP_OVERHEAD);
    155166  if(lpMem == NULL) {
    156167      dprintf(("OS2Heap::Alloc, lpMem == NULL"));
     
    158169  }
    159170  if(dwFlags & HEAP_ZERO_MEMORY) {
    160       memset(lpMem, 0, dwBytes+HEAP_OVERHEAD);
     171      memset(lpMem, 0, dwAllocBytes+HEAP_OVERHEAD);
    161172  }
    162173 
    163174#ifdef DEBUG
    164   totalAlloc += dwBytes;
     175  totalAlloc += dwAllocBytes;
    165176#endif
    166177 
    167178  //align at 8 byte boundary
    168   lpHeapObj = (HEAPELEM *)(((ULONG)lpMem+7) & ~7);
    169   lpHeapObj->lpMem = lpMem;
     179  lpHeapObj          = (HEAPELEM *)HEAP_ALIGN(lpMem);
     180  lpHeapObj->lpMem   = lpMem;
    170181  lpHeapObj->magic   = MAGIC_NR_HEAP;
    171 
     182  lpHeapObj->orgsize = dwBytes; //original size
     183  lpHeapObj->cursize = dwBytes; //original size
     184 
    172185  return(LPVOID)(lpHeapObj+1);
    173186}
     
    194207        return -1;
    195208  }
    196 
    197   return(_msize(helem->lpMem) - HEAP_OVERHEAD);
     209  return helem->cursize;  //return current size of memory block
    198210}
    199211//******************************************************************************
     
    203215  HEAPELEM *helem = GET_HEAPOBJ(lpMem);
    204216  LPVOID lpNewMem;
    205   int    i, oldSize;
     217  int    i, maxSize;
    206218
    207219  if (dwBytes == 0) return NULL;         // intercept stupid parameters
     
    218230  }
    219231
    220   oldSize = Size(0,lpMem);
    221   if (oldSize >= dwBytes) {
    222        dprintf(("ReAlloc with smaller size than original (%d); return old pointer", oldSize));
     232  maxSize = HEAP_ALIGN(helem->orgsize);
     233  if (dwBytes <= maxSize) {
     234       dprintf(("ReAlloc with smaller size than original (%d); return old pointer", maxSize));
     235       //update current size so HeapSize will return the right value
     236       helem->cursize = dwBytes; 
    223237       return lpMem; // if reallocation with same size don't do anything
    224238  }
    225239  lpNewMem = Alloc(dwFlags, dwBytes);
    226   memcpy(lpNewMem, lpMem, dwBytes < oldSize ? dwBytes : oldSize);
     240  memcpy(lpNewMem, lpMem, dwBytes < maxSize ? dwBytes : maxSize);
    227241  Free(0, lpMem);
    228242
Note: See TracChangeset for help on using the changeset viewer.