| 1268 | |
| 1269 | #if !defined(TORQUE_DISABLE_MEMORY_MANAGER) |
| 1270 | static void* alloc(dsize_t size, bool array, const char* fileName, const U32 line) |
| 1271 | { |
| 1272 | AssertFatal(size < MaxAllocationAmount, "Memory::alloc - tried to allocate > MaxAllocationAmount!"); |
| 1273 | |
| 1274 | #ifdef TORQUE_MULTITHREAD |
| 1275 | if(!gMemMutex && !gReentrantGuard) |
| 1276 | { |
| 1277 | gReentrantGuard = true; |
| 1278 | gMemMutex = Mutex::createMutex(); |
| 1279 | gReentrantGuard = false; |
| 1280 | } |
| 1281 | |
| 1282 | if(!gReentrantGuard) |
| 1283 | Mutex::lockMutex(gMemMutex); |
| 1284 | |
| 1285 | #endif |
| 1286 | |
| 1287 | AssertFatal(size < MaxAllocationAmount, "Size error."); |
| 1288 | //validate(); |
| 1289 | if (size == 0) |
| 1290 | { |
| 1291 | #ifdef TORQUE_MULTITHREAD |
| 1292 | if(!gReentrantGuard) |
| 1293 | Mutex::unlockMutex(gMemMutex); |
| 1294 | #endif |
| 1295 | return NULL; |
| 1296 | } |
| 1297 | |
| 1298 | #ifndef TORQUE_ENABLE_PROFILE_PATH |
| 1299 | // Note: will cause crash if profile path is on |
| 1300 | PROFILE_START(MemoryAlloc); |
| 1301 | #endif |
| 1302 | |
| 1303 | #ifdef TORQUE_DEBUG_GUARD |
| 1304 | // if we're guarding, round up to the nearest DWORD |
| 1305 | size = ((size + 3) & ~0x3); |
| 1306 | #else |
| 1307 | // round up size to nearest 16 byte boundary (cache lines and all...) |
| 1308 | size = ((size + 15) & ~0xF); |
| 1309 | #endif |
| 1310 | |
| 1311 | FreeHeader *header = treeFindSmallestGreaterThan(size); |
| 1312 | if(header) |
| 1313 | treeRemove(header); |
| 1314 | else |
| 1315 | header = (FreeHeader *) allocMemPage(size); |
| 1316 | |
| 1317 | // ok, see if there's enough room in the block to make another block |
| 1318 | // for this to happen it has to have enough room for a header |
| 1319 | // and 16 more bytes. |
| 1320 | |
| 1321 | U8 *basePtr = (U8 *) header; |
| 1322 | basePtr += sizeof(Header); |
| 1323 | |
| 1324 | checkUnusedAlloc(header, size); |
| 1325 | |
| 1326 | AllocatedHeader *retHeader = (AllocatedHeader *) header; |
| 1327 | retHeader->flags = array ? (Allocated | Array) : Allocated; |
no test coverage detected