2 * PROJECT: ReactOS Spooler Router
3 * LICENSE: GNU LGPL v2.1 or any later version as published by the Free Software Foundation
4 * PURPOSE: Functions for allocating and freeing memory
5 * COPYRIGHT: Copyright 2015 Colin Finck <colin@reactos.org>
14 * Allocates memory for a Unicode string and copies the input string into it.
15 * Equivalent of wcsdup, but the returned buffer is allocated from the spooler heap and must be freed with DllFreeSplStr.
18 * The input string to copy
21 * Pointer to the copied string or NULL if no memory could be allocated.
24 AllocSplStr(PCWSTR pwszInput
)
33 // Get the length of the input string.
34 cbInput
= (wcslen(pwszInput
) + 1) * sizeof(WCHAR
);
36 // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
37 pwszOutput
= HeapAlloc(hProcessHeap
, 0, cbInput
);
40 ERR("HeapAlloc failed with error %lu!\n", GetLastError());
44 // Copy the string and return it.
45 CopyMemory(pwszOutput
, pwszInput
, cbInput
);
50 * @name DllAllocSplMem
52 * Allocate a block of zeroed memory.
53 * Windows allocates from a separate spooler heap here while we just use the process heap.
56 * Number of bytes to allocate.
59 * A pointer to the allocated memory or NULL in case of an error.
60 * You have to free this memory using DllFreeSplMem.
63 DllAllocSplMem(DWORD dwBytes
)
65 return HeapAlloc(hProcessHeap
, HEAP_ZERO_MEMORY
, dwBytes
);
71 * Frees the memory allocated with DllAllocSplMem.
74 * Pointer to the allocated memory.
77 * TRUE in case of success, FALSE otherwise.
80 DllFreeSplMem(PVOID pMem
)
82 return HeapFree(hProcessHeap
, 0, pMem
);
88 * Frees the string allocated with AllocSplStr.
91 * Pointer to the allocated string.
94 * TRUE in case of success, FALSE otherwise.
97 DllFreeSplStr(PWSTR pwszString
)
99 return HeapFree(hProcessHeap
, 0, pwszString
);
103 * @name ReallocSplMem
105 * Allocates a new block of memory and copies the contents of the old block into the new one.
108 * Pointer to the old block of memory.
109 * If this parameter is NULL, ReallocSplMem behaves exactly like DllAllocSplMem.
112 * Number of bytes to copy from the old block into the new one.
115 * Number of bytes to allocate for the new block.
118 * A pointer to the allocated new block or NULL in case of an error.
119 * You have to free this memory using DllFreeSplMem.
122 ReallocSplMem(PVOID pOldMem
, DWORD cbOld
, DWORD cbNew
)
126 // Always allocate the new block of memory.
127 pNewMem
= DllAllocSplMem(cbNew
);
130 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
134 // Copy the old memory into the new block and free it.
137 CopyMemory(pNewMem
, pOldMem
, min(cbOld
, cbNew
));
138 DllFreeSplMem(pOldMem
);
145 * @name ReallocSplStr
147 * Frees a string allocated by AllocSplStr and copies the given Unicode string into a newly allocated block of memory.
150 * Pointer to the string pointer allocated by AllocSplStr.
151 * When the function returns, the variable receives the pointer to the copied string.
154 * The Unicode string to copy into the new block of memory.
157 * Returns TRUE in any case.
160 ReallocSplStr(PWSTR
* ppwszString
, PCWSTR pwszInput
)
163 DllFreeSplStr(*ppwszString
);
165 *ppwszString
= AllocSplStr(pwszInput
);