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
3d3b32cb
Commit
3d3b32cb
authored
12 years ago
by
Martin Nolte
Browse files
Options
Downloads
Patches
Plain Diff
bring back smallobject.hh (for my personal use only)
[[Imported from SVN: r6902]]
parent
4d35beaa
No related branches found
Branches containing commit
No related tags found
Tags containing commit
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/smallobject.hh
+188
-0
188 additions, 0 deletions
dune/common/smallobject.hh
with
189 additions
and
0 deletions
dune/common/Makefile.am
+
1
−
0
View file @
3d3b32cb
...
...
@@ -69,6 +69,7 @@ commoninclude_HEADERS = \
reservedvector.hh
\
shared_ptr.hh
\
singleton.hh
\
smallobject.hh
\
static_assert.hh
\
stdstreams.hh
\
timer.hh
\
...
...
This diff is collapsed.
Click to expand it.
dune/common/smallobject.hh
0 → 100644
+
188
−
0
View file @
3d3b32cb
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_SMALLOBJECT_HH
#define DUNE_SMALLOBJECT_HH
#include
<cassert>
#include
<memory>
#include
<new>
namespace
Dune
{
// SmallObjectPool
// ---------------
class
SmallObjectPool
{
union
Block
{
Block
*
next
;
unsigned
int
blocks
;
};
public
:
enum
{
blockSize
=
sizeof
(
Block
)
};
enum
{
maxBlocks
=
(
1
<<
10
)
-
1
};
enum
{
maxSize
=
maxBlocks
*
blockSize
};
private
:
Block
*
list_
[
maxBlocks
];
SmallObjectPool
()
{
for
(
unsigned
int
i
=
0
;
i
<
maxBlocks
;
++
i
)
list_
[
i
]
=
0
;
}
~
SmallObjectPool
()
{
for
(
unsigned
int
i
=
0
;
i
<
maxBlocks
;
++
i
)
{
for
(
Block
*
next
=
list_
[
i
];
next
!=
0
;
)
{
Block
*
current
=
next
;
next
=
current
->
next
;
delete
[]
current
;
}
}
}
static
SmallObjectPool
&
instance
()
{
static
SmallObjectPool
inst
;
return
inst
;
}
static
Block
*&
list
(
unsigned
int
blocks
)
{
assert
(
blocks
<
maxBlocks
);
return
instance
().
list_
[
blocks
];
}
public
:
static
void
*
allocate
(
unsigned
int
size
)
{
const
unsigned
int
blocks
=
(
size
+
(
blockSize
-
1
))
/
blockSize
;
if
(
blocks
>=
maxBlocks
)
return
0
;
Block
*&
next
=
list
(
blocks
);
Block
*
current
=
next
;
if
(
current
!=
0
)
next
=
current
->
next
;
else
current
=
new
Block
[
blocks
+
1
];
current
->
blocks
=
blocks
;
return
current
+
1
;
}
static
void
free
(
void
*
p
)
{
if
(
p
!=
0
)
{
Block
*
current
=
reinterpret_cast
<
Block
*
>
(
p
)
-
1
;
const
unsigned
int
blocks
=
current
->
blocks
;
Block
*&
next
=
list
(
blocks
);
current
->
next
=
next
;
next
=
current
;
}
}
};
// SmallObject
// -----------
struct
SmallObject
{
void
*
operator
new
(
size_t
size
)
{
return
SmallObjectPool
::
allocate
(
size
);
}
void
operator
delete
(
void
*
p
)
{
SmallObjectPool
::
free
(
p
);
}
};
// SmallObjectAllocator
// --------------------
template
<
class
T
>
struct
SmallObjectAllocator
;
template
<
>
struct
SmallObjectAllocator
<
void
>
{
typedef
void
value_type
;
typedef
void
*
pointer
;
typedef
const
void
*
const_pointer
;
template
<
class
U
>
struct
rebind
{
typedef
SmallObjectAllocator
<
U
>
other
;
};
};
template
<
class
T
>
struct
SmallObjectAllocator
{
typedef
T
value_type
;
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
;
template
<
class
U
>
struct
rebind
{
typedef
SmallObjectAllocator
<
U
>
other
;
};
SmallObjectAllocator
()
throw
()
{}
template
<
class
U
>
SmallObjectAllocator
(
const
SmallObjectAllocator
<
U
>
&
)
throw
()
{}
~
SmallObjectAllocator
()
throw
()
{}
pointer
address
(
reference
r
)
const
{
return
&
r
;
}
const_pointer
address
(
const_reference
r
)
const
{
return
&
r
;
}
pointer
allocate
(
size_type
n
,
SmallObjectAllocator
<
void
>::
const_pointer
hint
=
0
);
void
deallocate
(
pointer
p
,
size_type
n
)
{
SmallObjectPool
::
free
(
p
);
}
void
construct
(
pointer
p
,
const
T
&
value
)
{
new
(
p
)
T
(
value
);
}
void
destroy
(
pointer
p
)
{
p
->~
T
();
}
size_type
max_size
()
const
throw
()
{
return
(
SmallObjectPool
::
maxSize
/
sizeof
(
T
));
}
};
// Implementation of SmallObjectAllocator
// --------------------------------------
template
<
class
T
>
inline
typename
SmallObjectAllocator
<
T
>::
pointer
SmallObjectAllocator
<
T
>::
allocate
(
size_type
n
,
SmallObjectAllocator
<
void
>::
const_pointer
hint
)
{
return
static_cast
<
pointer
>
(
SmallObjectPool
::
allocate
(
n
*
sizeof
(
T
)
)
);
}
template
<
class
T
>
bool
operator
==
(
const
SmallObjectAllocator
<
T
>
&
,
const
SmallObjectAllocator
<
T
>
&
)
throw
()
{
return
true
;
}
template
<
class
T
>
bool
operator
!=
(
const
SmallObjectAllocator
<
T
>
&
,
const
SmallObjectAllocator
<
T
>
&
)
throw
()
{
return
false
;
}
}
// namespace Dune
#endif // #ifndef DUNE_SMALLOBJECT_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