1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*++
6
7
8
9Module Name:
10
11 malloc.cpp
12
13Abstract:
14
15 Implementation of suspension safe memory allocation functions.
16
17Revision History:
18
19
20
21--*/
22
23#include "pal/corunix.hpp"
24#include "pal/thread.hpp"
25#include "pal/malloc.hpp"
26#include "pal/dbgmsg.h"
27
28#include <string.h>
29
30SET_DEFAULT_DEBUG_CHANNEL(CRT);
31
32using namespace CorUnix;
33
34void *
35__cdecl
36PAL_realloc(
37 void* pvMemblock,
38 size_t szSize
39 )
40{
41 return InternalRealloc(pvMemblock, szSize);
42}
43
44void *
45CorUnix::InternalRealloc(
46 void* pvMemblock,
47 size_t szSize
48 )
49{
50 void *pvMem;
51
52 PERF_ENTRY(InternalRealloc);
53 ENTRY("realloc (memblock:%p size=%d)\n", pvMemblock, szSize);
54
55 if (szSize == 0)
56 {
57 // If pvMemblock is NULL, there's no reason to call free.
58 if (pvMemblock != NULL)
59 {
60 free(pvMemblock);
61 }
62 pvMem = NULL;
63 }
64 else
65 {
66 pvMem = realloc(pvMemblock, szSize);
67 }
68
69 LOGEXIT("realloc returns void * %p\n", pvMem);
70 PERF_EXIT(InternalRealloc);
71 return pvMem;
72}
73
74void
75__cdecl
76PAL_free(
77 void *pvMem
78 )
79{
80 free(pvMem);
81}
82
83void *
84__cdecl
85PAL_malloc(
86 size_t szSize
87 )
88{
89 return InternalMalloc(szSize);
90}
91
92void *
93CorUnix::InternalMalloc(
94 size_t szSize
95 )
96{
97 void *pvMem;
98
99 if (szSize == 0)
100 {
101 // malloc may return null for a requested size of zero bytes. Force a nonzero size to get a valid pointer.
102 szSize = 1;
103 }
104
105 pvMem = (void*)malloc(szSize);
106 return pvMem;
107}
108
109char *
110__cdecl
111PAL__strdup(
112 const char *c_szStr
113 )
114{
115 return strdup(c_szStr);
116}