Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/java/Handles.cpp

raw
1 /*
2 * Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2020 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 29 авг. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/fmt/java/Handles.h>
23 #include <stdlib.h>
24
25 #define CAP_GRANULARITY 0x400
26
27 namespace lsp
28 {
29 namespace java
30 {
31
32 Handles::Handles()
33 {
34 nCapacity = 0;
35 vItems = NULL;
36 nHandle = 0;
37 }
38
39 Handles::~Handles()
40 {
41 flush();
42 }
43
44 Object *Handles::get(size_t handle)
45 {
46 return (handle < nHandle) ? vItems[handle] : NULL;
47 }
48
49 status_t Handles::assign(Object *obj)
50 {
51 if (obj == NULL)
52 return STATUS_BAD_ARGUMENTS;
53
54 size_t cap = ((nHandle + CAP_GRANULARITY) / CAP_GRANULARITY) * CAP_GRANULARITY;
55 if (nCapacity < cap)
56 {
57 Object **x = reinterpret_cast<Object **>(::realloc(vItems, sizeof(Object *) * cap));
58 if (x == NULL)
59 return STATUS_NO_MEM;
60 for (size_t i=nCapacity; i<cap; ++i)
61 x[i] = NULL;
62 vItems = x;
63 nCapacity = cap;
64 }
65
66 vItems[nHandle++] = obj;
67 return STATUS_OK;
68 }
69
70 void Handles::clear()
71 {
72 if (vItems == NULL)
73 return;
74 for (size_t i=0; i<nCapacity; ++i)
75 {
76 if (vItems[i] == NULL)
77 continue;
78 delete vItems[i];
79 vItems[i] = NULL;
80 }
81
82 nHandle = 0;
83 }
84
85 void Handles::flush()
86 {
87 if (vItems == NULL)
88 return;
89 for (size_t i=0; i<nCapacity; ++i)
90 {
91 if (vItems[i] == NULL)
92 continue;
93 delete vItems[i];
94 }
95 ::free(vItems);
96 vItems = NULL;
97 nCapacity = 0;
98 nHandle = 0;
99 }
100
101 } /* namespace java */
102 } /* namespace lsp */