X-Git-Url: https://git.xonotic.org/?a=blobdiff_plain;f=util.c;h=bbd2799200914cb87854927aa06927c2b0c5a784;hb=4580dcf1eacfce517e95b654b73d6e72db366273;hp=2b2f1b0e1ffa5d00d0e58c9d11a400cd692cea91;hpb=9ba655e74cffab0bade87aad855d045a710b0177;p=xonotic%2Fgmqcc.git diff --git a/util.c b/util.c index 2b2f1b0..bbd2799 100644 --- a/util.c +++ b/util.c @@ -25,6 +25,7 @@ #include #include "gmqcc.h" +/* TODO: remove globals ... */ uint64_t mem_ab = 0; uint64_t mem_db = 0; uint64_t mem_at = 0; @@ -41,7 +42,7 @@ struct memblock_t { 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; @@ -169,7 +170,7 @@ char *util_strdup(const char *s) { 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'; } @@ -347,54 +348,6 @@ uint16_t util_crc16(const char *k, int len, const short clamp) { } #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) @@ -411,26 +364,17 @@ size_t util_strtononcmd(const char *in, char *out, size_t outsz) { 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; } /* @@ -482,7 +426,7 @@ GMQCC_INLINE size_t util_hthash(hash_table_t *ht, const char *key) { 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))) { @@ -508,10 +452,10 @@ hash_table_t *util_htnew(size_t size) { 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; }