ReactOS  0.4.14-dev-49-gfb4591c
blkalloc.c File Reference
#include "bl.h"
Include dependency graph for blkalloc.c:

Go to the source code of this file.

Classes

struct  _BL_BLOCK_DESCRIPTOR
 
struct  _BL_BLOCK_ENTRY
 

Typedefs

typedef struct _BL_BLOCK_DESCRIPTOR BL_BLOCK_DESCRIPTOR
 
typedef struct _BL_BLOCK_DESCRIPTORPBL_BLOCK_DESCRIPTOR
 
typedef struct _BL_BLOCK_ENTRY BL_BLOCK_ENTRY
 
typedef struct _BL_BLOCK_ENTRYPBL_BLOCK_ENTRY
 

Functions

BOOLEAN MmBapCompareBlockAllocatorTableEntry (_In_ PVOID Entry, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
 
PBL_BLOCK_DESCRIPTOR MmBapFindBlockInformation (ULONG BlockId)
 
NTSTATUS MmBapFreeBlockAllocatorDescriptor (_In_ PBL_BLOCK_DESCRIPTOR BlockInfo, _In_ PBL_BLOCK_ENTRY BlockEntry)
 
NTSTATUS BlpMmDeleteBlockAllocator (_In_ ULONG BlockId)
 
NTSTATUS MmBapFreeBlockAllocatorTableEntry (_In_ PVOID Entry, _In_ ULONG Index)
 
NTSTATUS MmBapPurgeBlockAllocatorTableEntry (_In_ PVOID Entry)
 
NTSTATUS BlpMmCreateBlockAllocator (VOID)
 
NTSTATUS MmBaInitialize (VOID)
 

Variables

PVOIDMmBlockAllocatorTable
 
ULONG MmBlockAllocatorTableEntries
 
BOOLEAN MmBlockAllocatorInitialized
 

Typedef Documentation

◆ BL_BLOCK_DESCRIPTOR

◆ BL_BLOCK_ENTRY

◆ PBL_BLOCK_DESCRIPTOR

◆ PBL_BLOCK_ENTRY

Function Documentation

◆ BlpMmCreateBlockAllocator()

NTSTATUS BlpMmCreateBlockAllocator ( VOID  )

Definition at line 213 of file blkalloc.c.

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 
259 Quickie:
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 }
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN MmBlockAllocatorInitialized
Definition: blkalloc.c:17
NTSTATUS BlMmFreeHeap(_In_ PVOID Buffer)
Definition: heapalloc.c:663
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
ULONG MmBlockAllocatorTableEntries
Definition: blkalloc.c:16
PVOID BlMmAllocateHeap(_In_ SIZE_T Size)
Definition: heapalloc.c:569
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
LIST_ENTRY ListHead
Definition: blkalloc.c:21
NTSTATUS MmBapPurgeBlockAllocatorTableEntry(_In_ PVOID Entry)
Definition: blkalloc.c:188
#define PAGE_SIZE
Definition: env_spec_w32.h:49
Status
Definition: gdiplustypes.h:24
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
NTSTATUS BlTblSetEntry(_Inout_ PVOID **Table, _Inout_ PULONG Count, _In_ PVOID Entry, _Out_ PULONG EntryIndex, _In_ PBL_TBL_SET_ROUTINE Callback)
Definition: util.c:321
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
BL_MEMORY_TYPE Type
Definition: blkalloc.c:23

Referenced by BcInitialize().

◆ BlpMmDeleteBlockAllocator()

NTSTATUS BlpMmDeleteBlockAllocator ( _In_ ULONG  BlockId)

Definition at line 88 of file blkalloc.c.

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 }
NTSTATUS MmBapFreeBlockAllocatorDescriptor(_In_ PBL_BLOCK_DESCRIPTOR BlockInfo, _In_ PBL_BLOCK_ENTRY BlockEntry)
Definition: blkalloc.c:77
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN MmBlockAllocatorInitialized
Definition: blkalloc.c:17
PBL_BLOCK_DESCRIPTOR MmBapFindBlockInformation(ULONG BlockId)
Definition: blkalloc.c:58
PFLT_MESSAGE_WAITER_QUEUE CONTAINING_RECORD(Csq, DEVICE_EXTENSION, IrpQueue)) -> WaiterQ.mLock) _IRQL_raises_(DISPATCH_LEVEL) VOID NTAPI FltpAcquireMessageWaiterLock(_In_ PIO_CSQ Csq, _Out_ PKIRQL Irql)
Definition: Messaging.c:560
Definition: blkalloc.c:33
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
LIST_ENTRY ListHead
Definition: blkalloc.c:21
Definition: typedefs.h:117
Status
Definition: gdiplustypes.h:24
return STATUS_SUCCESS
Definition: btrfs.c:2966

Referenced by BcInitialize(), and MmBapFreeBlockAllocatorTableEntry().

◆ MmBaInitialize()

NTSTATUS MmBaInitialize ( VOID  )

Definition at line 271 of file blkalloc.c.

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 }
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN MmBlockAllocatorInitialized
Definition: blkalloc.c:17
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
ULONG MmBlockAllocatorTableEntries
Definition: blkalloc.c:16
PVOID BlMmAllocateHeap(_In_ SIZE_T Size)
Definition: heapalloc.c:569
struct _BL_BLOCK_DESCRIPTOR BL_BLOCK_DESCRIPTOR
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
#define STATUS_NO_MEMORY
Definition: ntstatus.h:246
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
return STATUS_SUCCESS
Definition: btrfs.c:2966

