Posted onInDatabase
,
MySQL
,
MySQL 源码解读Views: Symbols count in article: 775Reading time ≈3 mins.
MySQL 源码解读 -- PFS
pfs buffer container
很3个核心的类,
template <class T, int PFS_PAGE_SIZE, int PFS_PAGE_COUNT, class U = PFS_buffer_default_array, class V = PFS_buffer_default_allocator> class PFS_buffer_scalable_container
template <class T> class PFS_buffer_default_array { bool m_full; PFS_cacheline_atomic_size_t m_monotonic; T *m_ptr; size_t m_max; /** Container. */ PFS_opaque_container *m_container; /* version number for dynamic reclaim, each time array is used, version increase. */ PFS_cacheline_atomic_size_t m_version; /* indicating whether page is used, this is to avoid new allocated page been freed without ever being used. */ std::atomic<bool> m_used{false}; /** page index */ uint m_page_index; }
template <class T> class PFS_buffer_default_array { bool m_full; PFS_cacheline_atomic_size_t m_monotonic; T *m_ptr; size_t m_max; /** Container. */ PFS_opaque_container *m_container; /* version number for dynamic reclaim, each time array is used, version increase. */ PFS_cacheline_atomic_size_t m_version; /* indicating whether page is used, this is to avoid new allocated page been freed without ever being used. */ std::atomic<bool> m_used{false}; /** page index */ uint m_page_index; }
唯一一个额外的地方,是alloc 函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
value_type *allocate(pfs_dirty_state *dirty_state) { 根据monotonic, 拿到对应位置的内存, 然后标记这个地方被申请 if (pfs->m_lock.free_to_dirty(dirty_state)) { /* increase version */ size_t version = m_version.m_size_t++; if (unlikely(version == SIZE_MAX)) { m_version.m_size_t.store(1); } m_used = true; return pfs; }