ReactOS  0.4.14-dev-376-gaedba84
memory.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS Spooler Router
3  * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE: Functions for allocating and freeing memory
5  * COPYRIGHT: Copyright 2015-2017 Colin Finck (colin@reactos.org)
6  */
7 
8 #include "precomp.h"
9 
10 
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 }
42 
56 AllocSplStr(PCWSTR pwszInput)
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 }
80 
96 {
97  return HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, dwBytes);
98 }
99 
111 BOOL WINAPI
113 {
114  return HeapFree(hProcessHeap, 0, pMem);
115 }
116 
128 BOOL WINAPI
129 DllFreeSplStr(PWSTR pwszString)
130 {
131  return HeapFree(hProcessHeap, 0, pwszString);
132 }
133 
154 ReallocSplMem(PVOID pOldMem, DWORD cbOld, DWORD cbNew)
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 }
175 
191 BOOL WINAPI
192 ReallocSplStr(PWSTR* ppwszString, PCWSTR pwszInput)
193 {
194  if (*ppwszString)
195  DllFreeSplStr(*ppwszString);
196 
197  *ppwszString = AllocSplStr(pwszInput);
198 
199  return TRUE;
200 }
201 
236 UndoAlignRpcPtr(PVOID pDestinationBuffer, PVOID pSourceBuffer, DWORD cbBuffer, PDWORD pcbNeeded)
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 }
const uint16_t * PCWSTR
Definition: typedefs.h:55
PWSTR WINAPI AllocSplStr(PCWSTR pwszInput)
Definition: memory.c:56
#define TRUE
Definition: types.h:120
uint16_t * PWSTR
Definition: typedefs.h:54
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
#define DWORD
Definition: nt_native.h:44
uint32_t ULONG_PTR
Definition: typedefs.h:63
BOOL WINAPI ReallocSplStr(PWSTR *ppwszString, PCWSTR pwszInput)
Definition: memory.c:192
unsigned int BOOL
Definition: ntddk_ex.h:94
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define WINAPI
Definition: msvc.h:8
#define CopyMemory
Definition: winbase.h:1640
unsigned long DWORD
Definition: ntddk_ex.h:95
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define ERR(fmt,...)
Definition: debug.h:109
PVOID WINAPI AlignRpcPtr(PVOID pBuffer, PDWORD pcbBuffer)
Definition: memory.c:29
_In_ DWORD _Out_ PDWORD pcbNeeded
Definition: winddi.h:3827
PDWORD WINAPI UndoAlignRpcPtr(PVOID pDestinationBuffer, PVOID pSourceBuffer, DWORD cbBuffer, PDWORD pcbNeeded)
Definition: memory.c:236
#define min(a, b)
Definition: monoChain.cc:55
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
DWORD * PDWORD
Definition: pedump.c:68
BOOL WINAPI DllFreeSplStr(PWSTR pwszString)
Definition: memory.c:129
PVOID WINAPI ReallocSplMem(PVOID pOldMem, DWORD cbOld, DWORD cbNew)
Definition: memory.c:154
PVOID WINAPI DllAllocSplMem(DWORD dwBytes)
Definition: memory.c:95
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:402
HANDLE hProcessHeap
Definition: kbswitch.c:25