wmii

git clone git://oldgit.suckless.org/wmii/
Log | Files | Refs | README | LICENSE

commit b8ec059f11740c2d052e09690c88bd29d1f31ae8
parent 6ad1e76b3e44e704108a74ab5bc66cf9c8dcd099
Author: Kris Maglione <bsdaemon@wmii.de>
Date:   Tue,  4 Jul 2006 17:53:04 -0400

Removed vectors from libcext and added emalloc, erealloc, and estrdup


Diffstat:
libcext/Makefile | 4++--
libcext/cext.h | 17++++-------------
libcext/emallocz.c | 22----------------------
libcext/malloc.c | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
libcext/vector.c | 46----------------------------------------------
5 files changed, 60 insertions(+), 83 deletions(-)

diff --git a/libcext/Makefile b/libcext/Makefile @@ -3,8 +3,8 @@ include ../config.mk -SRC = assert.c emallocz.c strlcat.c strlcpy.c tokenize.c \ - trim.c vector.c +SRC = assert.c malloc.c strlcat.c strlcpy.c tokenize.c \ + trim.c OBJ = ${SRC:.c=.o} diff --git a/libcext/cext.h b/libcext/cext.h @@ -12,8 +12,11 @@ /* asprintf.c */ int cext_asprintf(char **str, char const *fmt, ...); -/* emallocz.c */ +/* malloc.c */ void *cext_emallocz(unsigned int size); +void *cext_emalloc(unsigned int size); +void *cext_erealloc(void *ptr, unsigned int size); +char *cext_estrdup(const char *str); /* strlcat.c */ unsigned int cext_strlcat(char *dst, const char *src, unsigned int siz); @@ -27,18 +30,6 @@ unsigned int cext_tokenize(char **result, unsigned int reslen, char *str, char d /* trim.c */ void cext_trim(char *str, const char *chars); -/* vector.c */ -#define VECTOR(name, type) \ -typedef struct { \ - unsigned int size; \ - type *data; \ -} name -VECTOR(Vector, void *); - -void cext_vattach(Vector *v, void *p); -void cext_vattachat(Vector *v, void *p, unsigned int pos); -void cext_vdetach(Vector *v, void *p); - /* assert.c */ #define cext_assert(a) do { \ if(!(a)) \ diff --git a/libcext/emallocz.c b/libcext/emallocz.c @@ -1,22 +0,0 @@ -/* - * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> - * See LICENSE file for license details. - */ - -#include <stdlib.h> -#include <stdio.h> - -#include "cext.h" - -void * -cext_emallocz(unsigned int size) -{ - void *res = calloc(1, size); - - if(!res) { - fprintf(stderr, "fatal: could not malloc() %d bytes\n", - (int) size); - exit(1); - } - return res; -} diff --git a/libcext/malloc.c b/libcext/malloc.c @@ -0,0 +1,54 @@ +/* + * (C)opyright MMIV-MMVI Anselm R. Garbe <garbeam at gmail dot com> + * See LICENSE file for license details. + */ + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "cext.h" + +static void +bad_malloc(unsigned int size) +{ + fprintf(stderr, "fatal: could not malloc() %d bytes\n", + (int) size); + exit(1); +} + +void * +cext_emallocz(unsigned int size) +{ + void *res = calloc(1, size); + if(!res) + bad_malloc(size); + return res; +} + +void * +cext_emalloc(unsigned int size) +{ + void *res = malloc(size); + if(!res) + bad_malloc(size); + return res; +} + +void * +cext_erealloc(void *ptr, unsigned int size) +{ + void *res = realloc(ptr, size); + if(!res) + bad_malloc(size); + return res; +} + +char * +cext_estrdup(const char *str) +{ + void *res = strdup(str); + if(!res) + bad_malloc(strlen(str)); + return res; +} diff --git a/libcext/vector.c b/libcext/vector.c @@ -1,46 +0,0 @@ -/* extendible vectors. Icarus Sparry 2006. Public domain. */ -#include <stdlib.h> -#include <stdio.h> -#include <string.h> -#include <unistd.h> - -#include "cext.h" - -void -cext_vattach(Vector *v, void *p) -{ - ++v->size; - if (!(v->data = realloc(v->data, v->size * sizeof(void *)))) { - fprintf(stderr, "%s\n", "Out of memory in cext_vattach\n"); - exit(1); - } - v->data[v->size - 1] = p; -} - -void -cext_vattachat(Vector *v, void *p, unsigned int pos) -{ - cext_vattach(v, p); - if(pos >= v->size) - return; - memmove(v->data + pos + 1, v->data + pos, - (v->size - pos - 1) * sizeof(void *)); - v->data[pos] = p; -} - -void -cext_vdetach(Vector *v, void *data) -{ - unsigned int i; - for(i = 0; i < v->size; i++) - if (v->data[i] == data) { - memmove(v->data + i, v->data + i + 1, - (v->size - i - 1) * sizeof(void *)); - v->data[--v->size] = nil; - break; - } - if(v->size == 0) { - free(v->data); - v->data = nil; - } -}