b311480e |
1 | // Created on: 2010-03-15 |
2 | // Created by: Sergey KUUL |
973c2be1 |
3 | // Copyright (c) 2010-2014 OPEN CASCADE SAS |
b311480e |
4 | // |
973c2be1 |
5 | // This file is part of Open CASCADE Technology software library. |
b311480e |
6 | // |
973c2be1 |
7 | // This library is free software; you can redistribute it and / or modify it |
8 | // under the terms of the GNU Lesser General Public version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
12 | // |
973c2be1 |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
7fd59977 |
15 | |
16 | #include <Standard_MMgrTBBalloc.hxx> |
17 | #include <Standard_OutOfMemory.hxx> |
18 | |
19 | // NOTE: replaced by more correct check |
20 | //#if defined(WNT) || defined(LIN) |
21 | //#define HAVE_TBB 1 |
22 | //#endif |
23 | |
24 | // paralleling with Intel TBB |
25 | #ifdef HAVE_TBB |
26 | #include <tbb/scalable_allocator.h> |
27 | using namespace tbb; |
28 | #else |
29 | #define scalable_malloc malloc |
30 | #define scalable_calloc calloc |
31 | #define scalable_realloc realloc |
32 | #define scalable_free free |
33 | #endif |
34 | |
35 | //======================================================================= |
36 | //function : Standard_MMgrTBBalloc |
37 | //purpose : |
38 | //======================================================================= |
39 | |
40 | Standard_MMgrTBBalloc::Standard_MMgrTBBalloc(const Standard_Boolean aClear) |
41 | { |
42 | myClear = aClear; |
43 | } |
44 | |
45 | //======================================================================= |
46 | //function : Allocate |
47 | //purpose : |
48 | //======================================================================= |
49 | |
50 | Standard_Address Standard_MMgrTBBalloc::Allocate(const Standard_Size aSize) |
51 | { |
52 | // the size is rounded up to 4 since some OCC classes |
53 | // (e.g. TCollection_AsciiString) assume memory to be double word-aligned |
54 | const Standard_Size aRoundSize = (aSize + 3) & ~0x3; |
55 | // we use ?: operator instead of if() since it is faster :-) |
56 | Standard_Address aPtr = ( myClear ? scalable_calloc(aRoundSize, sizeof(char)) : |
57 | scalable_malloc(aRoundSize) ); |
58 | if ( ! aPtr ) |
59 | Standard_OutOfMemory::Raise("Standard_MMgrTBBalloc::Allocate(): malloc failed"); |
60 | return aPtr; |
61 | } |
62 | |
63 | //======================================================================= |
64 | //function : Free |
65 | //purpose : |
66 | //======================================================================= |
67 | |
547702a1 |
68 | void Standard_MMgrTBBalloc::Free (Standard_Address theStorage) |
7fd59977 |
69 | { |
547702a1 |
70 | scalable_free (theStorage); |
7fd59977 |
71 | } |
72 | |
73 | //======================================================================= |
74 | //function : Reallocate |
75 | //purpose : |
76 | //======================================================================= |
77 | |
547702a1 |
78 | Standard_Address Standard_MMgrTBBalloc::Reallocate (Standard_Address theStorage, |
79 | const Standard_Size theSize) |
7fd59977 |
80 | { |
81 | // the size is rounded up to 4 since some OCC classes |
82 | // (e.g. TCollection_AsciiString) assume memory to be double word-aligned |
547702a1 |
83 | const Standard_Size aRoundSize = (theSize + 3) & ~0x3; |
84 | Standard_Address newStorage = (Standard_Address)scalable_realloc(theStorage, aRoundSize); |
7fd59977 |
85 | if ( ! newStorage ) |
86 | Standard_OutOfMemory::Raise("Standard_MMgrTBBalloc::Reallocate(): realloc failed"); |
87 | // Note that it is not possible to ensure that additional memory |
88 | // allocated by realloc will be cleared (so as to satisfy myClear mode); |
89 | // in order to do that we would need using memset... |
7fd59977 |
90 | return newStorage; |
91 | } |