2001-12-19 20:16:27 +01:00
|
|
|
/*
|
|
|
|
* Win32 heap functions
|
|
|
|
*
|
|
|
|
* Copyright 1996 Alexandre Julliard
|
|
|
|
* Copyright 1998 Ulrich Weigand
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2008-10-29 17:32:45 +01:00
|
|
|
#include "wine/port.h"
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2001-12-19 20:16:27 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2003-04-02 03:42:06 +02:00
|
|
|
#ifdef HAVE_VALGRIND_MEMCHECK_H
|
|
|
|
#include <valgrind/memcheck.h>
|
2010-01-27 13:35:32 +01:00
|
|
|
#else
|
|
|
|
#define RUNNING_ON_VALGRIND 0
|
2003-04-02 03:42:06 +02:00
|
|
|
#endif
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2005-11-28 17:32:54 +01:00
|
|
|
#define WIN32_NO_STATUS
|
2015-03-14 03:38:17 +01:00
|
|
|
#define NONAMELESSUNION
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
2001-12-19 20:16:27 +01:00
|
|
|
#include "winnt.h"
|
2003-06-23 20:12:28 +02:00
|
|
|
#include "winternl.h"
|
2013-01-08 14:00:06 +01:00
|
|
|
#include "ntdll_misc.h"
|
2005-08-12 18:05:46 +02:00
|
|
|
#include "wine/list.h"
|
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/server.h"
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(heap);
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2007-10-02 18:50:49 +02:00
|
|
|
/* Note: the heap data structures are loosely based on what Pietrek describes in his
|
2007-10-01 15:32:14 +02:00
|
|
|
* book 'Windows 95 System Programming Secrets', with some adaptations for
|
|
|
|
* better compatibility with NT.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct tagARENA_INUSE
|
|
|
|
{
|
|
|
|
DWORD size; /* Block size; must be the first field */
|
2005-08-15 22:49:47 +02:00
|
|
|
DWORD magic : 24; /* Magic number */
|
|
|
|
DWORD unused_bytes : 8; /* Number of bytes in the block not used by user data (max value is HEAP_MIN_DATA_SIZE+HEAP_MIN_SHRINK_SIZE) */
|
2001-12-19 20:16:27 +01:00
|
|
|
} ARENA_INUSE;
|
|
|
|
|
|
|
|
typedef struct tagARENA_FREE
|
|
|
|
{
|
|
|
|
DWORD size; /* Block size; must be the first field */
|
|
|
|
DWORD magic; /* Magic number */
|
2005-08-12 18:05:46 +02:00
|
|
|
struct list entry; /* Entry in free list */
|
2001-12-19 20:16:27 +01:00
|
|
|
} ARENA_FREE;
|
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
struct list entry; /* entry in heap large blocks list */
|
|
|
|
SIZE_T data_size; /* size of user data */
|
|
|
|
SIZE_T block_size; /* total size of virtual memory block */
|
|
|
|
DWORD pad[2]; /* padding to ensure 16-byte alignment of data */
|
|
|
|
DWORD size; /* fields for compatibility with normal arenas */
|
|
|
|
DWORD magic; /* these must remain at the end of the structure */
|
|
|
|
} ARENA_LARGE;
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
#define ARENA_FLAG_FREE 0x00000001 /* flags OR'ed with arena size */
|
|
|
|
#define ARENA_FLAG_PREV_FREE 0x00000002
|
|
|
|
#define ARENA_SIZE_MASK (~3)
|
2008-10-29 17:32:45 +01:00
|
|
|
#define ARENA_LARGE_SIZE 0xfedcba90 /* magic value for 'size' field in large blocks */
|
|
|
|
|
|
|
|
/* Value for arena 'magic' field */
|
|
|
|
#define ARENA_INUSE_MAGIC 0x455355
|
2010-01-28 19:43:38 +01:00
|
|
|
#define ARENA_PENDING_MAGIC 0xbedead
|
2008-10-29 17:32:45 +01:00
|
|
|
#define ARENA_FREE_MAGIC 0x45455246
|
|
|
|
#define ARENA_LARGE_MAGIC 0x6752614c
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
#define ARENA_INUSE_FILLER 0x55
|
2010-01-26 14:29:51 +01:00
|
|
|
#define ARENA_TAIL_FILLER 0xab
|
2010-01-26 14:30:25 +01:00
|
|
|
#define ARENA_FREE_FILLER 0xfeeefeee
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-12-11 20:38:41 +01:00
|
|
|
/* everything is aligned on 8 byte boundaries (16 for Win64) */
|
|
|
|
#define ALIGNMENT (2*sizeof(void*))
|
2008-10-29 17:32:45 +01:00
|
|
|
#define LARGE_ALIGNMENT 16 /* large blocks have stricter alignment */
|
2008-12-11 20:38:41 +01:00
|
|
|
#define ARENA_OFFSET (ALIGNMENT - sizeof(ARENA_INUSE))
|
2008-10-29 17:32:45 +01:00
|
|
|
|
2009-06-25 14:33:15 +02:00
|
|
|
C_ASSERT( sizeof(ARENA_LARGE) % LARGE_ALIGNMENT == 0 );
|
|
|
|
|
2008-12-11 20:38:41 +01:00
|
|
|
#define ROUND_SIZE(size) ((((size) + ALIGNMENT - 1) & ~(ALIGNMENT-1)) + ARENA_OFFSET)
|
2002-07-01 20:41:47 +02:00
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
#define QUIET 1 /* Suppress messages */
|
|
|
|
#define NOISY 0 /* Report all errors */
|
|
|
|
|
2005-08-15 22:49:47 +02:00
|
|
|
/* minimum data size (without arenas) of an allocated block */
|
2006-07-21 11:53:52 +02:00
|
|
|
/* make sure that it's larger than a free list entry */
|
2008-12-11 20:38:41 +01:00
|
|
|
#define HEAP_MIN_DATA_SIZE ROUND_SIZE(2 * sizeof(struct list))
|
2017-10-09 14:15:52 +02:00
|
|
|
#define HEAP_MIN_ARENA_SIZE (HEAP_MIN_DATA_SIZE + sizeof(ARENA_INUSE))
|
2005-08-15 22:49:47 +02:00
|
|
|
/* minimum size that must remain to shrink an allocated block */
|
|
|
|
#define HEAP_MIN_SHRINK_SIZE (HEAP_MIN_DATA_SIZE+sizeof(ARENA_FREE))
|
2008-10-29 17:32:45 +01:00
|
|
|
/* minimum size to start allocating large blocks */
|
|
|
|
#define HEAP_MIN_LARGE_BLOCK_SIZE 0x7f000
|
2010-01-27 13:33:53 +01:00
|
|
|
/* extra size to add at the end of block for tail checking */
|
2010-01-29 12:10:17 +01:00
|
|
|
#define HEAP_TAIL_EXTRA_SIZE(flags) \
|
|
|
|
((flags & HEAP_TAIL_CHECKING_ENABLED) || RUNNING_ON_VALGRIND ? ALIGNMENT : 0)
|
2005-08-15 22:49:47 +02:00
|
|
|
|
2017-10-09 14:15:52 +02:00
|
|
|
/* There will be a free list bucket for every arena size up to and including this value */
|
|
|
|
#define HEAP_MAX_SMALL_FREE_LIST 0x100
|
|
|
|
C_ASSERT( HEAP_MAX_SMALL_FREE_LIST % ALIGNMENT == 0 );
|
|
|
|
#define HEAP_NB_SMALL_FREE_LISTS (((HEAP_MAX_SMALL_FREE_LIST - HEAP_MIN_ARENA_SIZE) / ALIGNMENT) + 1)
|
|
|
|
|
|
|
|
/* Max size of the blocks on the free lists above HEAP_MAX_SMALL_FREE_LIST */
|
2007-05-23 09:36:29 +02:00
|
|
|
static const SIZE_T HEAP_freeListSizes[] =
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2017-10-09 14:15:52 +02:00
|
|
|
0x200, 0x400, 0x1000, ~0UL
|
2001-12-19 20:16:27 +01:00
|
|
|
};
|
2018-08-02 21:54:37 +02:00
|
|
|
#define HEAP_NB_FREE_LISTS (ARRAY_SIZE( HEAP_freeListSizes ) + HEAP_NB_SMALL_FREE_LISTS)
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-12-11 20:38:41 +01:00
|
|
|
typedef union
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
ARENA_FREE arena;
|
2008-12-11 20:38:41 +01:00
|
|
|
void *alignment[4];
|
2001-12-19 20:16:27 +01:00
|
|
|
} FREE_LIST_ENTRY;
|
|
|
|
|
|
|
|
struct tagHEAP;
|
|
|
|
|
|
|
|
typedef struct tagSUBHEAP
|
|
|
|
{
|
2007-10-01 15:28:50 +02:00
|
|
|
void *base; /* Base address of the sub-heap memory block */
|
|
|
|
SIZE_T size; /* Size of the whole sub-heap */
|
2009-06-25 12:11:14 +02:00
|
|
|
SIZE_T min_commit; /* Minimum committed size */
|
2007-10-01 15:28:50 +02:00
|
|
|
SIZE_T commitSize; /* Committed size of the sub-heap */
|
2007-10-01 15:31:14 +02:00
|
|
|
struct list entry; /* Entry in sub-heap list */
|
2001-12-19 20:16:27 +01:00
|
|
|
struct tagHEAP *heap; /* Main heap structure */
|
2007-10-01 15:32:14 +02:00
|
|
|
DWORD headerSize; /* Size of the heap header */
|
2001-12-19 20:16:27 +01:00
|
|
|
DWORD magic; /* Magic number */
|
|
|
|
} SUBHEAP;
|
|
|
|
|
|
|
|
#define SUBHEAP_MAGIC ((DWORD)('S' | ('U'<<8) | ('B'<<16) | ('H'<<24)))
|
|
|
|
|
|
|
|
typedef struct tagHEAP
|
|
|
|
{
|
2010-01-29 12:10:17 +01:00
|
|
|
DWORD_PTR unknown1[2];
|
|
|
|
DWORD unknown2;
|
2007-10-01 15:32:14 +02:00
|
|
|
DWORD flags; /* Heap flags */
|
|
|
|
DWORD force_flags; /* Forced heap flags for debugging */
|
2001-12-19 20:16:27 +01:00
|
|
|
SUBHEAP subheap; /* First sub-heap */
|
2005-08-12 20:25:16 +02:00
|
|
|
struct list entry; /* Entry in process heap list */
|
2007-10-01 15:31:14 +02:00
|
|
|
struct list subheap_list; /* Sub-heap list */
|
2008-10-29 17:32:45 +01:00
|
|
|
struct list large_list; /* Large blocks list */
|
2008-11-18 12:49:12 +01:00
|
|
|
SIZE_T grow_size; /* Size of next subheap for growing heap */
|
2007-10-01 15:32:14 +02:00
|
|
|
DWORD magic; /* Magic number */
|
2010-01-28 19:43:38 +01:00
|
|
|
DWORD pending_pos; /* Position in pending free requests ring */
|
|
|
|
ARENA_INUSE **pending_free; /* Ring buffer for pending free requests */
|
2005-06-25 20:00:57 +02:00
|
|
|
RTL_CRITICAL_SECTION critSection; /* Critical section for serialization */
|
2009-06-25 14:33:15 +02:00
|
|
|
FREE_LIST_ENTRY *freeList; /* Free lists */
|
2001-12-19 20:16:27 +01:00
|
|
|
} HEAP;
|
|
|
|
|
|
|
|
#define HEAP_MAGIC ((DWORD)('H' | ('E'<<8) | ('A'<<16) | ('P'<<24)))
|
|
|
|
|
|
|
|
#define HEAP_DEF_SIZE 0x110000 /* Default heap size = 1Mb + 64Kb */
|
|
|
|
#define COMMIT_MASK 0xffff /* bitmask for commit/decommit granularity */
|
2010-01-28 19:43:38 +01:00
|
|
|
#define MAX_FREE_PENDING 1024 /* max number of free requests to delay */
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2010-01-22 12:33:03 +01:00
|
|
|
/* some undocumented flags (names are made up) */
|
|
|
|
#define HEAP_PAGE_ALLOCS 0x01000000
|
|
|
|
#define HEAP_VALIDATE 0x10000000
|
|
|
|
#define HEAP_VALIDATE_ALL 0x20000000
|
|
|
|
#define HEAP_VALIDATE_PARAMS 0x40000000
|
|
|
|
|
2005-08-12 20:25:16 +02:00
|
|
|
static HEAP *processHeap; /* main process heap */
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
static BOOL HEAP_IsRealArena( HEAP *heapPtr, DWORD flags, LPCVOID block, BOOL quiet );
|
|
|
|
|
2003-04-02 03:42:06 +02:00
|
|
|
/* mark a block of memory as free for debugging purposes */
|
2010-01-25 15:02:31 +01:00
|
|
|
static inline void mark_block_free( void *ptr, SIZE_T size, DWORD flags )
|
2003-04-02 03:42:06 +02:00
|
|
|
{
|
2010-01-26 14:30:25 +01:00
|
|
|
if (flags & HEAP_FREE_CHECKING_ENABLED)
|
|
|
|
{
|
|
|
|
SIZE_T i;
|
|
|
|
for (i = 0; i < size / sizeof(DWORD); i++) ((DWORD *)ptr)[i] = ARENA_FREE_FILLER;
|
|
|
|
}
|
2007-12-23 14:57:36 +01:00
|
|
|
#if defined(VALGRIND_MAKE_MEM_NOACCESS)
|
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr, size ));
|
|
|
|
#elif defined( VALGRIND_MAKE_NOACCESS)
|
2003-04-02 03:42:06 +02:00
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark a block of memory as initialized for debugging purposes */
|
2005-07-15 12:01:30 +02:00
|
|
|
static inline void mark_block_initialized( void *ptr, SIZE_T size )
|
2003-04-02 03:42:06 +02:00
|
|
|
{
|
2007-12-23 14:57:36 +01:00
|
|
|
#if defined(VALGRIND_MAKE_MEM_DEFINED)
|
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_MEM_DEFINED( ptr, size ));
|
|
|
|
#elif defined(VALGRIND_MAKE_READABLE)
|
2003-04-02 03:42:06 +02:00
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_READABLE( ptr, size ));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mark a block of memory as uninitialized for debugging purposes */
|
2010-01-26 14:29:51 +01:00
|
|
|
static inline void mark_block_uninitialized( void *ptr, SIZE_T size )
|
2003-04-02 03:42:06 +02:00
|
|
|
{
|
2007-12-23 14:57:36 +01:00
|
|
|
#if defined(VALGRIND_MAKE_MEM_UNDEFINED)
|
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_MEM_UNDEFINED( ptr, size ));
|
|
|
|
#elif defined(VALGRIND_MAKE_WRITABLE)
|
2003-04-02 03:42:06 +02:00
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_WRITABLE( ptr, size ));
|
|
|
|
#endif
|
2010-01-26 14:29:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* mark a block of memory as a tail block */
|
|
|
|
static inline void mark_block_tail( void *ptr, SIZE_T size, DWORD flags )
|
|
|
|
{
|
|
|
|
if (flags & HEAP_TAIL_CHECKING_ENABLED)
|
2003-04-02 03:42:06 +02:00
|
|
|
{
|
2010-01-27 13:35:32 +01:00
|
|
|
mark_block_uninitialized( ptr, size );
|
2010-01-26 14:29:51 +01:00
|
|
|
memset( ptr, ARENA_TAIL_FILLER, size );
|
2010-01-27 13:35:32 +01:00
|
|
|
}
|
2010-01-26 14:29:51 +01:00
|
|
|
#if defined(VALGRIND_MAKE_MEM_NOACCESS)
|
2010-01-27 13:35:32 +01:00
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_MEM_NOACCESS( ptr, size ));
|
2010-01-26 14:29:51 +01:00
|
|
|
#elif defined( VALGRIND_MAKE_NOACCESS)
|
2010-01-27 13:35:32 +01:00
|
|
|
VALGRIND_DISCARD( VALGRIND_MAKE_NOACCESS( ptr, size ));
|
2003-04-02 03:42:06 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2010-01-26 14:29:51 +01:00
|
|
|
/* initialize contents of a newly created block of memory */
|
|
|
|
static inline void initialize_block( void *ptr, SIZE_T size, SIZE_T unused, DWORD flags )
|
2003-04-02 03:42:06 +02:00
|
|
|
{
|
2010-01-26 14:29:51 +01:00
|
|
|
if (flags & HEAP_ZERO_MEMORY)
|
|
|
|
{
|
|
|
|
mark_block_initialized( ptr, size );
|
|
|
|
memset( ptr, 0, size );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mark_block_uninitialized( ptr, size );
|
|
|
|
if (flags & HEAP_FREE_CHECKING_ENABLED)
|
|
|
|
{
|
|
|
|
memset( ptr, ARENA_INUSE_FILLER, size );
|
|
|
|
mark_block_uninitialized( ptr, size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mark_block_tail( (char *)ptr + size, unused, flags );
|
2003-04-02 03:42:06 +02:00
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2006-10-19 21:49:11 +02:00
|
|
|
/* notify that a new block of memory has been allocated for debugging purposes */
|
|
|
|
static inline void notify_alloc( void *ptr, SIZE_T size, BOOL init )
|
|
|
|
{
|
|
|
|
#ifdef VALGRIND_MALLOCLIKE_BLOCK
|
|
|
|
VALGRIND_MALLOCLIKE_BLOCK( ptr, size, 0, init );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* notify that a block of memory has been freed for debugging purposes */
|
2008-07-24 19:21:01 +02:00
|
|
|
static inline void notify_free( void const *ptr )
|
2006-10-19 21:49:11 +02:00
|
|
|
{
|
|
|
|
#ifdef VALGRIND_FREELIKE_BLOCK
|
|
|
|
VALGRIND_FREELIKE_BLOCK( ptr, 0 );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-11-11 22:12:25 +01:00
|
|
|
static inline void notify_realloc( void const *ptr, SIZE_T size_old, SIZE_T size_new )
|
|
|
|
{
|
|
|
|
#ifdef VALGRIND_RESIZEINPLACE_BLOCK
|
2015-12-16 22:00:58 +01:00
|
|
|
/* zero is not a valid size */
|
|
|
|
VALGRIND_RESIZEINPLACE_BLOCK( ptr, size_old ? size_old : 1, size_new ? size_new : 1, 0 );
|
2011-11-11 22:12:25 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-07-24 19:21:01 +02:00
|
|
|
static void subheap_notify_free_all(SUBHEAP const *subheap)
|
|
|
|
{
|
|
|
|
#ifdef VALGRIND_FREELIKE_BLOCK
|
|
|
|
char const *ptr = (char const *)subheap->base + subheap->headerSize;
|
|
|
|
|
|
|
|
if (!RUNNING_ON_VALGRIND) return;
|
|
|
|
|
|
|
|
while (ptr < (char const *)subheap->base + subheap->size)
|
|
|
|
{
|
|
|
|
if (*(const DWORD *)ptr & ARENA_FLAG_FREE)
|
|
|
|
{
|
|
|
|
ARENA_FREE const *pArena = (ARENA_FREE const *)ptr;
|
|
|
|
if (pArena->magic!=ARENA_FREE_MAGIC) ERR("bad free_magic @%p\n", pArena);
|
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARENA_INUSE const *pArena = (ARENA_INUSE const *)ptr;
|
2010-02-01 12:35:32 +01:00
|
|
|
if (pArena->magic == ARENA_INUSE_MAGIC) notify_free(pArena + 1);
|
|
|
|
else if (pArena->magic != ARENA_PENDING_MAGIC) ERR("bad inuse_magic @%p\n", pArena);
|
2008-07-24 19:21:01 +02:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-08-15 22:49:47 +02:00
|
|
|
/* locate a free list entry of the appropriate size */
|
|
|
|
/* size is the size of the whole block including the arena header */
|
|
|
|
static inline unsigned int get_freelist_index( SIZE_T size )
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2017-10-09 14:15:52 +02:00
|
|
|
if (size <= HEAP_MAX_SMALL_FREE_LIST)
|
|
|
|
return (size - HEAP_MIN_ARENA_SIZE) / ALIGNMENT;
|
|
|
|
|
|
|
|
for (i = HEAP_NB_SMALL_FREE_LISTS; i < HEAP_NB_FREE_LISTS - 1; i++)
|
|
|
|
if (size <= HEAP_freeListSizes[i - HEAP_NB_SMALL_FREE_LISTS]) break;
|
2005-08-15 22:49:47 +02:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2006-12-05 15:43:13 +01:00
|
|
|
/* get the memory protection type to use for a given heap */
|
|
|
|
static inline ULONG get_protection_type( DWORD flags )
|
|
|
|
{
|
|
|
|
return (flags & HEAP_CREATE_ENABLE_EXECUTE) ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
|
|
|
|
}
|
|
|
|
|
2005-08-03 18:03:15 +02:00
|
|
|
static RTL_CRITICAL_SECTION_DEBUG process_heap_critsect_debug =
|
|
|
|
{
|
|
|
|
0, 0, NULL, /* will be set later */
|
|
|
|
{ &process_heap_critsect_debug.ProcessLocksList, &process_heap_critsect_debug.ProcessLocksList },
|
2005-09-09 12:19:44 +02:00
|
|
|
0, 0, { (DWORD_PTR)(__FILE__ ": main process heap section") }
|
2005-08-03 18:03:15 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Dump
|
|
|
|
*/
|
2005-06-16 12:45:25 +02:00
|
|
|
static void HEAP_Dump( HEAP *heap )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2008-10-23 23:24:04 +02:00
|
|
|
unsigned int i;
|
2001-12-19 20:16:27 +01:00
|
|
|
SUBHEAP *subheap;
|
|
|
|
char *ptr;
|
|
|
|
|
2005-07-15 13:44:46 +02:00
|
|
|
DPRINTF( "Heap: %p\n", heap );
|
2007-10-01 15:31:14 +02:00
|
|
|
DPRINTF( "Next: %p Sub-heaps:", LIST_ENTRY( heap->entry.next, HEAP, entry ) );
|
|
|
|
LIST_FOR_EACH_ENTRY( subheap, &heap->subheap_list, SUBHEAP, entry ) DPRINTF( " %p", subheap );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
DPRINTF( "\nFree lists:\n Block Stat Size Id\n" );
|
|
|
|
for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
|
2007-05-23 09:36:29 +02:00
|
|
|
DPRINTF( "%p free %08lx prev=%p next=%p\n",
|
2017-10-09 14:15:52 +02:00
|
|
|
&heap->freeList[i].arena, i < HEAP_NB_SMALL_FREE_LISTS ?
|
|
|
|
HEAP_MIN_ARENA_SIZE + i * ALIGNMENT : HEAP_freeListSizes[i - HEAP_NB_SMALL_FREE_LISTS],
|
2005-08-12 18:05:46 +02:00
|
|
|
LIST_ENTRY( heap->freeList[i].arena.entry.prev, ARENA_FREE, entry ),
|
|
|
|
LIST_ENTRY( heap->freeList[i].arena.entry.next, ARENA_FREE, entry ));
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2007-10-01 15:31:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( subheap, &heap->subheap_list, SUBHEAP, entry )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T freeSize = 0, usedSize = 0, arenaSize = subheap->headerSize;
|
2007-10-01 15:28:50 +02:00
|
|
|
DPRINTF( "\n\nSub-heap %p: base=%p size=%08lx committed=%08lx\n",
|
|
|
|
subheap, subheap->base, subheap->size, subheap->commitSize );
|
2002-06-01 01:06:46 +02:00
|
|
|
|
2007-11-25 09:56:31 +01:00
|
|
|
DPRINTF( "\n Block Arena Stat Size Id\n" );
|
2007-10-01 15:28:50 +02:00
|
|
|
ptr = (char *)subheap->base + subheap->headerSize;
|
|
|
|
while (ptr < (char *)subheap->base + subheap->size)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
if (*(DWORD *)ptr & ARENA_FLAG_FREE)
|
|
|
|
{
|
|
|
|
ARENA_FREE *pArena = (ARENA_FREE *)ptr;
|
2007-11-25 09:56:31 +01:00
|
|
|
DPRINTF( "%p %08x free %08x prev=%p next=%p\n",
|
|
|
|
pArena, pArena->magic,
|
|
|
|
pArena->size & ARENA_SIZE_MASK,
|
2005-08-12 18:05:46 +02:00
|
|
|
LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry ),
|
|
|
|
LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry ) );
|
2001-12-19 20:16:27 +01:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_FREE);
|
|
|
|
freeSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
else if (*(DWORD *)ptr & ARENA_FLAG_PREV_FREE)
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
2007-11-25 09:56:31 +01:00
|
|
|
DPRINTF( "%p %08x Used %08x back=%p\n",
|
|
|
|
pArena, pArena->magic, pArena->size & ARENA_SIZE_MASK, *((ARENA_FREE **)pArena - 1) );
|
2001-12-19 20:16:27 +01:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_INUSE);
|
|
|
|
usedSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
2010-01-28 19:43:38 +01:00
|
|
|
DPRINTF( "%p %08x %s %08x\n",
|
|
|
|
pArena, pArena->magic, pArena->magic == ARENA_INUSE_MAGIC ? "used" : "pend",
|
|
|
|
pArena->size & ARENA_SIZE_MASK );
|
2001-12-19 20:16:27 +01:00
|
|
|
ptr += sizeof(*pArena) + (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
arenaSize += sizeof(ARENA_INUSE);
|
|
|
|
usedSize += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
}
|
2007-10-01 15:28:50 +02:00
|
|
|
DPRINTF( "\nTotal: Size=%08lx Committed=%08lx Free=%08lx Used=%08lx Arenas=%08lx (%ld%%)\n\n",
|
2001-12-19 20:16:27 +01:00
|
|
|
subheap->size, subheap->commitSize, freeSize, usedSize,
|
|
|
|
arenaSize, (arenaSize * 100) / subheap->size );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-16 12:45:25 +02:00
|
|
|
static void HEAP_DumpEntry( LPPROCESS_HEAP_ENTRY entry )
|
2005-03-03 14:50:21 +01:00
|
|
|
{
|
|
|
|
WORD rem_flags;
|
|
|
|
TRACE( "Dumping entry %p\n", entry );
|
|
|
|
TRACE( "lpData\t\t: %p\n", entry->lpData );
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE( "cbData\t\t: %08x\n", entry->cbData);
|
2005-03-03 14:50:21 +01:00
|
|
|
TRACE( "cbOverhead\t: %08x\n", entry->cbOverhead);
|
|
|
|
TRACE( "iRegionIndex\t: %08x\n", entry->iRegionIndex);
|
|
|
|
TRACE( "WFlags\t\t: ");
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_REGION)
|
|
|
|
TRACE( "PROCESS_HEAP_REGION ");
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_UNCOMMITTED_RANGE)
|
|
|
|
TRACE( "PROCESS_HEAP_UNCOMMITTED_RANGE ");
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_ENTRY_BUSY)
|
|
|
|
TRACE( "PROCESS_HEAP_ENTRY_BUSY ");
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE)
|
|
|
|
TRACE( "PROCESS_HEAP_ENTRY_MOVEABLE ");
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_ENTRY_DDESHARE)
|
|
|
|
TRACE( "PROCESS_HEAP_ENTRY_DDESHARE ");
|
|
|
|
rem_flags = entry->wFlags &
|
|
|
|
~(PROCESS_HEAP_REGION | PROCESS_HEAP_UNCOMMITTED_RANGE |
|
|
|
|
PROCESS_HEAP_ENTRY_BUSY | PROCESS_HEAP_ENTRY_MOVEABLE|
|
|
|
|
PROCESS_HEAP_ENTRY_DDESHARE);
|
|
|
|
if (rem_flags)
|
|
|
|
TRACE( "Unknown %08x", rem_flags);
|
|
|
|
TRACE( "\n");
|
|
|
|
if ((entry->wFlags & PROCESS_HEAP_ENTRY_BUSY )
|
|
|
|
&& (entry->wFlags & PROCESS_HEAP_ENTRY_MOVEABLE))
|
|
|
|
{
|
|
|
|
/* Treat as block */
|
|
|
|
TRACE( "BLOCK->hMem\t\t:%p\n", entry->u.Block.hMem);
|
|
|
|
}
|
|
|
|
if (entry->wFlags & PROCESS_HEAP_REGION)
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE( "Region.dwCommittedSize\t:%08x\n",entry->u.Region.dwCommittedSize);
|
|
|
|
TRACE( "Region.dwUnCommittedSize\t:%08x\n",entry->u.Region.dwUnCommittedSize);
|
2005-03-03 14:50:21 +01:00
|
|
|
TRACE( "Region.lpFirstBlock\t:%p\n",entry->u.Region.lpFirstBlock);
|
|
|
|
TRACE( "Region.lpLastBlock\t:%p\n",entry->u.Region.lpLastBlock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_GetPtr
|
|
|
|
* RETURNS
|
|
|
|
* Pointer to the heap
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
static HEAP *HEAP_GetPtr(
|
|
|
|
HANDLE heap /* [in] Handle to the heap */
|
|
|
|
) {
|
2009-03-17 00:12:11 +01:00
|
|
|
HEAP *heapPtr = heap;
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!heapPtr || (heapPtr->magic != HEAP_MAGIC))
|
|
|
|
{
|
2002-11-21 04:45:01 +01:00
|
|
|
ERR("Invalid heap %p!\n", heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-25 15:02:31 +01:00
|
|
|
if ((heapPtr->flags & HEAP_VALIDATE_ALL) && !HEAP_IsRealArena( heapPtr, 0, NULL, NOISY ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:34:31 +01:00
|
|
|
if (TRACE_ON(heap))
|
|
|
|
{
|
|
|
|
HEAP_Dump( heapPtr );
|
|
|
|
assert( FALSE );
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return heapPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_InsertFreeBlock
|
|
|
|
*
|
|
|
|
* Insert a free block into the free list.
|
|
|
|
*/
|
2003-04-14 23:44:21 +02:00
|
|
|
static inline void HEAP_InsertFreeBlock( HEAP *heap, ARENA_FREE *pArena, BOOL last )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-08-15 22:49:47 +02:00
|
|
|
FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( pArena->size + sizeof(*pArena) );
|
2003-04-14 23:44:21 +02:00
|
|
|
if (last)
|
|
|
|
{
|
|
|
|
/* insert at end of free list, i.e. before the next free list entry */
|
|
|
|
pEntry++;
|
|
|
|
if (pEntry == &heap->freeList[HEAP_NB_FREE_LISTS]) pEntry = heap->freeList;
|
2005-08-12 18:05:46 +02:00
|
|
|
list_add_before( &pEntry->arena.entry, &pArena->entry );
|
2003-04-14 23:44:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* insert at head of free list */
|
2005-08-12 18:05:46 +02:00
|
|
|
list_add_after( &pEntry->arena.entry, &pArena->entry );
|
2003-04-14 23:44:21 +02:00
|
|
|
}
|
|
|
|
pArena->size |= ARENA_FLAG_FREE;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_FindSubHeap
|
|
|
|
* Find the sub-heap containing a given address.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Pointer: Success
|
|
|
|
* NULL: Failure
|
|
|
|
*/
|
|
|
|
static SUBHEAP *HEAP_FindSubHeap(
|
2004-06-15 02:47:00 +02:00
|
|
|
const HEAP *heap, /* [in] Heap pointer */
|
2007-10-01 15:31:14 +02:00
|
|
|
LPCVOID ptr ) /* [in] Address */
|
|
|
|
{
|
|
|
|
SUBHEAP *sub;
|
|
|
|
LIST_FOR_EACH_ENTRY( sub, &heap->subheap_list, SUBHEAP, entry )
|
2009-03-17 00:12:11 +01:00
|
|
|
if ((ptr >= sub->base) &&
|
2007-10-01 15:28:50 +02:00
|
|
|
((const char *)ptr < (const char *)sub->base + sub->size - sizeof(ARENA_INUSE)))
|
2007-10-01 15:31:14 +02:00
|
|
|
return sub;
|
2001-12-19 20:16:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Commit
|
|
|
|
*
|
2005-08-15 22:49:47 +02:00
|
|
|
* Make sure the heap storage is committed for a given size in the specified arena.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2005-08-15 22:49:47 +02:00
|
|
|
static inline BOOL HEAP_Commit( SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T data_size )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-08-15 22:49:47 +02:00
|
|
|
void *ptr = (char *)(pArena + 1) + data_size + sizeof(ARENA_FREE);
|
2007-10-01 15:28:50 +02:00
|
|
|
SIZE_T size = (char *)ptr - (char *)subheap->base;
|
2001-12-19 20:16:27 +01:00
|
|
|
size = (size + COMMIT_MASK) & ~COMMIT_MASK;
|
|
|
|
if (size > subheap->size) size = subheap->size;
|
|
|
|
if (size <= subheap->commitSize) return TRUE;
|
2002-09-17 20:54:42 +02:00
|
|
|
size -= subheap->commitSize;
|
2007-10-01 15:28:50 +02:00
|
|
|
ptr = (char *)subheap->base + subheap->commitSize;
|
2005-02-22 20:33:50 +01:00
|
|
|
if (NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 0,
|
2006-12-05 15:43:13 +01:00
|
|
|
&size, MEM_COMMIT, get_protection_type( subheap->heap->flags ) ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2004-10-11 22:59:06 +02:00
|
|
|
WARN("Could not commit %08lx bytes at %p for heap %p\n",
|
|
|
|
size, ptr, subheap->heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
subheap->commitSize += size;
|
2001-12-19 20:16:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_Decommit
|
|
|
|
*
|
|
|
|
* If possible, decommit the heap storage from (including) 'ptr'.
|
|
|
|
*/
|
|
|
|
static inline BOOL HEAP_Decommit( SUBHEAP *subheap, void *ptr )
|
|
|
|
{
|
2002-09-17 20:54:42 +02:00
|
|
|
void *addr;
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T decommit_size;
|
2007-10-01 15:28:50 +02:00
|
|
|
SIZE_T size = (char *)ptr - (char *)subheap->base;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/* round to next block and add one full block */
|
|
|
|
size = ((size + COMMIT_MASK) & ~COMMIT_MASK) + COMMIT_MASK + 1;
|
2009-06-25 12:11:14 +02:00
|
|
|
size = max( size, subheap->min_commit );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (size >= subheap->commitSize) return TRUE;
|
2002-09-17 20:54:42 +02:00
|
|
|
decommit_size = subheap->commitSize - size;
|
2007-10-01 15:28:50 +02:00
|
|
|
addr = (char *)subheap->base + size;
|
2002-09-17 20:54:42 +02:00
|
|
|
|
2005-02-22 20:33:50 +01:00
|
|
|
if (NtFreeVirtualMemory( NtCurrentProcess(), &addr, &decommit_size, MEM_DECOMMIT ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
WARN("Could not decommit %08lx bytes at %p for heap %p\n",
|
2007-10-01 15:28:50 +02:00
|
|
|
decommit_size, (char *)subheap->base + size, subheap->heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
2002-09-17 20:54:42 +02:00
|
|
|
subheap->commitSize -= decommit_size;
|
2001-12-19 20:16:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_CreateFreeBlock
|
|
|
|
*
|
|
|
|
* Create a free block at a specified address. 'size' is the size of the
|
|
|
|
* whole block, including the new arena.
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
static void HEAP_CreateFreeBlock( SUBHEAP *subheap, void *ptr, SIZE_T size )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
ARENA_FREE *pFree;
|
2003-04-02 03:42:06 +02:00
|
|
|
char *pEnd;
|
2003-04-14 23:44:21 +02:00
|
|
|
BOOL last;
|
2010-01-25 15:02:31 +01:00
|
|
|
DWORD flags = subheap->heap->flags;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Create a free arena */
|
2010-01-26 14:29:51 +01:00
|
|
|
mark_block_uninitialized( ptr, sizeof(ARENA_FREE) );
|
2009-03-17 00:12:11 +01:00
|
|
|
pFree = ptr;
|
2001-12-19 20:16:27 +01:00
|
|
|
pFree->magic = ARENA_FREE_MAGIC;
|
|
|
|
|
|
|
|
/* If debugging, erase the freed block content */
|
|
|
|
|
2003-04-02 03:42:06 +02:00
|
|
|
pEnd = (char *)ptr + size;
|
2007-10-01 15:28:50 +02:00
|
|
|
if (pEnd > (char *)subheap->base + subheap->commitSize)
|
|
|
|
pEnd = (char *)subheap->base + subheap->commitSize;
|
2010-01-25 15:02:31 +01:00
|
|
|
if (pEnd > (char *)(pFree + 1)) mark_block_free( pFree + 1, pEnd - (char *)(pFree + 1), flags );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Check if next block is free also */
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
if (((char *)ptr + size < (char *)subheap->base + subheap->size) &&
|
2001-12-19 20:16:27 +01:00
|
|
|
(*(DWORD *)((char *)ptr + size) & ARENA_FLAG_FREE))
|
|
|
|
{
|
|
|
|
/* Remove the next arena from the free list */
|
|
|
|
ARENA_FREE *pNext = (ARENA_FREE *)((char *)ptr + size);
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pNext->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
size += (pNext->size & ARENA_SIZE_MASK) + sizeof(*pNext);
|
2010-01-25 15:02:31 +01:00
|
|
|
mark_block_free( pNext, sizeof(ARENA_FREE), flags );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the next block PREV_FREE flag and pointer */
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
last = ((char *)ptr + size >= (char *)subheap->base + subheap->size);
|
2003-04-14 23:44:21 +02:00
|
|
|
if (!last)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
DWORD *pNext = (DWORD *)((char *)ptr + size);
|
|
|
|
*pNext |= ARENA_FLAG_PREV_FREE;
|
2010-02-21 15:17:46 +01:00
|
|
|
mark_block_initialized( (ARENA_FREE **)pNext - 1, sizeof( ARENA_FREE * ) );
|
2005-09-16 20:54:19 +02:00
|
|
|
*((ARENA_FREE **)pNext - 1) = pFree;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Last, insert the new block into the free list */
|
|
|
|
|
|
|
|
pFree->size = size - sizeof(*pFree);
|
2003-04-14 23:44:21 +02:00
|
|
|
HEAP_InsertFreeBlock( subheap->heap, pFree, last );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_MakeInUseBlockFree
|
|
|
|
*
|
|
|
|
* Turn an in-use block into a free block. Can also decommit the end of
|
|
|
|
* the heap, and possibly even free the sub-heap altogether.
|
|
|
|
*/
|
|
|
|
static void HEAP_MakeInUseBlockFree( SUBHEAP *subheap, ARENA_INUSE *pArena )
|
|
|
|
{
|
2010-01-28 19:43:38 +01:00
|
|
|
HEAP *heap = subheap->heap;
|
2001-12-19 20:16:27 +01:00
|
|
|
ARENA_FREE *pFree;
|
2010-01-28 19:43:38 +01:00
|
|
|
SIZE_T size;
|
|
|
|
|
|
|
|
if (heap->pending_free)
|
|
|
|
{
|
|
|
|
ARENA_INUSE *prev = heap->pending_free[heap->pending_pos];
|
|
|
|
heap->pending_free[heap->pending_pos] = pArena;
|
|
|
|
heap->pending_pos = (heap->pending_pos + 1) % MAX_FREE_PENDING;
|
|
|
|
pArena->magic = ARENA_PENDING_MAGIC;
|
|
|
|
mark_block_free( pArena + 1, pArena->size & ARENA_SIZE_MASK, heap->flags );
|
|
|
|
if (!prev) return;
|
|
|
|
pArena = prev;
|
|
|
|
subheap = HEAP_FindSubHeap( heap, pArena );
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Check if we can merge with previous block */
|
|
|
|
|
2010-01-28 19:43:38 +01:00
|
|
|
size = (pArena->size & ARENA_SIZE_MASK) + sizeof(*pArena);
|
2001-12-19 20:16:27 +01:00
|
|
|
if (pArena->size & ARENA_FLAG_PREV_FREE)
|
|
|
|
{
|
|
|
|
pFree = *((ARENA_FREE **)pArena - 1);
|
|
|
|
size += (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
|
|
|
|
/* Remove it from the free list */
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pFree->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
else pFree = (ARENA_FREE *)pArena;
|
|
|
|
|
|
|
|
/* Create a free block */
|
|
|
|
|
|
|
|
HEAP_CreateFreeBlock( subheap, pFree, size );
|
|
|
|
size = (pFree->size & ARENA_SIZE_MASK) + sizeof(ARENA_FREE);
|
2007-10-01 15:28:50 +02:00
|
|
|
if ((char *)pFree + size < (char *)subheap->base + subheap->size)
|
2001-12-19 20:16:27 +01:00
|
|
|
return; /* Not the last block, so nothing more to do */
|
|
|
|
|
|
|
|
/* Free the whole sub-heap if it's empty and not the original one */
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
if (((char *)pFree == (char *)subheap->base + subheap->headerSize) &&
|
2001-12-19 20:16:27 +01:00
|
|
|
(subheap != &subheap->heap->subheap))
|
|
|
|
{
|
2007-10-01 15:28:50 +02:00
|
|
|
void *addr = subheap->base;
|
2011-10-20 01:53:36 +02:00
|
|
|
|
|
|
|
size = 0;
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Remove the free block from the list */
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pFree->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Remove the subheap from the list */
|
2007-10-01 15:31:14 +02:00
|
|
|
list_remove( &subheap->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Free the memory */
|
|
|
|
subheap->magic = 0;
|
2007-10-01 15:28:50 +02:00
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
|
2001-12-19 20:16:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decommit the end of the heap */
|
|
|
|
|
|
|
|
if (!(subheap->heap->flags & HEAP_SHARED)) HEAP_Decommit( subheap, pFree + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ShrinkBlock
|
|
|
|
*
|
|
|
|
* Shrink an in-use block.
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
static void HEAP_ShrinkBlock(SUBHEAP *subheap, ARENA_INUSE *pArena, SIZE_T size)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-08-15 22:49:47 +02:00
|
|
|
if ((pArena->size & ARENA_SIZE_MASK) >= size + HEAP_MIN_SHRINK_SIZE)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
HEAP_CreateFreeBlock( subheap, (char *)(pArena + 1) + size,
|
|
|
|
(pArena->size & ARENA_SIZE_MASK) - size );
|
|
|
|
/* assign size plus previous arena flags */
|
|
|
|
pArena->size = size | (pArena->size & ~ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Turn off PREV_FREE flag in next block */
|
|
|
|
char *pNext = (char *)(pArena + 1) + (pArena->size & ARENA_SIZE_MASK);
|
2007-10-01 15:28:50 +02:00
|
|
|
if (pNext < (char *)subheap->base + subheap->size)
|
2001-12-19 20:16:27 +01:00
|
|
|
*(DWORD *)pNext &= ~ARENA_FLAG_PREV_FREE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* allocate_large_block
|
|
|
|
*/
|
|
|
|
static void *allocate_large_block( HEAP *heap, DWORD flags, SIZE_T size )
|
|
|
|
{
|
|
|
|
ARENA_LARGE *arena;
|
2010-01-27 13:33:53 +01:00
|
|
|
SIZE_T block_size = sizeof(*arena) + ROUND_SIZE(size) + HEAP_TAIL_EXTRA_SIZE(flags);
|
2008-10-29 17:32:45 +01:00
|
|
|
LPVOID address = NULL;
|
|
|
|
|
|
|
|
if (block_size < size) return NULL; /* overflow */
|
2009-06-30 14:06:55 +02:00
|
|
|
if (NtAllocateVirtualMemory( NtCurrentProcess(), &address, 5,
|
2008-10-29 17:32:45 +01:00
|
|
|
&block_size, MEM_COMMIT, get_protection_type( flags ) ))
|
|
|
|
{
|
|
|
|
WARN("Could not allocate block for %08lx bytes\n", size );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
arena = address;
|
|
|
|
arena->data_size = size;
|
|
|
|
arena->block_size = block_size;
|
|
|
|
arena->size = ARENA_LARGE_SIZE;
|
|
|
|
arena->magic = ARENA_LARGE_MAGIC;
|
2010-01-26 16:04:45 +01:00
|
|
|
mark_block_tail( (char *)(arena + 1) + size, block_size - sizeof(*arena) - size, flags );
|
2008-10-29 17:32:45 +01:00
|
|
|
list_add_tail( &heap->large_list, &arena->entry );
|
2010-01-26 16:03:07 +01:00
|
|
|
notify_alloc( arena + 1, size, flags & HEAP_ZERO_MEMORY );
|
2008-10-29 17:32:45 +01:00
|
|
|
return arena + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* free_large_block
|
|
|
|
*/
|
|
|
|
static void free_large_block( HEAP *heap, DWORD flags, void *ptr )
|
|
|
|
{
|
|
|
|
ARENA_LARGE *arena = (ARENA_LARGE *)ptr - 1;
|
|
|
|
LPVOID address = arena;
|
|
|
|
SIZE_T size = 0;
|
|
|
|
|
|
|
|
list_remove( &arena->entry );
|
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &address, &size, MEM_RELEASE );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* realloc_large_block
|
|
|
|
*/
|
|
|
|
static void *realloc_large_block( HEAP *heap, DWORD flags, void *ptr, SIZE_T size )
|
|
|
|
{
|
|
|
|
ARENA_LARGE *arena = (ARENA_LARGE *)ptr - 1;
|
|
|
|
void *new_ptr;
|
|
|
|
|
|
|
|
if (arena->block_size - sizeof(*arena) >= size)
|
|
|
|
{
|
2010-01-26 16:04:45 +01:00
|
|
|
SIZE_T unused = arena->block_size - sizeof(*arena) - size;
|
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
/* FIXME: we could remap zero-pages instead */
|
2011-11-11 22:12:25 +01:00
|
|
|
#ifdef VALGRIND_RESIZEINPLACE_BLOCK
|
|
|
|
if (RUNNING_ON_VALGRIND)
|
|
|
|
notify_realloc( arena + 1, arena->data_size, size );
|
|
|
|
else
|
|
|
|
#endif
|
2010-01-26 16:04:45 +01:00
|
|
|
if (size > arena->data_size)
|
|
|
|
initialize_block( (char *)ptr + arena->data_size, size - arena->data_size, unused, flags );
|
|
|
|
else
|
|
|
|
mark_block_tail( (char *)ptr + size, unused, flags );
|
2008-10-29 17:32:45 +01:00
|
|
|
arena->data_size = size;
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
if (flags & HEAP_REALLOC_IN_PLACE_ONLY) return NULL;
|
|
|
|
if (!(new_ptr = allocate_large_block( heap, flags, size )))
|
|
|
|
{
|
|
|
|
WARN("Could not allocate block for %08lx bytes\n", size );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy( new_ptr, ptr, arena->data_size );
|
|
|
|
free_large_block( heap, flags, ptr );
|
2010-02-01 20:25:35 +01:00
|
|
|
notify_free( ptr );
|
2008-10-29 17:32:45 +01:00
|
|
|
return new_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* find_large_block
|
|
|
|
*/
|
|
|
|
static ARENA_LARGE *find_large_block( HEAP *heap, const void *ptr )
|
|
|
|
{
|
|
|
|
ARENA_LARGE *arena;
|
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( arena, &heap->large_list, ARENA_LARGE, entry )
|
2009-03-17 00:12:11 +01:00
|
|
|
if (ptr == arena + 1) return arena;
|
2008-10-29 17:32:45 +01:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* validate_large_arena
|
|
|
|
*/
|
|
|
|
static BOOL validate_large_arena( HEAP *heap, const ARENA_LARGE *arena, BOOL quiet )
|
|
|
|
{
|
2010-01-26 16:08:22 +01:00
|
|
|
DWORD flags = heap->flags;
|
|
|
|
|
2013-01-08 14:00:06 +01:00
|
|
|
if ((ULONG_PTR)arena % page_size)
|
2008-10-29 17:32:45 +01:00
|
|
|
{
|
|
|
|
if (quiet == NOISY)
|
|
|
|
{
|
|
|
|
ERR( "Heap %p: invalid large arena pointer %p\n", heap, arena );
|
|
|
|
if (TRACE_ON(heap)) HEAP_Dump( heap );
|
|
|
|
}
|
|
|
|
else if (WARN_ON(heap))
|
|
|
|
{
|
|
|
|
WARN( "Heap %p: unaligned arena pointer %p\n", heap, arena );
|
|
|
|
if (TRACE_ON(heap)) HEAP_Dump( heap );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (arena->size != ARENA_LARGE_SIZE || arena->magic != ARENA_LARGE_MAGIC)
|
|
|
|
{
|
|
|
|
if (quiet == NOISY)
|
|
|
|
{
|
|
|
|
ERR( "Heap %p: invalid large arena %p values %x/%x\n",
|
|
|
|
heap, arena, arena->size, arena->magic );
|
|
|
|
if (TRACE_ON(heap)) HEAP_Dump( heap );
|
|
|
|
}
|
|
|
|
else if (WARN_ON(heap))
|
|
|
|
{
|
|
|
|
WARN( "Heap %p: invalid large arena %p values %x/%x\n",
|
|
|
|
heap, arena, arena->size, arena->magic );
|
|
|
|
if (TRACE_ON(heap)) HEAP_Dump( heap );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2010-01-26 16:08:22 +01:00
|
|
|
if (arena->data_size > arena->block_size - sizeof(*arena))
|
|
|
|
{
|
|
|
|
ERR( "Heap %p: invalid large arena %p size %lx/%lx\n",
|
|
|
|
heap, arena, arena->data_size, arena->block_size );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if (flags & HEAP_TAIL_CHECKING_ENABLED)
|
|
|
|
{
|
|
|
|
SIZE_T i, unused = arena->block_size - sizeof(*arena) - arena->data_size;
|
|
|
|
const unsigned char *data = (const unsigned char *)(arena + 1) + arena->data_size;
|
|
|
|
|
|
|
|
for (i = 0; i < unused; i++)
|
|
|
|
{
|
|
|
|
if (data[i] == ARENA_TAIL_FILLER) continue;
|
|
|
|
ERR("Heap %p: block %p tail overwritten at %p (byte %lu/%lu == 0x%02x)\n",
|
|
|
|
heap, arena + 1, data + i, i, unused, data[i] );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2008-10-29 17:32:45 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/***********************************************************************
|
2008-10-31 13:00:15 +01:00
|
|
|
* HEAP_CreateSubHeap
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2008-10-31 13:00:15 +01:00
|
|
|
static SUBHEAP *HEAP_CreateSubHeap( HEAP *heap, LPVOID address, DWORD flags,
|
|
|
|
SIZE_T commitSize, SIZE_T totalSize )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2002-09-17 20:54:42 +02:00
|
|
|
SUBHEAP *subheap;
|
2001-12-19 20:16:27 +01:00
|
|
|
FREE_LIST_ENTRY *pEntry;
|
2008-10-23 23:24:04 +02:00
|
|
|
unsigned int i;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-10-31 13:00:15 +01:00
|
|
|
if (!address)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-28 19:47:01 +01:00
|
|
|
if (!commitSize) commitSize = COMMIT_MASK + 1;
|
2008-12-11 20:38:41 +01:00
|
|
|
totalSize = min( totalSize, 0xffff0000 ); /* don't allow a heap larger than 4Gb */
|
2008-10-31 13:00:15 +01:00
|
|
|
if (totalSize < commitSize) totalSize = commitSize;
|
|
|
|
if (flags & HEAP_SHARED) commitSize = totalSize; /* always commit everything in a shared heap */
|
2010-06-29 10:55:20 +02:00
|
|
|
commitSize = min( totalSize, (commitSize + COMMIT_MASK) & ~COMMIT_MASK );
|
2008-10-31 13:00:15 +01:00
|
|
|
|
|
|
|
/* allocate the memory block */
|
2014-03-26 11:24:14 +01:00
|
|
|
if (NtAllocateVirtualMemory( NtCurrentProcess(), &address, 0, &totalSize,
|
2008-10-31 13:00:15 +01:00
|
|
|
MEM_RESERVE, get_protection_type( flags ) ))
|
|
|
|
{
|
|
|
|
WARN("Could not allocate %08lx bytes\n", totalSize );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (NtAllocateVirtualMemory( NtCurrentProcess(), &address, 0,
|
|
|
|
&commitSize, MEM_COMMIT, get_protection_type( flags ) ))
|
|
|
|
{
|
|
|
|
WARN("Could not commit %08lx bytes for sub-heap %p\n", commitSize, address );
|
|
|
|
return NULL;
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
if (heap)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
/* If this is a secondary subheap, insert it into list */
|
|
|
|
|
2009-03-17 00:12:11 +01:00
|
|
|
subheap = address;
|
2007-10-01 15:28:50 +02:00
|
|
|
subheap->base = address;
|
|
|
|
subheap->heap = heap;
|
|
|
|
subheap->size = totalSize;
|
2009-06-25 12:11:14 +02:00
|
|
|
subheap->min_commit = 0x10000;
|
2007-10-01 15:28:50 +02:00
|
|
|
subheap->commitSize = commitSize;
|
|
|
|
subheap->magic = SUBHEAP_MAGIC;
|
2002-07-01 20:41:47 +02:00
|
|
|
subheap->headerSize = ROUND_SIZE( sizeof(SUBHEAP) );
|
2007-10-01 15:31:14 +02:00
|
|
|
list_add_head( &heap->subheap_list, &subheap->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If this is a primary subheap, initialize main heap */
|
|
|
|
|
2009-03-17 00:12:11 +01:00
|
|
|
heap = address;
|
2001-12-19 20:16:27 +01:00
|
|
|
heap->flags = flags;
|
|
|
|
heap->magic = HEAP_MAGIC;
|
2008-11-18 12:49:12 +01:00
|
|
|
heap->grow_size = max( HEAP_DEF_SIZE, totalSize );
|
2007-10-01 15:31:14 +02:00
|
|
|
list_init( &heap->subheap_list );
|
2008-10-29 17:32:45 +01:00
|
|
|
list_init( &heap->large_list );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
subheap = &heap->subheap;
|
|
|
|
subheap->base = address;
|
|
|
|
subheap->heap = heap;
|
|
|
|
subheap->size = totalSize;
|
2009-06-25 12:11:14 +02:00
|
|
|
subheap->min_commit = commitSize;
|
2007-10-01 15:28:50 +02:00
|
|
|
subheap->commitSize = commitSize;
|
|
|
|
subheap->magic = SUBHEAP_MAGIC;
|
|
|
|
subheap->headerSize = ROUND_SIZE( sizeof(HEAP) );
|
2007-10-01 15:31:14 +02:00
|
|
|
list_add_head( &heap->subheap_list, &subheap->entry );
|
2007-10-01 15:28:50 +02:00
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Build the free lists */
|
|
|
|
|
2009-06-25 14:33:15 +02:00
|
|
|
heap->freeList = (FREE_LIST_ENTRY *)((char *)heap + subheap->headerSize);
|
|
|
|
subheap->headerSize += HEAP_NB_FREE_LISTS * sizeof(FREE_LIST_ENTRY);
|
2005-08-12 18:05:46 +02:00
|
|
|
list_init( &heap->freeList[0].arena.entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
for (i = 0, pEntry = heap->freeList; i < HEAP_NB_FREE_LISTS; i++, pEntry++)
|
|
|
|
{
|
|
|
|
pEntry->arena.size = 0 | ARENA_FLAG_FREE;
|
|
|
|
pEntry->arena.magic = ARENA_FREE_MAGIC;
|
2005-08-12 18:05:46 +02:00
|
|
|
if (i) list_add_after( &pEntry[-1].arena.entry, &pEntry->arena.entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize critical section */
|
|
|
|
|
2005-08-03 18:03:15 +02:00
|
|
|
if (!processHeap) /* do it by hand to avoid memory allocations */
|
|
|
|
{
|
|
|
|
heap->critSection.DebugInfo = &process_heap_critsect_debug;
|
|
|
|
heap->critSection.LockCount = -1;
|
|
|
|
heap->critSection.RecursionCount = 0;
|
|
|
|
heap->critSection.OwningThread = 0;
|
|
|
|
heap->critSection.LockSemaphore = 0;
|
|
|
|
heap->critSection.SpinCount = 0;
|
|
|
|
process_heap_critsect_debug.CriticalSection = &heap->critSection;
|
|
|
|
}
|
2007-03-10 22:09:39 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
RtlInitializeCriticalSection( &heap->critSection );
|
|
|
|
heap->critSection.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": HEAP.critSection");
|
|
|
|
}
|
2005-08-03 18:03:15 +02:00
|
|
|
|
2002-06-13 23:46:46 +02:00
|
|
|
if (flags & HEAP_SHARED)
|
2003-06-23 20:12:28 +02:00
|
|
|
{
|
|
|
|
/* let's assume that only one thread at a time will try to do this */
|
|
|
|
HANDLE sem = heap->critSection.LockSemaphore;
|
|
|
|
if (!sem) NtCreateSemaphore( &sem, SEMAPHORE_ALL_ACCESS, NULL, 0, 1 );
|
|
|
|
|
2005-02-22 20:33:50 +01:00
|
|
|
NtDuplicateObject( NtCurrentProcess(), sem, NtCurrentProcess(), &sem, 0, 0,
|
2003-06-23 20:12:28 +02:00
|
|
|
DUP_HANDLE_MAKE_GLOBAL | DUP_HANDLE_SAME_ACCESS | DUP_HANDLE_CLOSE_SOURCE );
|
|
|
|
heap->critSection.LockSemaphore = sem;
|
2007-02-06 18:02:16 +01:00
|
|
|
RtlFreeHeap( processHeap, 0, heap->critSection.DebugInfo );
|
|
|
|
heap->critSection.DebugInfo = NULL;
|
2003-06-23 20:12:28 +02:00
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the first free block */
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
HEAP_CreateFreeBlock( subheap, (LPBYTE)subheap->base + subheap->headerSize,
|
2001-12-19 20:16:27 +01:00
|
|
|
subheap->size - subheap->headerSize );
|
|
|
|
|
2007-10-03 10:08:34 +02:00
|
|
|
return subheap;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_FindFreeBlock
|
|
|
|
*
|
|
|
|
* Find a free block at least as large as the requested size, and make sure
|
|
|
|
* the requested size is committed.
|
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
static ARENA_FREE *HEAP_FindFreeBlock( HEAP *heap, SIZE_T size,
|
2001-12-19 20:16:27 +01:00
|
|
|
SUBHEAP **ppSubHeap )
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
2005-08-12 18:05:46 +02:00
|
|
|
struct list *ptr;
|
2007-05-22 11:59:18 +02:00
|
|
|
SIZE_T total_size;
|
2005-08-15 22:49:47 +02:00
|
|
|
FREE_LIST_ENTRY *pEntry = heap->freeList + get_freelist_index( size + sizeof(ARENA_INUSE) );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Find a suitable free list, and in it find a block large enough */
|
|
|
|
|
2005-08-12 18:05:46 +02:00
|
|
|
ptr = &pEntry->arena.entry;
|
|
|
|
while ((ptr = list_next( &heap->freeList[0].arena.entry, ptr )))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-08-12 18:05:46 +02:00
|
|
|
ARENA_FREE *pArena = LIST_ENTRY( ptr, ARENA_FREE, entry );
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T arena_size = (pArena->size & ARENA_SIZE_MASK) +
|
2001-12-19 20:16:27 +01:00
|
|
|
sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
|
|
|
if (arena_size >= size)
|
|
|
|
{
|
|
|
|
subheap = HEAP_FindSubHeap( heap, pArena );
|
2005-08-15 22:49:47 +02:00
|
|
|
if (!HEAP_Commit( subheap, (ARENA_INUSE *)pArena, size )) return NULL;
|
2001-12-19 20:16:27 +01:00
|
|
|
*ppSubHeap = subheap;
|
|
|
|
return pArena;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If no block was found, attempt to grow the heap */
|
|
|
|
|
|
|
|
if (!(heap->flags & HEAP_GROWABLE))
|
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
WARN("Not enough space in heap %p for %08lx bytes\n", heap, size );
|
2001-12-19 20:16:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* make sure that we have a big enough size *committed* to fit another
|
|
|
|
* last free arena in !
|
|
|
|
* So just one heap struct, one first free arena which will eventually
|
2005-09-26 12:58:41 +02:00
|
|
|
* get used, and a second free arena that might get assigned all remaining
|
2005-08-15 22:49:47 +02:00
|
|
|
* free space in HEAP_ShrinkBlock() */
|
2007-05-22 11:59:18 +02:00
|
|
|
total_size = size + ROUND_SIZE(sizeof(SUBHEAP)) + sizeof(ARENA_INUSE) + sizeof(ARENA_FREE);
|
|
|
|
if (total_size < size) return NULL; /* overflow */
|
|
|
|
|
2009-06-30 14:07:18 +02:00
|
|
|
if ((subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, total_size,
|
|
|
|
max( heap->grow_size, total_size ) )))
|
|
|
|
{
|
|
|
|
if (heap->grow_size < 128 * 1024 * 1024) heap->grow_size *= 2;
|
|
|
|
}
|
|
|
|
else while (!subheap) /* shrink the grow size again if we are running out of space */
|
|
|
|
{
|
|
|
|
if (heap->grow_size <= total_size || heap->grow_size <= 4 * 1024 * 1024) return NULL;
|
|
|
|
heap->grow_size /= 2;
|
|
|
|
subheap = HEAP_CreateSubHeap( heap, NULL, heap->flags, total_size,
|
|
|
|
max( heap->grow_size, total_size ) );
|
|
|
|
}
|
2008-11-18 12:49:12 +01:00
|
|
|
|
2005-07-15 13:44:46 +02:00
|
|
|
TRACE("created new sub-heap %p of %08lx bytes for heap %p\n",
|
2008-11-18 12:49:12 +01:00
|
|
|
subheap, subheap->size, heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
*ppSubHeap = subheap;
|
2007-10-01 15:28:50 +02:00
|
|
|
return (ARENA_FREE *)((char *)subheap->base + subheap->headerSize);
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_IsValidArenaPtr
|
|
|
|
*
|
|
|
|
* Check that the pointer is inside the range possible for arenas.
|
|
|
|
*/
|
2007-04-05 22:06:38 +02:00
|
|
|
static BOOL HEAP_IsValidArenaPtr( const HEAP *heap, const ARENA_FREE *ptr )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2008-10-23 23:24:04 +02:00
|
|
|
unsigned int i;
|
2004-06-15 02:47:00 +02:00
|
|
|
const SUBHEAP *subheap = HEAP_FindSubHeap( heap, ptr );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!subheap) return FALSE;
|
2007-10-01 15:28:50 +02:00
|
|
|
if ((const char *)ptr >= (const char *)subheap->base + subheap->headerSize) return TRUE;
|
2001-12-19 20:16:27 +01:00
|
|
|
if (subheap != &heap->subheap) return FALSE;
|
|
|
|
for (i = 0; i < HEAP_NB_FREE_LISTS; i++)
|
2009-03-17 00:12:11 +01:00
|
|
|
if (ptr == &heap->freeList[i].arena) return TRUE;
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ValidateFreeArena
|
|
|
|
*/
|
|
|
|
static BOOL HEAP_ValidateFreeArena( SUBHEAP *subheap, ARENA_FREE *pArena )
|
|
|
|
{
|
2010-01-26 14:35:08 +01:00
|
|
|
DWORD flags = subheap->heap->flags;
|
|
|
|
SIZE_T size;
|
2005-08-12 18:05:46 +02:00
|
|
|
ARENA_FREE *prev, *next;
|
2007-10-01 15:28:50 +02:00
|
|
|
char *heapEnd = (char *)subheap->base + subheap->size;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Check for unaligned pointers */
|
2008-12-11 20:38:41 +01:00
|
|
|
if ((ULONG_PTR)pArena % ALIGNMENT != ARENA_OFFSET)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check magic number */
|
|
|
|
if (pArena->magic != ARENA_FREE_MAGIC)
|
|
|
|
{
|
2007-11-25 09:56:31 +01:00
|
|
|
ERR("Heap %p: invalid free arena magic %08x for %p\n", subheap->heap, pArena->magic, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check size flags */
|
|
|
|
if (!(pArena->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pArena->size & ARENA_FLAG_PREV_FREE))
|
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR("Heap %p: bad flags %08x for free arena %p\n",
|
2005-07-15 13:44:46 +02:00
|
|
|
subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
|
2005-07-15 12:01:30 +02:00
|
|
|
return FALSE;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
/* Check arena size */
|
2010-01-26 14:35:08 +01:00
|
|
|
size = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
if ((char *)(pArena + 1) + size > heapEnd)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:35:08 +01:00
|
|
|
ERR("Heap %p: bad size %08lx for free arena %p\n", subheap->heap, size, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next pointer is valid */
|
2005-08-12 18:05:46 +02:00
|
|
|
next = LIST_ENTRY( pArena->entry.next, ARENA_FREE, entry );
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, next ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: bad next ptr %p for arena %p\n",
|
2005-08-12 18:05:46 +02:00
|
|
|
subheap->heap, next, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next arena is free */
|
2005-08-12 18:05:46 +02:00
|
|
|
if (!(next->size & ARENA_FLAG_FREE) || (next->magic != ARENA_FREE_MAGIC))
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: next arena %p invalid for %p\n",
|
2005-08-12 18:05:46 +02:00
|
|
|
subheap->heap, next, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev pointer is valid */
|
2005-08-12 18:05:46 +02:00
|
|
|
prev = LIST_ENTRY( pArena->entry.prev, ARENA_FREE, entry );
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, prev ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: bad prev ptr %p for arena %p\n",
|
2005-08-12 18:05:46 +02:00
|
|
|
subheap->heap, prev, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is free */
|
2005-08-12 18:05:46 +02:00
|
|
|
if (!(prev->size & ARENA_FLAG_FREE) || (prev->magic != ARENA_FREE_MAGIC))
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
2001-12-19 20:16:27 +01:00
|
|
|
/* this often means that the prev arena got overwritten
|
|
|
|
* by a memory write before that prev arena */
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: prev arena %p invalid for %p\n",
|
2005-08-12 18:05:46 +02:00
|
|
|
subheap->heap, prev, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that next block has PREV_FREE flag */
|
2010-01-26 14:35:08 +01:00
|
|
|
if ((char *)(pArena + 1) + size < heapEnd)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:35:08 +01:00
|
|
|
if (!(*(DWORD *)((char *)(pArena + 1) + size) & ARENA_FLAG_PREV_FREE))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: free arena %p next block has no PREV_FREE flag\n",
|
|
|
|
subheap->heap, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check next block back pointer */
|
2010-01-26 14:35:08 +01:00
|
|
|
if (*((ARENA_FREE **)((char *)(pArena + 1) + size) - 1) != pArena)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-09-18 13:15:16 +02:00
|
|
|
ERR("Heap %p: arena %p has wrong back ptr %p\n",
|
2005-07-15 13:44:46 +02:00
|
|
|
subheap->heap, pArena,
|
2010-01-26 14:35:08 +01:00
|
|
|
*((ARENA_FREE **)((char *)(pArena+1) + size) - 1));
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2010-01-26 14:35:08 +01:00
|
|
|
if (flags & HEAP_FREE_CHECKING_ENABLED)
|
|
|
|
{
|
|
|
|
DWORD *ptr = (DWORD *)(pArena + 1);
|
|
|
|
char *end = (char *)(pArena + 1) + size;
|
|
|
|
|
|
|
|
if (end >= heapEnd) end = (char *)subheap->base + subheap->commitSize;
|
2010-01-29 12:10:17 +01:00
|
|
|
else end -= sizeof(ARENA_FREE *);
|
|
|
|
while (ptr < (DWORD *)end)
|
2010-01-26 14:35:08 +01:00
|
|
|
{
|
|
|
|
if (*ptr != ARENA_FREE_FILLER)
|
|
|
|
{
|
|
|
|
ERR("Heap %p: free block %p overwritten at %p by %08x\n",
|
|
|
|
subheap->heap, (ARENA_INUSE *)pArena + 1, ptr, *ptr );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_ValidateInUseArena
|
|
|
|
*/
|
2004-06-15 02:47:00 +02:00
|
|
|
static BOOL HEAP_ValidateInUseArena( const SUBHEAP *subheap, const ARENA_INUSE *pArena, BOOL quiet )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:34:31 +01:00
|
|
|
SIZE_T size;
|
|
|
|
DWORD i, flags = subheap->heap->flags;
|
2007-10-01 15:28:50 +02:00
|
|
|
const char *heapEnd = (const char *)subheap->base + subheap->size;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Check for unaligned pointers */
|
2008-12-11 20:38:41 +01:00
|
|
|
if ((ULONG_PTR)pArena % ALIGNMENT != ARENA_OFFSET)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
if ( quiet == NOISY )
|
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
if ( TRACE_ON(heap) )
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
}
|
|
|
|
else if ( WARN_ON(heap) )
|
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
WARN( "Heap %p: unaligned arena pointer %p\n", subheap->heap, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
if ( TRACE_ON(heap) )
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check magic number */
|
2010-01-28 19:43:38 +01:00
|
|
|
if (pArena->magic != ARENA_INUSE_MAGIC && pArena->magic != ARENA_PENDING_MAGIC)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
if (quiet == NOISY) {
|
2007-11-25 09:56:31 +01:00
|
|
|
ERR("Heap %p: invalid in-use arena magic %08x for %p\n", subheap->heap, pArena->magic, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (TRACE_ON(heap))
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
} else if (WARN_ON(heap)) {
|
2007-11-25 09:56:31 +01:00
|
|
|
WARN("Heap %p: invalid in-use arena magic %08x for %p\n", subheap->heap, pArena->magic, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (TRACE_ON(heap))
|
|
|
|
HEAP_Dump( subheap->heap );
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check size flags */
|
2002-06-01 01:06:46 +02:00
|
|
|
if (pArena->size & ARENA_FLAG_FREE)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
ERR("Heap %p: bad flags %08x for in-use arena %p\n",
|
2005-07-15 13:44:46 +02:00
|
|
|
subheap->heap, pArena->size & ~ARENA_SIZE_MASK, pArena );
|
2003-05-12 05:32:07 +02:00
|
|
|
return FALSE;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
/* Check arena size */
|
2010-01-26 14:34:31 +01:00
|
|
|
size = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
if ((const char *)(pArena + 1) + size > heapEnd ||
|
|
|
|
(const char *)(pArena + 1) + size < (const char *)(pArena + 1))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:34:31 +01:00
|
|
|
ERR("Heap %p: bad size %08lx for in-use arena %p\n", subheap->heap, size, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check next arena PREV_FREE flag */
|
2010-01-26 14:34:31 +01:00
|
|
|
if (((const char *)(pArena + 1) + size < heapEnd) &&
|
|
|
|
(*(const DWORD *)((const char *)(pArena + 1) + size) & ARENA_FLAG_PREV_FREE))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2010-01-26 14:34:31 +01:00
|
|
|
ERR("Heap %p: in-use arena %p next block %p has PREV_FREE flag %x\n",
|
|
|
|
subheap->heap, pArena, (const char *)(pArena + 1) + size,*(const DWORD *)((const char *)(pArena + 1) + size) );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check prev free arena */
|
|
|
|
if (pArena->size & ARENA_FLAG_PREV_FREE)
|
|
|
|
{
|
2004-06-15 02:47:00 +02:00
|
|
|
const ARENA_FREE *pPrev = *((const ARENA_FREE * const*)pArena - 1);
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Check prev pointer */
|
|
|
|
if (!HEAP_IsValidArenaPtr( subheap->heap, pPrev ))
|
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: bad back ptr %p for arena %p\n",
|
|
|
|
subheap->heap, pPrev, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is free */
|
|
|
|
if (!(pPrev->size & ARENA_FLAG_FREE) ||
|
|
|
|
(pPrev->magic != ARENA_FREE_MAGIC))
|
2002-06-01 01:06:46 +02:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: prev arena %p invalid for in-use %p\n",
|
|
|
|
subheap->heap, pPrev, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check that prev arena is really the previous block */
|
2004-06-15 02:47:00 +02:00
|
|
|
if ((const char *)(pPrev + 1) + (pPrev->size & ARENA_SIZE_MASK) != (const char *)pArena)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 13:44:46 +02:00
|
|
|
ERR("Heap %p: prev arena %p is not prev for in-use %p\n",
|
|
|
|
subheap->heap, pPrev, pArena );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2010-01-26 14:34:31 +01:00
|
|
|
/* Check unused size */
|
|
|
|
if (pArena->unused_bytes > size)
|
|
|
|
{
|
|
|
|
ERR("Heap %p: invalid unused size %08x/%08lx\n", subheap->heap, pArena->unused_bytes, size );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* Check unused bytes */
|
2010-01-28 19:43:38 +01:00
|
|
|
if (pArena->magic == ARENA_PENDING_MAGIC)
|
|
|
|
{
|
2010-03-27 16:41:14 +01:00
|
|
|
const DWORD *ptr = (const DWORD *)(pArena + 1);
|
|
|
|
const DWORD *end = (const DWORD *)((const char *)ptr + size);
|
2010-01-28 19:43:38 +01:00
|
|
|
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
if (*ptr != ARENA_FREE_FILLER)
|
|
|
|
{
|
|
|
|
ERR("Heap %p: free block %p overwritten at %p by %08x\n",
|
2010-03-27 16:41:14 +01:00
|
|
|
subheap->heap, (const ARENA_INUSE *)pArena + 1, ptr, *ptr );
|
2010-01-28 19:43:38 +01:00
|
|
|
if (!*ptr) { HEAP_Dump( subheap->heap ); DbgBreakPoint(); }
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (flags & HEAP_TAIL_CHECKING_ENABLED)
|
2010-01-26 14:34:31 +01:00
|
|
|
{
|
|
|
|
const unsigned char *data = (const unsigned char *)(pArena + 1) + size - pArena->unused_bytes;
|
|
|
|
|
|
|
|
for (i = 0; i < pArena->unused_bytes; i++)
|
|
|
|
{
|
|
|
|
if (data[i] == ARENA_TAIL_FILLER) continue;
|
|
|
|
ERR("Heap %p: block %p tail overwritten at %p (byte %u/%u == 0x%02x)\n",
|
|
|
|
subheap->heap, pArena + 1, data + i, i, pArena->unused_bytes, data[i] );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* HEAP_IsRealArena [Internal]
|
|
|
|
* Validates a block is a valid arena.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* TRUE: Success
|
|
|
|
* FALSE: Failure
|
|
|
|
*/
|
|
|
|
static BOOL HEAP_IsRealArena( HEAP *heapPtr, /* [in] ptr to the heap */
|
|
|
|
DWORD flags, /* [in] Bit flags that control access during operation */
|
|
|
|
LPCVOID block, /* [in] Optional pointer to memory block to validate */
|
|
|
|
BOOL quiet ) /* [in] Flag - if true, HEAP_ValidateInUseArena
|
|
|
|
* does not complain */
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
BOOL ret = TRUE;
|
2008-10-29 17:32:45 +01:00
|
|
|
const ARENA_LARGE *large_arena;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
flags &= HEAP_NO_SERIALIZE;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
/* calling HeapLock may result in infinite recursion, so do the critsect directly */
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
|
|
|
RtlEnterCriticalSection( &heapPtr->critSection );
|
|
|
|
|
2007-04-05 22:06:38 +02:00
|
|
|
if (block) /* only check this single memory block */
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2007-04-05 22:06:38 +02:00
|
|
|
const ARENA_INUSE *arena = (const ARENA_INUSE *)block - 1;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2007-04-05 22:06:38 +02:00
|
|
|
if (!(subheap = HEAP_FindSubHeap( heapPtr, arena )) ||
|
2007-10-01 15:28:50 +02:00
|
|
|
((const char *)arena < (char *)subheap->base + subheap->headerSize))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2008-10-29 17:32:45 +01:00
|
|
|
if (!(large_arena = find_large_block( heapPtr, block )))
|
|
|
|
{
|
|
|
|
if (quiet == NOISY)
|
|
|
|
ERR("Heap %p: block %p is not inside heap\n", heapPtr, block );
|
|
|
|
else if (WARN_ON(heap))
|
|
|
|
WARN("Heap %p: block %p is not inside heap\n", heapPtr, block );
|
|
|
|
ret = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = validate_large_arena( heapPtr, large_arena, quiet );
|
2001-12-19 20:16:27 +01:00
|
|
|
} else
|
2007-04-05 22:06:38 +02:00
|
|
|
ret = HEAP_ValidateInUseArena( subheap, arena, quiet );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE))
|
|
|
|
RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-10-01 15:31:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( subheap, &heapPtr->subheap_list, SUBHEAP, entry )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2007-10-01 15:28:50 +02:00
|
|
|
char *ptr = (char *)subheap->base + subheap->headerSize;
|
|
|
|
while (ptr < (char *)subheap->base + subheap->size)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
if (*(DWORD *)ptr & ARENA_FLAG_FREE)
|
|
|
|
{
|
|
|
|
if (!HEAP_ValidateFreeArena( subheap, (ARENA_FREE *)ptr )) {
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr += sizeof(ARENA_FREE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!HEAP_ValidateInUseArena( subheap, (ARENA_INUSE *)ptr, NOISY )) {
|
|
|
|
ret = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ptr += sizeof(ARENA_INUSE) + (*(DWORD *)ptr & ARENA_SIZE_MASK);
|
|
|
|
}
|
|
|
|
}
|
2007-10-01 15:31:14 +02:00
|
|
|
if (!ret) break;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
LIST_FOR_EACH_ENTRY( large_arena, &heapPtr->large_list, ARENA_LARGE, entry )
|
|
|
|
if (!(ret = validate_large_arena( heapPtr, large_arena, quiet ))) break;
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-27 13:30:52 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* validate_block_pointer
|
|
|
|
*
|
|
|
|
* Minimum validation needed to catch bad parameters in heap functions.
|
|
|
|
*/
|
|
|
|
static BOOL validate_block_pointer( HEAP *heap, SUBHEAP **ret_subheap, const ARENA_INUSE *arena )
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
|
|
|
|
if (!(*ret_subheap = subheap = HEAP_FindSubHeap( heap, arena )))
|
|
|
|
{
|
|
|
|
ARENA_LARGE *large_arena = find_large_block( heap, arena + 1 );
|
|
|
|
|
|
|
|
if (!large_arena)
|
|
|
|
{
|
|
|
|
WARN( "Heap %p: pointer %p is not inside heap\n", heap, arena + 1 );
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
if ((heap->flags & HEAP_VALIDATE) && !validate_large_arena( heap, large_arena, QUIET ))
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-27 16:41:14 +01:00
|
|
|
if ((const char *)arena < (char *)subheap->base + subheap->headerSize)
|
2010-01-27 13:30:52 +01:00
|
|
|
WARN( "Heap %p: pointer %p is inside subheap %p header\n", subheap->heap, arena + 1, subheap );
|
|
|
|
else if (subheap->heap->flags & HEAP_VALIDATE) /* do the full validation */
|
|
|
|
ret = HEAP_ValidateInUseArena( subheap, arena, QUIET );
|
|
|
|
else if ((ULONG_PTR)arena % ALIGNMENT != ARENA_OFFSET)
|
|
|
|
WARN( "Heap %p: unaligned arena pointer %p\n", subheap->heap, arena );
|
2010-01-28 19:43:38 +01:00
|
|
|
else if (arena->magic == ARENA_PENDING_MAGIC)
|
|
|
|
WARN( "Heap %p: block %p used after free\n", subheap->heap, arena + 1 );
|
2010-01-27 13:30:52 +01:00
|
|
|
else if (arena->magic != ARENA_INUSE_MAGIC)
|
|
|
|
WARN( "Heap %p: invalid in-use arena magic %08x for %p\n", subheap->heap, arena->magic, arena );
|
|
|
|
else if (arena->size & ARENA_FLAG_FREE)
|
|
|
|
ERR( "Heap %p: bad flags %08x for in-use arena %p\n",
|
|
|
|
subheap->heap, arena->size & ~ARENA_SIZE_MASK, arena );
|
|
|
|
else if ((const char *)(arena + 1) + (arena->size & ARENA_SIZE_MASK) > (const char *)subheap->base + subheap->size ||
|
|
|
|
(const char *)(arena + 1) + (arena->size & ARENA_SIZE_MASK) < (const char *)(arena + 1))
|
|
|
|
ERR( "Heap %p: bad size %08x for in-use arena %p\n",
|
|
|
|
subheap->heap, arena->size & ARENA_SIZE_MASK, arena );
|
|
|
|
else
|
|
|
|
ret = TRUE;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-01-22 12:33:03 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* heap_set_debug_flags
|
|
|
|
*/
|
|
|
|
void heap_set_debug_flags( HANDLE handle )
|
|
|
|
{
|
|
|
|
HEAP *heap = HEAP_GetPtr( handle );
|
|
|
|
ULONG global_flags = RtlGetNtGlobalFlags();
|
|
|
|
ULONG flags = 0;
|
|
|
|
|
2010-01-25 15:02:31 +01:00
|
|
|
if (TRACE_ON(heap)) global_flags |= FLG_HEAP_VALIDATE_ALL;
|
2010-01-26 14:31:22 +01:00
|
|
|
if (WARN_ON(heap)) global_flags |= FLG_HEAP_VALIDATE_PARAMETERS;
|
2010-01-25 15:02:31 +01:00
|
|
|
|
2010-01-22 12:33:03 +01:00
|
|
|
if (global_flags & FLG_HEAP_ENABLE_TAIL_CHECK) flags |= HEAP_TAIL_CHECKING_ENABLED;
|
|
|
|
if (global_flags & FLG_HEAP_ENABLE_FREE_CHECK) flags |= HEAP_FREE_CHECKING_ENABLED;
|
|
|
|
if (global_flags & FLG_HEAP_DISABLE_COALESCING) flags |= HEAP_DISABLE_COALESCE_ON_FREE;
|
|
|
|
if (global_flags & FLG_HEAP_PAGE_ALLOCS) flags |= HEAP_PAGE_ALLOCS | HEAP_GROWABLE;
|
|
|
|
|
|
|
|
if (global_flags & FLG_HEAP_VALIDATE_PARAMETERS)
|
|
|
|
flags |= HEAP_VALIDATE | HEAP_VALIDATE_PARAMS |
|
|
|
|
HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
|
|
|
|
if (global_flags & FLG_HEAP_VALIDATE_ALL)
|
|
|
|
flags |= HEAP_VALIDATE | HEAP_VALIDATE_ALL |
|
|
|
|
HEAP_TAIL_CHECKING_ENABLED | HEAP_FREE_CHECKING_ENABLED;
|
|
|
|
|
2010-01-28 19:42:31 +01:00
|
|
|
if (RUNNING_ON_VALGRIND) flags = 0; /* no sense in validating since Valgrind catches accesses */
|
|
|
|
|
2010-01-22 12:33:03 +01:00
|
|
|
heap->flags |= flags;
|
|
|
|
heap->force_flags |= flags & ~(HEAP_VALIDATE | HEAP_DISABLE_COALESCE_ON_FREE);
|
2010-01-26 14:31:22 +01:00
|
|
|
|
|
|
|
if (flags & (HEAP_FREE_CHECKING_ENABLED | HEAP_TAIL_CHECKING_ENABLED)) /* fix existing blocks */
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
2010-01-26 16:06:59 +01:00
|
|
|
ARENA_LARGE *large;
|
2010-01-26 14:31:22 +01:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( subheap, &heap->subheap_list, SUBHEAP, entry )
|
|
|
|
{
|
|
|
|
char *ptr = (char *)subheap->base + subheap->headerSize;
|
|
|
|
char *end = (char *)subheap->base + subheap->commitSize;
|
|
|
|
while (ptr < end)
|
|
|
|
{
|
|
|
|
ARENA_INUSE *arena = (ARENA_INUSE *)ptr;
|
|
|
|
SIZE_T size = arena->size & ARENA_SIZE_MASK;
|
|
|
|
if (arena->size & ARENA_FLAG_FREE)
|
|
|
|
{
|
|
|
|
SIZE_T count = size;
|
|
|
|
|
|
|
|
ptr += sizeof(ARENA_FREE) + size;
|
2010-01-29 12:10:17 +01:00
|
|
|
if (ptr >= end) count = end - (char *)((ARENA_FREE *)arena + 1);
|
|
|
|
else count -= sizeof(ARENA_FREE *);
|
2010-01-26 14:31:22 +01:00
|
|
|
mark_block_free( (ARENA_FREE *)arena + 1, count, flags );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-28 19:43:38 +01:00
|
|
|
if (arena->magic == ARENA_PENDING_MAGIC)
|
|
|
|
mark_block_free( arena + 1, size, flags );
|
|
|
|
else
|
|
|
|
mark_block_tail( (char *)(arena + 1) + size - arena->unused_bytes,
|
|
|
|
arena->unused_bytes, flags );
|
2010-01-26 14:31:22 +01:00
|
|
|
ptr += sizeof(ARENA_INUSE) + size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-01-26 16:06:59 +01:00
|
|
|
|
|
|
|
LIST_FOR_EACH_ENTRY( large, &heap->large_list, ARENA_LARGE, entry )
|
|
|
|
mark_block_tail( (char *)(large + 1) + large->data_size,
|
|
|
|
large->block_size - sizeof(*large) - large->data_size, flags );
|
2010-01-26 14:31:22 +01:00
|
|
|
}
|
2010-01-28 19:43:38 +01:00
|
|
|
|
|
|
|
if ((heap->flags & HEAP_GROWABLE) && !heap->pending_free &&
|
|
|
|
((flags & HEAP_FREE_CHECKING_ENABLED) || RUNNING_ON_VALGRIND))
|
|
|
|
{
|
|
|
|
void *ptr = NULL;
|
|
|
|
SIZE_T size = MAX_FREE_PENDING * sizeof(*heap->pending_free);
|
|
|
|
|
|
|
|
if (!NtAllocateVirtualMemory( NtCurrentProcess(), &ptr, 4, &size, MEM_COMMIT, PAGE_READWRITE ))
|
|
|
|
{
|
|
|
|
heap->pending_free = ptr;
|
|
|
|
heap->pending_pos = 0;
|
|
|
|
}
|
|
|
|
}
|
2010-01-22 12:33:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* RtlCreateHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Create a new Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* addr [I] Desired base address
|
|
|
|
* totalSize [I] Total size of the heap, or 0 for a growable heap
|
|
|
|
* commitSize [I] Amount of heap space to commit
|
|
|
|
* unknown [I] Not yet understood
|
|
|
|
* definition [I] Heap definition
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: A HANDLE to the newly created heap.
|
|
|
|
* Failure: a NULL HANDLE.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
HANDLE WINAPI RtlCreateHeap( ULONG flags, PVOID addr, SIZE_T totalSize, SIZE_T commitSize,
|
2001-12-19 20:16:27 +01:00
|
|
|
PVOID unknown, PRTL_HEAP_DEFINITION definition )
|
|
|
|
{
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
|
|
|
|
/* Allocate the heap block */
|
|
|
|
|
|
|
|
if (!totalSize)
|
|
|
|
{
|
|
|
|
totalSize = HEAP_DEF_SIZE;
|
|
|
|
flags |= HEAP_GROWABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(subheap = HEAP_CreateSubHeap( NULL, addr, flags, commitSize, totalSize ))) return 0;
|
|
|
|
|
2011-02-28 11:39:57 +01:00
|
|
|
heap_set_debug_flags( subheap->heap );
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/* link it into the per-process heap list */
|
|
|
|
if (processHeap)
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = subheap->heap;
|
2005-08-12 20:25:16 +02:00
|
|
|
RtlEnterCriticalSection( &processHeap->critSection );
|
|
|
|
list_add_head( &processHeap->entry, &heapPtr->entry );
|
|
|
|
RtlLeaveCriticalSection( &processHeap->critSection );
|
|
|
|
}
|
2008-10-31 13:00:59 +01:00
|
|
|
else if (!addr)
|
2005-08-12 20:25:16 +02:00
|
|
|
{
|
|
|
|
processHeap = subheap->heap; /* assume the first heap we create is the process main heap */
|
|
|
|
list_init( &processHeap->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
2003-10-10 02:12:17 +02:00
|
|
|
|
2009-03-17 00:12:11 +01:00
|
|
|
return subheap->heap;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlDestroyHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Destroy a Heap created with RtlCreateHeap().
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap to destroy.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: A NULL HANDLE, if heap is NULL or it was destroyed
|
|
|
|
* Failure: The Heap handle, if heap is the process heap.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
HANDLE WINAPI RtlDestroyHeap( HANDLE heap )
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
2007-10-01 15:31:14 +02:00
|
|
|
SUBHEAP *subheap, *next;
|
2008-10-29 17:32:45 +01:00
|
|
|
ARENA_LARGE *arena, *arena_next;
|
2007-10-01 15:31:14 +02:00
|
|
|
SIZE_T size;
|
|
|
|
void *addr;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2002-11-21 04:45:01 +01:00
|
|
|
TRACE("%p\n", heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!heapPtr) return heap;
|
|
|
|
|
|
|
|
if (heap == processHeap) return heap; /* cannot delete the main process heap */
|
2005-08-12 20:25:16 +02:00
|
|
|
|
|
|
|
/* remove it from the per-process list */
|
|
|
|
RtlEnterCriticalSection( &processHeap->critSection );
|
|
|
|
list_remove( &heapPtr->entry );
|
|
|
|
RtlLeaveCriticalSection( &processHeap->critSection );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2007-03-10 22:09:39 +01:00
|
|
|
heapPtr->critSection.DebugInfo->Spare[0] = 0;
|
2001-12-19 20:16:27 +01:00
|
|
|
RtlDeleteCriticalSection( &heapPtr->critSection );
|
2007-10-01 15:31:14 +02:00
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( arena, arena_next, &heapPtr->large_list, ARENA_LARGE, entry )
|
|
|
|
{
|
|
|
|
list_remove( &arena->entry );
|
|
|
|
size = 0;
|
|
|
|
addr = arena;
|
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
|
|
|
|
}
|
2007-10-01 15:31:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY_SAFE( subheap, next, &heapPtr->subheap_list, SUBHEAP, entry )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2007-10-01 15:31:14 +02:00
|
|
|
if (subheap == &heapPtr->subheap) continue; /* do this one last */
|
2008-07-24 19:21:01 +02:00
|
|
|
subheap_notify_free_all(subheap);
|
2007-10-01 15:31:14 +02:00
|
|
|
list_remove( &subheap->entry );
|
|
|
|
size = 0;
|
|
|
|
addr = subheap->base;
|
2005-02-22 20:33:50 +01:00
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
2008-07-24 19:21:01 +02:00
|
|
|
subheap_notify_free_all(&heapPtr->subheap);
|
2010-01-28 19:43:38 +01:00
|
|
|
if (heapPtr->pending_free)
|
|
|
|
{
|
|
|
|
size = 0;
|
|
|
|
addr = heapPtr->pending_free;
|
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
|
|
|
|
}
|
2007-10-01 15:31:14 +02:00
|
|
|
size = 0;
|
|
|
|
addr = heapPtr->subheap.base;
|
|
|
|
NtFreeVirtualMemory( NtCurrentProcess(), &addr, &size, MEM_RELEASE );
|
2001-12-19 20:16:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlAllocateHeap (NTDLL.@)
|
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* Allocate a memory block from a Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap to allocate block from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* size [I] Size of the memory block to allocate
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: A pointer to the newly allocated block
|
|
|
|
* Failure: NULL.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* This call does not SetLastError().
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2018-09-18 08:58:32 +02:00
|
|
|
void * WINAPI DECLSPEC_HOTPATCH RtlAllocateHeap( HANDLE heap, ULONG flags, SIZE_T size )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
ARENA_FREE *pArena;
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *subheap;
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T rounded_size;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
if (!heapPtr) return NULL;
|
|
|
|
flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY;
|
|
|
|
flags |= heapPtr->flags;
|
2010-01-27 13:33:53 +01:00
|
|
|
rounded_size = ROUND_SIZE(size) + HEAP_TAIL_EXTRA_SIZE( flags );
|
2007-05-22 11:59:18 +02:00
|
|
|
if (rounded_size < size) /* overflow */
|
|
|
|
{
|
|
|
|
if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-15 22:49:47 +02:00
|
|
|
if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
|
2008-10-29 17:32:45 +01:00
|
|
|
|
|
|
|
if (rounded_size >= HEAP_MIN_LARGE_BLOCK_SIZE && (flags & HEAP_GROWABLE))
|
|
|
|
{
|
|
|
|
void *ret = allocate_large_block( heap, flags, size );
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
if (!ret && (flags & HEAP_GENERATE_EXCEPTIONS)) RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
TRACE("(%p,%08x,%08lx): returning %p\n", heap, flags, size, ret );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/* Locate a suitable free block */
|
|
|
|
|
2005-07-11 15:20:40 +02:00
|
|
|
if (!(pArena = HEAP_FindFreeBlock( heapPtr, rounded_size, &subheap )))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%08lx): returning NULL\n",
|
2001-12-19 20:16:27 +01:00
|
|
|
heap, flags, size );
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remove the arena from the free list */
|
|
|
|
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pArena->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Build the in-use arena */
|
|
|
|
|
|
|
|
pInUse = (ARENA_INUSE *)pArena;
|
|
|
|
|
|
|
|
/* in-use arena is smaller than free arena,
|
|
|
|
* so we have to add the difference to the size */
|
|
|
|
pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE) + sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
|
|
|
pInUse->magic = ARENA_INUSE_MAGIC;
|
|
|
|
|
|
|
|
/* Shrink the block */
|
|
|
|
|
2005-07-11 15:20:40 +02:00
|
|
|
HEAP_ShrinkBlock( subheap, pInUse, rounded_size );
|
|
|
|
pInUse->unused_bytes = (pInUse->size & ARENA_SIZE_MASK) - size;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2006-10-19 21:49:11 +02:00
|
|
|
notify_alloc( pInUse + 1, size, flags & HEAP_ZERO_MEMORY );
|
2010-01-26 14:29:51 +01:00
|
|
|
initialize_block( pInUse + 1, size, pInUse->unused_bytes, flags );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%08lx): returning %p\n", heap, flags, size, pInUse + 1 );
|
2009-03-17 00:12:11 +01:00
|
|
|
return pInUse + 1;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlFreeHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Free a memory block allocated with RtlAllocateHeap().
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap that block was allocated from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* ptr [I] Block to free
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE, if ptr is NULL or was freed successfully.
|
|
|
|
* Failure: FALSE.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2018-09-18 08:58:32 +02:00
|
|
|
BOOLEAN WINAPI DECLSPEC_HOTPATCH RtlFreeHeap( HANDLE heap, ULONG flags, void *ptr )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *subheap;
|
2005-01-24 12:27:27 +01:00
|
|
|
HEAP *heapPtr;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
if (!ptr) return TRUE; /* freeing a NULL ptr isn't an error in Win2k */
|
2005-01-24 12:27:27 +01:00
|
|
|
|
|
|
|
heapPtr = HEAP_GetPtr( heap );
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!heapPtr)
|
|
|
|
{
|
2005-01-04 13:09:32 +01:00
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
|
2001-12-19 20:16:27 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
flags &= HEAP_NO_SERIALIZE;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
|
|
|
|
|
2008-05-22 00:29:31 +02:00
|
|
|
/* Inform valgrind we are trying to free memory, so it can throw up an error message */
|
|
|
|
notify_free( ptr );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-05-22 00:29:31 +02:00
|
|
|
/* Some sanity checks */
|
2001-12-19 20:16:27 +01:00
|
|
|
pInUse = (ARENA_INUSE *)ptr - 1;
|
2010-01-27 13:30:52 +01:00
|
|
|
if (!validate_block_pointer( heapPtr, &subheap, pInUse )) goto error;
|
2010-01-26 16:08:22 +01:00
|
|
|
|
2010-01-27 13:30:52 +01:00
|
|
|
if (!subheap)
|
2008-10-29 17:32:45 +01:00
|
|
|
free_large_block( heapPtr, flags, ptr );
|
2010-01-27 13:30:52 +01:00
|
|
|
else
|
|
|
|
HEAP_MakeInUseBlockFree( subheap, pInUse );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%p): returning TRUE\n", heap, flags, ptr );
|
2001-12-19 20:16:27 +01:00
|
|
|
return TRUE;
|
2006-07-21 13:22:13 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%p): returning FALSE\n", heap, flags, ptr );
|
2006-07-21 13:22:13 +02:00
|
|
|
return FALSE;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlReAllocateHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Change the size of a memory block allocated with RtlAllocateHeap().
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap that block was allocated from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* ptr [I] Block to resize
|
|
|
|
* size [I] Size of the memory block to allocate
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: A pointer to the resized block (which may be different).
|
|
|
|
* Failure: NULL.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2005-07-15 12:01:30 +02:00
|
|
|
PVOID WINAPI RtlReAllocateHeap( HANDLE heap, ULONG flags, PVOID ptr, SIZE_T size )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena;
|
|
|
|
HEAP *heapPtr;
|
|
|
|
SUBHEAP *subheap;
|
2008-02-20 13:56:59 +01:00
|
|
|
SIZE_T oldBlockSize, oldActualSize, rounded_size;
|
2008-10-29 17:32:45 +01:00
|
|
|
void *ret;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2003-11-25 02:02:02 +01:00
|
|
|
if (!ptr) return NULL;
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!(heapPtr = HEAP_GetPtr( heap )))
|
|
|
|
{
|
2005-01-04 13:09:32 +01:00
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
|
2003-11-25 02:02:02 +01:00
|
|
|
return NULL;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate the parameters */
|
|
|
|
|
|
|
|
flags &= HEAP_GENERATE_EXCEPTIONS | HEAP_NO_SERIALIZE | HEAP_ZERO_MEMORY |
|
|
|
|
HEAP_REALLOC_IN_PLACE_ONLY;
|
|
|
|
flags |= heapPtr->flags;
|
2008-10-29 17:32:45 +01:00
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
|
|
|
|
|
2010-01-27 13:33:53 +01:00
|
|
|
rounded_size = ROUND_SIZE(size) + HEAP_TAIL_EXTRA_SIZE(flags);
|
2008-10-29 17:32:45 +01:00
|
|
|
if (rounded_size < size) goto oom; /* overflow */
|
2005-08-15 22:49:47 +02:00
|
|
|
if (rounded_size < HEAP_MIN_DATA_SIZE) rounded_size = HEAP_MIN_DATA_SIZE;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2006-07-21 13:22:13 +02:00
|
|
|
pArena = (ARENA_INUSE *)ptr - 1;
|
2010-01-27 13:30:52 +01:00
|
|
|
if (!validate_block_pointer( heapPtr, &subheap, pArena )) goto error;
|
|
|
|
if (!subheap)
|
2008-10-29 17:32:45 +01:00
|
|
|
{
|
|
|
|
if (!(ret = realloc_large_block( heapPtr, flags, ptr, size ))) goto oom;
|
|
|
|
goto done;
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Check if we need to grow the block */
|
|
|
|
|
2008-02-20 13:56:59 +01:00
|
|
|
oldBlockSize = (pArena->size & ARENA_SIZE_MASK);
|
|
|
|
oldActualSize = (pArena->size & ARENA_SIZE_MASK) - pArena->unused_bytes;
|
|
|
|
if (rounded_size > oldBlockSize)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2008-02-20 13:56:59 +01:00
|
|
|
char *pNext = (char *)(pArena + 1) + oldBlockSize;
|
2008-10-29 17:32:45 +01:00
|
|
|
|
|
|
|
if (rounded_size >= HEAP_MIN_LARGE_BLOCK_SIZE && (flags & HEAP_GROWABLE))
|
|
|
|
{
|
2009-08-05 20:35:36 +02:00
|
|
|
if (flags & HEAP_REALLOC_IN_PLACE_ONLY) goto oom;
|
2008-10-29 17:32:45 +01:00
|
|
|
if (!(ret = allocate_large_block( heapPtr, flags, size ))) goto oom;
|
|
|
|
memcpy( ret, pArena + 1, oldActualSize );
|
2009-05-16 11:33:06 +02:00
|
|
|
notify_free( pArena + 1 );
|
|
|
|
HEAP_MakeInUseBlockFree( subheap, pArena );
|
2008-10-29 17:32:45 +01:00
|
|
|
goto done;
|
|
|
|
}
|
2007-10-01 15:28:50 +02:00
|
|
|
if ((pNext < (char *)subheap->base + subheap->size) &&
|
2001-12-19 20:16:27 +01:00
|
|
|
(*(DWORD *)pNext & ARENA_FLAG_FREE) &&
|
2008-02-20 13:56:59 +01:00
|
|
|
(oldBlockSize + (*(DWORD *)pNext & ARENA_SIZE_MASK) + sizeof(ARENA_FREE) >= rounded_size))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
/* The next block is free and large enough */
|
|
|
|
ARENA_FREE *pFree = (ARENA_FREE *)pNext;
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pFree->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
pArena->size += (pFree->size & ARENA_SIZE_MASK) + sizeof(*pFree);
|
2008-10-29 17:32:45 +01:00
|
|
|
if (!HEAP_Commit( subheap, pArena, rounded_size )) goto oom;
|
2011-11-11 22:12:25 +01:00
|
|
|
notify_realloc( pArena + 1, oldActualSize, size );
|
2005-07-11 15:20:40 +02:00
|
|
|
HEAP_ShrinkBlock( subheap, pArena, rounded_size );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
else /* Do it the hard way */
|
|
|
|
{
|
|
|
|
ARENA_FREE *pNew;
|
|
|
|
ARENA_INUSE *pInUse;
|
|
|
|
SUBHEAP *newsubheap;
|
|
|
|
|
|
|
|
if ((flags & HEAP_REALLOC_IN_PLACE_ONLY) ||
|
2005-07-11 15:20:40 +02:00
|
|
|
!(pNew = HEAP_FindFreeBlock( heapPtr, rounded_size, &newsubheap )))
|
2008-10-29 17:32:45 +01:00
|
|
|
goto oom;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Build the in-use arena */
|
|
|
|
|
2005-08-12 18:05:46 +02:00
|
|
|
list_remove( &pNew->entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
pInUse = (ARENA_INUSE *)pNew;
|
|
|
|
pInUse->size = (pInUse->size & ~ARENA_FLAG_FREE)
|
|
|
|
+ sizeof(ARENA_FREE) - sizeof(ARENA_INUSE);
|
|
|
|
pInUse->magic = ARENA_INUSE_MAGIC;
|
2005-07-11 15:20:40 +02:00
|
|
|
HEAP_ShrinkBlock( newsubheap, pInUse, rounded_size );
|
2008-02-20 13:56:59 +01:00
|
|
|
|
|
|
|
mark_block_initialized( pInUse + 1, oldActualSize );
|
2006-10-19 21:49:11 +02:00
|
|
|
notify_alloc( pInUse + 1, size, FALSE );
|
2008-02-20 13:56:59 +01:00
|
|
|
memcpy( pInUse + 1, pArena + 1, oldActualSize );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Free the previous block */
|
|
|
|
|
2006-10-19 21:49:11 +02:00
|
|
|
notify_free( pArena + 1 );
|
2001-12-19 20:16:27 +01:00
|
|
|
HEAP_MakeInUseBlockFree( subheap, pArena );
|
|
|
|
subheap = newsubheap;
|
|
|
|
pArena = pInUse;
|
|
|
|
}
|
|
|
|
}
|
2006-10-19 21:49:11 +02:00
|
|
|
else
|
|
|
|
{
|
2011-11-11 22:12:25 +01:00
|
|
|
notify_realloc( pArena + 1, oldActualSize, size );
|
2012-06-10 04:42:13 +02:00
|
|
|
HEAP_ShrinkBlock( subheap, pArena, rounded_size );
|
2006-10-19 21:49:11 +02:00
|
|
|
}
|
2005-07-11 15:20:40 +02:00
|
|
|
|
|
|
|
pArena->unused_bytes = (pArena->size & ARENA_SIZE_MASK) - size;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Clear the extra bytes if needed */
|
|
|
|
|
2008-02-20 13:57:30 +01:00
|
|
|
if (size > oldActualSize)
|
2010-01-26 14:29:51 +01:00
|
|
|
initialize_block( (char *)(pArena + 1) + oldActualSize, size - oldActualSize,
|
|
|
|
pArena->unused_bytes, flags );
|
|
|
|
else
|
|
|
|
mark_block_tail( (char *)(pArena + 1) + size, pArena->unused_bytes, flags );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
/* Return the new arena */
|
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
ret = pArena + 1;
|
|
|
|
done:
|
2001-12-19 20:16:27 +01:00
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
2008-10-29 17:32:45 +01:00
|
|
|
TRACE("(%p,%08x,%p,%08lx): returning %p\n", heap, flags, ptr, size, ret );
|
|
|
|
return ret;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
oom:
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
if (flags & HEAP_GENERATE_EXCEPTIONS) RtlRaiseStatus( STATUS_NO_MEMORY );
|
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_NO_MEMORY );
|
|
|
|
TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
|
|
|
|
return NULL;
|
2006-07-21 13:22:13 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%p,%08lx): returning NULL\n", heap, flags, ptr, size );
|
2006-07-21 13:22:13 +02:00
|
|
|
return NULL;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlCompactHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Compact the free space in a Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap that block was allocated from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The number of bytes compacted.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* This function is a harmless stub.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
ULONG WINAPI RtlCompactHeap( HANDLE heap, ULONG flags )
|
|
|
|
{
|
2007-08-27 17:11:36 +02:00
|
|
|
static BOOL reported;
|
|
|
|
if (!reported++) FIXME( "(%p, 0x%x) stub\n", heap, flags );
|
2001-12-19 20:16:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlLockHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Lock a Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap to lock
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE. The Heap is locked.
|
|
|
|
* Failure: FALSE, if heap is invalid.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
BOOLEAN WINAPI RtlLockHeap( HANDLE heap )
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
if (!heapPtr) return FALSE;
|
|
|
|
RtlEnterCriticalSection( &heapPtr->critSection );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlUnlockHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Unlock a Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap to unlock
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE. The Heap is unlocked.
|
|
|
|
* Failure: FALSE, if heap is invalid.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
BOOLEAN WINAPI RtlUnlockHeap( HANDLE heap )
|
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
if (!heapPtr) return FALSE;
|
|
|
|
RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlSizeHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Get the actual size of a memory block allocated from a Heap.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap that block was allocated from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* ptr [I] Block to get the size of
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: The size of the block.
|
|
|
|
* Failure: -1, heap or ptr are invalid.
|
|
|
|
*
|
|
|
|
* NOTES
|
|
|
|
* The size may be bigger than what was passed to RtlAllocateHeap().
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2007-05-02 22:24:46 +02:00
|
|
|
SIZE_T WINAPI RtlSizeHeap( HANDLE heap, ULONG flags, const void *ptr )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-07-15 12:01:30 +02:00
|
|
|
SIZE_T ret;
|
2010-01-27 13:30:52 +01:00
|
|
|
const ARENA_INUSE *pArena;
|
|
|
|
SUBHEAP *subheap;
|
2001-12-19 20:16:27 +01:00
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
|
|
|
|
if (!heapPtr)
|
|
|
|
{
|
2005-01-04 13:09:32 +01:00
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_HANDLE );
|
2005-07-15 12:01:30 +02:00
|
|
|
return ~0UL;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
flags &= HEAP_NO_SERIALIZE;
|
|
|
|
flags |= heapPtr->flags;
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
|
2010-01-27 13:30:52 +01:00
|
|
|
|
|
|
|
pArena = (const ARENA_INUSE *)ptr - 1;
|
|
|
|
if (!validate_block_pointer( heapPtr, &subheap, pArena ))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2005-01-04 13:09:32 +01:00
|
|
|
RtlSetLastWin32ErrorAndNtStatusFromNtStatus( STATUS_INVALID_PARAMETER );
|
2005-07-15 12:01:30 +02:00
|
|
|
ret = ~0UL;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
2010-01-27 13:30:52 +01:00
|
|
|
else if (!subheap)
|
|
|
|
{
|
|
|
|
const ARENA_LARGE *large_arena = (const ARENA_LARGE *)ptr - 1;
|
|
|
|
ret = large_arena->data_size;
|
|
|
|
}
|
2001-12-19 20:16:27 +01:00
|
|
|
else
|
|
|
|
{
|
2010-01-27 13:30:52 +01:00
|
|
|
ret = (pArena->size & ARENA_SIZE_MASK) - pArena->unused_bytes;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
if (!(flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
|
|
|
|
2006-10-16 13:49:06 +02:00
|
|
|
TRACE("(%p,%08x,%p): returning %08lx\n", heap, flags, ptr, ret );
|
2001-12-19 20:16:27 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlValidateHeap (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
2005-02-25 15:07:56 +01:00
|
|
|
* Determine if a block is a valid allocation from a heap.
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* heap [I] Heap that block was allocated from
|
|
|
|
* flags [I] HEAP_ flags from "winnt.h"
|
|
|
|
* ptr [I] Block to check
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: TRUE. The block was allocated from heap.
|
|
|
|
* Failure: FALSE, if heap is invalid or ptr was not allocated from it.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
2003-03-18 19:35:48 +01:00
|
|
|
BOOLEAN WINAPI RtlValidateHeap( HANDLE heap, ULONG flags, LPCVOID ptr )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr( heap );
|
|
|
|
if (!heapPtr) return FALSE;
|
2003-03-18 19:35:48 +01:00
|
|
|
return HEAP_IsRealArena( heapPtr, flags, ptr, QUIET );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlWalkHeap (NTDLL.@)
|
|
|
|
*
|
2003-03-18 19:35:48 +01:00
|
|
|
* FIXME
|
|
|
|
* The PROCESS_HEAP_ENTRY flag values seem different between this
|
|
|
|
* function and HeapWalk(). To be checked.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlWalkHeap( HANDLE heap, PVOID entry_ptr )
|
|
|
|
{
|
|
|
|
LPPROCESS_HEAP_ENTRY entry = entry_ptr; /* FIXME */
|
|
|
|
HEAP *heapPtr = HEAP_GetPtr(heap);
|
|
|
|
SUBHEAP *sub, *currentheap = NULL;
|
|
|
|
NTSTATUS ret;
|
|
|
|
char *ptr;
|
|
|
|
int region_index = 0;
|
|
|
|
|
|
|
|
if (!heapPtr || !entry) return STATUS_INVALID_PARAMETER;
|
|
|
|
|
2002-07-31 19:56:54 +02:00
|
|
|
if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlEnterCriticalSection( &heapPtr->critSection );
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2008-10-29 17:32:45 +01:00
|
|
|
/* FIXME: enumerate large blocks too */
|
|
|
|
|
2001-12-19 20:16:27 +01:00
|
|
|
/* set ptr to the next arena to be examined */
|
|
|
|
|
|
|
|
if (!entry->lpData) /* first call (init) ? */
|
|
|
|
{
|
2002-12-05 20:56:15 +01:00
|
|
|
TRACE("begin walking of heap %p.\n", heap);
|
2001-12-19 20:16:27 +01:00
|
|
|
currentheap = &heapPtr->subheap;
|
2007-10-01 15:28:50 +02:00
|
|
|
ptr = (char*)currentheap->base + currentheap->headerSize;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ptr = entry->lpData;
|
2007-10-01 15:31:14 +02:00
|
|
|
LIST_FOR_EACH_ENTRY( sub, &heapPtr->subheap_list, SUBHEAP, entry )
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
2007-12-28 18:21:48 +01:00
|
|
|
if ((ptr >= (char *)sub->base) &&
|
|
|
|
(ptr < (char *)sub->base + sub->size))
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
currentheap = sub;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
region_index++;
|
|
|
|
}
|
|
|
|
if (currentheap == NULL)
|
|
|
|
{
|
|
|
|
ERR("no matching subheap found, shouldn't happen !\n");
|
|
|
|
ret = STATUS_NO_MORE_ENTRIES;
|
|
|
|
goto HW_end;
|
|
|
|
}
|
|
|
|
|
2010-01-28 19:43:38 +01:00
|
|
|
if (((ARENA_INUSE *)ptr - 1)->magic == ARENA_INUSE_MAGIC ||
|
|
|
|
((ARENA_INUSE *)ptr - 1)->magic == ARENA_PENDING_MAGIC)
|
2007-04-11 15:08:25 +02:00
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr - 1;
|
|
|
|
ptr += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
else if (((ARENA_FREE *)ptr - 1)->magic == ARENA_FREE_MAGIC)
|
|
|
|
{
|
|
|
|
ARENA_FREE *pArena = (ARENA_FREE *)ptr - 1;
|
|
|
|
ptr += pArena->size & ARENA_SIZE_MASK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ptr += entry->cbData; /* point to next arena */
|
|
|
|
|
2007-10-01 15:28:50 +02:00
|
|
|
if (ptr > (char *)currentheap->base + currentheap->size - 1)
|
2001-12-19 20:16:27 +01:00
|
|
|
{ /* proceed with next subheap */
|
2007-10-01 15:31:14 +02:00
|
|
|
struct list *next = list_next( &heapPtr->subheap_list, ¤theap->entry );
|
|
|
|
if (!next)
|
2001-12-19 20:16:27 +01:00
|
|
|
{ /* successfully finished */
|
|
|
|
TRACE("end reached.\n");
|
|
|
|
ret = STATUS_NO_MORE_ENTRIES;
|
|
|
|
goto HW_end;
|
|
|
|
}
|
2007-10-01 15:31:14 +02:00
|
|
|
currentheap = LIST_ENTRY( next, SUBHEAP, entry );
|
2007-10-01 15:28:50 +02:00
|
|
|
ptr = (char *)currentheap->base + currentheap->headerSize;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->wFlags = 0;
|
|
|
|
if (*(DWORD *)ptr & ARENA_FLAG_FREE)
|
|
|
|
{
|
|
|
|
ARENA_FREE *pArena = (ARENA_FREE *)ptr;
|
|
|
|
|
|
|
|
/*TRACE("free, magic: %04x\n", pArena->magic);*/
|
|
|
|
|
|
|
|
entry->lpData = pArena + 1;
|
|
|
|
entry->cbData = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
entry->cbOverhead = sizeof(ARENA_FREE);
|
|
|
|
entry->wFlags = PROCESS_HEAP_UNCOMMITTED_RANGE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ARENA_INUSE *pArena = (ARENA_INUSE *)ptr;
|
|
|
|
|
|
|
|
/*TRACE("busy, magic: %04x\n", pArena->magic);*/
|
|
|
|
|
|
|
|
entry->lpData = pArena + 1;
|
|
|
|
entry->cbData = pArena->size & ARENA_SIZE_MASK;
|
|
|
|
entry->cbOverhead = sizeof(ARENA_INUSE);
|
2010-01-28 19:43:38 +01:00
|
|
|
entry->wFlags = (pArena->magic == ARENA_PENDING_MAGIC) ?
|
|
|
|
PROCESS_HEAP_UNCOMMITTED_RANGE : PROCESS_HEAP_ENTRY_BUSY;
|
2001-12-19 20:16:27 +01:00
|
|
|
/* FIXME: can't handle PROCESS_HEAP_ENTRY_MOVEABLE
|
|
|
|
and PROCESS_HEAP_ENTRY_DDESHARE yet */
|
|
|
|
}
|
|
|
|
|
|
|
|
entry->iRegionIndex = region_index;
|
|
|
|
|
|
|
|
/* first element of heap ? */
|
2007-10-01 15:28:50 +02:00
|
|
|
if (ptr == (char *)currentheap->base + currentheap->headerSize)
|
2001-12-19 20:16:27 +01:00
|
|
|
{
|
|
|
|
entry->wFlags |= PROCESS_HEAP_REGION;
|
|
|
|
entry->u.Region.dwCommittedSize = currentheap->commitSize;
|
|
|
|
entry->u.Region.dwUnCommittedSize =
|
|
|
|
currentheap->size - currentheap->commitSize;
|
|
|
|
entry->u.Region.lpFirstBlock = /* first valid block */
|
2007-10-01 15:28:50 +02:00
|
|
|
(char *)currentheap->base + currentheap->headerSize;
|
2001-12-19 20:16:27 +01:00
|
|
|
entry->u.Region.lpLastBlock = /* first invalid block */
|
2007-10-01 15:28:50 +02:00
|
|
|
(char *)currentheap->base + currentheap->size;
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
|
|
|
ret = STATUS_SUCCESS;
|
2005-03-03 14:50:21 +01:00
|
|
|
if (TRACE_ON(heap)) HEAP_DumpEntry(entry);
|
2001-12-19 20:16:27 +01:00
|
|
|
|
|
|
|
HW_end:
|
2002-07-31 19:56:54 +02:00
|
|
|
if (!(heapPtr->flags & HEAP_NO_SERIALIZE)) RtlLeaveCriticalSection( &heapPtr->critSection );
|
2001-12-19 20:16:27 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlGetProcessHeaps (NTDLL.@)
|
2003-03-18 19:35:48 +01:00
|
|
|
*
|
|
|
|
* Get the Heaps belonging to the current process.
|
|
|
|
*
|
|
|
|
* PARAMS
|
|
|
|
* count [I] size of heaps
|
|
|
|
* heaps [O] Destination array for heap HANDLE's
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* Success: The number of Heaps allocated by the process.
|
|
|
|
* Failure: 0.
|
2001-12-19 20:16:27 +01:00
|
|
|
*/
|
|
|
|
ULONG WINAPI RtlGetProcessHeaps( ULONG count, HANDLE *heaps )
|
|
|
|
{
|
2005-08-12 20:25:16 +02:00
|
|
|
ULONG total = 1; /* main heap */
|
|
|
|
struct list *ptr;
|
2001-12-19 20:16:27 +01:00
|
|
|
|
2005-08-12 20:25:16 +02:00
|
|
|
RtlEnterCriticalSection( &processHeap->critSection );
|
|
|
|
LIST_FOR_EACH( ptr, &processHeap->entry ) total++;
|
2001-12-19 20:16:27 +01:00
|
|
|
if (total <= count)
|
|
|
|
{
|
2005-08-12 20:25:16 +02:00
|
|
|
*heaps++ = processHeap;
|
|
|
|
LIST_FOR_EACH( ptr, &processHeap->entry )
|
|
|
|
*heaps++ = LIST_ENTRY( ptr, HEAP, entry );
|
2001-12-19 20:16:27 +01:00
|
|
|
}
|
2005-08-12 20:25:16 +02:00
|
|
|
RtlLeaveCriticalSection( &processHeap->critSection );
|
2001-12-19 20:16:27 +01:00
|
|
|
return total;
|
|
|
|
}
|
2009-10-12 10:20:57 +02:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlQueryHeapInformation (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlQueryHeapInformation( HANDLE heap, HEAP_INFORMATION_CLASS info_class,
|
|
|
|
PVOID info, SIZE_T size_in, PSIZE_T size_out)
|
|
|
|
{
|
|
|
|
switch (info_class)
|
|
|
|
{
|
|
|
|
case HeapCompatibilityInformation:
|
|
|
|
if (size_out) *size_out = sizeof(ULONG);
|
|
|
|
|
|
|
|
if (size_in < sizeof(ULONG))
|
|
|
|
return STATUS_BUFFER_TOO_SMALL;
|
|
|
|
|
|
|
|
*(ULONG *)info = 0; /* standard heap */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
|
|
|
|
default:
|
|
|
|
FIXME("Unknown heap information class %u\n", info_class);
|
|
|
|
return STATUS_INVALID_INFO_CLASS;
|
|
|
|
}
|
|
|
|
}
|
2014-12-15 18:15:07 +01:00
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* RtlSetHeapInformation (NTDLL.@)
|
|
|
|
*/
|
|
|
|
NTSTATUS WINAPI RtlSetHeapInformation( HANDLE heap, HEAP_INFORMATION_CLASS info_class, PVOID info, SIZE_T size)
|
|
|
|
{
|
|
|
|
FIXME("%p %d %p %ld stub\n", heap, info_class, info, size);
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|