snapraid/raid/memory.h
Andrea Mazzoleni b709371d2d
Some checks are pending
Build / build-mac (push) Waiting to run
Build / build-linux (push) Waiting to run
Build / build-win (push) Waiting to run
CodeQL Advanced / Analyze (c-cpp) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
Fix typos in the comment of the raid library
2026-04-15 10:53:25 +02:00

85 lines
2.3 KiB
C

// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2013 Andrea Mazzoleni
#ifndef __RAID_MEMORY_H
#define __RAID_MEMORY_H
/**
* Memory alignment provided by raid_malloc().
*
* It should guarantee good cache performance everywhere.
*/
#define RAID_MALLOC_ALIGN 256
/**
* Memory displacement to avoid cache address sharing on contiguous blocks,
* used by raid_malloc_vector().
*
* When allocating a sequence of blocks with a size of power of 2,
* there is the risk that the addresses of each block are mapped into the
* same cache line and prefetching predictor, resulting in a lot of cache
* sharing if you access all the blocks in parallel, from the start to the
* end.
*
* To avoid this effect, it's better if all the blocks are allocated
* with a fixed displacement trying to reduce the cache address sharing.
*
* The selected displacement was chosen empirically with some speed tests
* with 8/12/16/20/24 data buffers of 256 KB.
*
* These are the results in MB/s with no displacement:
*
* sse2
* gen1 15368 [MB/s]
* gen2 6814 [MB/s]
* genz 3033 [MB/s]
*
* These are the results with displacement, resulting in improvements
* in the order of 20% or more:
*
* sse2
* gen1 21936 [MB/s]
* gen2 11902 [MB/s]
* genz 5838 [MB/s]
*
*/
#define RAID_MALLOC_DISPLACEMENT (7*256)
/**
* Aligned malloc.
* Use an alignment suitable for the raid functions.
*/
void *raid_malloc(size_t size, void **freeptr);
/**
* Arbitrary aligned malloc.
*/
void *raid_malloc_align(size_t size, size_t align_size, void **freeptr);
/**
* Aligned vector allocation.
* Use an alignment suitable for the raid functions.
* Returns a vector of @n pointers, each one pointing to a block of
* the specified @size.
* The first @nd elements are reversed in order.
*/
void **raid_malloc_vector(int nd, int n, size_t size, void **freeptr);
/**
* Arbitrary aligned vector allocation.
*/
void **raid_malloc_vector_align(int nd, int n, size_t size, size_t align_size, size_t displacement_size, void **freeptr);
/**
* Fills the memory vector with pseudo-random data based on the specified seed.
*/
void raid_mrand_vector(unsigned seed, int n, size_t size, void **vv);
/**
* Tests the memory vector for RAM problems.
* If a problem is found, it crashes.
*/
int raid_mtest_vector(int n, size_t size, void **vv);
#endif