ReactOS 0.4.16-dev-297-gc569aee
memory.c File Reference
#include "precomp.h"
Include dependency graph for memory.c:

Go to the source code of this file.

Functions

AlignRpcPtr

Checks if the input buffer and buffer size are 4-byte aligned. If the buffer size is not 4-byte aligned, it is aligned down. If the input buffer is not 4-byte aligned, a 4-byte aligned buffer of the aligned down buffer size is allocated and returned.

Parameters
pBufferThe buffer to check.
pcbBufferPointer to the buffer size to check. Its value is aligned down if needed.
Returns
pBuffer if pBuffer is already 4-byte aligned, or a newly allocated 4-byte aligned buffer of the aligned down buffer size otherwise. If a buffer was allocated, you have to free it using UndoAlignRpcPtr.
PVOID WINAPI AlignRpcPtr (PVOID pBuffer, PDWORD pcbBuffer)
 
AllocSplStr

Allocates memory for a Unicode string and copies the input string into it. Equivalent of wcsdup, but the returned buffer is allocated from the spooler heap and must be freed with DllFreeSplStr.

Parameters
pwszInputThe input string to copy
Returns
Pointer to the copied string or NULL if no memory could be allocated.
PWSTR WINAPI AllocSplStr (PCWSTR pwszInput)
 
DllAllocSplMem

Allocate a block of zeroed memory. Windows allocates from a separate spooler heap here while we just use the process heap.

Parameters
dwBytesNumber of bytes to allocate.
Returns
A pointer to the allocated memory or NULL in case of an error. You have to free this memory using DllFreeSplMem.
PVOID WINAPI DllAllocSplMem (DWORD dwBytes)
 
DllFreeSplMem

Frees the memory allocated with DllAllocSplMem.

Parameters
pMemPointer to the allocated memory.
Returns
TRUE in case of success, FALSE otherwise.
BOOL WINAPI DllFreeSplMem (PVOID pMem)
 
DllFreeSplStr

Frees the string allocated with AllocSplStr.

Parameters
pwszStringPointer to the allocated string.
Returns
TRUE in case of success, FALSE otherwise.
BOOL WINAPI DllFreeSplStr (PWSTR pwszString)
 
ReallocSplMem

Allocates a new block of memory and copies the contents of the old block into the new one.

Parameters
pOldMemPointer to the old block of memory. If this parameter is NULL, ReallocSplMem behaves exactly like DllAllocSplMem.
cbOldNumber of bytes to copy from the old block into the new one.
cbNewNumber of bytes to allocate for the new block.
Returns
A pointer to the allocated new block or NULL in case of an error. You have to free this memory using DllFreeSplMem.
PVOID WINAPI ReallocSplMem (PVOID pOldMem, DWORD cbOld, DWORD cbNew)
 
ReallocSplStr

Frees a string allocated by AllocSplStr and copies the given Unicode string into a newly allocated block of memory.

Parameters
ppwszStringPointer to the string pointer allocated by AllocSplStr. When the function returns, the variable receives the pointer to the copied string.
pwszInputThe Unicode string to copy into the new block of memory.
Returns
Returns TRUE in any case.
BOOL WINAPI ReallocSplStr (PWSTR *ppwszString, PCWSTR pwszInput)
 
UndoAlignRpcPtr

Copies the data from the aligned buffer previously allocated by AlignRpcPtr back to the original unaligned buffer. The aligned buffer is freed.

Also aligns up the returned required buffer size of a function to a 4-byte boundary.

Parameters
pDestinationBufferThe original unaligned buffer, which you input as pBuffer to AlignRpcPtr. The data from pSourceBuffer is copied into this buffer before pSourceBuffer is freed. If AlignRpcPtr did not allocate a buffer, pDestinationBuffer equals pSourceBuffer and no memory is copied or freed. This parameter may be NULL if pSourceBuffer is NULL or cbBuffer is 0.
pSourceBufferThe aligned buffer, which is returned by AlignRpcPtr. Its data is copied into pDestinationBuffer and then pSourceBuffer is freed. If AlignRpcPtr did not allocate a buffer, pDestinationBuffer equals pSourceBuffer and no memory is copied or freed. This parameter may be NULL.
cbBufferNumber of bytes to copy. Set this to the size returned by AlignRpcPtr's pcbBuffer or less.
pcbNeededLet this parameter point to your variable calculating the needed bytes for a buffer and returning this value to the user. It is then aligned up to a 4-byte boundary, so that the user supplies a large enough buffer in the next call. Otherwise, AlignRpcPtr would align down the buffer size in the next call and your buffer would be smaller than intended. This parameter may be NULL.
Returns
pcbNeeded
PDWORD WINAPI UndoAlignRpcPtr (PVOID pDestinationBuffer, PVOID pSourceBuffer, DWORD cbBuffer, PDWORD pcbNeeded)
 

Function Documentation

◆ AlignRpcPtr()

PVOID WINAPI AlignRpcPtr ( PVOID  pBuffer,
PDWORD  pcbBuffer 
)

Definition at line 29 of file memory.c.

30{
31 ASSERT(pcbBuffer);
32
33 // Align down the buffer size in pcbBuffer to a 4-byte boundary.
34 *pcbBuffer -= *pcbBuffer % sizeof(DWORD);
35
36 // Check if pBuffer is 4-byte aligned. If not, allocate a 4-byte aligned buffer.
37 if ((ULONG_PTR)pBuffer % sizeof(DWORD))
38 pBuffer = DllAllocSplMem(*pcbBuffer);
39
40 return pBuffer;
41}
#define ASSERT(a)
Definition: mode.c:44
#define DWORD
Definition: nt_native.h:44
PVOID pBuffer
uint32_t ULONG_PTR
Definition: typedefs.h:65
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95

Referenced by _RpcAddJob(), _RpcEnumForms(), _RpcEnumJobs(), _RpcEnumMonitors(), _RpcEnumPorts(), _RpcEnumPrinterDrivers(), _RpcEnumPrinters(), _RpcEnumPrintProcessorDatatypes(), _RpcEnumPrintProcessors(), _RpcGetForm(), _RpcGetJob(), _RpcGetPrinter(), _RpcGetPrinterDriver(), _RpcGetPrinterDriver2(), START_TEST(), and YGetPrinterDriver2().

◆ AllocSplStr()

PWSTR WINAPI AllocSplStr ( PCWSTR  pwszInput)

Definition at line 56 of file memory.c.

57{
58 DWORD cbInput;
59 PWSTR pwszOutput;
60
61 // Sanity check
62 if (!pwszInput)
63 return NULL;
64
65 // Get the length of the input string.
66 cbInput = (wcslen(pwszInput) + 1) * sizeof(WCHAR);
67
68 // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
69 pwszOutput = HeapAlloc(hProcessHeap, 0, cbInput);
70 if (!pwszOutput)
71 {
72 ERR("HeapAlloc failed!\n");
73 return NULL;
74 }
75
76 // Copy the string and return it.
77 CopyMemory(pwszOutput, pwszInput, cbInput);
78 return pwszOutput;
79}
#define ERR(fmt,...)
Definition: precomp.h:57
#define NULL
Definition: types.h:112
#define HeapAlloc
Definition: compat.h:733
unsigned long DWORD
Definition: ntddk_ex.h:95
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HANDLE hProcessHeap
Definition: kbswitch.c:37
uint16_t * PWSTR
Definition: typedefs.h:56
#define CopyMemory
Definition: winbase.h:1735
__wchar_t WCHAR
Definition: xmlstorage.h:180

Referenced by _CreateNonspooledPort(), _LocalOpenPrinterHandle(), AddPrintMonitorList(), CreateJob(), CreatePrinterFriendlyName(), InitializePrinterList(), OpenPrintProcessor(), ReadJobShadowFile(), ReallocSplStr(), and START_TEST().

◆ DllAllocSplMem()

PVOID WINAPI DllAllocSplMem ( DWORD  dwBytes)

◆ DllFreeSplMem()

BOOL WINAPI DllFreeSplMem ( PVOID  pMem)

Definition at line 112 of file memory.c.

113{
114 if ( !pMem ) return TRUE;
115 return HeapFree(hProcessHeap, 0, pMem);
116}
#define TRUE
Definition: types.h:120
#define HeapFree(x, y, z)
Definition: compat.h:735

