a9ccba4a0031943e8c72f78f0839e60b87ceda2d
[reactos.git] / reactos / lib / crt / wine / heap.c
1 /*
2 * msvcrt.dll heap functions
3 *
4 * Copyright 2000 Jon Griffiths
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Note: Win32 heap operations are MT safe. We only lock the new
21 * handler and non atomic heap operations
22 */
23
24 #include "precomp.h"
25
26 #include <errno.h>
27 #include <malloc.h>
28 #include <stdlib.h>
29 #include <internal/mtdll.h>
30
31 #include "wine/debug.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
34
35 /* MT */
36 #define LOCK_HEAP _mlock( _HEAP_LOCK )
37 #define UNLOCK_HEAP _munlock( _HEAP_LOCK )
38
39
40 typedef void (*MSVCRT_new_handler_func)(unsigned long size);
41
42 static MSVCRT_new_handler_func MSVCRT_new_handler;
43 static int MSVCRT_new_mode;
44
45
46 /*********************************************************************
47 * ??2@YAPAXI@Z (MSVCRT.@)
48 */
49 void* MSVCRT_operator_new(unsigned long size)
50 {
51 void *retval = malloc(size);
52 TRACE("(%ld) returning %p\n", size, retval);
53 LOCK_HEAP;
54 if(!retval && MSVCRT_new_handler)
55 (*MSVCRT_new_handler)(size);
56 UNLOCK_HEAP;
57 return retval;
58 }
59
60 /*********************************************************************
61 * ??3@YAXPAX@Z (MSVCRT.@)
62 */
63 void MSVCRT_operator_delete(void *mem)
64 {
65 TRACE("(%p)\n", mem);
66 free(mem);
67 }
68
69
70 /*********************************************************************
71 * ?_query_new_handler@@YAP6AHI@ZXZ (MSVCRT.@)
72 */
73 MSVCRT_new_handler_func MSVCRT__query_new_handler(void)
74 {
75 return MSVCRT_new_handler;
76 }
77
78
79 /*********************************************************************
80 * ?_query_new_mode@@YAHXZ (MSVCRT.@)
81 */
82 int MSVCRT__query_new_mode(void)
83 {
84 return MSVCRT_new_mode;
85 }
86
87 /*********************************************************************
88 * ?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z (MSVCRT.@)
89 */
90 MSVCRT_new_handler_func MSVCRT__set_new_handler(MSVCRT_new_handler_func func)
91 {
92 MSVCRT_new_handler_func old_handler;
93 LOCK_HEAP;
94 old_handler = MSVCRT_new_handler;
95 MSVCRT_new_handler = func;
96 UNLOCK_HEAP;
97 return old_handler;
98 }
99
100 /*********************************************************************
101 * ?set_new_handler@@YAP6AXXZP6AXXZ@Z (MSVCRT.@)
102 */
103 MSVCRT_new_handler_func MSVCRT_set_new_handler(void *func)
104 {
105 TRACE("(%p)\n",func);
106 MSVCRT__set_new_handler(NULL);
107 return NULL;
108 }
109
110 /*********************************************************************
111 * ?_set_new_mode@@YAHH@Z (MSVCRT.@)
112 */
113 int MSVCRT__set_new_mode(int mode)
114 {
115 int old_mode;
116 LOCK_HEAP;
117 old_mode = MSVCRT_new_mode;
118 MSVCRT_new_mode = mode;
119 UNLOCK_HEAP;
120 return old_mode;
121 }
122
123 /*********************************************************************
124 * _heapadd (MSVCRT.@)
125 */
126 int _heapadd(void* mem, size_t size)
127 {
128 TRACE("(%p,%d) unsupported in Win32\n", mem,size);
129 *_errno() = ENOSYS;
130 return -1;
131 }