Cleanup and improve the mbcs support.
[wine/wine64.git] / include / heap.h
blobb9fc6fcc643c052536255bb57d7d789d59968e5c
1 /*
2 * Win32 heap definitions
4 * Copyright 1996 Alexandre Julliard
5 */
7 #ifndef __WINE_HEAP_H
8 #define __WINE_HEAP_H
10 #include <string.h>
12 #include "winbase.h"
13 #include "winnls.h"
14 #include "wine/unicode.h"
15 #include "wine/windef16.h" /* for SEGPTR */
17 extern SEGPTR HEAP_GetSegptr( HANDLE heap, DWORD flags, LPCVOID ptr );
19 /* SEGPTR helper macros */
21 #define SEGPTR_ALLOC(size) \
22 (HeapAlloc( GetProcessHeap(), HEAP_WINE_SEGPTR, (size) ))
23 #define SEGPTR_NEW(type) \
24 ((type *)HeapAlloc( GetProcessHeap(), HEAP_WINE_SEGPTR, sizeof(type) ))
25 #define SEGPTR_STRDUP_WtoA(str) \
26 (HIWORD(str) ? HEAP_strdupWtoA( GetProcessHeap(), HEAP_WINE_SEGPTR, (str) ) : (LPSTR)(str))
27 #define SEGPTR_FREE(ptr) \
28 (HIWORD(ptr) ? HeapFree( GetProcessHeap(), HEAP_WINE_SEGPTR, (ptr) ) : 0)
29 #define SEGPTR_GET(ptr) MapLS(ptr)
31 inline static LPSTR SEGPTR_STRDUP( LPCSTR str )
33 if (HIWORD(str))
35 INT len = strlen(str) + 1;
36 LPSTR p = HeapAlloc( GetProcessHeap(), HEAP_WINE_SEGPTR, len );
37 if (p) memcpy( p, str, len );
38 return p;
40 return (LPSTR)str;
43 /* strdup macros */
44 /* DO NOT USE THEM!! they will go away soon */
46 inline static LPWSTR HEAP_strdupAtoW( HANDLE heap, DWORD flags, LPCSTR str )
48 LPWSTR ret;
49 INT len;
51 if (!str) return NULL;
52 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
53 ret = HeapAlloc( heap, flags, len * sizeof(WCHAR) );
54 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
55 return ret;
58 inline static LPSTR HEAP_strdupWtoA( HANDLE heap, DWORD flags, LPCWSTR str )
60 LPSTR ret;
61 INT len;
63 if (!str) return NULL;
64 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
65 ret = HeapAlloc( heap, flags, len );
66 if(ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
67 return ret;
70 #endif /* __WINE_HEAP_H */