extern */
| 29 | namespace dsn { |
| 30 | |
| 31 | /* extern */ void *cacheline_aligned_alloc(size_t size) |
| 32 | { |
| 33 | if (dsn_unlikely(size == 0)) { |
| 34 | return nullptr; |
| 35 | } |
| 36 | |
| 37 | void *buffer = nullptr; |
| 38 | // CACHELINE_SIZE must be a power of 2 and a multiple of sizeof(void *), which have been |
| 39 | // checked statically at compile time when CACHELINE_SIZE is defined as macro. |
| 40 | int err = posix_memalign(&buffer, CACHELINE_SIZE, size); |
| 41 | |
| 42 | // Generally there are 2 possible errors for posix_memalign as below: |
| 43 | // [EINVAL] |
| 44 | // The value of the alignment parameter is not a power of two multiple of sizeof(void *). |
| 45 | // [ENOMEM] |
| 46 | // There is insufficient memory available with the requested alignment. |
| 47 | // Thus making an assertion here is enough. |
| 48 | CHECK_EQ_MSG(err, 0, "error calling posix_memalign: {}", utils::safe_strerror(err)); |
| 49 | |
| 50 | return buffer; |
| 51 | } |
| 52 | |
| 53 | /* extern */ void cacheline_aligned_free(void *mem_block) { free(mem_block); } |
| 54 |
no test coverage detected