|
| 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 | +/// @brief Coarse Memory Provider allocation strategy |
| 19 | +typedef enum coarse_memory_provider_strategy_t { |
| 20 | + /// Always allocate a free block of the (size + alignment) size |
| 21 | + /// and cut out the properly aligned part leaving two remaining parts. |
| 22 | + /// It is the fastest strategy but causes memory fragmentation |
| 23 | + /// when alignment is greater than 0. |
| 24 | + /// It is the best strategy when alignment always equals 0. |
| 25 | + UMF_COARSE_MEMORY_STRATEGY_FASTEST = 0, |
| 26 | + |
| 27 | + /// Check if the first free block of the 'size' size has the correct alignment. |
| 28 | + /// If not, use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy. |
| 29 | + UMF_COARSE_MEMORY_STRATEGY_FASTEST_BUT_ONE, |
| 30 | + |
| 31 | + /// Look through all free blocks of the 'size' size |
| 32 | + /// and choose the first one with the correct alignment. |
| 33 | + /// If none of them had the correct alignment, |
| 34 | + /// use the `UMF_COARSE_MEMORY_STRATEGY_FASTEST` strategy. |
| 35 | + UMF_COARSE_MEMORY_STRATEGY_CHECK_ALL_SIZE, |
| 36 | + |
| 37 | + /// The maximum value (it has to be the last one). |
| 38 | + UMF_COARSE_MEMORY_STRATEGY_MAX |
| 39 | +} coarse_memory_provider_strategy_t; |
| 40 | + |
| 41 | +/// @brief Coarse Memory Provider settings struct. |
| 42 | +typedef struct coarse_memory_provider_params_t { |
| 43 | + /// Handle to the upstream memory provider. |
| 44 | + /// It has to be NULL if init_buffer is set |
| 45 | + /// (exactly one of them has to be non-NULL). |
| 46 | + umf_memory_provider_handle_t upstream_memory_provider; |
| 47 | + |
| 48 | + /// Memory allocation strategy. |
| 49 | + /// See coarse_memory_provider_strategy_t for details. |
| 50 | + coarse_memory_provider_strategy_t allocation_strategy; |
| 51 | + |
| 52 | + /// A pre-allocated buffer that will be the only memory that |
| 53 | + /// the coarse provider can provide (the fixed-size memory provider option). |
| 54 | + /// If it is non-NULL, `init_buffer_size ` has to contain its size. |
| 55 | + /// It has to be NULL if upstream_memory_provider is set |
| 56 | + /// (exactly one of them has to be non-NULL). |
| 57 | + void *init_buffer; |
| 58 | + |
| 59 | + /// Size of the initial buffer: |
| 60 | + /// 1) `init_buffer` if it is non-NULL xor |
| 61 | + /// 2) that will be allocated from the upstream_memory_provider |
| 62 | + /// (if it is non-NULL) in the `.initialize` operation. |
| 63 | + size_t init_buffer_size; |
| 64 | + |
| 65 | + /// When it is true and the upstream_memory_provider is given, |
| 66 | + /// the init buffer (of `init_buffer_size` bytes) would be pre-allocated |
| 67 | + /// during creation time using the `upstream_memory_provider`. |
| 68 | + /// If upstream_memory_provider is not given, |
| 69 | + /// the init_buffer is always used instead |
| 70 | + /// (regardless of the value of this parameter). |
| 71 | + bool immediate_init_from_upstream; |
| 72 | +} coarse_memory_provider_params_t; |
| 73 | + |
| 74 | +/// @brief Coarse Memory Provider stats (TODO move to CTL) |
| 75 | +typedef struct coarse_memory_provider_stats_t { |
| 76 | + /// Total allocation size. |
| 77 | + size_t alloc_size; |
| 78 | + |
| 79 | + /// Size of used memory. |
| 80 | + size_t used_size; |
| 81 | + |
| 82 | + /// Number of memory blocks allocated from the upstream provider. |
| 83 | + size_t num_upstream_blocks; |
| 84 | + |
| 85 | + /// Total number of allocated memory blocks. |
| 86 | + size_t num_all_blocks; |
| 87 | + |
| 88 | + /// Number of free memory blocks. |
| 89 | + size_t num_free_blocks; |
| 90 | +} coarse_memory_provider_stats_t; |
| 91 | + |
| 92 | +umf_memory_provider_ops_t *umfCoarseMemoryProviderOps(void); |
| 93 | + |
| 94 | +// TODO use CTL |
| 95 | +coarse_memory_provider_stats_t |
| 96 | +umfCoarseMemoryProviderGetStats(umf_memory_provider_handle_t provider); |
| 97 | + |
| 98 | +#ifdef __cplusplus |
| 99 | +} |
| 100 | +#endif |
| 101 | + |
| 102 | +#endif // UMF_COARSE_PROVIDER_H |
0 commit comments