#include <errno.h>
#include "gmqcc.h"
+/* TODO: remove globals ... */
uint64_t mem_ab = 0;
uint64_t mem_db = 0;
uint64_t mem_at = 0;
static struct memblock_t *mem_start = NULL;
void *util_memory_a(size_t byte, unsigned int line, const char *file) {
- struct memblock_t *info = malloc(sizeof(struct memblock_t) + byte);
+ struct memblock_t *info = (struct memblock_t*)malloc(sizeof(struct memblock_t) + byte);
void *data = (void*)(info+1);
if (!info) return NULL;
info->line = line;
if (!s)
return NULL;
- if ((len = strlen(s)) && (ptr = mem_a(len+1))) {
+ if ((len = strlen(s)) && (ptr = (char*)mem_a(len+1))) {
memcpy(ptr, s, len);
ptr[len] = '\0';
}
return ptr;
}
-/*
- * Remove quotes from a string, escapes from \ in string
- * as well. This function shouldn't be used to create a
- * char array that is later freed (it uses pointer arith)
- */
-char *util_strrq(const char *s) {
- char *dst = (char*)s;
- char *src = (char*)s;
- char chr;
- while ((chr = *src++) != '\0') {
- if (chr == '\\') {
- *dst++ = chr;
- if ((chr = *src++) == '\0')
- break;
- *dst++ = chr;
- } else if (chr != '"')
- *dst++ = chr;
- }
- *dst = '\0';
- return dst;
-}
-
-/*
- * Chops a substring from an existing string by creating a
- * copy of it and null terminating it at the required position.
- */
-char *util_strchp(const char *s, const char *e) {
- const char *c = NULL;
- if (!s || !e)
- return NULL;
-
- c = s;
- while (c != e)
- c++;
-
- return util_strdup(s);
-}
-
-/*
- * Returns true if string is all uppercase, otherwise
- * it returns false.
- */
-bool util_strupper(const char *str) {
- while (*str) {
- if(!isupper(*str))
- return false;
- str++;
- }
- return true;
-}
-
-/*
- * Returns true if string is all digits, otherwise
- * it returns false.
- */
-bool util_strdigit(const char *str) {
- while (*str) {
- if(!isdigit(*str))
- return false;
- str++;
- }
- return true;
-}
-
-bool util_strncmpexact(const char *src, const char *ned, size_t len) {
- return (!strncmp(src, ned, len) && !src[len]);
-}
-
void util_debug(const char *area, const char *ms, ...) {
va_list va;
if (!opts.debug)
va_end (va);
}
-/*
- * Endianess swapping, all data must be stored little-endian. This
- * reorders by stride and length, much nicer than other functions for
- * certian-sized types like short or int.
- */
-#if 0
-void util_endianswap(void *m, int s, int l) {
- size_t w = 0;
- size_t i = 0;
-
- /* ignore if we're already LE */
- if(*((char *)&s))
- return;
-
- for(; w < (size_t)l; w++) {
- for(; i < (size_t)(s << 1); i++) {
- unsigned char *p = (unsigned char *)m+w*s;
- unsigned char t = p[i];
- p[i] = p[s-i-1];
- p[s-i-1] = t;
- }
- }
-}
-#endif
-
/*
* only required if big endian .. otherwise no need to swap
* data.
*/
#if PLATFORM_BYTE_ORDER == GMQCC_BYTE_ORDER_BIG
- static void util_swap16(uint16_t *d, size_t l) {
+ static GMQCC_INLINE void util_swap16(uint16_t *d, size_t l) {
while (l--) {
d[l] = (d[l] << 8) | (d[l] >> 8);
}
}
- static void util_swap32(uint32_t *d, size_t l) {
+ static GMQCC_INLINE void util_swap32(uint32_t *d, size_t l) {
while (l--) {
uint32_t v;
v = ((d[l] << 8) & 0xFF00FF00) | ((d[l] >> 8) & 0x00FF00FF);
/* Some strange system doesn't like constants that big, AND doesn't recognize an ULL suffix
* so let's go the safe way
*/
- static void util_swap64(uint32_t *d, size_t l) {
+ static GMQCC_INLINE void util_swap64(uint32_t *d, size_t l) {
/*
while (l--) {
uint64_t v;
}
#endif
-/*
- * Implements libc getline for systems that don't have it, which is
- * assmed all. This works the same as getline().
- */
-int util_getline(char **lineptr, size_t *n, FILE *stream) {
- int chr;
- int ret;
- char *pos;
-
- if (!lineptr || !n || !stream)
- return -1;
- if (!*lineptr) {
- if (!(*lineptr = (char*)mem_a((*n=64))))
- return -1;
- }
-
- chr = *n;
- pos = *lineptr;
-
- for (;;) {
- int c = getc(stream);
-
- if (chr < 2) {
- *n += (*n > 16) ? *n : 64;
- chr = *n + *lineptr - pos;
- if (!(*lineptr = (char*)mem_r(*lineptr,*n)))
- return -1;
- pos = *n - chr + *lineptr;
- }
-
- if (ferror(stream))
- return -1;
- if (c == EOF) {
- if (pos == *lineptr)
- return -1;
- else
- break;
- }
-
- *pos++ = c;
- chr--;
- if (c == '\n')
- break;
- }
- *pos = '\0';
- return (ret = pos - *lineptr);
-}
-
size_t util_strtocmd(const char *in, char *out, size_t outsz) {
size_t sz = 1;
for (; *in && sz < outsz; ++in, ++out, ++sz)
return sz-1;
}
-
-FILE *util_fopen(const char *filename, const char *mode)
-{
-#ifdef _MSC_VER
- FILE *out;
- if (fopen_s(&out, filename, mode) != 0)
- return NULL;
- return out;
-#else
- return fopen(filename, mode);
-#endif
-}
-
+/* TODO: rewrite ... when I redo the ve cleanup */
void _util_vec_grow(void **a, size_t i, size_t s) {
- size_t m = *a ? 2*_vec_beg(*a)+i : i+1;
- void *p = mem_r((*a ? _vec_raw(*a) : NULL), s * m + sizeof(size_t)*2);
+ vector_t *d = vec_meta(*a);
+ size_t m = *a ? 2 * d->allocated +i : i+1;
+ void *p = mem_r((*a ? d : NULL), s * m + sizeof(vector_t));
+
if (!*a)
- ((size_t*)p)[1] = 0;
- *a = (void*)((size_t*)p + 2);
- _vec_beg(*a) = m;
+ ((vector_t*)p)->used = 0;
+ *a = (vector_t*)p + 1;
+
+ vec_meta(*a)->allocated = m;
}
/*
hash_node_t *_util_htnewpair(const char *key, void *value) {
hash_node_t *node;
- if (!(node = mem_a(sizeof(hash_node_t))))
+ if (!(node = (hash_node_t*)mem_a(sizeof(hash_node_t))))
return NULL;
if (!(node->key = util_strdup(key))) {
if (size < 1)
return NULL;
- if (!(hashtable = mem_a(sizeof(hash_table_t))))
+ if (!(hashtable = (hash_table_t*)mem_a(sizeof(hash_table_t))))
return NULL;
- if (!(hashtable->table = mem_a(sizeof(hash_node_t*) * size))) {
+ if (!(hashtable->table = (hash_node_t**)mem_a(sizeof(hash_node_t*) * size))) {
mem_d(hashtable);
return NULL;
}