Start using import tables for dlls that can already support it.
[wine] / dlls / crtdll / memory.c
1 /*
2  * CRTDLL memory functions
3  * 
4  * Copyright 1996,1998 Marcus Meissner
5  * Copyright 1996 Jukka Iivonen
6  * Copyright 1997,2000 Uwe Bonnes
7  * Copyright 2000 Jon Griffiths
8  *
9  * Implementation Notes:
10  * MT Safe.
11  */
12
13 #include "crtdll.h"
14
15
16 DEFAULT_DEBUG_CHANNEL(crtdll);
17
18 static new_handler_type new_handler;
19
20
21 /*********************************************************************
22  *                  new           (CRTDLL.001)
23  *
24  * Allocate memory.
25  */
26 LPVOID __cdecl CRTDLL_new(DWORD size)
27 {
28     VOID* result;
29     if(!(result = HeapAlloc(GetProcessHeap(),0,size)) && new_handler)
30         (*new_handler)();
31     return result;
32 }
33
34
35 /*********************************************************************
36  *                  delete       (CRTDLL.002)
37  *
38  * Free memory created with new.
39  */
40 VOID __cdecl CRTDLL_delete(LPVOID ptr)
41 {
42     HeapFree(GetProcessHeap(),0,ptr);
43 }
44
45
46 /*********************************************************************
47  *                  set_new_handler(CRTDLL.003)
48  */
49 new_handler_type __cdecl CRTDLL_set_new_handler(new_handler_type func)
50 {
51     new_handler_type old_handler = new_handler;
52     new_handler = func;
53     return old_handler;
54 }
55
56
57 /*********************************************************************
58  *                  _expand        (CRTDLL.088)
59  *
60  * Increase the size of a block of memory allocated with malloc()
61  * or realloc().
62  */
63 LPVOID __cdecl CRTDLL__expand(LPVOID ptr, INT size)
64 {
65     return HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, ptr, size );
66 }
67
68
69 /*********************************************************************
70  *                  _msize     (CRTDLL.234)
71  *
72  * Return the actual used size of an allocated block of memory.
73  *
74  */
75 LONG __cdecl CRTDLL__msize(LPVOID mem)
76 {
77   LONG size = HeapSize(GetProcessHeap(),0,mem);
78   if (size == -1)
79   {
80     WARN(":Probably called with non wine-allocated memory, ret = -1\n");
81     /* At least the win98/nt crtdlls also return -1 in this case */
82   }
83   return size;
84 }
85
86
87 /*********************************************************************
88  *                  calloc        (CRTDLL.350)
89  *
90  * Allocate memory from the heap and initialise it to zero.
91  */
92 LPVOID __cdecl CRTDLL_calloc(DWORD size, DWORD count)
93 {
94     return HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, size * count );
95 }
96
97
98 /*********************************************************************
99  *                  free          (CRTDLL.375)
100  *
101  * Free a block of memory allocated with malloc()
102  */
103 VOID __cdecl CRTDLL_free(LPVOID ptr)
104 {
105     HeapFree(GetProcessHeap(),0,ptr);
106 }
107
108
109 /*********************************************************************
110  *                  malloc        (CRTDLL.424)
111  * 
112  * Alocate memory from the heap.
113  */
114 LPVOID __cdecl CRTDLL_malloc(DWORD size)
115 {
116   LPVOID ret = HeapAlloc(GetProcessHeap(),0,size);
117   if (!ret)
118     __CRTDLL__set_errno(GetLastError());
119   return ret;
120 }
121
122
123 /*********************************************************************
124  *                  realloc        (CRTDLL.444)
125  *
126  * Resize a block of memory allocated with malloc() or realloc().
127  */
128 LPVOID __cdecl CRTDLL_realloc( VOID *ptr, DWORD size )
129 {
130     return HeapReAlloc( GetProcessHeap(), 0, ptr, size );
131 }