:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / include / kernel32 / heap.h
1 /*
2  * kernel/heap.c
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.
6  */
7
8 #ifndef __INCLUDE_KERNEL32_HEAP_H
9 #define __INCLUDE_KERNEL32_HEAP_H
10
11 /* System wide includes ****************************************************/
12 #include <windows.h>
13
14 /* System library's private includes ***************************************/
15
16 #include <ntdll/pagesize.h>
17
18 /* definitions */
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
23
24 #define SIZE_TOTAL(s)     ROUNDUP((s)+HEAP_ADMIN_SIZE,8)
25 #define SIZE_ROUND(s)     ROUNDUP((s),8)
26
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 */
35
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)\
40                           ==HEAP_NORMAL_TAG)
41 #define HEAP_ISSUB(p)     (((((PHEAP_BLOCK)p)->Size) & HEAP_ALLOC_MASK)\
42                           ==HEAP_SUB_TAG)
43 #define HEAP_ISOLD(p)     (((((PHEAP_BLOCK)p)->Size) & HEAP_ALLOC_MASK)\
44                           ==HEAP_MOVEABLE_TAG)
45
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)
51
52 #define HEAP_PREV(p)      ((PHEAP_BLOCK)(((LPVOID)(p))-HEAP_PREVSIZE(p)))
53 #define HEAP_NEXT(p)      ((PHEAP_BLOCK)(((LPVOID)(p))+HEAP_TSIZE(p)))
54
55 typedef struct __HEAP_BLOCK
56 {
57    ULONG  Size;            /* this is relative to Data */
58    ULONG  PrevSize;        /* p - p->PrevSize is the previous block */
59 } HEAP_BLOCK, *PHEAP_BLOCK;
60
61 struct __HEAP_SUBALLOC;
62
63 typedef struct __HEAP_FRAGMENT
64 {
65    UCHAR                         Magic;
66    UCHAR                         Number;
67    ULONG                         Size;
68    struct __HEAP_SUBALLOC       *Sub;
69
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;
74
75 typedef struct __HEAP_SUBALLOC
76 {
77    ULONG                         Magic;
78    ULONG                         NumberFree;
79    struct __HEAP_SUBALLOC       *Next;
80
81    struct __HEAP_SUBALLOC       *Prev;
82    struct __HEAP_FRAGMENT       *FirstFree;
83    struct __HEAP_BUCKET         *Bucket;
84    ULONG                         Bitmap;
85 } HEAP_SUBALLOC, *PHEAP_SUBALLOC, *LPHEAP_SUBALLOC;
86
87 typedef struct __HEAP_BUCKET
88 {
89    struct __HEAP_SUBALLOC       *FirstFree;
90    ULONG                         Size;
91    ULONG                         Number;
92    ULONG                         TotalSize;
93 } HEAP_BUCKET, *PHEAP_BUCKET, *LPHEAP_BUCKET;
94
95 typedef struct __HEAP
96 {
97    ULONG                Magic;
98    LPVOID               End;
99    ULONG                Flags;
100    CRITICAL_SECTION     Synchronize;
101    HEAP_BUCKET          Bucket[8];
102    struct __HEAP        *NextHeap;
103    LPVOID               LastBlock;
104    /* this has to aligned on an 8 byte boundary */
105    HEAP_BLOCK           Start  __attribute__((aligned (8)));
106 } HEAP, *PHEAP;
107
108 #endif /* __INCLUDE_KERNEL32_HEAP_H */