| 1 |
/************************************************************************* |
| 2 |
* * |
| 3 |
* ODER's Utilities Library. Copyright (C) 2008 Oleh Derevenko. * |
| 4 |
* All rights reserved. e-mail: [email protected] (change all "a" to "e") * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of EITHER: * |
| 8 |
* (1) The GNU Lesser General Public License as published by the Free * |
| 9 |
* Software Foundation; either version 3 of the License, or (at * |
| 10 |
* your option) any later version. The text of the GNU Lesser * |
| 11 |
* General Public License is included with this library in the * |
| 12 |
* file LICENSE-LESSER.TXT. Since LGPL is the extension of GPL * |
| 13 |
* the text of GNU General Public License is also provided for * |
| 14 |
* your information in file LICENSE.TXT. * |
| 15 |
* (2) The BSD-style license that is included with this library in * |
| 16 |
* the file LICENSE-BSD.TXT. * |
| 17 |
* (3) The zlib/libpng license that is included with this library in * |
| 18 |
* the file LICENSE-ZLIB.TXT * |
| 19 |
* * |
| 20 |
* This library is distributed WITHOUT ANY WARRANTY, including implied * |
| 21 |
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 22 |
* See the files LICENSE.TXT and LICENSE-LESSER.TXT or LICENSE-BSD.TXT * |
| 23 |
* or LICENSE-ZLIB.TXT for more details. * |
| 24 |
* * |
| 25 |
*************************************************************************/ |
| 26 |
|
| 27 |
#include <ou/malloc.h> |
| 28 |
#include <ou/assert.h> |
| 29 |
#include <ou/customization.h> |
| 30 |
#include <ou/macros.h> |
| 31 |
|
| 32 |
#if _OU_TARGET_OS == _OU_TARGET_OS_MAC || _OU_TARGET_OS == _OU_TARGET_OS_IOS |
| 33 |
|
| 34 |
#include <stdlib.h> |
| 35 |
|
| 36 |
|
| 37 |
#else // #if _OU_TARGET_OS != _OU_TARGET_OS_MAC && _OU_TARGET_OS != _OU_TARGET_OS_IOS |
| 38 |
|
| 39 |
#include <malloc.h> |
| 40 |
|
| 41 |
|
| 42 |
#endif // #if _OU_TARGET_OS != _OU_TARGET_OS_MAC && _OU_TARGET_OS != _OU_TARGET_OS_IOS |
| 43 |
|
| 44 |
|
| 45 |
BEGIN_NAMESPACE_OU(); |
| 46 |
|
| 47 |
|
| 48 |
/*extern*/ void *_OU_CONVENTION_API AllocateMemoryBlock(size_t nBlockSize) |
| 49 |
{ |
| 50 |
void *pv_NewBlock; |
| 51 |
|
| 52 |
CMemoryAllocationProcedure fnMemoryAllocationProcedure = CMemoryManagerCustomization::GetMemoryAllocationCustomProcedure(); |
| 53 |
|
| 54 |
if (fnMemoryAllocationProcedure) |
| 55 |
{ |
| 56 |
pv_NewBlock = fnMemoryAllocationProcedure(nBlockSize); |
| 57 |
OU_ASSERT(OU_ALIGNED_SIZE((size_t)pv_NewBlock, _OU_MEMORY_REQUIRED_ALIGNMENT) == (size_t)pv_NewBlock); // Memory must be aligned |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
pv_NewBlock = malloc(nBlockSize); |
| 62 |
} |
| 63 |
|
| 64 |
return pv_NewBlock; |
| 65 |
} |
| 66 |
|
| 67 |
/*extern*/ void *_OU_CONVENTION_API ReallocateMemoryBlock(void *pv_ExistingBlock, size_t nNewBlockSize) |
| 68 |
{ |
| 69 |
OU_ASSERT(OU_ALIGNED_SIZE((size_t)pv_ExistingBlock, _OU_MEMORY_REQUIRED_ALIGNMENT) == (size_t)pv_ExistingBlock); // Memory must be aligned |
| 70 |
|
| 71 |
void *pv_NewBlock; |
| 72 |
|
| 73 |
CMemoryReallocationProcedure fnMemoryReallocationProcedure = CMemoryManagerCustomization::GetMemoryReallocationCustomProcedure(); |
| 74 |
|
| 75 |
if (fnMemoryReallocationProcedure) |
| 76 |
{ |
| 77 |
pv_NewBlock = fnMemoryReallocationProcedure(pv_ExistingBlock, nNewBlockSize); |
| 78 |
OU_ASSERT(OU_ALIGNED_SIZE((size_t)pv_NewBlock, _OU_MEMORY_REQUIRED_ALIGNMENT) == (size_t)pv_NewBlock); // Memory must be aligned |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
pv_NewBlock = realloc(pv_ExistingBlock, nNewBlockSize); |
| 83 |
} |
| 84 |
|
| 85 |
return pv_NewBlock; |
| 86 |
} |
| 87 |
|
| 88 |
/*extern*/ void _OU_CONVENTION_API FreeMemoryBlock(void *pv_ExistingBlock) |
| 89 |
{ |
| 90 |
OU_ASSERT(OU_ALIGNED_SIZE((size_t)pv_ExistingBlock, _OU_MEMORY_REQUIRED_ALIGNMENT) == (size_t)pv_ExistingBlock); // Memory must be aligned |
| 91 |
|
| 92 |
CMemoryDeallocationProcedure fnMemoryDeallocationProcedure = CMemoryManagerCustomization::GetMemoryDeallocationCustomProcedure(); |
| 93 |
|
| 94 |
if (fnMemoryDeallocationProcedure) |
| 95 |
{ |
| 96 |
fnMemoryDeallocationProcedure(pv_ExistingBlock); |
| 97 |
} |
| 98 |
else |
| 99 |
{ |
| 100 |
free(pv_ExistingBlock); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
END_NAMESPACE_OU(); |
| 106 |
|