2022-01-24 03:28:16 +00:00
|
|
|
#ifndef LAIKA_MEM_H
|
|
|
|
#define LAIKA_MEM_H
|
|
|
|
|
|
|
|
#include "laika.h"
|
|
|
|
|
|
|
|
#define GROW_FACTOR 2
|
|
|
|
|
2022-03-14 17:06:32 +00:00
|
|
|
/* microsoft strikes again with their lack of support for VLAs */
|
|
|
|
#if _MSC_VER
|
2022-03-14 17:17:19 +00:00
|
|
|
#define VLA(type, var, sz) type *var = laikaM_malloc(sizeof(type)*sz);
|
|
|
|
#define ENDVLA(var) laikaM_free(var);
|
2022-03-14 17:06:32 +00:00
|
|
|
#else
|
|
|
|
#define VLA(type, var, sz) type var[sz];
|
2022-03-28 19:02:33 +00:00
|
|
|
#define ENDVLA(var) ((void)0) /* no op */
|
2022-03-14 17:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
|
|
|
|
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
|
|
|
|
2022-01-28 01:55:28 +00:00
|
|
|
#define laikaM_growarray(type, buf, needed, count, capacity) \
|
|
|
|
if (count + needed >= capacity || buf == NULL) { \
|
|
|
|
capacity = (capacity + needed) * GROW_FACTOR; \
|
2022-01-25 03:46:29 +00:00
|
|
|
buf = (type*)laikaM_realloc(buf, sizeof(type)*capacity); \
|
2022-01-24 03:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* moves array elements above indx down by numElem, removing numElem elements at indx */
|
2022-03-28 19:02:33 +00:00
|
|
|
#define laikaM_rmvarray(buf, count, indx, numElem) do { \
|
2022-01-31 19:27:12 +00:00
|
|
|
int _i, _sz = ((count-indx)-numElem); \
|
2022-01-28 01:55:28 +00:00
|
|
|
for (_i = 0; _i < _sz; _i++) \
|
|
|
|
buf[indx+_i] = buf[indx+numElem+_i]; \
|
2022-01-24 03:28:16 +00:00
|
|
|
count -= numElem; \
|
2022-03-28 19:02:33 +00:00
|
|
|
} while(0);
|
2022-01-24 03:28:16 +00:00
|
|
|
|
2022-02-25 04:13:05 +00:00
|
|
|
/* moves array elements above indx up by numElem, inserting numElem elements at indx */
|
2022-03-28 19:02:33 +00:00
|
|
|
#define laikaM_insertarray(buf, count, indx, numElem) do { \
|
2022-02-25 04:13:05 +00:00
|
|
|
int _i; \
|
|
|
|
for (_i = count; _i > indx; _i--) \
|
|
|
|
buf[_i] = buf[_i-1]; \
|
|
|
|
count += numElem; \
|
2022-03-28 19:02:33 +00:00
|
|
|
} while(0);
|
2022-02-25 04:13:05 +00:00
|
|
|
|
2022-01-24 03:28:16 +00:00
|
|
|
void *laikaM_realloc(void *buf, size_t sz);
|
|
|
|
|
|
|
|
#endif
|