diff options
author | D. Shah <dave@ds0.me> | 2021-01-29 11:01:36 +0000 |
---|---|---|
committer | D. Shah <dave@ds0.me> | 2021-02-02 16:58:57 +0000 |
commit | 90edf33c95de975b7f8204624a995c4826c52b84 (patch) | |
tree | 02f43a6a5bb938de5fcb852fa113d65168a2f3fd | |
parent | 9c5d13a630581c08c7f828a8db192721314e32c3 (diff) | |
download | nextpnr-90edf33c95de975b7f8204624a995c4826c52b84.tar.gz nextpnr-90edf33c95de975b7f8204624a995c4826c52b84.tar.bz2 nextpnr-90edf33c95de975b7f8204624a995c4826c52b84.zip |
common: Adding IdStringList type
Using an optimised storage for <=4 objects to avoid excessive heap
allocations.
Signed-off-by: D. Shah <dave@ds0.me>
-rw-r--r-- | common/nextpnr.h | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/common/nextpnr.h b/common/nextpnr.h index b4f68f93..68a341f3 100644 --- a/common/nextpnr.h +++ b/common/nextpnr.h @@ -145,6 +145,73 @@ template <> struct hash<NEXTPNR_NAMESPACE_PREFIX IdString> NEXTPNR_NAMESPACE_BEGIN +// An small size optimised array that is statically allocated when the size is N or less; heap allocated otherwise +template <typename T, size_t N> class SSOArray +{ + union + { + T data_static[N]; + T *data_heap; + }; + size_t m_size; + + private: + inline bool is_heap() { return (m_size > N); } + void alloc() + { + if (is_heap()) { + data_heap = new T[m_size]; + } + } + + public: + T *data() { return is_heap() ? data_heap : &data_static; } + const T *data() const { return is_heap() ? data_heap : &data_static; } + size_t size() const { return m_size; } + + T *begin() { return data(); } + T *end() { return data() + m_size; } + const T *begin() const { return data(); } + const T *end() const { return data() + m_size; } + + SSOArray(size_t size, const T &init = T()) : m_size(size) + { + alloc(); + std::fill(begin(), end(), init); + } + + template <typename Tother> SSOArray(const Tother &other) : m_size(other.size()) + { + alloc(); + std::copy(other.begin(), other.end(), begin()); + } + + ~SSOArray() + { + if (is_heap()) { + delete[] data_heap; + } + } + + bool operator==(const SSOArray &other) const + { + if (size() != other.size()) + return false; + return std::equal(begin(), end(), other.begin()); + } + bool operator!=(const SSOArray &other) const + { + if (size() != other.size()) + return true; + return !std::equal(begin(), end(), other.begin()); + } +}; + +struct IdStringList +{ + SSOArray<IdString, 4> ids; +}; + struct GraphicElement { enum type_t |