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 | // |
d5f74e42 |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
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 | |
7fd59977 |
19 | // paralleling with Intel TBB |
20 | #ifdef HAVE_TBB |
21 | #include <tbb/scalable_allocator.h> |
22 | using namespace tbb; |
23 | #else |
24 | #define scalable_malloc malloc |
25 | #define scalable_calloc calloc |
26 | #define scalable_realloc realloc |
27 | #define scalable_free free |
28 | #endif |
29 | |
30 | //======================================================================= |
31 | //function : Standard_MMgrTBBalloc |
32 | //purpose : |
33 | //======================================================================= |
34 | |
35 | Standard_MMgrTBBalloc::Standard_MMgrTBBalloc(const Standard_Boolean aClear) |
36 | { |
37 | myClear = aClear; |
38 | } |
39 | |
40 | //======================================================================= |
41 | //function : Allocate |
42 | //purpose : |
43 | //======================================================================= |
44 | |
45 | Standard_Address Standard_MMgrTBBalloc::Allocate(const Standard_Size aSize) |
46 | { |
47 | // the size is rounded up to 4 since some OCC classes |
48 | // (e.g. TCollection_AsciiString) assume memory to be double word-aligned |
49 | const Standard_Size aRoundSize = (aSize + 3) & ~0x3; |
50 | // we use ?: operator instead of if() since it is faster :-) |
51 | Standard_Address aPtr = ( myClear ? scalable_calloc(aRoundSize, sizeof(char)) : |
52 | scalable_malloc(aRoundSize) ); |
53 | if ( ! aPtr ) |
54 | Standard_OutOfMemory::Raise("Standard_MMgrTBBalloc::Allocate(): malloc failed"); |
55 | return aPtr; |
56 | } |
57 | |
58 | //======================================================================= |
59 | //function : Free |
60 | //purpose : |
61 | //======================================================================= |
62 | |
547702a1 |
63 | void Standard_MMgrTBBalloc::Free (Standard_Address theStorage) |
7fd59977 |
64 | { |
547702a1 |
65 | scalable_free (theStorage); |
7fd59977 |
66 | } |
67 | |
68 | //======================================================================= |
69 | //function : Reallocate |
70 | //purpose : |
71 | //======================================================================= |
72 | |
547702a1 |
73 | Standard_Address Standard_MMgrTBBalloc::Reallocate (Standard_Address theStorage, |
74 | const Standard_Size theSize) |
7fd59977 |
75 | { |
76 | // the size is rounded up to 4 since some OCC classes |
77 | // (e.g. TCollection_AsciiString) assume memory to be double word-aligned |
547702a1 |
78 | const Standard_Size aRoundSize = (theSize + 3) & ~0x3; |
79 | Standard_Address newStorage = (Standard_Address)scalable_realloc(theStorage, aRoundSize); |
7fd59977 |
80 | if ( ! newStorage ) |
81 | Standard_OutOfMemory::Raise("Standard_MMgrTBBalloc::Reallocate(): realloc failed"); |
82 | // Note that it is not possible to ensure that additional memory |
83 | // allocated by realloc will be cleared (so as to satisfy myClear mode); |
84 | // in order to do that we would need using memset... |
7fd59977 |
85 | return newStorage; |
86 | } |