ReactOS  0.4.15-dev-1377-ga59cecd
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  if ( !pMem ) return TRUE;
115  return HeapFree(hProcessHeap, 0, pMem);
116 }
117 
129 BOOL WINAPI
130 DllFreeSplStr(PWSTR pwszString)
131 {
132  if ( pwszString )
133  return HeapFree(hProcessHeap, 0, pwszString);
134  return FALSE;
135 }
136 
157 ReallocSplMem(PVOID pOldMem, DWORD cbOld, DWORD cbNew)
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 }
178 
194 BOOL WINAPI
195 ReallocSplStr(PWSTR* ppwszString, PCWSTR pwszInput)
196 {
197  if (*ppwszString)
198  DllFreeSplStr(*ppwszString);
199 
200  *ppwszString = AllocSplStr(pwszInput);
201 
202  return TRUE;
203 }
204 
239 UndoAlignRpcPtr(PVOID pDestinationBuffer, PVOID pSourceBuffer, DWORD cbBuffer, PDWORD pcbNeeded)
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 }
const uint16_t * PCWSTR
Definition: typedefs.h:57
PWSTR WINAPI AllocSplStr(PCWSTR pwszInput)
Definition: memory.c:56
#define TRUE
Definition: types.h:120
uint16_t * PWSTR
Definition: typedefs.h:56
BOOL WINAPI DllFreeSplMem(PVOID pMem)
Definition: memory.c:112
#define DWORD
Definition: nt_native.h:44
uint32_t ULONG_PTR
Definition: typedefs.h:65
BOOL WINAPI ReallocSplStr(PWSTR *ppwszString, PCWSTR pwszInput)
Definition: memory.c:195
#define FALSE
Definition: types.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
PVOID pBuffer
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define ASSERT(a)
Definition: mode.c:45
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define WINAPI
Definition: msvc.h:6
#define CopyMemory
Definition: winbase.h:1646
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ERR(fmt,...)
Definition: debug.h:110
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:239
#define min(a, b)
Definition: monoChain.cc:55
#define NULL
Definition: types.h:112
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
DWORD * PDWORD
Definition: pedump.c:68
BOOL WINAPI DllFreeSplStr(PWSTR pwszString)
Definition: memory.c:130
PVOID WINAPI ReallocSplMem(PVOID pOldMem, DWORD cbOld, DWORD cbNew)
Definition: memory.c:157
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:594
HANDLE hProcessHeap
Definition: kbswitch.c:25