1 /*
2  * Copyright (C) 2015 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef _gcache_limits_hpp_
6 #define _gcache_limits_hpp_
7 
8 #include "gcache_bh.hpp"
9 #include <gu_macros.hpp> // GU_COMPILE_ASSERT
10 #include <limits>
11 
12 namespace gcache
13 {
14     class Limits
15     {
16     public:
17 
18         typedef MemOps::size_type  size_type;
19         typedef MemOps::ssize_type ssize_type;
20 
21         static ssize_type const SSIZE_MAX_ =
22             (1ULL << (sizeof(ssize_type)*8 - 1)) - 1;
23 
24         static size_type const MAX_SIZE = sizeof(BufferHeader) + SSIZE_MAX_;
25 
26         static size_type const MIN_SIZE = sizeof(BufferHeader) + 1;
27 
assert_size(unsigned long long s)28         static inline void assert_size(unsigned long long s)
29         {
30 #ifndef NDEBUG
31             assert(s <= MAX_SIZE);
32             assert(s >= MIN_SIZE);
33 #endif /* NDEBUG */
34         }
35 
36     private:
37 
38         /* the difference between MAX_SIZE and MIN_SIZE should never exceed
39          * diff_type capacity */
40 
41         GU_COMPILE_ASSERT(MAX_SIZE > MIN_SIZE, max_min);
42 
43         typedef MemOps::diff_type diff_type;
44 
45         static diff_type const DIFF_MAX =
46             (1ULL << (sizeof(diff_type)*8 - 1)) - 1;
47 
48         GU_COMPILE_ASSERT(DIFF_MAX >= 0, diff_max);
49         GU_COMPILE_ASSERT(size_type(DIFF_MAX) >= MAX_SIZE - MIN_SIZE, max_diff);
50 
51         static diff_type const DIFF_MIN = -DIFF_MAX - 1;
52 
53         typedef long long long_long;
54 
55         GU_COMPILE_ASSERT(DIFF_MIN < 0, diff_min);
56         GU_COMPILE_ASSERT(DIFF_MIN + MAX_SIZE <= MIN_SIZE, min_diff);
57 
58     }; /* class Limits */
59 }
60 
61 #endif /* _gcache_limits_hpp_ */
62