Merge r67713 and r67936 (Eric's parport driver) from trunk
[reactos.git] / reactos / win32ss / printing / base / spoolss / memory.c
1 /*
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>
6 */
7
8 #include "precomp.h"
9
10
11 /**
12 * @name AllocSplStr
13 *
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.
16 *
17 * @param pwszInput
18 * The input string to copy
19 *
20 * @return
21 * Pointer to the copied string or NULL if no memory could be allocated.
22 */
23 PWSTR WINAPI
24 AllocSplStr(PCWSTR pwszInput)
25 {
26 DWORD cbInput;
27 PWSTR pwszOutput;
28
29 // Sanity check
30 if (!pwszInput)
31 return NULL;
32
33 // Get the length of the input string.
34 cbInput = (wcslen(pwszInput) + 1) * sizeof(WCHAR);
35
36 // Allocate it. We don't use DllAllocSplMem here, because it unnecessarily zeroes the memory.
37 pwszOutput = HeapAlloc(hProcessHeap, 0, cbInput);
38 if (!pwszOutput)
39 {
40 ERR("HeapAlloc failed with error %lu!\n", GetLastError());
41 return NULL;
42 }
43
44 // Copy the string and return it.
45 CopyMemory(pwszOutput, pwszInput, cbInput);
46 return pwszOutput;
47 }
48
49 /**
50 * @name DllAllocSplMem
51 *
52 * Allocate a block of zeroed memory.
53 * Windows allocates from a separate spooler heap here while we just use the process heap.
54 *
55 * @param dwBytes
56 * Number of bytes to allocate.
57 *
58 * @return
59 * A pointer to the allocated memory or NULL in case of an error.
60 * You have to free this memory using DllFreeSplMem.
61 */
62 PVOID WINAPI
63 DllAllocSplMem(DWORD dwBytes)
64 {
65 return HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, dwBytes);
66 }
67
68 /**
69 * @name DllFreeSplMem
70 *
71 * Frees the memory allocated with DllAllocSplMem.
72 *
73 * @param pMem
74 * Pointer to the allocated memory.
75 *
76 * @return
77 * TRUE in case of success, FALSE otherwise.
78 */
79 BOOL WINAPI
80 DllFreeSplMem(PVOID pMem)
81 {
82 return HeapFree(hProcessHeap, 0, pMem);
83 }
84
85 /**
86 * @name DllFreeSplStr
87 *
88 * Frees the string allocated with AllocSplStr.
89 *
90 * @param pwszString
91 * Pointer to the allocated string.
92 *
93 * @return
94 * TRUE in case of success, FALSE otherwise.
95 */
96 BOOL WINAPI
97 DllFreeSplStr(PWSTR pwszString)
98 {
99 return HeapFree(hProcessHeap, 0, pwszString);
100 }
101
102 /**
103 * @name ReallocSplMem
104 *
105 * Allocates a new block of memory and copies the contents of the old block into the new one.
106 *
107 * @param pOldMem
108 * Pointer to the old block of memory.
109 * If this parameter is NULL, ReallocSplMem behaves exactly like DllAllocSplMem.
110 *
111 * @param cbOld
112 * Number of bytes to copy from the old block into the new one.
113 *
114 * @param cbNew
115 * Number of bytes to allocate for the new block.
116 *
117 * @return
118 * A pointer to the allocated new block or NULL in case of an error.
119 * You have to free this memory using DllFreeSplMem.
120 */
121 PVOID WINAPI
122 ReallocSplMem(PVOID pOldMem, DWORD cbOld, DWORD cbNew)
123 {
124 PVOID pNewMem;
125
126 // Always allocate the new block of memory.
127 pNewMem = DllAllocSplMem(cbNew);
128 if (!pNewMem)
129 {
130 ERR("DllAllocSplMem failed with error %lu!\n", GetLastError());
131 return NULL;
132 }
133
134 // Copy the old memory into the new block and free it.
135 if (pOldMem)
136 {
137 CopyMemory(pNewMem, pOldMem, min(cbOld, cbNew));
138 DllFreeSplMem(pOldMem);
139 }
140
141 return pNewMem;
142 }
143
144 /**
145 * @name ReallocSplStr
146 *
147 * Frees a string allocated by AllocSplStr and copies the given Unicode string into a newly allocated block of memory.
148 *
149 * @param ppwszString
150 * Pointer to the string pointer allocated by AllocSplStr.
151 * When the function returns, the variable receives the pointer to the copied string.
152 *
153 * @param pwszInput
154 * The Unicode string to copy into the new block of memory.
155 *
156 * @return
157 * Returns TRUE in any case.
158 */
159 BOOL WINAPI
160 ReallocSplStr(PWSTR* ppwszString, PCWSTR pwszInput)
161 {
162 if (*ppwszString)
163 DllFreeSplStr(*ppwszString);
164
165 *ppwszString = AllocSplStr(pwszInput);
166
167 return TRUE;
168 }