add single instance mode
[uwplayer.git] / ucx / mempool.c
1 /*
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3  *
4  * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   1. Redistributions of source code must retain the above copyright
10  *      notice, this list of conditions and the following disclaimer.
11  *
12  *   2. Redistributions in binary form must reproduce the above copyright
13  *      notice, this list of conditions and the following disclaimer in the
14  *      documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "cx/mempool.h"
30 #include "cx/utils.h"
31 #include <string.h>
32
33 struct cx_mempool_memory_s {
34     /** The destructor. */
35     cx_destructor_func destructor;
36     /** The actual memory. */
37     char c[];
38 };
39
40 static void *cx_mempool_malloc(
41         void *p,
42         size_t n
43 ) {
44     struct cx_mempool_s *pool = p;
45
46     if (pool->size >= pool->capacity) {
47         size_t newcap = pool->capacity - (pool->capacity % 16) + 16;
48         struct cx_mempool_memory_s **newdata = realloc(pool->data, newcap*sizeof(struct cx_mempool_memory_s*));
49         if (newdata == NULL) {
50             return NULL;
51         }
52         pool->data = newdata;
53         pool->capacity = newcap;
54     }
55
56     struct cx_mempool_memory_s *mem = malloc(sizeof(cx_destructor_func) + n);
57     if (mem == NULL) {
58         return NULL;
59     }
60
61     mem->destructor = pool->auto_destr;
62     pool->data[pool->size] = mem;
63     pool->size++;
64
65     return mem->c;
66 }
67
68 static void *cx_mempool_calloc(
69         void *p,
70         size_t nelem,
71         size_t elsize
72 ) {
73     size_t msz;
74     if (cx_szmul(nelem, elsize, &msz)) {
75         return NULL;
76     }
77     void *ptr = cx_mempool_malloc(p, msz);
78     if (ptr == NULL) {
79         return NULL;
80     }
81     memset(ptr, 0, nelem * elsize);
82     return ptr;
83 }
84
85 static void *cx_mempool_realloc(
86         void *p,
87         void *ptr,
88         size_t n
89 ) {
90     struct cx_mempool_s *pool = p;
91
92     struct cx_mempool_memory_s *mem, *newm;
93     mem = (struct cx_mempool_memory_s*)(((char *) ptr) - sizeof(cx_destructor_func));
94     newm = realloc(mem, n + sizeof(cx_destructor_func));
95
96     if (newm == NULL) {
97         return NULL;
98     }
99     if (mem != newm) {
100         cx_for_n(i, pool->size) {
101             if (pool->data[i] == mem) {
102                 pool->data[i] = newm;
103                 return ((char*)newm) + sizeof(cx_destructor_func);
104             }
105         }
106         abort();
107     } else {
108         return ptr;
109     }
110 }
111
112 static void cx_mempool_free(
113         void *p,
114         void *ptr
115 ) {
116     struct cx_mempool_s *pool = p;
117
118     struct cx_mempool_memory_s *mem = (struct cx_mempool_memory_s *)
119             ((char *) ptr - sizeof(cx_destructor_func));
120
121     cx_for_n(i, pool->size) {
122         if (mem == pool->data[i]) {
123             if (mem->destructor) {
124                 mem->destructor(mem->c);
125             }
126             free(mem);
127             size_t last_index = pool->size - 1;
128             if (i != last_index) {
129                 pool->data[i] = pool->data[last_index];
130                 pool->data[last_index] = NULL;
131             }
132             pool->size--;
133             return;
134         }
135     }
136     abort();
137 }
138
139 void cxMempoolDestroy(CxMempool *pool) {
140     struct cx_mempool_memory_s *mem;
141     cx_for_n(i, pool->size) {
142         mem = pool->data[i];
143         if (mem->destructor) {
144             mem->destructor(mem->c);
145         }
146         free(mem);
147     }
148     free(pool->data);
149     free((void*) pool->allocator);
150     free(pool);
151 }
152
153 void cxMempoolSetDestructor(
154         void *ptr,
155         cx_destructor_func func
156 ) {
157     *(cx_destructor_func *) ((char *) ptr - sizeof(cx_destructor_func)) = func;
158 }
159
160 struct cx_mempool_foreign_mem_s {
161     cx_destructor_func destr;
162     void* mem;
163 };
164
165 static void cx_mempool_destr_foreign_mem(void* ptr) {
166     struct cx_mempool_foreign_mem_s *fm = ptr;
167     fm->destr(fm->mem);
168 }
169
170 int cxMempoolRegister(
171         CxMempool *pool,
172         void *memory,
173         cx_destructor_func destr
174 ) {
175     struct cx_mempool_foreign_mem_s *fm = cx_mempool_malloc(
176             pool,
177             sizeof(struct cx_mempool_foreign_mem_s)
178     );
179     if (fm == NULL) return 1;
180
181     fm->mem = memory;
182     fm->destr = destr;
183     *(cx_destructor_func *) ((char *) fm - sizeof(cx_destructor_func)) = cx_mempool_destr_foreign_mem;
184
185     return 0;
186 }
187
188 static cx_allocator_class cx_mempool_allocator_class = {
189         cx_mempool_malloc,
190         cx_mempool_realloc,
191         cx_mempool_calloc,
192         cx_mempool_free
193 };
194
195 CxMempool *cxMempoolCreate(
196         size_t capacity,
197         cx_destructor_func destr
198 ) {
199     size_t poolsize;
200     if (cx_szmul(capacity, sizeof(struct cx_mempool_memory_s*), &poolsize)) {
201         return NULL;
202     }
203
204     struct cx_mempool_s *pool =
205             malloc(sizeof(struct cx_mempool_s));
206     if (pool == NULL) {
207         return NULL;
208     }
209
210     CxAllocator *provided_allocator = malloc(sizeof(CxAllocator));
211     if (provided_allocator == NULL) {
212         free(pool);
213         return NULL;
214     }
215     provided_allocator->cl = &cx_mempool_allocator_class;
216     provided_allocator->data = pool;
217
218     pool->allocator = provided_allocator;
219
220     pool->data = malloc(poolsize);
221     if (pool->data == NULL) {
222         free(provided_allocator);
223         free(pool);
224         return NULL;
225     }
226
227     pool->size = 0;
228     pool->capacity = capacity;
229     pool->auto_destr = destr;
230
231     return (CxMempool *) pool;
232 }