[c] Made kvec.h use Spine FREE/MALLOC so custom allocators need only be injected in a single place. Thanks @Rattenhirn.

This commit is contained in:
badlogic 2017-01-23 10:45:24 +01:00
parent 369cda4335
commit c2cfbc6cb8

View File

@ -1,92 +1,105 @@
/* The MIT License /* The MIT License
Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk> Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk>
Permission is hereby granted, free of charge, to any person obtaining Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to permit persons to whom the Software is furnished to do so, subject to
the following conditions: the following conditions:
The above copyright notice and this permission notice shall be The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software. included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
/* /*
An example: An example:
#include "kvec.h" #include "kvec.h"
int main() { int main() {
kvec_t(int) array; kvec_t(int) array;
kv_init(array); kv_init(array);
kv_push(int, array, 10); // append kv_push(int, array, 10); // append
kv_a(int, array, 20) = 5; // dynamic kv_a(int, array, 20) = 5; // dynamic
kv_A(array, 20) = 4; // static kv_A(array, 20) = 4; // static
kv_destroy(array); kv_destroy(array);
return 0; return 0;
} }
*/ */
/* /*
2008-09-22 (0.1.0): 2008-09-22 (0.1.0):
* The initial version. * The initial version.
*/ 2017-19-18 (0.1.1):
#ifndef AC_KVEC_H Spine Special Edition
#define AC_KVEC_H * Made helper macros for alloc, free and memcpy, which can be overridden.
* Made these helpers point to the Spine C Runtime alloc and free functions by default
#include <stdlib.h> * Reimplemented kv_resize to use alloc and free instead of realloc
* Changed kv_push to use kv_resize instead of realloc
#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) * Removed kv_pushp and kv_a macros because the weren't used
* Removed stdlib include
#define kvec_t(type) struct { size_t n, m; type *a; } */
#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
#define kv_destroy(v) free((v).a) #ifndef AC_KVEC_H
#define kv_A(v, i) ((v).a[(i)]) #define AC_KVEC_H
#define kv_array(v) ((v).a)
#define kv_pop(v) ((v).a[--(v).n]) #ifndef _kv_free
#define kv_size(v) ((v).n) #define _kv_free(type, p) (FREE(p))
#define kv_max(v) ((v).m) #endif
#define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m)) #ifndef _kv_alloc
#define kv_trim(type, v) (kv_resize(type, (v), kv_size(v))) #define _kv_alloc(type, s) ((type*)(MALLOC(type, (s))))
#endif
#define kv_copy(type, v1, v0) do { \
if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \ #ifndef _kv_copy
(v1).n = (v0).n; \ #define _kv_copy(type, d, s, n) memcpy((d), (s), sizeof(type) * (n))
memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \ #endif
} while (0) \
#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
#define kv_push(type, v, x) do { \
if ((v).n == (v).m) { \ #define kvec_t(type) struct { size_t n, m; type *a; }
(v).m = (v).m? (v).m<<1 : 2; \ #define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m); \ #define kv_destroy(v) _kv_free(type, (v).a)
} \ #define kv_A(v, i) ((v).a[(i)])
(v).a[(v).n++] = (x); \ #define kv_array(v) ((v).a)
} while (0) #define kv_pop(v) ((v).a[--(v).n])
#define kv_size(v) ((v).n)
#define kv_pushp(type, v) (((v).n == (v).m)? \ #define kv_max(v) ((v).m)
((v).m = ((v).m? (v).m<<1 : 2), \
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ #define kv_resize(type, v, s) do { \
: 0), ((v).a + ((v).n++)) type* b = _kv_alloc(type, (s)); \
if (((s) > 0) && ((v).m > 0)) \
#define kv_a(type, v, i) (((v).m <= (size_t)(i)? \ _kv_copy(type, b, (v).a, ((s) < (v).m)? (s) : (v).m); \
((v).m = (v).n = (i) + 1, kv_roundup32((v).m), \ _kv_free(type, (v).a); \
(v).a = (type*)realloc((v).a, sizeof(type) * (v).m), 0) \ (v).a = b; (v).m = (s); \
: (v).n <= (size_t)(i)? (v).n = (i) + 1 \ } while (0)
: 0), (v).a[(i)])
#define kv_trim(type, v) kv_resize(type, (v), kv_size(v))
#endif
#define kv_copy(type, v1, v0) do { \
if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \
(v1).n = (v0).n; \
_kv_copy(type, (v1).a, (v0).a, (v0).n); \
} while (0) \
#define kv_push(type, v, x) do { \
if ((v).n == (v).m) \
kv_resize(type, (v), ((v).m? (v).m<<1 : 2)); \
(v).a[(v).n++] = (x); \
} while (0)
#endif