blob: fe71a3be757f0d6998f12edeed0781230527efb5 [file] [log] [blame]
Jeff Thompson86b6d642013-10-17 15:01:56 -07001 /*
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 <ndnboost/version.hpp>
15 * DESCRIPTION: memory block cache used by the non-recursive matcher.
16 */
17
18#ifndef NDNBOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
19#define NDNBOOST_REGEX_V4_MEM_BLOCK_CACHE_HPP
20
21#include <new>
22#ifdef NDNBOOST_HAS_THREADS
23#include <ndnboost/regex/pending/static_mutex.hpp>
24#endif
25
26#ifdef NDNBOOST_HAS_ABI_HEADERS
27# include NDNBOOST_ABI_PREFIX
28#endif
29
30namespace ndnboost{
31namespace re_detail{
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 NDNBOOST_HAS_THREADS
44 ndnboost::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 NDNBOOST_HAS_THREADS
59 ndnboost::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(NDNBOOST_REGEX_BLOCKSIZE);
69 }
70 void put(void* p)
71 {
72#ifdef NDNBOOST_HAS_THREADS
73 ndnboost::static_mutex::scoped_lock g(mut);
74#endif
75 if(cached_blocks >= NDNBOOST_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 ndnboost
93
94#ifdef NDNBOOST_HAS_ABI_HEADERS
95# include NDNBOOST_ABI_SUFFIX
96#endif
97
98#endif
99