|
|
1.1 ! root 1: // ! 2: // nono ! 3: // Copyright (C) 2024 nono project ! 4: // Licensed under nono-license.txt ! 5: // ! 6: ! 7: // ! 8: // アラインされた配列 ! 9: // ! 10: ! 11: #pragma once ! 12: ! 13: #include <stdlib.h> ! 14: ! 15: template<typename T, std::size_t align> ! 16: class AlignedArray ! 17: { ! 18: public: ! 19: AlignedArray() ! 20: { ! 21: } ! 22: ! 23: explicit AlignedArray(std::size_t num_) ! 24: { ! 25: Alloc(num_); ! 26: } ! 27: ! 28: ~AlignedArray() ! 29: { ! 30: Reset(); ! 31: } ! 32: ! 33: void Reset() ! 34: { ! 35: free(ptr); ! 36: ptr = NULL; ! 37: num = 0; ! 38: } ! 39: ! 40: bool Reset(std::size_t new_num) ! 41: { ! 42: if (new_num == num) { ! 43: return true; ! 44: } ! 45: Reset(); ! 46: return Alloc(new_num); ! 47: } ! 48: ! 49: T *get() const noexcept ! 50: { ! 51: return ptr; ! 52: } ! 53: ! 54: explicit operator bool() const noexcept ! 55: { ! 56: return (ptr != NULL); ! 57: } ! 58: ! 59: private: ! 60: bool Alloc(std::size_t new_num) ! 61: { ! 62: assert(ptr == NULL); ! 63: ! 64: void *tmp; ! 65: int r = posix_memalign(&tmp, align, new_num * sizeof(T)); ! 66: if (__predict_false(r != 0)) { ! 67: return false; ! 68: } ! 69: ptr = reinterpret_cast<T*>(tmp); ! 70: num = new_num; ! 71: return true; ! 72: } ! 73: ! 74: T *ptr {}; ! 75: std::size_t num {}; ! 76: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.