Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
S
spdlog
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
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
spdlog
Commits
b4349e42
Commit
b4349e42
authored
Jul 03, 2018
by
gabime
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pre allocate async q memory
parent
92e2cef6
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
80 additions
and
20 deletions
+80
-20
circular_q.h
include/spdlog/details/circular_q.h
+62
-0
fmt_helper.h
include/spdlog/details/fmt_helper.h
+3
-2
mpmc_blocking_q.h
include/spdlog/details/mpmc_blocking_q.h
+15
-17
formatter.h
include/spdlog/formatter.h
+0
-1
No files found.
include/spdlog/details/circular_q.h
0 → 100644
View file @
b4349e42
//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
// cirucal q view of std::vector.
#pragma once
namespace
spdlog
{
namespace
details
{
template
<
typename
T
>
class
circular_q
{
public
:
using
item_type
=
T
;
explicit
circular_q
(
size_t
max_items
)
:
max_items_
(
max_items
+
1
)
,
v_
(
max_items_
)
{
}
// push back, overrun last item if no room left
void
push_back
(
T
&&
item
)
{
v_
[
head_
]
=
std
::
move
(
item
);
head_
=
(
head_
+
1
)
%
max_items_
;
if
(
head_
==
tail_
)
{
tail_
=
(
tail_
+
1
)
%
max_items_
;
}
}
// Pop item from front.
// If there are no elements in the container, the behavior is undefined.
void
pop_front
(
T
&
popped_item
)
{
popped_item
=
std
::
move
(
v_
[
tail_
]);
tail_
=
(
tail_
+
1
)
%
max_items_
;
}
bool
empty
()
{
return
head_
==
tail_
;
}
bool
full
()
{
// tail is ahead of the head by 1
return
((
head_
+
1
)
%
max_items_
)
==
tail_
;
}
private
:
size_t
max_items_
;
typename
std
::
vector
<
T
>::
size_type
head_
=
0
;
typename
std
::
vector
<
T
>::
size_type
tail_
=
0
;
std
::
vector
<
T
>
v_
;
};
}
// namespace details
}
// namespace spdlog
\ No newline at end of file
include/spdlog/details/fmt_helper.h
View file @
b4349e42
...
...
@@ -24,9 +24,10 @@ inline void append_c_str(const char *c_str, fmt::memory_buffer &dest)
}
}
inline
void
append_buf
(
const
fmt
::
memory_buffer
&
buf
,
fmt
::
memory_buffer
&
dest
)
template
<
size_t
N1
,
size_t
N2
>
inline
void
append_buf
(
const
fmt
::
basic_memory_buffer
<
char
,
N1
>
&
buf
,
fmt
::
basic_memory_buffer
<
char
,
N2
>
&
dest
)
{
const
char
*
buf_ptr
=
buf
.
data
();
auto
*
buf_ptr
=
buf
.
data
();
dest
.
append
(
buf_ptr
,
buf_ptr
+
buf
.
size
());
}
...
...
include/spdlog/details/mpmc_blocking_q.h
View file @
b4349e42
...
...
@@ -5,15 +5,15 @@
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
// async log helper :
// multi producer-multi consumer blocking queue
// enqueue(..) - will block until room found to put the new message
// enqueue_nowait(..) - will return immediatly with false if no room left in the queue
// dequeue_for(..) - will block until the queue is not empty or timeout passed
// multi producer-multi consumer blocking queue.
// enqueue(..) - will block until room found to put the new message.
// enqueue_nowait(..) - will return immediately with false if no room left in the queue.
// dequeue_for(..) - will block until the queue is not empty or timeout have passed.
#include "spdlog/details/circular_q.h"
#include <condition_variable>
#include <mutex>
#include <queue>
namespace
spdlog
{
namespace
details
{
...
...
@@ -24,7 +24,7 @@ class mpmc_blocking_queue
public
:
using
item_type
=
T
;
explicit
mpmc_blocking_queue
(
size_t
max_items
)
:
max_items
_
(
max_items
)
:
q
_
(
max_items
)
{
}
...
...
@@ -33,22 +33,22 @@ public:
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
pop_cv_
.
wait
(
lock
,
[
this
]
{
return
this
->
q_
.
size
()
<
this
->
max_items_
;
});
q_
.
push
(
std
::
move
(
item
));
pop_cv_
.
wait
(
lock
,
[
this
]
{
return
!
this
->
q_
.
full
()
;
});
q_
.
push
_back
(
std
::
move
(
item
));
}
push_cv_
.
notify_one
();
}
// try to enqueue and return imm
deialt
y false if no room left
// try to enqueue and return imm
ediatel
y false if no room left
bool
enqueue_nowait
(
T
&&
item
)
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
q_
.
size
()
==
this
->
max_items_
)
if
(
q_
.
full
()
)
{
return
false
;
}
q_
.
push
(
std
::
forward
<
T
>
(
item
));
q_
.
push
_back
(
std
::
forward
<
T
>
(
item
));
}
push_cv_
.
notify_one
();
return
true
;
...
...
@@ -60,25 +60,23 @@ public:
{
{
std
::
unique_lock
<
std
::
mutex
>
lock
(
queue_mutex_
);
if
(
!
push_cv_
.
wait_for
(
lock
,
wait_duration
,
[
this
]
{
return
this
->
q_
.
size
()
>
0
;
}))
if
(
!
push_cv_
.
wait_for
(
lock
,
wait_duration
,
[
this
]
{
return
!
this
->
q_
.
empty
()
;
}))
{
return
false
;
}
popped_item
=
std
::
move
(
q_
.
front
());
q_
.
pop
();
q_
.
pop_front
(
popped_item
);
}
pop_cv_
.
notify_one
();
return
true
;
}
private
:
size_t
max_items_
;
std
::
mutex
queue_mutex_
;
std
::
condition_variable
push_cv_
;
std
::
condition_variable
pop_cv_
;
s
td
::
queue
<
T
>
q_
;
s
pdlog
::
details
::
circular_q
<
T
>
q_
;
};
}
// namespace details
}
// namespace spdlog
include/spdlog/formatter.h
View file @
b4349e42
...
...
@@ -8,7 +8,6 @@
#include "fmt/fmt.h"
#include "spdlog/details/log_msg.h"
namespace
spdlog
{
class
formatter
...
...
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