1 /*
2 * Copyright (c) 2002
3 * John Maddock
4 *
5 * Use, modification and distribution are subject to the
6 * Boost Software License, Version 1.0. (See accompanying file
7 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 *
9 */
10
11 /*
12 * LOCATION: see http://www.boost.org for most recent version.
13 * FILE mem_block_cache.hpp
14 * VERSION see <boost/version.hpp>
15 * DESCRIPTION: memory block cache used by the non-recursive matcher.
16 */
17
18#ifndef BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
19#define BOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
20
21#include <new>
22#ifdef BOOST_HAS_THREADS
23#include <boost/regex/pending/static_mutex.hpp>
24#endif
25
26#ifdef BOOST_HAS_ABI_HEADERS
27# include BOOST_ABI_PREFIX
28#endif
29
30namespace boost{
31namespace BOOST_REGEX_DETAIL_NS{
32
33struct mem_block_node
34{
35 mem_block_node* next;
36};
37
38struct mem_block_cache
39{
40 // this member has to be statically initialsed:
41 mem_block_node* next;
42 unsigned cached_blocks;
43#ifdef BOOST_HAS_THREADS
44 boost::static_mutex mut;
45#endif
46
47 ~mem_block_cache()
48 {
49 while(next)
50 {
51 mem_block_node* old = next;
52 next = next->next;
53 ::operator delete(old);
54 }
55 }
56 void* get()
57 {
58#ifdef BOOST_HAS_THREADS
59 boost::static_mutex::scoped_lock g(mut);
60#endif
61 if(next)
62 {
63 mem_block_node* result = next;
64 next = next->next;
65 --cached_blocks;
66 return result;
67 }
68 return ::operator new(BOOST_REGEX_BLOCKSIZE);
69 }
70 void put(void* p)
71 {
72#ifdef BOOST_HAS_THREADS
73 boost::static_mutex::scoped_lock g(mut);
74#endif
75 if(cached_blocks >= BOOST_REGEX_MAX_CACHE_BLOCKS)
76 {
77 ::operator delete(p);
78 }
79 else
80 {
81 mem_block_node* old = static_cast<mem_block_node*>(p);
82 old->next = next;
83 next = old;
84 ++cached_blocks;
85 }
86 }
87};
88
89extern mem_block_cache block_cache;
90
91}
92} // namespace boost
93
94#ifdef BOOST_HAS_ABI_HEADERS
95# include BOOST_ABI_SUFFIX
96#endif
97
98#endif
99
100

source code of boost/boost/regex/v4/mem_block_cache.hpp