|
| 1 | +/* |
| 2 | + * Copyright (C) 2023-2024 Intel Corporation |
| 3 | + * |
| 4 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 5 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +*/ |
| 7 | + |
| 8 | +#ifndef UMF_COARSE_PROVIDER_H |
| 9 | +#define UMF_COARSE_PROVIDER_H |
| 10 | + |
| 11 | +#include <stdbool.h> |
| 12 | +#include <umf/memory_provider.h> |
| 13 | + |
| 14 | +#ifdef __cplusplus |
| 15 | +extern "C" { |
| 16 | +#endif |
| 17 | + |
| 18 | +typedef enum coarse_memory_provider_strategy_t { |
| 19 | + /// Always allocate a free block of the (size + alignment) size |
| 20 | + /// and cut out the properly aligned part leaving two remaining parts. |
| 21 | + /// It is the fastest strategy but causes memory fragmentation |
| 22 | + /// when alignment is greater than 0. |
| 23 | + /// It is the best strategy when alignment always equals 0. |
| 24 | + UMF_COARSE_MEMORY_STRATEGY_FASTEST = 0, |
| 25 | + |
| 26 | + /// First check if the first free block of the 'size' size has the correct alignment. |
| 27 | + /// If not, use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy. |
| 28 | + UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE, |
| 29 | + |
| 30 | + /// First look through all free blocks of the 'size' size |
| 31 | + /// and choose the first one with the correct alignment. |
| 32 | + /// If none of them had the correct alignment, |
| 33 | + /// use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy. |
| 34 | + UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE, |
| 35 | + |
| 36 | + /// The maximum value (it has to be the last one). |
| 37 | + UMF_COARSE_MEMORY_STRATEGY_MAX |
| 38 | +} coarse_memory_provider_strategy_t; |
| 39 | + |
| 40 | +/// @brief Coarse Memory Provider settings struct. |
| 41 | +typedef struct coarse_memory_provider_params_t { |
| 42 | + /// Handle to the upstream memory provider. |
| 43 | + /// It has to be NULL if init_buffer is set. |
| 44 | + /// Exactly one of them has to be non-NULL. |
| 45 | + umf_memory_provider_handle_t upstream_memory_provider; |
| 46 | + |
| 47 | + /// Memory allocation strategy. |
| 48 | + /// See coarse_memory_provider_strategy_t for details. |
| 49 | + coarse_memory_provider_strategy_t memory_strategy; |
| 50 | + |
| 51 | + /// Init buffer used to pre-allocate memory at the creation time. |
| 52 | + /// It has to be NULL if upstream_memory_provider is set. |
| 53 | + /// Exactly one of them has to be non-NULL. |
| 54 | + void *init_buffer; |
| 55 | + |
| 56 | + /// Size of the pre-allocated buffer. If the `init_buffer` is set, |
| 57 | + /// the `init_buffer_size` should be the size of this buffer. |
| 58 | + size_t init_buffer_size; |
| 59 | + |
| 60 | + /// When it is true and the upstream_memory_provider is given, |
| 61 | + /// the init buffer (of `init_buffer_size` bytes) would be pre-allocated |
| 62 | + /// during creation time using the `upstream_memory_provider`. |
| 63 | + /// If upstream_memory_provider is not given, |
| 64 | + /// the init_buffer is always used instead |
| 65 | + /// (regardless of the value of this parameter). |
| 66 | + bool immediate_init_from_upstream; |
| 67 | +} coarse_memory_provider_params_t; |
| 68 | + |
| 69 | +/// @brief Coarse Memory Provider stats (TODO move to CTL) |
| 70 | +typedef struct coarse_memory_provider_stats_t { |
| 71 | + /// Total allocation size. |
| 72 | + size_t alloc_size; |
| 73 | + |
| 74 | + /// Size of used memory. |
| 75 | + size_t used_size; |
| 76 | + |
| 77 | + /// Number of memory blocks allocated from the upstream provider. |
| 78 | + size_t num_upstream_blocks; |
| 79 | + |
| 80 | + /// Total number of allocated memory blocks. |
| 81 | + size_t num_all_blocks; |
| 82 | + |
| 83 | + /// Number of free memory blocks. |
| 84 | + size_t num_free_blocks; |
| 85 | +} coarse_memory_provider_stats_t; |
| 86 | + |
| 87 | +umf_memory_provider_ops_t *umfCoarseMemoryProviderOps(void); |
| 88 | + |
| 89 | +// TODO use CTL |
| 90 | +coarse_memory_provider_stats_t |
| 91 | +umfCoarseMemoryProviderGetStats(umf_memory_provider_handle_t provider); |
| 92 | + |
| 93 | +#ifdef __cplusplus |
| 94 | +} |
| 95 | +#endif |
| 96 | + |
| 97 | +#endif // UMF_COARSE_PROVIDER_H |
0 commit comments