ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

baseheap.h
Go to the documentation of this file.
00001 /*
00002  * PROJECT:         ReactOS Win32 Base API
00003  * LICENSE:         GPL - See COPYING in the top level directory
00004  * FILE:            dll/win32/kernel32/include/baseheap.h
00005  * PURPOSE:         Base Heap Structures
00006  * PROGRAMMERS:     Alex Ionescu (alex.ionescu@reactos.org)
00007  */
00008 
00009 //
00010 // Some important implementation notes.
00011 //
00012 // Firstly, the Global* APIs in Win32 are largely similar to the Local* APIs,
00013 // but there are a number of small differences (for example, there is no such
00014 // thing as DDE/Shared Local memory, and the re-allocation semantics are also
00015 // simpler, because you cannot force a move).
00016 //
00017 // Note something VERY IMPORTANT: This implementation *depends* on the fact
00018 // that all heap pointers are, at the very least, 8 byte aligned, and that heap
00019 // handles are actually pointers inside our HandleEntry->Object, which happens
00020 // to be at offset 0x4, which means testing with bit 3 tells us if the handle
00021 // is a pointer or a real "handle". On 64-bit, heap pointers should be 16-byte
00022 // aligned, and our offset should be 0x8, so the trick works anyways, but using
00023 // the 4th bit.
00024 //
00025 // Apart from MSDN, a wonderful source of information about how this works is
00026 // available on Raymond's blog, in a 4-parter series starting at:
00027 // http://blogs.msdn.com/oldnewthing/archive/2004/11/04/252258.aspx.
00028 //
00029 // Finally, as Raymond points out, be aware that some applications depend on
00030 // the way this implementation was done, since global memory handles are a
00031 // straight-forward overlay on top of the RTL Handle implementation, and rogue
00032 // applications can easily do the conversion manually without calling the right
00033 // API for it (such as GlobalLock).
00034 //
00035 
00036 // Tracing Support
00037 // Define _BASE_HANDLE_TRACE for Traces
00038 //
00039 #ifdef _BASE_HANDLE_TRACE_
00040 #define BH_PRINT DbgPrint
00041 #else
00042 #define BH_PRINT DPRINT
00043 #endif
00044 #define BASE_TRACE_ALLOC(x, y)                                              \
00045     BH_PRINT("[BASE_HEAP] %s : Allocating %lx bytes with flags: %lx\n",     \
00046              __FUNCTION__, x, y)
00047 #define BASE_TRACE_ALLOC2(x)                                                \
00048     BH_PRINT("[BASE_HEAP] %s : Allocated %p\n",                             \
00049              __FUNCTION__, x)
00050 #define BASE_TRACE_PTR(x, y)                                                \
00051     BH_PRINT("[BASE_HEAP] %s : Using handle: %lx for pointer: %p\n",        \
00052              __FUNCTION__, x, y)
00053 #define BASE_TRACE_HANDLE(x, y)                                             \
00054     BH_PRINT("[BASE_HEAP] %s : Using handle: %lx for block: %p\n",          \
00055              __FUNCTION__, x, y)
00056 #define BASE_TRACE_DEALLOC(x)                                               \
00057     BH_PRINT("[BASE_HEAP] %s : Freeing %p\n",                               \
00058              __FUNCTION__, x)
00059 #define BASE_TRACE_FAILURE()                                                \
00060     BH_PRINT("[BASE_HEAP] %s : Failing %d\n",                               \
00061              __FUNCTION__, __LINE__)
00062 
00063 //
00064 // The handle structure for global heap handles.
00065 // Notice that it nicely overlays with RTL_HANDLE_ENTRY.
00066 // KEEP IT THAT WAY! ;-)
00067 //
00068 typedef struct _BASE_HEAP_HANDLE_ENTRY
00069 {
00070     USHORT Flags;
00071     USHORT LockCount;
00072     union
00073     {
00074         PVOID Object;
00075         ULONG OldSize;
00076     };
00077 } BASE_HEAP_HANDLE_ENTRY, *PBASE_HEAP_HANDLE_ENTRY;
00078 
00079 //
00080 // Handle entry flags
00081 // Note that 0x0001 is the shared/generic RTL_HANDLE_VALID
00082 //
00083 #define BASE_HEAP_ENTRY_FLAG_MOVABLE        0x0002
00084 #define BASE_HEAP_ENTRY_FLAG_REUSABLE       0x0004
00085 #define BASE_HEAP_ENTRY_FLAG_REUSE          0x0008
00086 #define BASE_HEAP_ENTRY_FLAG_DDESHARE       0x0010
00087 
00088 //
00089 // Easy way to check if the global handle is actually an entry in our table
00090 //
00091 #define BASE_HEAP_IS_HANDLE_ENTRY           \
00092     (ULONG_PTR)FIELD_OFFSET(BASE_HEAP_HANDLE_ENTRY, Object)
00093 
00094 //
00095 // Tags for the entire heap allocation for this global memory.
00096 // They are set part of the User Flags of the RTL Heap.
00097 //
00098 #define BASE_HEAP_FLAG_MOVABLE              HEAP_SETTABLE_USER_FLAG1
00099 #define BASE_HEAP_FLAG_DDESHARE             HEAP_SETTABLE_USER_FLAG2
00100 
00101 //
00102 // Internal Handle Functions
00103 //
00104 #define BaseHeapAllocEntry()                \
00105     (PBASE_HEAP_HANDLE_ENTRY)RtlAllocateHandle(&BaseHeapHandleTable, NULL)
00106 
00107 #define BaseHeapGetEntry(h)                 \
00108     (PBASE_HEAP_HANDLE_ENTRY)               \
00109         CONTAINING_RECORD(h,                \
00110             BASE_HEAP_HANDLE_ENTRY,         \
00111             Object);
00112 
00113 #define BaseHeapValidateEntry(he)           \
00114     RtlIsValidHandle(&BaseHeapHandleTable, (PRTL_HANDLE_TABLE_ENTRY)he)
00115 
00116 #define BaseHeapFreeEntry(he)               \
00117     RtlFreeHandle(&BaseHeapHandleTable, (PRTL_HANDLE_TABLE_ENTRY)he);
00118 

Generated on Sun May 27 2012 04:24:33 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.