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
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
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
Core Modules
dune-common
Commits
484bb2d7
Commit
484bb2d7
authored
11 years ago
by
Dominic Kempf
Browse files
Options
Downloads
Patches
Plain Diff
Update the allocator and its doxgen docu
parent
299e8a63
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
dune/common/debugallocator.hh
+25
-30
25 additions, 30 deletions
dune/common/debugallocator.hh
with
25 additions
and
30 deletions
dune/common/debugallocator.hh
+
25
−
30
View file @
484bb2d7
...
...
@@ -290,85 +290,80 @@ namespace Dune
}
};
template
<
class
T
>
/** @brief a debug allocator that initializes its memory with random bits
* @tparam T the type to allocate storage for
* based on a template by N. Josuttis
*
* Implicitly relying on allocated memory to contain zeroes is an important
* source of errors, as different systems might behave different and such.
* Tools as valgrind are of great use, but dont find all problems. This allocator
* simply wraps new and delete, but fills all allocated memory with random bits.
* This should help testing and debugging code, as in a correct implementation
* using this allocator mustnt change the result at all.
*/
template
<
class
T
>
class
InitAllocator
{
public:
// type definitions
typedef
T
value_type
;
typedef
T
*
pointer
;
typedef
T
value_type
;
typedef
T
*
pointer
;
typedef
const
T
*
const_pointer
;
typedef
T
&
reference
;
typedef
T
&
reference
;
typedef
const
T
&
const_reference
;
typedef
std
::
size_t
size_type
;
typedef
std
::
size_t
size_type
;
typedef
std
::
ptrdiff_t
difference_type
;
// rebind allocator to type U
template
<
class
U
>
struct
rebind
{
typedef
InitAllocator
<
U
>
other
;
};
// return address of values
//
!
return address of values
pointer
address
(
reference
value
)
const
{
return
&
value
;
}
const_pointer
address
(
const_reference
value
)
const
{
return
&
value
;
}
/* constructors and destructor
* - nothing to do because the allocator has no state
*/
//! constructors and destructors do nothing
InitAllocator
()
throw
()
{}
InitAllocator
(
const
InitAllocator
&
)
throw
()
{}
template
<
class
U
>
InitAllocator
(
const
InitAllocator
<
U
>&
)
throw
()
{}
~
InitAllocator
()
throw
()
{}
// return maximum number of elements that can be allocated
//
!
return maximum number of elements that can be allocated
size_type
max_size
()
const
throw
()
{
return
std
::
numeric_limits
<
std
::
size_t
>::
max
()
/
sizeof
(
T
);
}
// allocate
but don't
initialize num elements of type T
//
!
allocate
and
initialize
with random bits
num elements of type T
pointer
allocate
(
size_type
num
,
const
void
*
=
0
)
{
srand
(
time
(
NULL
));
pointer
ret
=
(
pointer
)(
::
operator
new
(
num
*
sizeof
(
T
)));
for
(
size_type
i
=
0
;
i
<
num
;
i
++
)
{
char
c
[
sizeof
(
T
)];
for
(
size_type
j
=
0
;
j
<
sizeof
(
T
);
j
++
)
c
[
j
]
=
rand
()
%
256
;
new
(
ret
+
i
)
value_type
(
*
reinterpret_cast
<
pointer
>
(
c
));
}
for
(
size_type
i
=
0
;
i
<
num
*
sizeof
(
T
);
i
++
)
*
(
reinterpret_cast
<
char
*>
(
ret
)
+
i
)
=
rand
()
%
256
;
return
ret
;
}
// initialize elements of allocated storage p with value value
//
!
initialize elements of allocated storage p with value value
void
construct
(
pointer
p
,
const
T
&
value
)
{
// initialize memory with placement new
new
((
void
*
)
p
)
T
(
value
);
}
// destroy elements of initialized storage p
//
!
destroy elements of initialized storage p
void
destroy
(
pointer
p
)
{
// destroy objects by calling their destructor
p
->~
T
();
}
// deallocate storage p of deleted elements
//
!
deallocate storage p of deleted elements
void
deallocate
(
pointer
p
,
size_type
num
)
{
::
operator
delete
((
void
*
)
p
);
...
...
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