ReactOS  0.4.14-dev-552-g2fad488
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 DWORD
Definition: nt_native.h:44
uint32_t ULONG_PTR
Definition: typedefs.h:63
PVOID pBuffer
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95

Referenced by _RpcAddJob(), _RpcEnumJobs(), _RpcEnumMonitors(), _RpcEnumPorts(), _RpcEnumPrinters(), _RpcEnumPrintProcessorDatatypes(), _RpcEnumPrintProcessors(), _RpcGetJob(), _RpcGetPrinter(), _RpcGetPrinterDriver(), and START_TEST().

◆ 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 }
uint16_t * PWSTR
Definition: typedefs.h:54
smooth NULL
Definition: ftsmooth.c:416
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define CopyMemory
Definition: winbase.h:1640
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERR(fmt,...)
Definition: debug.h:109
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
HANDLE hProcessHeap
Definition: kbswitch.c:25

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

◆ DllAllocSplMem()

◆ DllFreeSplMem()

◆ DllFreeSplStr()

BOOL WINAPI DllFreeSplStr ( PWSTR  pwszString)

Definition at line 129 of file memory.c.

130 {
131  return HeapFree(hProcessHeap, 0, pwszString);
132 }
#define HeapFree(x, y, z)
Definition: compat.h:402
HANDLE hProcessHeap
Definition: kbswitch.c:25

Referenced by _ClosePortHandles(), _CreateNonspooledPort(), _LocalClosePrinterHandle(), _LocalOpenPrinterHandle(), ClosePrintProcessor(), FreeJob(), InitializePrinterList(), InitializePrintProcessorList(), and ReallocSplStr().

◆ ReallocSplMem()

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

Definition at line 154 of file memory.c.

155 {
156  PVOID pNewMem;
157 
158  // Always allocate the new block of memory.
159  pNewMem = DllAllocSplMem(cbNew);
160  if (!pNewMem)
161  {
162  ERR("DllAllocSplMem failed!\n");
163  return NULL;
164  }
165 
166  // Copy the old memory into the new block and free it.
167  if (pOldMem)
168  {
169  CopyMemory(pNewMem, pOldMem, min(cbOld, cbNew));
170  DllFreeSplMem(pOldMem);
171  }
172 
173  return pNewMem;
174 }
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
smooth NULL
Definition: ftsmooth.c:416
#define CopyMemory
Definition: winbase.h:1640
#define ERR(fmt,...)
Definition: debug.h:109
#define min(a, b)
Definition: monoChain.cc:55
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95

◆ ReallocSplStr()

BOOL WINAPI ReallocSplStr ( PWSTR ppwszString,
PCWSTR  pwszInput 
)

Definition at line 192 of file memory.c.

193 {
194  if (*ppwszString)
195  DllFreeSplStr(*ppwszString);
196 
197  *ppwszString = AllocSplStr(pwszInput);
198 
199  return TRUE;
200 }
PWSTR WINAPI AllocSplStr(PCWSTR pwszInput)
Definition: memory.c:56
#define TRUE
Definition: types.h:120
BOOL WINAPI DllFreeSplStr(PWSTR pwszString)
Definition: memory.c:129

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

◆ UndoAlignRpcPtr()

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

Definition at line 236 of file memory.c.

237 {
238  // pDestinationBuffer is accessed unless pSourceBuffer equals pDestinationBuffer or cbBuffer is 0.
239  ASSERT(pDestinationBuffer || pSourceBuffer == pDestinationBuffer || cbBuffer == 0);
240 
241  // If pSourceBuffer is given, and source and destination pointers don't match,
242  // we assume that pSourceBuffer is the buffer allocated by AlignRpcPtr.
243  if (pSourceBuffer && pSourceBuffer != pDestinationBuffer)
244  {
245  // Copy back the buffer data to the (usually unaligned) destination buffer
246  // and free the buffer allocated by AlignRpcPtr.
247  CopyMemory(pDestinationBuffer, pSourceBuffer, cbBuffer);
248  DllFreeSplMem(pSourceBuffer);
249  }
250 
251  // If pcbNeeded is given, align it up to a 4-byte boundary.
252  if (pcbNeeded && *pcbNeeded % sizeof(DWORD))
253  *pcbNeeded += sizeof(DWORD) - *pcbNeeded % sizeof(DWORD);
254 
255  return pcbNeeded;
256 }
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
#define DWORD
Definition: nt_native.h:44
#define CopyMemory
Definition: winbase.h:1640
unsigned long DWORD
Definition: ntddk_ex.h:95
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
_In_ DWORD _Out_ PDWORD pcbNeeded
Definition: winddi.h:3827

Referenced by _RpcAddJob(), _RpcEnumJobs(), _RpcEnumMonitors(), _RpcEnumPorts(), _RpcEnumPrinters(), _RpcEnumPrintProcessorDatatypes(), _RpcEnumPrintProcessors(), _RpcGetJob(), _RpcGetPrinter(), _RpcGetPrinterDriver(), and START_TEST().