Commit | Line | Data |
---|---|---|
3719602c PC |
1 | /* Copyright (C) 2010-2020 The RetroArch team |
2 | * | |
3 | * --------------------------------------------------------------------------------------- | |
4 | * The following license statement only applies to this file (vector_list.c). | |
5 | * --------------------------------------------------------------------------------------- | |
6 | * | |
7 | * Permission is hereby granted, free of charge, | |
8 | * to any person obtaining a copy of this software and associated documentation files (the "Software"), | |
9 | * to deal in the Software without restriction, including without limitation the rights to | |
10 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, | |
11 | * and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | |
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
19 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
21 | */ | |
22 | ||
23 | #include <boolean.h> | |
24 | #include <stdlib.h> | |
25 | #include <stddef.h> | |
26 | ||
27 | /* default type is void*, override by defining VECTOR_LIST_TYPE before inclusion */ | |
28 | #ifndef VECTOR_LIST_TYPE | |
29 | #define VECTOR_LIST_TYPE void* | |
30 | #define VECTOR_LIST_TYPE_DEFINED | |
31 | #endif | |
32 | ||
33 | /* default name is void, override by defining VECTOR_LIST_NAME before inclusion */ | |
34 | #ifndef VECTOR_LIST_NAME | |
35 | #define VECTOR_LIST_NAME void | |
36 | #define VECTOR_LIST_NAME_DEFINED | |
37 | #endif | |
38 | ||
39 | #define CAT_I(a,b) a##b | |
40 | #define CAT(a,b) CAT_I(a, b) | |
41 | #define MAKE_TYPE_NAME() CAT(VECTOR_LIST_NAME, _vector_list) | |
42 | #define TYPE_NAME() MAKE_TYPE_NAME() | |
43 | ||
44 | struct TYPE_NAME() | |
45 | { | |
46 | /* VECTOR_LIST_TYPE for pointers will expand to a pointer-to-pointer */ | |
47 | VECTOR_LIST_TYPE *data; | |
48 | unsigned size; | |
49 | unsigned count; | |
50 | }; | |
51 | ||
52 | static struct TYPE_NAME()* CAT(TYPE_NAME(), _new(void)) | |
53 | { | |
54 | struct TYPE_NAME() *list = (struct TYPE_NAME()*)calloc(1, sizeof(*list)); | |
55 | ||
56 | list->size = 8; | |
57 | list->data = (VECTOR_LIST_TYPE*)calloc(list->size, sizeof(*list->data)); | |
58 | ||
59 | return list; | |
60 | } | |
61 | ||
62 | static bool CAT(TYPE_NAME(), _append(struct TYPE_NAME() *list, VECTOR_LIST_TYPE elem)) | |
63 | { | |
64 | if (list->size == list->count) | |
65 | { | |
66 | list->size *= 2; | |
67 | list->data = (VECTOR_LIST_TYPE*)realloc(list->data, list->size * sizeof(*list->data)); | |
68 | ||
69 | if (!list->data) | |
70 | return false; | |
71 | } | |
72 | ||
73 | list->data[list->count] = elem; | |
74 | list->count++; | |
75 | ||
76 | return true; | |
77 | } | |
78 | ||
79 | static void CAT(TYPE_NAME(), _free(struct TYPE_NAME() *list)) | |
80 | { | |
81 | if (list) | |
82 | { | |
83 | if (list->data) | |
84 | free(list->data); | |
85 | free(list); | |
86 | } | |
87 | } | |
88 | ||
89 | #ifdef VECTOR_LIST_TYPE_DEFINED | |
90 | #undef VECTOR_LIST_TYPE | |
91 | #endif | |
92 | ||
93 | #ifdef VECTOR_LIST_NAME_DEFINED | |
94 | #undef VECTOR_LIST_NAME | |
95 | #endif |