updated copyright.
[platform/core/graphics/tizenvg.git] / src / lib / sw_engine / tvgSwMemPool.cpp
1 /*
2  * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22
23 #include "tvgSwCommon.h"
24
25
26 /************************************************************************/
27 /* Internal Class Implementation                                        */
28 /************************************************************************/
29
30
31 /************************************************************************/
32 /* External Class Implementation                                        */
33 /************************************************************************/
34
35 SwOutline* mpoolReqOutline(SwMpool* mpool, unsigned idx)
36 {
37     return &mpool->outline[idx];
38 }
39
40
41 void mpoolRetOutline(SwMpool* mpool, unsigned idx)
42 {
43     mpool->outline[idx].cntrsCnt = 0;
44     mpool->outline[idx].ptsCnt = 0;
45 }
46
47
48 SwOutline* mpoolReqStrokeOutline(SwMpool* mpool, unsigned idx)
49 {
50     return &mpool->strokeOutline[idx];
51 }
52
53
54 void mpoolRetStrokeOutline(SwMpool* mpool, unsigned idx)
55 {
56     mpool->strokeOutline[idx].cntrsCnt = 0;
57     mpool->strokeOutline[idx].ptsCnt = 0;
58 }
59
60
61 SwMpool* mpoolInit(unsigned threads)
62 {
63     if (threads == 0) threads = 1;
64
65     auto mpool = static_cast<SwMpool*>(calloc(sizeof(SwMpool), 1));
66     mpool->outline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
67     if (!mpool->outline) goto err;
68
69     mpool->strokeOutline = static_cast<SwOutline*>(calloc(1, sizeof(SwOutline) * threads));
70     if (!mpool->strokeOutline) goto err;
71
72     mpool->allocSize = threads;
73
74     return mpool;
75
76 err:
77     if (mpool->outline) {
78         free(mpool->outline);
79         mpool->outline = nullptr;
80     }
81
82     if (mpool->strokeOutline) {
83         free(mpool->strokeOutline);
84         mpool->strokeOutline = nullptr;
85     }
86     free(mpool);
87     return nullptr;
88 }
89
90
91 bool mpoolClear(SwMpool* mpool)
92 {
93     SwOutline* p;
94
95     for (unsigned i = 0; i < mpool->allocSize; ++i) {
96
97         //Outline
98         p = &mpool->outline[i];
99
100         free(p->cntrs);
101         p->cntrs = nullptr;
102
103         free(p->pts);
104         p->pts = nullptr;
105
106         free(p->types);
107         p->types = nullptr;
108
109         free(p->closed);
110         p->closed = nullptr;
111
112         p->cntrsCnt = p->reservedCntrsCnt = 0;
113         p->ptsCnt = p->reservedPtsCnt = 0;
114
115         //StrokeOutline
116         p = &mpool->strokeOutline[i];
117
118         free(p->cntrs);
119         p->cntrs = nullptr;
120
121         free(p->pts);
122         p->pts = nullptr;
123
124         free(p->types);
125         p->types = nullptr;
126
127         free(p->closed);
128         p->closed = nullptr;
129
130         p->cntrsCnt = p->reservedCntrsCnt = 0;
131         p->ptsCnt = p->reservedPtsCnt = 0;
132     }
133
134     return true;
135 }
136
137
138 bool mpoolTerm(SwMpool* mpool)
139 {
140     if (!mpool) return false;
141
142     mpoolClear(mpool);
143
144     if (mpool->outline) {
145         free(mpool->outline);
146         mpool->outline = nullptr;
147     }
148
149     if (mpool->strokeOutline) {
150         free(mpool->strokeOutline);
151         mpool->strokeOutline = nullptr;
152     }
153
154     free(mpool);
155
156     return true;
157 }