LMMS
Loading...
Searching...
No Matches
LocklessAllocator.h
Go to the documentation of this file.
1/*
2 * LocklessAllocator.h - allocator with lockless alloc and free
3 *
4 * Copyright (c) 2016 Javier Serrano Polo <javier@jasp.net>
5 *
6 * This file is part of LMMS - https://lmms.io
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program (see COPYING); if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301 USA.
22 *
23 */
24
25#ifndef LMMS_LOCKLESS_ALLOCATOR_H
26#define LMMS_LOCKLESS_ALLOCATOR_H
27
28#include <atomic>
29#include <cstddef>
30
31
32namespace lmms
33{
34
35
37{
38public:
39 LocklessAllocator( size_t nmemb, size_t size );
40 virtual ~LocklessAllocator();
41 void * alloc();
42 void free( void * ptr );
43
44
45private:
46 char * m_pool;
47 size_t m_capacity;
49
50 std::atomic_int * m_freeState;
52
53 std::atomic_size_t m_available;
54 std::atomic_size_t m_startIndex;
55
56} ;
57
58
59
60
61template<typename T>
63{
64public:
65 LocklessAllocatorT( size_t nmemb ) :
66 LocklessAllocator( nmemb, sizeof( T ) )
67 {
68 }
69
70 ~LocklessAllocatorT() override = default;
71
72 T * alloc()
73 {
74 return (T *)LocklessAllocator::alloc();
75 }
76
77 void free( T * ptr )
78 {
80 }
81
82} ;
83
84
85} // namespace lmms
86
87#endif // LMMS_LOCKLESS_ALLOCATOR_H
size_t m_elementSize
Definition LocklessAllocator.h:48
std::atomic_size_t m_available
Definition LocklessAllocator.h:53
std::atomic_size_t m_startIndex
Definition LocklessAllocator.h:54
void free(void *ptr)
Definition LocklessAllocator.cpp:142
size_t m_freeStateSets
Definition LocklessAllocator.h:51
LocklessAllocator(size_t nmemb, size_t size)
Definition LocklessAllocator.cpp:55
virtual ~LocklessAllocator()
Definition LocklessAllocator.cpp:72
std::atomic_int * m_freeState
Definition LocklessAllocator.h:50
void * alloc()
Definition LocklessAllocator.cpp:107
char * m_pool
Definition LocklessAllocator.h:46
size_t m_capacity
Definition LocklessAllocator.h:47
LocklessAllocatorT(size_t nmemb)
Definition LocklessAllocator.h:65
void free(T *ptr)
Definition LocklessAllocator.h:77
~LocklessAllocatorT() override=default
T * alloc()
Definition LocklessAllocator.h:72
Definition AudioAlsa.cpp:35
ulg size
Definition extract.c:2350