1 /*
2  * Copyright (c) 2022 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #pragma once
25 
26 #include <reflist.h>
27 #include <stdint.h>
28 #include <sys/types.h>
29 
30 struct res_group {
31     struct obj obj;
32     size_t reserved_pages;
33     size_t used_pages;
34     bool is_shutdown;
35 };
36 
37 /**
38  * res_group_create() - Create a resource group.
39  * @pages: the number of pages to reserve.
40  * @ref: the `obj_ref` for the group.
41  */
42 struct res_group* res_group_create(size_t pages, struct obj_ref* ref);
43 
44 /**
45  * res_group_add_ref() - Add a reference to the resource group.
46  * @res_group: the resource group to operate on.
47  * @ref: the `obj_ref` to add.
48  */
49 void res_group_add_ref(struct res_group* res_group, struct obj_ref* ref);
50 
51 /**
52  * res_group_del_ref() - Remove a reference from the resource group.
53  * @res_group: the resource group to operate on.
54  * @ref: the `obj_ref` to remove.
55  */
56 void res_group_del_ref(struct res_group* res_group, struct obj_ref* ref);
57 
58 /**
59  * res_group_shutdown() - Mark the resource group as shutdown. Unreserves
60  *                        unused memory and prevents new allocations.
61  * @res_group: the resource group to operate on.
62  */
63 status_t res_group_shutdown(struct res_group* res_group);
64 
65 /**
66  * res_group_take_mem() - Take memory.
67  * @res_group: the resource group to operate on.
68  * @pages: the number of pages to take.
69  */
70 status_t res_group_take_mem(struct res_group* res_group, size_t pages);
71 
72 /**
73  * res_group_release_mem() - Release memory.
74  * @res_group: the resource group to operate on.
75  * @pages: the number of pages to release.
76  */
77 void res_group_release_mem(struct res_group* res_group, size_t pages);
78 
79