ReactOS 0.4.15-dev-7842-g558ab78
blkalloc.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING.ARM in the top level directory
3 * PROJECT: ReactOS UEFI Boot Library
4 * FILE: boot/environ/lib/mm/blkalloc.c
5 * PURPOSE: Boot Library Memory Manager Block Allocator
6 * PROGRAMMER: Alex Ionescu (alex.ionescu@reactos.org)
7*/
8
9/* INCLUDES ******************************************************************/
10
11#include "bl.h"
12
13/* DATA VARIABLES ************************************************************/
14
18
20{
32
33typedef struct _BL_BLOCK_ENTRY
34{
38
39/* FUNCTIONS *****************************************************************/
40
46 _In_ PVOID Argument3,
48 )
49{
51 ULONG BlockId = PtrToUlong(Argument1);
52
53 /* Check if the block ID matches */
54 return BlockInfo->BlockId == BlockId;
55}
56
59 ULONG BlockId
60 )
61{
62 ULONG EntryId;
63
64 /* Find the block that matches */
65 EntryId = BlockId;
68 &EntryId,
70 UlongToPtr(EntryId),
71 NULL,
72 NULL,
73 NULL);
74}
75
78 _In_ PBL_BLOCK_DESCRIPTOR BlockInfo,
79 _In_ PBL_BLOCK_ENTRY BlockEntry
80 )
81{
82 /* @TODO FIXME: Later */
83 EfiPrintf(L"Block free not yet implemented\r\n");
85}
86
89 _In_ ULONG BlockId
90 )
91{
92 NTSTATUS Status, LocalStatus;
93 PBL_BLOCK_DESCRIPTOR BlockInfo;
94 PLIST_ENTRY ListHead, NextEntry;
95 PBL_BLOCK_ENTRY BlockEntry;
96
97 /* Nothing to delete if we're not initialized */
99 {
100 return STATUS_UNSUCCESSFUL;
101 }
102
103 /* Find the block descriptor */
104 BlockInfo = MmBapFindBlockInformation(BlockId);
105 if (BlockInfo)
106 {
107 /* Assume success for now */
109
110 /* Do we have at least one reference? */
111 if (BlockInfo->ReferenceCount)
112 {
113 /* Iterate over the allocated blocks */
114 ListHead = &BlockInfo->ListHead;
115 NextEntry = ListHead->Flink;
116 while (NextEntry != ListHead)
117 {
118 /* Free each one */
119 BlockEntry = CONTAINING_RECORD(NextEntry,
121 ListEntry);
122 LocalStatus = MmBapFreeBlockAllocatorDescriptor(BlockInfo,
123 BlockEntry);
124 if (!NT_SUCCESS(LocalStatus))
125 {
126 /* Remember status on failure only */
127 Status = LocalStatus;
128 }
129 }
130
131 /* Drop a reference */
132 BlockInfo->ReferenceCount--;
133 }
134 else
135 {
136 /* There aren't any references, so why are we being called? */
138 }
139 }
140 else
141 {
142 /* No block exists with this ID */
144 }
145
146 /* Return back */
147 return Status;
148}
149
154 )
155{
157 NTSTATUS Status, LocalStatus;
158
159 /* Assume success */
161
162 /* Check if there was at least one reference */
163 if (BlockInfo->ReferenceCount > 1)
164 {
165 /* Try to delete the allocator */
166 LocalStatus = BlpMmDeleteBlockAllocator(BlockInfo->BlockId);
167 if (!NT_SUCCESS(LocalStatus))
168 {
169 /* Remember status on failure only */
170 Status = LocalStatus;
171 }
172 }
173
174 /* Now destroy the allocator's descriptor */
175 LocalStatus = BlMmFreeHeap(BlockInfo);
176 if (!NT_SUCCESS(LocalStatus))
177 {
178 /* Remember status on failure only */
179 Status = LocalStatus;
180 }
181
182 /* Free the entry, and return failure, if any */
184 return Status;
185}
186
190 )
191{
194
195 /* Check if there's a reference on the block descriptor */
196 if (BlockInfo->ReferenceCount)
197 {
198 /* Don't allow purging */
200 }
201 else
202 {
203 /* Free the entry */
205 BlockInfo->BlockId);
206 }
207
208 /* Return purge status */
209 return Status;
210}
211
214 VOID
215 )
216{
217 PBL_BLOCK_DESCRIPTOR BlockInfo;
218 ULONG BlockId;
220
221 /* If the block allocator isn't initialized, bail out */
222 BlockId = -1;
224 {
225 goto Quickie;
226 }
227
228 /* Allocate a block descriptor and zero it out */
229 BlockInfo = BlMmAllocateHeap(sizeof(*BlockInfo));
230 if (!BlockInfo)
231 {
232 goto Quickie;
233 }
234 RtlZeroMemory(BlockInfo, sizeof(*BlockInfo));
235
236 /* Setup the block descriptor */
237 BlockInfo->Attributes = 0;
238 BlockInfo->Type = BlLoaderBlockMemory;
239 BlockInfo->Unknown = 1;
240 BlockInfo->Unknown2 = 1;
241 BlockInfo->Size = PAGE_SIZE;
242 BlockInfo->Count = 128;
243 BlockInfo->Count2 = 128;
244 InitializeListHead(&BlockInfo->ListHead);
245
246 /* Add it to the list of block descriptors */
249 BlockInfo,
250 &BlockId,
252 if (NT_SUCCESS(Status))
253 {
254 /* Add the initial reference and store the block ID */
255 BlockInfo->ReferenceCount = 1;
256 BlockInfo->BlockId = BlockId;
257 }
258
259Quickie:
260 /* On failure, free the block descriptor */
261 if (BlockId == -1)
262 {
263 BlMmFreeHeap(BlockInfo);
264 }
265
266 /* Return the block descriptor ID, or -1 on failure */
267 return BlockId;
268}
269
272 VOID
273 )
274{
276 ULONG Size;
277
278 /* Allocate 8 table entries */
283 {
284 /* Zero them out -- we're all done */
288 }
289 else
290 {
291 /* Bail out since we're out of memory */
294 }
295
296 /* Return initialization status */
297 return Status;
298}
unsigned char BOOLEAN
LONG NTSTATUS
Definition: precomp.h:26
@ BlLoaderBlockMemory
Definition: bl.h:315
VOID EfiPrintf(_In_ PWCHAR Format,...)
Definition: firmware.c:126
PVOID BlMmAllocateHeap(_In_ SIZE_T Size)
Definition: heapalloc.c:569
enum _BL_MEMORY_TYPE BL_MEMORY_TYPE
PVOID BlTblFindEntry(_In_ PVOID *Table, _In_ ULONG Count, _Out_ PULONG EntryIndex, _In_ PBL_TBL_LOOKUP_ROUTINE Callback, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
Definition: util.c:273
NTSTATUS BlTblSetEntry(_Inout_ PVOID **Table, _Inout_ PULONG Count, _In_ PVOID Entry, _Out_ PULONG EntryIndex, _In_ PBL_TBL_SET_ROUTINE Callback)
Definition: util.c:321
NTSTATUS BlMmFreeHeap(_In_ PVOID Buffer)
Definition: heapalloc.c:663
PBL_BLOCK_DESCRIPTOR MmBapFindBlockInformation(ULONG BlockId)
Definition: blkalloc.c:58
struct _BL_BLOCK_ENTRY BL_BLOCK_ENTRY
struct _BL_BLOCK_DESCRIPTOR BL_BLOCK_DESCRIPTOR
struct _BL_BLOCK_ENTRY * PBL_BLOCK_ENTRY
NTSTATUS MmBapPurgeBlockAllocatorTableEntry(_In_ PVOID Entry)
Definition: blkalloc.c:188
struct _BL_BLOCK_DESCRIPTOR * PBL_BLOCK_DESCRIPTOR
NTSTATUS MmBapFreeBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ ULONG Index)
Definition: blkalloc.c:151
BOOLEAN MmBlockAllocatorInitialized
Definition: blkalloc.c:17
ULONG MmBlockAllocatorTableEntries
Definition: blkalloc.c:16
NTSTATUS MmBapFreeBlockAllocatorDescriptor(_In_ PBL_BLOCK_DESCRIPTOR BlockInfo, _In_ PBL_BLOCK_ENTRY BlockEntry)
Definition: blkalloc.c:77
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
NTSTATUS BlpMmDeleteBlockAllocator(_In_ ULONG BlockId)
Definition: blkalloc.c:88
BOOLEAN MmBapCompareBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
Definition: blkalloc.c:42
NTSTATUS BlpMmCreateBlockAllocator(VOID)
Definition: blkalloc.c:213
NTSTATUS MmBaInitialize(VOID)
Definition: blkalloc.c:271
_In_ PVOID Argument2
Definition: classpnp.h:721
#define NULL
Definition: types.h:112
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define UlongToPtr(u)
Definition: config.h:106
#define PtrToUlong(u)
Definition: config.h:107
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
InternalIoctlParams Argument4
Status
Definition: gdiplustypes.h:25
#define _In_
Definition: ms_sal.h:308
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:239
#define L(x)
Definition: ntvdm.h:50
#define STATUS_SUCCESS
Definition: shellext.h:65
base of all file and directory entries
Definition: entries.h:83
LIST_ENTRY ListHead
Definition: blkalloc.c:21
BL_MEMORY_TYPE Type
Definition: blkalloc.c:23
Definition: blkalloc.c:34
LIST_ENTRY ListEntry
Definition: blkalloc.c:35
ULONG Todo
Definition: blkalloc.c:36
Definition: typedefs.h:120
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
_IRQL_requires_same_ _In_opt_ PVOID Argument1
Definition: cmtypes.h:696