@@ -23,9 +23,9 @@ char *JoinPath(const char *p1, const char *p2)
2323 if (* p2_start == PATH_SEPARATOR ) { p2_start ++ ; p2_len -- ; }
2424
2525 size_t joined_len = p1_len + 1 + p2_len ;
26- char * joined_path = (char * )LocalAlloc ( LPTR , joined_len + 1 );
27- if (joined_path == NULL ) {
28- APP_ERROR ("Failed to allocate buffer for join path (%lu)" , GetLastError () );
26+ char * joined_path = (char * )calloc ( 1 , joined_len + 1 );
27+ if (! joined_path ) {
28+ APP_ERROR ("Failed to allocate buffer for join path" );
2929 return NULL ;
3030 }
3131 memcpy (joined_path , p1 , p1_len );
@@ -55,9 +55,9 @@ bool CreateDirectoriesRecursively(const char *dir)
5555 }
5656
5757 size_t dir_len = strlen (dir );
58- char * path = (char * )LocalAlloc ( LPTR , dir_len + 1 );
59- if (path == NULL ) {
60- APP_ERROR ("LocalAlloc failed (%lu)" , GetLastError () );
58+ char * path = (char * )calloc ( 1 , dir_len + 1 );
59+ if (! path ) {
60+ APP_ERROR ("Failed to allocate memory" );
6161 return false;
6262 }
6363 strcpy (path , dir );
@@ -73,7 +73,7 @@ bool CreateDirectoriesRecursively(const char *dir)
7373 break ;
7474 } else {
7575 APP_ERROR ("Directory name conflicts with a file(%s)" , path );
76- LocalFree (path );
76+ free (path );
7777 return false;
7878 }
7979 } else {
@@ -82,7 +82,7 @@ bool CreateDirectoriesRecursively(const char *dir)
8282 continue ;
8383 } else {
8484 APP_ERROR ("Cannot access the directory (%lu)" , GetLastError ());
85- LocalFree (path );
85+ free (path );
8686 return false;
8787 }
8888 }
@@ -97,13 +97,13 @@ bool CreateDirectoriesRecursively(const char *dir)
9797
9898 if (!CreateDirectory (path , NULL ) && GetLastError () != ERROR_ALREADY_EXISTS ) {
9999 APP_ERROR ("Failed to create directory (%lu)" , GetLastError ());
100- LocalFree (path );
100+ free (path );
101101 return false;
102102 }
103103 }
104104 }
105105
106- LocalFree (path );
106+ free (path );
107107 return true;
108108}
109109
@@ -119,17 +119,17 @@ bool CreateParentDirectories(const char *file)
119119 for (; i > 0 ; i -- ) { if (file [i ] == PATH_SEPARATOR ) break ; }
120120 if (i == 0 ) { return true; }
121121
122- char * dir = (char * )LocalAlloc ( LPTR , i + 1 );
123- if (dir == NULL ) {
124- APP_ERROR ("LocalAlloc failed (%lu)" , GetLastError () );
122+ char * dir = (char * )calloc ( 1 , i + 1 );
123+ if (! dir ) {
124+ APP_ERROR ("Failed to allocate memory" );
125125 return false;
126126 }
127127
128128 strncpy (dir , file , i );
129129 dir [i ] = '\0' ;
130130 bool result = CreateDirectoriesRecursively (dir );
131131
132- LocalFree (dir );
132+ free (dir );
133133 return result ;
134134}
135135
@@ -170,11 +170,11 @@ bool DeleteRecursively(const char *path)
170170 }
171171 }
172172
173- LocalFree (subPath );
173+ free (subPath );
174174 } while (FindNextFile (handle , & findData ));
175175 FindClose (handle );
176176 }
177- LocalFree (findPath );
177+ free (findPath );
178178
179179 if (!RemoveDirectory (path )) {
180180 APP_ERROR ("Failed to delete directory (%lu)" , GetLastError ());
@@ -190,9 +190,9 @@ char *GenerateUniqueName(const char *prefix)
190190 size_t prefix_len = 0 ;
191191 if (prefix != NULL ) { prefix_len = strlen (prefix ); }
192192
193- char * name = (char * )LocalAlloc ( LPTR , prefix_len + UID_LENGTH + 1 );
194- if (name == NULL ) {
195- APP_ERROR ("Failed to allocate memory for unique name (%lu)" , GetLastError () );
193+ char * name = (char * )calloc ( 1 , prefix_len + UID_LENGTH + 1 );
194+ if (! name ) {
195+ APP_ERROR ("Failed to allocate memory for unique name" );
196196 return NULL ;
197197 }
198198
@@ -229,7 +229,7 @@ char *CreateUniqueDirectory(const char *base_path, const char *prefix)
229229 }
230230
231231 char * full_path = JoinPath (base_path , temp_name );
232- LocalFree (temp_name );
232+ free (temp_name );
233233 if (full_path == NULL ) {
234234 APP_ERROR ("Failed to construct a unique directory path" );
235235 return NULL ;
@@ -239,10 +239,10 @@ char *CreateUniqueDirectory(const char *base_path, const char *prefix)
239239 return full_path ;
240240 } else if (GetLastError () != ERROR_ALREADY_EXISTS ) {
241241 APP_ERROR ("Failed to create a unique directory (%lu)" , GetLastError ());
242- LocalFree (full_path );
242+ free (full_path );
243243 return NULL ;
244244 } else {
245- LocalFree (full_path );
245+ free (full_path );
246246 }
247247
248248 Sleep (10 ); // To avoid sequential generation and prevent name duplication.
@@ -262,41 +262,41 @@ char *GetImagePath(void)
262262 https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
263263 */
264264 DWORD buffer_size = 32767 ;
265- wchar_t * image_path_w = (wchar_t * )LocalAlloc ( LPTR , buffer_size * sizeof (wchar_t ));
266- if (image_path_w == NULL ) {
267- APP_ERROR ("Failed to allocate buffer for image path (%lu)" , GetLastError () );
265+ wchar_t * image_path_w = (wchar_t * )calloc ( 1 , buffer_size * sizeof (wchar_t ));
266+ if (! image_path_w ) {
267+ APP_ERROR ("Failed to allocate buffer for image path" );
268268 return NULL ;
269269 }
270270
271271 DWORD copied = GetModuleFileNameW (NULL , image_path_w , buffer_size );
272272 if (copied == 0 || GetLastError () == ERROR_INSUFFICIENT_BUFFER ) {
273273 APP_ERROR ("Failed to get image path (%lu)" , GetLastError ());
274- LocalFree (image_path_w );
274+ free (image_path_w );
275275 return NULL ;
276276 }
277277
278278 int utf8_size = WideCharToMultiByte (CP_UTF8 , 0 , image_path_w , -1 , NULL , 0 , NULL , NULL );
279279 if (utf8_size == 0 ) {
280280 APP_ERROR ("Failed to calculate buffer size for UTF-8 conversion (%lu)" , GetLastError ());
281- LocalFree (image_path_w );
281+ free (image_path_w );
282282 return NULL ;
283283 }
284284
285- char * image_path_utf8 = (char * )LocalAlloc ( LPTR , utf8_size );
286- if (image_path_utf8 == NULL ) {
287- APP_ERROR ("Failed to allocate buffer for UTF-8 image path (%lu)" , GetLastError () );
288- LocalFree (image_path_w );
285+ char * image_path_utf8 = (char * )calloc ( 1 , utf8_size );
286+ if (! image_path_utf8 ) {
287+ APP_ERROR ("Failed to allocate buffer for UTF-8 image path" );
288+ free (image_path_w );
289289 return NULL ;
290290 }
291291
292292 if (WideCharToMultiByte (CP_UTF8 , 0 , image_path_w , -1 , image_path_utf8 , utf8_size , NULL , NULL ) == 0 ) {
293293 APP_ERROR ("Failed to convert image path to UTF-8 (%lu)" , GetLastError ());
294- LocalFree (image_path_w );
295- LocalFree (image_path_utf8 );
294+ free (image_path_w );
295+ free (image_path_utf8 );
296296 return NULL ;
297297 }
298298
299- LocalFree (image_path_w );
299+ free (image_path_w );
300300 return image_path_utf8 ;
301301}
302302
@@ -318,7 +318,7 @@ char *GetImageDirectoryPath(void) {
318318
319319 if (i == 0 ) {
320320 APP_ERROR ("Executable path does not contain a directory" );
321- LocalFree (image_path );
321+ free (image_path );
322322 return NULL ;
323323 }
324324
@@ -328,16 +328,15 @@ char *GetImageDirectoryPath(void) {
328328// Retrieves the path to the temporary directory for the current user.
329329char * GetTempDirectoryPath (void )
330330{
331- char * temp_dir = (char * )LocalAlloc (LPTR , MAX_PATH );
332-
333- if (temp_dir == NULL ) {
334- APP_ERROR ("Failed to memory allocate for get temp directory (%lu)" , GetLastError ());
331+ char * temp_dir = (char * )calloc (1 , MAX_PATH );
332+ if (!temp_dir ) {
333+ APP_ERROR ("Failed to memory allocate for get temp directory" );
335334 return NULL ;
336335 }
337336
338337 if (!GetTempPath (MAX_PATH , temp_dir )) {
339338 APP_ERROR ("Failed to get temp path (%lu)" , GetLastError ());
340- LocalFree (temp_dir );
339+ free (temp_dir );
341340 return NULL ;
342341 }
343342
@@ -370,14 +369,14 @@ bool ChangeDirectoryToSafeDirectory(void)
370369{
371370 char * working_dir = GetTempDirectoryPath ();
372371 bool changed = working_dir && SetCurrentDirectory (working_dir );
373- LocalFree (working_dir );
372+ free (working_dir );
374373
375374 if (changed ) return true;
376375
377376 DEBUG ("Failed to change to temporary directory. Trying executable's directory" );
378377 working_dir = GetImageDirectoryPath ();
379378 changed = working_dir && SetCurrentDirectory (working_dir );
380- LocalFree (working_dir );
379+ free (working_dir );
381380
382381 if (!changed ) {
383382 APP_ERROR ("Failed to change to executable's directory" );
@@ -427,7 +426,7 @@ MappedFile OpenAndMapFile(const char *file_path, unsigned long long *file_size,
427426 return NULL ;
428427 }
429428
430- MappedFileHandle * handle = (MappedFileHandle * )LocalAlloc ( LPTR , sizeof (MappedFileHandle ));
429+ MappedFileHandle * handle = (MappedFileHandle * )calloc ( 1 , sizeof (MappedFileHandle ));
431430 if (handle ) {
432431 handle -> hFile = hFile ;
433432 handle -> hMapping = hMapping ;
@@ -437,7 +436,7 @@ MappedFile OpenAndMapFile(const char *file_path, unsigned long long *file_size,
437436 }
438437 return (MappedFile )handle ;
439438 } else {
440- APP_ERROR ("Failed to allocate memory for handle (%lu)" , GetLastError () );
439+ APP_ERROR ("Failed to allocate memory for handle" );
441440 UnmapViewOfFile (lpBaseAddress );
442441 CloseHandle (hMapping );
443442 CloseHandle (hFile );
@@ -472,7 +471,7 @@ bool FreeMappedFile(MappedFile handle) {
472471 }
473472 }
474473
475- LocalFree (h );
474+ free (h );
476475 }
477476
478477 return success ;
0 commit comments