EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
handle_manager.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/handle_manager.h: A utility to manage handing out integer handles to resources. */
21 
22 #ifndef ETCPAL_HANDLE_MANAGER_H_
23 #define ETCPAL_HANDLE_MANAGER_H_
24 
25 #include <stdbool.h>
26 #include <stdint.h>
27 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
50 typedef bool (*HandleValueInUseFunction)(int handle_val, void* context);
51 
53 typedef struct IntHandleManager
54 {
59  int max_value;
68  void* context;
70 
72  int max_value,
73  HandleValueInUseFunction value_in_use_func,
74  void* context);
76 
77 #ifdef __cplusplus
78 }
79 #endif
80 
85 #endif /* ETCPAL_HANDLE_MANAGER_H_ */
void init_int_handle_manager(IntHandleManager *manager, int max_value, HandleValueInUseFunction value_in_use_func, void *context)
Initialize an IntHandleManager instance.
Definition: handle_manager.c:33
int get_next_int_handle(IntHandleManager *manager)
Once the int handle manager is initialized, this is used to get the next handle value.
Definition: handle_manager.c:53
bool(* HandleValueInUseFunction)(int handle_val, void *context)
A pointer to a function that tests if a given handle value is currently assigned to a resource.
Definition: handle_manager.h:50
struct IntHandleManager IntHandleManager
State for managing generic integer handle values.
State for managing generic integer handle values.
Definition: handle_manager.h:54
int last_handle
The last handle that was handed out, or -1 if none have been handed out yet.
Definition: handle_manager.h:56
bool check_value_in_use
Optimize the handle generation algorithm by tracking whether the handle value has wrapped around.
Definition: handle_manager.h:63
HandleValueInUseFunction value_in_use
Function pointer to determine if a handle value is currently in use.
Definition: handle_manager.h:66
void * context
A context passed to the value in use function.
Definition: handle_manager.h:68
int max_value
Determines a custom wrap-around point.
Definition: handle_manager.h:59