1 /*****************************************************************************
2 
3         CellPool.h
4         Author: Laurent de Soras, 2011
5 
6 --- Legal stuff ---
7 
8 This program is free software. It comes without any warranty, to
9 the extent permitted by applicable law. You can redistribute it
10 and/or modify it under the terms of the Do What The Fuck You Want
11 To Public License, Version 2, as published by Sam Hocevar. See
12 http://sam.zoy.org/wtfpl/COPYING for more details.
13 
14 *Tab=3***********************************************************************/
15 
16 
17 
18 #if ! defined (conc_CellPool_HEADER_INCLUDED)
19 #define	conc_CellPool_HEADER_INCLUDED
20 
21 #if defined (_MSC_VER)
22 	#pragma once
23 	#pragma warning (4 : 4250)
24 #endif
25 
26 
27 
28 /*\\\ INCLUDE FILES \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
29 
30 #include "conc/AtomicPtr.h"
31 #include "conc/AtomicInt.h"
32 #include "conc/LockFreeCell.h"
33 #include "conc/LockFreeStack.h"
34 #include "fstb/SingleObj.h"
35 
36 #include <array>
37 #include <mutex>
38 
39 #include <cstddef>
40 #include <cstdint>
41 
42 
43 
44 namespace conc
45 {
46 
47 
48 
49 template <class T>
50 class CellPool
51 {
52 
53 /*\\\ PUBLIC \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
54 
55 public:
56 
57 	typedef	T	DataType;
58 	typedef	LockFreeCell <T>	CellType;
59 
60 	               CellPool ();
61 	virtual        ~CellPool ();
62 
63 	void           clear_all ();
64 	void           expand_to (size_t nbr_cells);
65 
66 	inline CellType *
67 	               take_cell (bool autogrow_flag = false);
68 	inline void    return_cell (CellType &cell) noexcept;
69 
70 
71 
72 /*\\\ PROTECTED \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
73 
74 protected:
75 
76 
77 
78 /*\\\ PRIVATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
79 
80 private:
81 
82 	static const int  MAX_NBR_ZONES  = 64;
83 	static const int  GROW_RATE_NUM  = 3;
84 	static const int  GROW_RATE_DEN  = 2;
85 	static const int  BASE_SIZE      = 64; // Number of cells for the first zone
86 
87 	typedef  LockFreeStack <T>    CellStack;
88 	typedef  AtomicInt <size_t>   CountCells;
89 	typedef  AtomicInt <int>      CountZones;
90 	typedef  std::array <AtomicPtr <CellType>, MAX_NBR_ZONES>  ZoneList;
91 
92 	class Members	// These ones must be aligned
93 	{
94 	public:
95 		CountCells     _nbr_avail_cells;
96 		CountZones     _nbr_zones;
97 		ZoneList       _zone_list;
98 	};
99 
100 	class AliAllo
101 	{
102 	public:
103 		uint8_t *      _ptr;
104 		size_t         _nbr_elt;
105 	};
106 
107 	void           allocate_zone (size_t cur_size, AtomicPtr <CellType> & zone_ptr_ref);
108 
109 	static inline size_t
110 	               compute_grown_size (size_t prev_size);
111 	static inline size_t
112 	               compute_total_size_for_zones (int nbr_zones);
113 	static CellType *
114 	               alloc_cells (size_t n);
115 	static void    dealloc_cells (CellType *ptr);
116 
117 	CellStack      _cell_stack;
118 	std::mutex     _alloc_mutex;
119 	fstb::SingleObj <Members>
120 	               _m_ptr;
121 
122 
123 
124 /*\\\ FORBIDDEN MEMBER FUNCTIONS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
125 
126 private:
127 
128 	               CellPool (const CellPool <T> &other)          = delete;
129 	               CellPool (CellPool <T> &&other)               = delete;
130 	CellPool <T> & operator = (const CellPool <T> &other)        = delete;
131 	CellPool <T> & operator = (CellPool <T> &&other)             = delete;
132 	bool           operator == (const CellPool <T> &other) const = delete;
133 	bool           operator != (const CellPool <T> &other) const = delete;
134 
135 };	// class CellPool
136 
137 
138 
139 }	// namespace conc
140 
141 
142 
143 #include "conc/CellPool.hpp"
144 
145 
146 
147 #endif	// conc_CellPool_HEADER_INCLUDED
148 
149 
150 
151 /*\\\ EOF \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/
152