Referenced by _AddPrintProviderToList(), _ClosePortHandles(), _CreateNonspooledPort(), _HandleDeletePort(), _HandleSetDefaultCommConfig(), _InitializePrintProviderList(), _LocalClosePrinterHandle(), _LocalGetPrinterHandleData(), _LocalGetPrinterLevel0(), _LocalGetPrinterLevel1(), _LocalGetPrinterLevel2(), _LocalGetPrinterLevel4(), _LocalGetPrinterLevel5(), _LocalOpenPortHandle(), _LocalOpenPrinterHandle(), _LocalOpenXcvHandle(), _LocalSetPrinterHandleData(), _OpenEnvironment(), _RpcAddPrinterDriver(), _RpcAddPrinterDriverEx(), AddPrintMonitorList(), AllocAndRegQueryWSZ(), ClosePrinter(), ClosePrintProcessor(), ConstructXcvName(), CreateJob(), CreatePrinterFriendlyName(), CreateUIUserData(), DestroyUIUserData(), DoesPortExist(), FreeJob(), FreeMonitorUI(), GetMonitorUI(), InitializeGlobalJobList(), InitializePortList(), InitializePrinterJobList(), InitializePrinterList(), InitializePrintMonitor2(), InitializePrintMonitorList(), InitializePrintProcessorList(), LocalAddPort(), LocalClosePrinter(), LocalDeleteForm(), LocalDeleteMonitor(), LocalDeletePort(), LocalEnumPrintProcessors(), LocalmonClosePort(), LocalmonDeletePort(), LocalmonShutdown(), LocalmonXcvClosePort(), LocalOpenPrinter(), OpenPrintProcessor(), PrintingThreadProc(), PrintRawJob(), ReadJobShadowFile(), ReallocSplMem(), START_TEST(), UndoAlignRpcPtr(), and WriteJobShadowFile().

◆ DllFreeSplStr()

BOOL WINAPI DllFreeSplStr ( PWSTR  pwszString)

◆ ReallocSplMem()

PVOID WINAPI ReallocSplMem ( PVOID  pOldMem,
DWORD  cbOld,
DWORD  cbNew 
)

Definition at line 157 of file memory.c.

158{
159 PVOID pNewMem;
160
161 // Always allocate the new block of memory.
162 pNewMem = DllAllocSplMem(cbNew);
163 if (!pNewMem)
164 {
165 ERR("DllAllocSplMem failed!\n");
166 return NULL;
167 }
168
169 // Copy the old memory into the new block and free it.
170 if (pOldMem)
171 {
172 CopyMemory(pNewMem, pOldMem, min(cbOld, cbNew));
173 DllFreeSplMem(pOldMem);
174 }
175
176 return pNewMem;
177}
#define min(a, b)
Definition: monoChain.cc:55
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112

◆ ReallocSplStr()

BOOL WINAPI ReallocSplStr ( PWSTR ppwszString,
PCWSTR  pwszInput 
)

Definition at line 195 of file memory.c.

196{
197 if (*ppwszString)
198 DllFreeSplStr(*ppwszString);
199
200 *ppwszString = AllocSplStr(pwszInput);
201
202 return TRUE;
203}
BOOL WINAPI DllFreeSplStr(PWSTR pwszString)
Definition: memory.c:130
PWSTR WINAPI AllocSplStr(PCWSTR pwszInput)
Definition: memory.c:56

Referenced by _LocalSetJobLevel1(), _LocalSetJobLevel2(), LocalStartDocPrinter(), and START_TEST().

◆ UndoAlignRpcPtr()

PDWORD WINAPI UndoAlignRpcPtr ( PVOID  pDestinationBuffer,
PVOID  pSourceBuffer,
DWORD  cbBuffer,
PDWORD  pcbNeeded 
)

Definition at line 239 of file memory.c.

240{
241 // pDestinationBuffer is accessed unless pSourceBuffer equals pDestinationBuffer or cbBuffer is 0.
242 ASSERT(pDestinationBuffer || pSourceBuffer == pDestinationBuffer || cbBuffer == 0);
243
244 // If pSourceBuffer is given, and source and destination pointers don't match,
245 // we assume that pSourceBuffer is the buffer allocated by AlignRpcPtr.
246 if (pSourceBuffer && pSourceBuffer != pDestinationBuffer)
247 {
248 // Copy back the buffer data to the (usually unaligned) destination buffer
249 // and free the buffer allocated by AlignRpcPtr.
250 CopyMemory(pDestinationBuffer, pSourceBuffer, cbBuffer);
251 DllFreeSplMem(pSourceBuffer);
252 }
253
254 // If pcbNeeded is given, align it up to a 4-byte boundary.
255 if (pcbNeeded && *pcbNeeded % sizeof(DWORD))
256 *pcbNeeded += sizeof(DWORD) - *pcbNeeded % sizeof(DWORD);
257
258 return pcbNeeded;
259}
_In_ DWORD _Out_ PDWORD pcbNeeded
Definition: winddi.h:3828

Referenced by _RpcAddJob(), _RpcEnumForms(), _RpcEnumJobs(), _RpcEnumMonitors(), _RpcEnumPorts(), _RpcEnumPrinterDrivers(), _RpcEnumPrinters(), _RpcEnumPrintProcessorDatatypes(), _RpcEnumPrintProcessors(), _RpcGetForm(), _RpcGetJob(), _RpcGetPrinter(), _RpcGetPrinterDriver(), _RpcGetPrinterDriver2(), START_TEST(), and YGetPrinterDriver2().