3 * Copyright (C) 1996, Onno Hovers, All rights reserved
4 * Adapted for the ReactOS system libraries by David Welch (welch@mcmail.com)
5 * todo: __processheap should be part of peb.
8 #ifndef __INCLUDE_KERNEL32_HEAP_H
9 #define __INCLUDE_KERNEL32_HEAP_H
11 /* System wide includes ****************************************************/
14 /* System library's private includes ***************************************/
16 #include <ntdll/pagesize.h>
19 #define HEAP_ADMIN_SIZE (sizeof(HEAP_BLOCK))
20 #define HEAP_FRAG_ADMIN_SIZE (sizeof(HEAP_FRAGMENT))
21 #define HEAP_ROUNDVAL (2*(HEAP_ADMIN_SIZE)-1)
22 #define HEAP_FRAGMENT_THRESHOLD 256
24 #define SIZE_TOTAL(s) ROUNDUP((s)+HEAP_ADMIN_SIZE,8)
25 #define SIZE_ROUND(s) ROUNDUP((s),8)
27 #define HEAP_FRAG_MAGIC 0x10
28 #define HEAP_ALLOC_MASK 0xF0000000
29 #define HEAP_FREE_MASK 0x80000000
30 #define HEAP_SIZE_MASK 0x0FFFFFFF
31 #define HEAP_FREE_TAG 0x80000000 /* free region */
32 #define HEAP_NORMAL_TAG 0x30000000 /* normal allocation */
33 #define HEAP_MOVEABLE_TAG 0x50000000 /* moveable handle */
34 #define HEAP_SUB_TAG 0x70000000 /* suballocated for fragments */
36 #define HEAP_ISFREE(p) ((((PHEAP_BLOCK)p)->Size) & HEAP_FREE_MASK)
37 #define HEAP_ISALLOC(p) (((((PHEAP_BLOCK)p)->Size) & HEAP_FREE_MASK)==0)
38 #define HEAP_ISFRAG(p) ((((PHEAP_FRAGMENT)p)->Magic)==HEAP_FRAG_MAGIC)
39 #define HEAP_ISNORMAL(p) (((((PHEAP_BLOCK)p)->Size) & HEAP_ALLOC_MASK)\
41 #define HEAP_ISSUB(p) (((((PHEAP_BLOCK)p)->Size) & HEAP_ALLOC_MASK)\
43 #define HEAP_ISOLD(p) (((((PHEAP_BLOCK)p)->Size) & HEAP_ALLOC_MASK)\
46 #define HEAP_SIZE(p) ((((PHEAP_BLOCK)p)->Size) & HEAP_SIZE_MASK )
47 #define HEAP_RSIZE(p) SIZE_ROUND(HEAP_SIZE(p))
48 #define HEAP_TSIZE(p) SIZE_TOTAL(HEAP_SIZE(p))
49 #define HEAP_PREVSIZE(p) ((((PHEAP_BLOCK)p)->PrevSize) & HEAP_SIZE_MASK )
50 #define HEAP_FRAG_SIZE(p) (((PHEAP_FRAGMENT)p)->Size)
52 #define HEAP_PREV(p) ((PHEAP_BLOCK)(((LPVOID)(p))-HEAP_PREVSIZE(p)))
53 #define HEAP_NEXT(p) ((PHEAP_BLOCK)(((LPVOID)(p))+HEAP_TSIZE(p)))
55 typedef struct __HEAP_BLOCK
57 ULONG Size; /* this is relative to Data */
58 ULONG PrevSize; /* p - p->PrevSize is the previous block */
59 } HEAP_BLOCK, *PHEAP_BLOCK;
61 struct __HEAP_SUBALLOC;
63 typedef struct __HEAP_FRAGMENT
68 struct __HEAP_SUBALLOC *Sub;
70 /* this is only used in free blocks */
71 struct __HEAP_FRAGMENT *FreeNext;
72 struct __HEAP_FRAGMENT *FreePrev;
73 } HEAP_FRAGMENT, *PHEAP_FRAGMENT, *LPHEAP_FRAGMENT;
75 typedef struct __HEAP_SUBALLOC
79 struct __HEAP_SUBALLOC *Next;
81 struct __HEAP_SUBALLOC *Prev;
82 struct __HEAP_FRAGMENT *FirstFree;
83 struct __HEAP_BUCKET *Bucket;
85 } HEAP_SUBALLOC, *PHEAP_SUBALLOC, *LPHEAP_SUBALLOC;
87 typedef struct __HEAP_BUCKET
89 struct __HEAP_SUBALLOC *FirstFree;
93 } HEAP_BUCKET, *PHEAP_BUCKET, *LPHEAP_BUCKET;
100 CRITICAL_SECTION Synchronize;
101 HEAP_BUCKET Bucket[8];
102 struct __HEAP *NextHeap;
104 /* this has to aligned on an 8 byte boundary */
105 HEAP_BLOCK Start __attribute__((aligned (8)));
108 #endif /* __INCLUDE_KERNEL32_HEAP_H */