Referenced by BlpMmInitialize().

◆ MmBapCompareBlockAllocatorTableEntry()

BOOLEAN MmBapCompareBlockAllocatorTableEntry ( _In_ PVOID  Entry,
_In_ PVOID  Argument1,
_In_ PVOID  Argument2,
_In_ PVOID  Argument3,
_In_ PVOID  Argument4 
)

Definition at line 42 of file blkalloc.c.

49 {
51  ULONG BlockId = PtrToUlong(Argument1);
52 
53  /* Check if the block ID matches */
54  return BlockInfo->BlockId == BlockId;
55 }
_IRQL_requires_same_ _In_opt_ PVOID Argument1
Definition: cmtypes.h:694
struct _BL_BLOCK_DESCRIPTOR * PBL_BLOCK_DESCRIPTOR
#define PtrToUlong(u)
Definition: config.h:107
unsigned int ULONG
Definition: retypes.h:1
base of all file and directory entries
Definition: entries.h:82

Referenced by MmBapFindBlockInformation().

◆ MmBapFindBlockInformation()

PBL_BLOCK_DESCRIPTOR MmBapFindBlockInformation ( ULONG  BlockId)

Definition at line 58 of file blkalloc.c.

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 }
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
ULONG MmBlockAllocatorTableEntries
Definition: blkalloc.c:16
smooth NULL
Definition: ftsmooth.c:416
#define UlongToPtr(u)
Definition: config.h:106
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
BOOLEAN MmBapCompareBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ PVOID Argument1, _In_ PVOID Argument2, _In_ PVOID Argument3, _In_ PVOID Argument4)
Definition: blkalloc.c:42
unsigned int ULONG
Definition: retypes.h:1

Referenced by BlpMmDeleteBlockAllocator().

◆ MmBapFreeBlockAllocatorDescriptor()

NTSTATUS MmBapFreeBlockAllocatorDescriptor ( _In_ PBL_BLOCK_DESCRIPTOR  BlockInfo,
_In_ PBL_BLOCK_ENTRY  BlockEntry 
)

Definition at line 77 of file blkalloc.c.

81 {
82  /* @TODO FIXME: Later */
83  EfiPrintf(L"Block free not yet implemented\r\n");
85 }
#define STATUS_NOT_IMPLEMENTED
Definition: ntstatus.h:225
static const WCHAR L[]
Definition: oid.c:1250
VOID EfiPrintf(_In_ PWCHAR Format,...)
Definition: firmware.c:126

Referenced by BlpMmDeleteBlockAllocator().

◆ MmBapFreeBlockAllocatorTableEntry()

NTSTATUS MmBapFreeBlockAllocatorTableEntry ( _In_ PVOID  Entry,
_In_ ULONG  Index 
)

Definition at line 151 of file blkalloc.c.

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 }
struct _BL_BLOCK_DESCRIPTOR * PBL_BLOCK_DESCRIPTOR
LONG NTSTATUS
Definition: precomp.h:26
NTSTATUS BlMmFreeHeap(_In_ PVOID Buffer)
Definition: heapalloc.c:663
NTSTATUS BlpMmDeleteBlockAllocator(_In_ ULONG BlockId)
Definition: blkalloc.c:88
PVOID * MmBlockAllocatorTable
Definition: blkalloc.c:15
smooth NULL
Definition: ftsmooth.c:416
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
static const UCHAR Index[8]
Definition: usbohci.c:18
Status
Definition: gdiplustypes.h:24
return STATUS_SUCCESS
Definition: btrfs.c:2966
base of all file and directory entries
Definition: entries.h:82

Referenced by MmBapPurgeBlockAllocatorTableEntry().

◆ MmBapPurgeBlockAllocatorTableEntry()

NTSTATUS MmBapPurgeBlockAllocatorTableEntry ( _In_ PVOID  Entry)

Definition at line 188 of file blkalloc.c.

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 }
struct _BL_BLOCK_DESCRIPTOR * PBL_BLOCK_DESCRIPTOR
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_UNSUCCESSFUL
Definition: udferr_usr.h:132
Status
Definition: gdiplustypes.h:24
NTSTATUS MmBapFreeBlockAllocatorTableEntry(_In_ PVOID Entry, _In_ ULONG Index)
Definition: blkalloc.c:151
base of all file and directory entries
Definition: entries.h:82

Referenced by BlpMmCreateBlockAllocator().

Variable Documentation

◆ MmBlockAllocatorInitialized

BOOLEAN MmBlockAllocatorInitialized

Definition at line 17 of file blkalloc.c.

Referenced by BlpMmCreateBlockAllocator(), BlpMmDeleteBlockAllocator(), and MmBaInitialize().

◆ MmBlockAllocatorTable

◆ MmBlockAllocatorTableEntries

ULONG MmBlockAllocatorTableEntries

Definition at line 16 of file blkalloc.c.

Referenced by BlpMmCreateBlockAllocator(), MmBaInitialize(), and MmBapFindBlockInformation().