831420fd4ebc09130026fe4cf81ab270c946b0c7
[reactos.git] / ntoskrnl / mm / pool.c
1 /* $Id$
2  * 
3  * COPYRIGHT:    See COPYING in the top level directory
4  * PROJECT:      ReactOS kernel
5  * FILE:         ntoskrnl/mm/pool.c
6  * PURPOSE:      Implements the kernel memory pool
7  * PROGRAMMER:   David Welch (welch@mcmail.com)
8  */
9
10 /* INCLUDES ****************************************************************/
11
12 #include <ddk/ntddk.h>
13 #include <reactos/bugcodes.h>
14 #include <internal/ntoskrnl.h>
15 #include <internal/pool.h>
16
17 #define NDEBUG
18 #include <internal/debug.h>
19
20 /* GLOBALS *****************************************************************/
21
22 #define TAG_NONE (ULONG)(('N'<<0) + ('o'<<8) + ('n'<<16) + ('e'<<24))
23
24 /* FUNCTIONS ***************************************************************/
25
26 PVOID STDCALL STATIC
27 EiAllocatePool(POOL_TYPE PoolType,
28                ULONG NumberOfBytes,
29                ULONG Tag,
30                PVOID Caller)
31 {
32    PVOID Block;
33   
34    
35    switch(PoolType)
36      {
37       case NonPagedPool:
38       case NonPagedPoolMustSucceed:
39       case NonPagedPoolCacheAligned:
40       case NonPagedPoolCacheAlignedMustS:
41         Block = 
42           ExAllocateNonPagedPoolWithTag(PoolType,
43                                         NumberOfBytes,
44                                         Tag,
45                                         Caller);
46         break;
47         
48       case PagedPool:
49       case PagedPoolCacheAligned:
50         Block = ExAllocatePagedPoolWithTag(PoolType,NumberOfBytes,Tag);
51         break;
52         
53       default:
54         return(NULL);
55      };
56    
57    if ((PoolType==NonPagedPoolMustSucceed || 
58         PoolType==NonPagedPoolCacheAlignedMustS) && Block==NULL)     
59      {
60         KEBUGCHECK(MUST_SUCCEED_POOL_EMPTY);
61      }
62    return(Block);
63 }
64
65 /*
66  * @implemented
67  */
68 PVOID STDCALL
69 ExAllocatePool (POOL_TYPE PoolType, ULONG NumberOfBytes)
70 /*
71  * FUNCTION: Allocates pool memory of a specified type and returns a pointer
72  * to the allocated block. This routine is used for general purpose allocation
73  * of memory
74  * ARGUMENTS:
75  *        PoolType
76  *               Specifies the type of memory to allocate which can be one
77  *               of the following:
78  *  
79  *               NonPagedPool
80  *               NonPagedPoolMustSucceed
81  *               NonPagedPoolCacheAligned
82  *               NonPagedPoolCacheAlignedMustS
83  *               PagedPool
84  *               PagedPoolCacheAligned
85  *        
86  *        NumberOfBytes
87  *               Specifies the number of bytes to allocate
88  * RETURNS: The allocated block on success
89  *          NULL on failure
90  */
91 {
92    PVOID Block;
93    Block = EiAllocatePool(PoolType,
94                           NumberOfBytes,
95                           TAG_NONE,
96                           (PVOID)__builtin_return_address(0));
97    return(Block);
98 }
99
100
101 /*
102  * @implemented
103  */
104 PVOID STDCALL
105 ExAllocatePoolWithTag (ULONG PoolType, ULONG NumberOfBytes, ULONG Tag)
106 {
107    PVOID Block;
108    Block = EiAllocatePool(PoolType,
109                           NumberOfBytes,
110                           Tag,
111                           (PVOID)__builtin_return_address(0));
112    return(Block);
113 }
114
115
116 /*
117  * @implemented
118  */
119 PVOID STDCALL
120 ExAllocatePoolWithQuota (POOL_TYPE PoolType, ULONG NumberOfBytes)
121 {
122   return(ExAllocatePoolWithQuotaTag(PoolType, NumberOfBytes, TAG_NONE));
123 }
124
125
126 /*
127  * @unimplemented
128  */
129 PVOID STDCALL
130 ExAllocatePoolWithQuotaTag (IN  POOL_TYPE       PoolType,
131                             IN  ULONG           NumberOfBytes,
132                             IN  ULONG           Tag)
133 {
134 #if 0
135   PVOID Block;
136   Block = EiAllocatePool(PoolType,
137                          NumberOfBytes,
138                          Tag,
139                          (PVOID)__builtin_return_address(0));
140   return(Block);
141 #endif
142   UNIMPLEMENTED;
143 }
144
145 /*
146  * @implemented
147  */
148 VOID STDCALL
149 ExFreePool(IN PVOID Block)
150 {
151   if (Block >= MmPagedPoolBase && Block < (MmPagedPoolBase + MmPagedPoolSize))
152     {
153       ExFreePagedPool(Block);
154     }
155   else
156     {
157       ExFreeNonPagedPool(Block);
158     }
159 }
160
161 /* EOF */
162
163
164
165