Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dune-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Timo Koch
dune-common
Commits
104d9c64
Commit
104d9c64
authored
12 years ago
by
Christian Engwer
Browse files
Options
Downloads
Patches
Plain Diff
add new allocator using malloc/free
[[Imported from SVN: r7009]]
parent
686b65e2
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
dune/common/Makefile.am
+1
-0
1 addition, 0 deletions
dune/common/Makefile.am
dune/common/mallocallocator.hh
+91
-0
91 additions, 0 deletions
dune/common/mallocallocator.hh
with
92 additions
and
0 deletions
dune/common/Makefile.am
+
1
−
0
View file @
104d9c64
...
...
@@ -57,6 +57,7 @@ commoninclude_HEADERS = \
iteratorfacades.hh
\
lcm.hh
\
lru.hh
\
mallocallocator.hh
\
math.hh
\
matvectraits.hh
\
misc.hh
\
...
...
This diff is collapsed.
Click to expand it.
dune/common/mallocallocator.hh
0 → 100644
+
91
−
0
View file @
104d9c64
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_MALLOC_ALLOCATOR_HH
#define DUNE_MALLOC_ALLOCATOR_HH
#include
<exception>
#include
<cstdlib>
#include
<new>
namespace
Dune
{
//! allocator which simply calls malloc/free
template
<
class
T
>
class
MallocAllocator
{
public:
typedef
std
::
size_t
size_type
;
typedef
std
::
ptrdiff_t
difference_type
;
typedef
T
*
pointer
;
typedef
const
T
*
const_pointer
;
typedef
T
&
reference
;
typedef
const
T
&
const_reference
;
typedef
T
value_type
;
template
<
class
U
>
struct
rebind
{
typedef
MallocAllocator
<
U
>
other
;
};
//! create a new MallocAllocator
MallocAllocator
()
throw
()
{}
//! copy construct from an other MallocAllocator, possibly for a different result type
template
<
class
U
>
MallocAllocator
(
const
MallocAllocator
<
U
>&
)
throw
()
{}
//! cleanup this allocator
~
MallocAllocator
()
throw
()
{}
pointer
address
(
reference
x
)
const
{
return
&
x
;
}
const_pointer
address
(
const_reference
x
)
const
{
return
&
x
;
}
//! allocate n objects of type T
pointer
allocate
(
size_type
n
,
const
void
*
hint
=
0
)
{
if
(
n
>
this
->
max_size
())
throw
std
::
bad_alloc
();
pointer
ret
=
static_cast
<
pointer
>
(
std
::
malloc
(
n
*
sizeof
(
T
)));
if
(
!
ret
)
throw
std
::
bad_alloc
();
return
ret
;
}
//! deallocate n objects of type T at address p
void
deallocate
(
pointer
p
,
size_type
n
)
{
std
::
free
(
p
);
}
//! max size for allocate
size_type
max_size
()
const
throw
()
{
return
size_type
(
-
1
)
/
sizeof
(
T
);
}
//! copy-construct an object of type T (i.e. make a placement new on p)
void
construct
(
pointer
p
,
const
T
&
val
)
{
::
new
((
void
*
)
p
)
T
(
val
);
}
#if HAVE_VARIADIC_TEMPLATES || DOXYGEN
//! construct an object of type T from variadic parameters
//! \note works only with newer C++ compilers
template
<
typename
...
_Args
>
void
construct
(
pointer
p
,
_Args
&&
...
__args
)
{
::
new
((
void
*
)
p
)
_Tp
(
std
::
forward
<
_Args
>
(
__args
)
...);
}
#endif
//! destroy an object of type T (i.e. call the Destructor)
void
destroy
(
pointer
p
)
{
p
->~
T
();
}
};
}
#endif // DUNE_MALLOC_ALLOCATOR_HH
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment