| 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 |  | 
|---|
| 9 | Module Name: | 
|---|
| 10 |  | 
|---|
| 11 | malloc.cpp | 
|---|
| 12 |  | 
|---|
| 13 | Abstract: | 
|---|
| 14 |  | 
|---|
| 15 | Implementation of suspension safe memory allocation functions. | 
|---|
| 16 |  | 
|---|
| 17 | Revision 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 |  | 
|---|
| 30 | SET_DEFAULT_DEBUG_CHANNEL(CRT); | 
|---|
| 31 |  | 
|---|
| 32 | using namespace CorUnix; | 
|---|
| 33 |  | 
|---|
| 34 | void * | 
|---|
| 35 | __cdecl | 
|---|
| 36 | PAL_realloc( | 
|---|
| 37 | void* pvMemblock, | 
|---|
| 38 | size_t szSize | 
|---|
| 39 | ) | 
|---|
| 40 | { | 
|---|
| 41 | return InternalRealloc(pvMemblock, szSize); | 
|---|
| 42 | } | 
|---|
| 43 |  | 
|---|
| 44 | void * | 
|---|
| 45 | CorUnix::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 |  | 
|---|
| 74 | void | 
|---|
| 75 | __cdecl | 
|---|
| 76 | PAL_free( | 
|---|
| 77 | void *pvMem | 
|---|
| 78 | ) | 
|---|
| 79 | { | 
|---|
| 80 | free(pvMem); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | void * | 
|---|
| 84 | __cdecl | 
|---|
| 85 | PAL_malloc( | 
|---|
| 86 | size_t szSize | 
|---|
| 87 | ) | 
|---|
| 88 | { | 
|---|
| 89 | return InternalMalloc(szSize); | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void * | 
|---|
| 93 | CorUnix::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 |  | 
|---|
| 109 | char * | 
|---|
| 110 | __cdecl | 
|---|
| 111 | PAL__strdup( | 
|---|
| 112 | const char *c_szStr | 
|---|
| 113 | ) | 
|---|
| 114 | { | 
|---|
| 115 | return strdup(c_szStr); | 
|---|
| 116 | } | 
|---|