|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) Adel Mamin |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +/** |
| 26 | + * @file |
| 27 | + * |
| 28 | + * Event API implementation. |
| 29 | + */ |
| 30 | + |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#include "common/compiler.h" /* IWYU pragma: keep */ |
| 34 | +#include "common/macros.h" |
| 35 | +#include "onesize/onesize.h" |
| 36 | +#include "blk/blk.h" |
| 37 | +#include "pal/pal.h" |
| 38 | +#include "event.h" |
| 39 | + |
| 40 | +#define AM_EVENT_IS_STATIC(event) (0 == (event)->pool_index) |
| 41 | + |
| 42 | +/** Event internal state. */ |
| 43 | +struct am_event_state { |
| 44 | + /** user defined event memory pools */ |
| 45 | + struct am_onesize pool[AM_EVENT_POOL_NUM_MAX]; |
| 46 | + /** the number of user defined event memory pools */ |
| 47 | + int npool; |
| 48 | +}; |
| 49 | + |
| 50 | +static struct am_event_state am_event_state_; |
| 51 | + |
| 52 | +void am_event_add_pool(void *pool, int size, int block_size, int alignment) { |
| 53 | + struct am_event_state *me = &am_event_state_; |
| 54 | + AM_ASSERT(me->npool < AM_EVENT_POOL_NUM_MAX); |
| 55 | + if (me->npool > 0) { |
| 56 | + int prev_size = am_onesize_get_block_size(&me->pool[me->npool - 1]); |
| 57 | + AM_ASSERT(block_size > prev_size); |
| 58 | + } |
| 59 | + |
| 60 | + struct am_blk blk; |
| 61 | + memset(&blk, 0, sizeof(blk)); |
| 62 | + blk.ptr = pool; |
| 63 | + blk.size = size; |
| 64 | + am_onesize_init(&me->pool[me->npool], &blk, block_size, alignment); |
| 65 | + |
| 66 | + ++me->npool; |
| 67 | +} |
| 68 | + |
| 69 | +struct am_event *am_event_allocate(int id, int size, int margin) { |
| 70 | + struct am_event_state *me = &am_event_state_; |
| 71 | + AM_ASSERT(size > 0); |
| 72 | + AM_ASSERT(me->npool); |
| 73 | + AM_ASSERT(size <= am_onesize_get_block_size(&me->pool[me->npool - 1])); |
| 74 | + AM_ASSERT(id >= AM_EVT_USER); |
| 75 | + AM_ASSERT(margin >= 0); |
| 76 | + |
| 77 | + for (int i = 0; i < me->npool; i++) { |
| 78 | + struct am_onesize *osz = &me->pool[i]; |
| 79 | + if (size > am_onesize_get_block_size(osz)) { |
| 80 | + continue; |
| 81 | + } |
| 82 | + |
| 83 | + am_pal_crit_enter(); |
| 84 | + |
| 85 | + int nfree = am_onesize_get_nfree(osz); |
| 86 | + if (margin && (nfree <= margin)) { |
| 87 | + am_pal_crit_exit(); |
| 88 | + break; |
| 89 | + } |
| 90 | + if (!nfree) { |
| 91 | + am_pal_crit_exit(); |
| 92 | + continue; |
| 93 | + } |
| 94 | + struct am_event *event = |
| 95 | + (struct am_event *)am_onesize_allocate(osz, size); |
| 96 | + am_pal_crit_exit(); |
| 97 | + |
| 98 | + AM_ASSERT(event); |
| 99 | + |
| 100 | + /* event pointer is guaranteeed to be non-NULL here */ |
| 101 | + AM_DISABLE_WARNING(AM_W_NULL_DEREFERENCE); |
| 102 | + memset(event, 0, sizeof(*event)); |
| 103 | + event->id = id; |
| 104 | + event->pool_index = |
| 105 | + (unsigned)i & ((1U << AM_EVENT_POOL_INDEX_BITS) - 1); |
| 106 | + AM_ENABLE_WARNING(AM_W_NULL_DEREFERENCE); |
| 107 | + |
| 108 | + return event; |
| 109 | + } |
| 110 | + |
| 111 | + AM_ASSERT(margin); |
| 112 | + |
| 113 | + return NULL; |
| 114 | +} |
| 115 | + |
| 116 | +void am_event_free(const struct am_event *event) { |
| 117 | + if (NULL == event) { |
| 118 | + return; |
| 119 | + } |
| 120 | + if (AM_EVENT_IS_STATIC(event)) { |
| 121 | + return; /* the event is statically allocated */ |
| 122 | + } |
| 123 | + |
| 124 | + am_pal_crit_enter(); |
| 125 | + |
| 126 | + if (event->ref_counter > 1) { |
| 127 | + --AM_CAST(struct am_event *, event)->ref_counter; |
| 128 | + am_pal_crit_exit(); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + AM_ASSERT(event->pool_index <= AM_EVENT_POOL_NUM_MAX); |
| 133 | + am_onesize_free(&am_event_state_.pool[event->pool_index - 1], event); |
| 134 | + |
| 135 | + am_pal_crit_exit(); |
| 136 | +} |
| 137 | + |
| 138 | +int am_event_get_pool_min_nfree(int index) { |
| 139 | + struct am_event_state *me = &am_event_state_; |
| 140 | + AM_ASSERT(index >= 0); |
| 141 | + AM_ASSERT(index < me->npool); |
| 142 | + return am_onesize_get_min_nfree(&me->pool[index]); |
| 143 | +} |
| 144 | + |
| 145 | +int am_event_get_pool_nfree_now(int index) { |
| 146 | + struct am_event_state *me = &am_event_state_; |
| 147 | + AM_ASSERT(index >= 0); |
| 148 | + AM_ASSERT(index < me->npool); |
| 149 | + return am_onesize_get_nfree(&me->pool[index]); |
| 150 | +} |
| 151 | + |
| 152 | +int am_event_get_pool_nblocks(int index) { |
| 153 | + struct am_event_state *me = &am_event_state_; |
| 154 | + AM_ASSERT(index >= 0); |
| 155 | + AM_ASSERT(index < me->npool); |
| 156 | + return am_onesize_get_nblocks(&me->pool[index]); |
| 157 | +} |
| 158 | + |
| 159 | +int am_event_get_pools_num(void) { return am_event_state_.npool; } |
0 commit comments