Mercurial > molko
comparison src/libmlk-core/core/alloc.h @ 320:8f9937403749
misc: improve loading of data
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 01 Oct 2021 20:30:00 +0200 |
parents | libmlk-core/core/alloc.h@d01e83210ca2 |
children | 19782ea1cf4a |
comparison
equal
deleted
inserted
replaced
319:b843eef4cc35 | 320:8f9937403749 |
---|---|
1 /* | |
2 * alloc.h -- custom allocators | |
3 * | |
4 * Copyright (c) 2020-2021 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #ifndef MOLKO_CORE_ALLOC_H | |
20 #define MOLKO_CORE_ALLOC_H | |
21 | |
22 #include <stddef.h> | |
23 | |
24 #include "core.h" | |
25 #include "util.h" | |
26 | |
27 /* Must be power of 2. */ | |
28 #define ALLOC_POOL_INIT_DEFAULT (32) | |
29 | |
30 /* Custom allocator. */ | |
31 struct alloc_funcs { | |
32 void *(*alloc)(size_t); | |
33 void *(*realloc)(void *, size_t); | |
34 void (*free)(void *); | |
35 }; | |
36 | |
37 /* Minimalist growable array for loading data. */ | |
38 struct alloc_pool { | |
39 void *data; | |
40 size_t elemsize; | |
41 size_t size; | |
42 size_t capacity; | |
43 void (*finalizer)(void *); | |
44 }; | |
45 | |
46 CORE_BEGIN_DECLS | |
47 | |
48 /* allocator functions. */ | |
49 void | |
50 alloc_set(const struct alloc_funcs *); | |
51 | |
52 void * | |
53 alloc_new(size_t); | |
54 | |
55 void * | |
56 alloc_new0(size_t); | |
57 | |
58 void * | |
59 alloc_array(size_t, size_t); | |
60 | |
61 void * | |
62 alloc_array0(size_t, size_t); | |
63 | |
64 void * | |
65 alloc_renew(void *, size_t); | |
66 | |
67 void * | |
68 alloc_rearray(void *, size_t, size_t); | |
69 | |
70 void * | |
71 alloc_rearray0(void *, size_t, size_t, size_t); | |
72 | |
73 void * | |
74 alloc_dup(const void *, size_t); | |
75 | |
76 char * | |
77 alloc_sdup(const char *); | |
78 | |
79 /* alloc_pool functions. */ | |
80 int | |
81 alloc_pool_init(struct alloc_pool *, size_t , void (*)(void *)); | |
82 | |
83 void * | |
84 alloc_pool_new(struct alloc_pool *); | |
85 | |
86 void * | |
87 alloc_pool_get(const struct alloc_pool *, size_t); | |
88 | |
89 void | |
90 alloc_pool_finish(struct alloc_pool *); | |
91 | |
92 CORE_END_DECLS | |
93 | |
94 #endif /* !MOLKO_CORE_ALLOC_H */ |