Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
447a86c9
Commit
447a86c9
authored
Jan 04, 2013
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
SliceBuffer: lazy initialization
Avoid page faults on MPD startup. This saves a lot of memory for an idle MPD.
parent
e3a2bd3a
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
18 additions
and
10 deletions
+18
-10
SliceBuffer.hxx
src/util/SliceBuffer.hxx
+18
-10
No files found.
src/util/SliceBuffer.hxx
View file @
447a86c9
...
...
@@ -48,6 +48,13 @@ class SliceBuffer {
const
unsigned
n_max
;
/**
* The number of slices that are initialized. This is used to
* avoid page faulting on the new allocation, so the kernel
* does not need to reserve physical memory pages.
*/
unsigned
n_initialized
;
/**
* The number of slices currently allocated.
*/
unsigned
n_allocated
;
...
...
@@ -61,15 +68,9 @@ class SliceBuffer {
public
:
SliceBuffer
(
unsigned
_count
)
:
n_max
(
_count
),
n_allocated
(
0
),
data
(
g_new
(
Slice
,
n_max
)),
available
(
data
)
{
:
n_max
(
_count
),
n_
initialized
(
0
),
n_
allocated
(
0
),
data
(
g_new
(
Slice
,
n_max
)),
available
(
nullptr
)
{
assert
(
n_max
>
0
);
Slice
*
const
last
=
data
+
n_max
-
1
;
for
(
Slice
*
slice
=
data
;
slice
!=
last
;
++
slice
)
slice
->
next
=
slice
+
1
;
last
->
next
=
nullptr
;
}
~
SliceBuffer
()
{
...
...
@@ -97,14 +98,20 @@ public:
template
<
typename
...
Args
>
T
*
Allocate
(
Args
&&
...
args
)
{
assert
(
n_allocated
<=
n_max
);
assert
(
n_initialized
<=
n_max
);
assert
(
n_allocated
<=
n_initialized
);
if
(
available
==
nullptr
)
{
if
(
n_initialized
==
n_max
)
{
/* out of (internal) memory, buffer is full */
assert
(
n_allocated
==
n_max
);
return
nullptr
;
}
available
=
&
data
[
n_initialized
++
];
available
->
next
=
nullptr
;
}
/* allocate a slice */
T
*
value
=
&
available
->
value
;
available
=
available
->
next
;
...
...
@@ -115,8 +122,9 @@ public:
}
void
Free
(
T
*
value
)
{
assert
(
n_initialized
<=
n_max
);
assert
(
n_allocated
>
0
);
assert
(
n_allocated
<=
n_
max
);
assert
(
n_allocated
<=
n_
initialized
);
Slice
*
slice
=
reinterpret_cast
<
Slice
*>
(
value
);
assert
(
slice
>=
data
&&
slice
<
data
+
n_max
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment