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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 #include <stdlib.h> 26 #include <malloc.h> 27 28 #define ROUND_DOWN(n, align) \ 29 (((ULONG)n) & ~((align) - 1l)) 30 31 #define ROUND_UP(n, align) \ 32 ROUND_DOWN(((ULONG)n) + (align) - 1, (align)) 33 34 /* round to 16 bytes + alloc at minimum 16 bytes */ 35 #define ROUND_SIZE(size) (max(16, ROUND_UP(size, 16))) 36 37 /* 38 * @implemented 39 */ 40 void* malloc(size_t _size) 41 { 42 size_t nSize = ROUND_SIZE(_size); 43 44 if (nSize<_size) 45 return NULL; 46 47 return HeapAlloc(GetProcessHeap(), 0, nSize); 48 } 49 50 /* 51 * @implemented 52 */ 53 void free(void* _ptr) 54 { 55 HeapFree(GetProcessHeap(),0,_ptr); 56 } 57 58 /* 59 * @implemented 60 */ 61 void* calloc(size_t _nmemb, size_t _size) 62 { 63 size_t nSize = _nmemb * _size; 64 size_t cSize = ROUND_SIZE(nSize); 65 66 if ( (_nmemb > ((size_t)-1 / _size)) || (cSize<nSize)) 67 return NULL; 68 69 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cSize ); 70 } 71 72 /* 73 * @implemented 74 */ 75 void* realloc(void* _ptr, size_t _size) 76 { 77 size_t nSize; 78 79 if (_ptr == NULL) 80 return malloc(_size); 81 82 if (_size == 0) 83 { 84 free(_ptr); 85 return NULL; 86 } 87 88 nSize = ROUND_SIZE(_size); 89 90 /* check for integer overflow */ 91 if (nSize<_size) 92 return NULL; 93 94 return HeapReAlloc(GetProcessHeap(), 0, _ptr, nSize); 95 } 96 97 /* 98 * @implemented 99 */ 100 void* _expand(void* _ptr, size_t _size) 101 { 102 size_t nSize; 103 104 nSize = ROUND_SIZE(_size); 105 106 if (nSize<_size) 107 return NULL; 108 109 return HeapReAlloc(GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, _ptr, nSize); 110 } 111 112 /* 113 * @implemented 114 */ 115 size_t _msize(void* _ptr) 116 { 117 return HeapSize(GetProcessHeap(), 0, _ptr); 118 } 119 120 /* 121 * @implemented 122 */ 123 int _heapchk(void) 124 { 125 if (!HeapValidate(GetProcessHeap(), 0, NULL)) 126 return -1; 127 return 0; 128 } 129 130 /* 131 * @implemented 132 */ 133 int _heapmin(void) 134 { 135 if (!HeapCompact(GetProcessHeap(), 0)) 136 return -1; 137 return 0; 138 } 139 140 /* 141 * @implemented 142 */ 143 int _heapset(unsigned int unFill) 144 { 145 if (_heapchk() == -1) 146 return -1; 147 return 0; 148 149 } 150 151 /* 152 * @implemented 153 */ 154 int _heapwalk(struct _heapinfo* entry) 155 { 156 return 0; 157 } 158 159