/* * The reallocate function for resizing vectors. */ void _util_vec_grow(void **a, size_t i, size_t s) { vector_t *d = vec_meta(*a); size_t m = 0; stat_size_entry_t *e = NULL; void *p = NULL; if (*a) { m = 2 * d->allocated + i; p = mem_r(d, s * m + sizeof(vector_t)); } else { m = i + 1; p = mem_a(s * m + sizeof(vector_t)); ((vector_t*)p)->used = 0; stat_used_vectors++; } if (!stat_size_vectors) stat_size_vectors = stat_size_new(); if ((e = stat_size_get(stat_size_vectors, s))) { e->value ++; } else { stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */ stat_type_vectors++; } *a = (vector_t*)p + 1; vec_meta(*a)->allocated = m; }
/* * The reallocate function for resizing vectors. */ void _util_vec_grow(void **a, size_t i, size_t s) { vector_t *d = (vector_t*)((char *)*a - IDENT_VEC_TOP); size_t m = 0; stat_size_entry_t *e = NULL; void *p = NULL; if (*a) { m = 2 * d->allocated + i; p = mem_r(d, s * m + IDENT_VEC_TOP); } else { m = i + 1; p = mem_a(s * m + IDENT_VEC_TOP); ((vector_t*)p)->used = 0; stat_used_vectors++; } if (!stat_size_vectors) stat_size_vectors = stat_size_new(); if ((e = stat_size_get(stat_size_vectors, s))) { e->value ++; } else { stat_size_put(stat_size_vectors, s, 1); /* start off with 1 */ stat_type_vectors++; } d = (vector_t*)p; d->allocated = m; memcpy(d + 1, IDENT_VEC, IDENT_SIZE); *a = (void *)((char *)d + IDENT_VEC_TOP); }
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); if (!*a) ((size_t*)p)[1] = 0; *a = (void*)((size_t*)p + 2); _vec_beg(*a) = m; }
/* * 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); }