2018-01-05 21:17:55 +00:00
|
|
|
/*
|
2022-03-21 23:22:59 +00:00
|
|
|
* Copyright (c) 2022 Calvin Rose
|
2018-01-05 21:17:55 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-12-31 00:06:15 +00:00
|
|
|
#include "features.h"
|
2018-07-04 03:07:35 +00:00
|
|
|
#include "vector.h"
|
2019-04-27 19:45:28 +00:00
|
|
|
#include "util.h"
|
2019-01-24 05:15:58 +00:00
|
|
|
#endif
|
2018-01-19 21:43:19 +00:00
|
|
|
|
|
|
|
/* Grow the buffer dynamically. Used for push operations. */
|
2018-09-06 02:18:42 +00:00
|
|
|
void *janet_v_grow(void *v, int32_t increment, int32_t itemsize) {
|
|
|
|
int32_t dbl_cur = (NULL != v) ? 2 * janet_v__cap(v) : 0;
|
|
|
|
int32_t min_needed = janet_v_count(v) + increment;
|
2018-01-19 21:43:19 +00:00
|
|
|
int32_t m = dbl_cur > min_needed ? dbl_cur : min_needed;
|
2019-10-11 03:59:43 +00:00
|
|
|
size_t newsize = ((size_t) itemsize) * m + sizeof(int32_t) * 2;
|
|
|
|
int32_t *p = (int32_t *) janet_srealloc(v ? janet_v__raw(v) : 0, newsize);
|
2019-06-02 03:31:39 +00:00
|
|
|
if (!v) p[1] = 0;
|
|
|
|
p[0] = m;
|
|
|
|
return p + 2;
|
2018-01-19 21:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a buffer to normal allocated memory (forget capacity) */
|
2018-09-06 02:18:42 +00:00
|
|
|
void *janet_v_flattenmem(void *v, int32_t itemsize) {
|
2018-01-19 21:43:19 +00:00
|
|
|
int32_t *p;
|
|
|
|
if (NULL == v) return NULL;
|
2020-01-03 04:02:57 +00:00
|
|
|
size_t size = (size_t) itemsize * janet_v__cnt(v);
|
2021-03-23 10:00:48 +00:00
|
|
|
p = janet_malloc(size);
|
2018-01-19 21:43:19 +00:00
|
|
|
if (NULL != p) {
|
2020-01-29 05:38:52 +00:00
|
|
|
safe_memcpy(p, v, size);
|
2018-01-19 21:43:19 +00:00
|
|
|
return p;
|
|
|
|
} else {
|
2020-01-03 04:02:57 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-01-19 21:43:19 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-05 21:17:55 +00:00
|
|
|
|