Archived
1
0
Fork 0
This commit is contained in:
H Hautakoski 2010-09-18 19:01:33 +02:00 committed by Henrik Hautakoski
parent 25d789c77b
commit 2c2c0781d9
6 changed files with 190 additions and 26 deletions

31
src/common/die.c Normal file
View file

@ -0,0 +1,31 @@
/* common/die.c - functionality for killing things in creative ways.
*
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include "util.h"
void die(const char *err, ...) {
va_list vl;
va_start(vl, err);
vfprintf(stderr, err, vl);
va_end(vl);
exit(EXIT_FAILURE);
}
void die_errno(const char *desc) {
if (errno)
perror(desc);
exit(EXIT_FAILURE);
}

View file

@ -10,31 +10,13 @@
#include <ctype.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include "xalloc.h"
#include "strbuf.h"
#define CHNK_SIZE 128
char strbuf_null = '\0';
static void* xrealloc(void *ptr, size_t size) {
assert(size);
ptr = realloc(ptr, size);
assert(ptr != NULL);
return ptr;
}
static void* xcalloc(size_t nmemb, size_t size) {
assert(nmemb);
assert(size);
void *ptr = calloc(nmemb, size);
assert(ptr != NULL);
return ptr;
}
void strbuf_init(strbuf_t *s) {
s->buf = &strbuf_null;
@ -69,7 +51,7 @@ char* strbuf_release(strbuf_t *s) {
char *ret;
if (!s->alloc_size)
ret = xcalloc(1, 1);
ret = xmallocz(1);
else if (s->len + 1 != s->alloc_size)
ret = xrealloc(s->buf, s->len + 1);
else
@ -85,7 +67,7 @@ void strbuf_free(strbuf_t *s) {
if (!s->alloc_size)
return;
free(s->buf);
xfree(s->buf);
strbuf_init(s);
}

18
src/common/util.h Normal file
View file

@ -0,0 +1,18 @@
/* common/util.h
*
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#ifndef __COMMON_UTIL_H
#define __COMMON_UTIL_H
void die(const char *, ...);
void die_errno(const char *);
#endif /* __COMMOT_UTIL_H */

85
src/common/xalloc.c Normal file
View file

@ -0,0 +1,85 @@
/* common/xalloc.c
*
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "util.h"
#include "xalloc.h"
#define CHECK(expr) \
if (!(expr)) \
goto bail
#define CHECK_SET_ERRNO(expr, no) \
if (!(expr)) { \
errno = no; \
goto bail; \
}
#ifdef __DEBUG__
# define CHECK_INPUT(s) CHECK_SET_ERRNO(s, EINVAL)
#else
# define CHECK_INPUT(s)
#endif
void* xmalloc(size_t size) {
CHECK_INPUT(size);
void *ptr = malloc(size);
CHECK(ptr);
return ptr;
bail:
die_errno("xmalloc");
}
void* xmallocz(size_t size) {
CHECK_INPUT(size);
void *ptr = malloc(size);
CHECK(ptr);
memset(ptr, 0, size);
return ptr;
bail:
die_errno("xmallocz");
}
void* xrealloc(void *ptr, size_t size) {
CHECK_INPUT(size);
ptr = realloc(ptr, size);
CHECK(ptr);
return ptr;
bail:
die_errno("xrealloc");
}
char* xstrdup(const char *s) {
CHECK_INPUT(s);
char *c = strdup(s);
CHECK(c);
return c;
bail:
die_errno("xstrdup");
}
void xfree(void *ptr) {
CHECK_INPUT(ptr);
free(ptr);
return;
bail:
die_errno("xfree");
}

26
src/common/xalloc.h Normal file
View file

@ -0,0 +1,26 @@
/* common/xalloc.h - stricter memory allocation.
*
* Copyright (C) 2010 Henrik Hautakoski <henrik@fiktivkod.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*/
#ifndef __COMMON_XALLOC_H
#define __COMMON_XALLOC_H
#include <stddef.h>
void* xmalloc(size_t);
void* xmallocz(size_t);
void* xrealloc(void *, size_t);
char* xstrdup(const char *);
void xfree(void *);
#endif /* __COMMON_XALLOC_H */

View file

@ -9,17 +9,30 @@ raw_inotify :
$(CC) -linotifytools t_raw_inotify.c -o test_raw_inotify
strbuf :
$(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf
$(CC) $(CFLAGS) \
../src/common/strbuf.c \
../src/common/xalloc.c \
../src/common/die.c \
t_strbuf.c -o test_strbuf
path :
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/strbuf.c ../src/common/path.c t_path.c -o test_path
$(CC) $(CFLAGS) \
unit.c \
../src/common/xalloc.c \
../src/common/die.c \
../src/common/strbuf.c \
../src/common/path.c \
t_path.c -o test_path
rbtree :
$(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree
$(CC) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree
inotify :
$(CC) -lpthread -D INOTIFY_DEBUG -D RB_DEBUG $(CFLAGS) \
../src/common/rbtree.c \
../src/common/xalloc.c \
../src/common/die.c \
../src/common/strbuf.c \
../src/common/path.c \
../src/notify/event.c \
../src/notify/tree.c \
@ -27,18 +40,27 @@ inotify :
t_inotify.c -o test_inotify
tree :
$(CC) $(CFLAGS) ../src/common/path.c ../src/notify/tree.c t_tree.c -o test_tree
$(CC) $(CFLAGS) \
../src/common/path.c \
../src/notify/tree.c \
../src/common/xalloc.c \
../src/common/die.c \
../src/common/strbuf.c \
t_tree.c -o test_tree
indexer :
$(CC) $(CFLAGS) $(LDFLAGS) \
../src/common/rbtree.c \
../src/common/path.c \
../src/common/xalloc.c \
../src/common/die.c \
../src/common/strbuf.c \
../src/notify/tree.c \
../src/notify/inotify.c \
../src/notify/event.c \
../src/arch/mysql.c \
../src/indexer.c \
t_indexer.c -o test_indexer
t_indexer.c -o test_indexer
mysql :
$(CC) -D DB_DEBUG $(CFLAGS) $(LDFLAGS) ../src/arch/mysql.c t_mysql.c -o test_mysql