Refactor DEFINE_ALLOCATOR to use helper method and asserts
[tig.git] / include / tig / util.h
blob2f21ee7136482ba35d71a17ec0c5db97517dfc7e
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #ifndef TIG_UTIL_H
15 #define TIG_UTIL_H
17 #include "tig/tig.h"
18 #include "tig/types.h"
21 * Error handling.
24 #define STATUS_CODE_INFO(_) \
25 _(CUSTOM_MESSAGE, NULL), \
26 _(NO_OPTION_VALUE, "No option value"), \
27 _(OUT_OF_MEMORY, "Out of memory"), \
28 _(FILE_DOES_NOT_EXIST, "File does not exist"), \
29 _(UNMATCHED_QUOTATION, "Unmatched quotation"), \
31 enum status_code {
32 SUCCESS,
33 #define STATUS_CODE_ENUM(name, msg) ERROR_ ## name
34 STATUS_CODE_INFO(STATUS_CODE_ENUM)
37 const char *get_status_message(enum status_code code);
38 enum status_code error(const char *fmt, ...) PRINTF_LIKE(1, 2);
39 enum status_code success(const char *fmt, ...) PRINTF_LIKE(1, 2);
41 typedef void (*die_fn)(void);
42 extern die_fn die_callback;
43 void TIG_NORETURN die(const char *err, ...) PRINTF_LIKE(1, 2);
44 void warn(const char *msg, ...) PRINTF_LIKE(1, 2);
46 static inline int
47 count_digits(unsigned long i)
49 int digits;
51 if (!i)
52 return 1;
54 for (digits = 0; i; digits++)
55 i /= 10;
56 return digits;
59 static inline int
60 apply_step(double step, int value)
62 if (step >= 1)
63 return (int) step;
64 value *= step;
65 return value ? value : 1;
69 * Git data formatters.
72 struct time {
73 time_t sec;
74 int tz;
77 struct ident {
78 const char *name;
79 const char *email;
82 extern const struct ident unknown_ident;
84 int timecmp(const struct time *t1, const struct time *t2);
85 int ident_compare(const struct ident *i1, const struct ident *i2);
87 const char *mkdate(const struct time *time, enum date date);
88 const char *mkfilesize(unsigned long size, enum file_size format);
89 const char *mkauthor(const struct ident *ident, int cols, enum author author);
90 const char *mkmode(mode_t mode);
91 const char *mkstatus(const char status, enum status_label label);
93 #define author_trim(cols) (cols == 0 || cols > 10)
96 * Allocation helper.
99 void *chunk_allocator(void *mem, size_t type_size, size_t chunk_size, size_t size, size_t increase);
101 #define DEFINE_ALLOCATOR(name, type, chunk_size) \
102 static type * \
103 name(type **mem, size_t size, size_t increase) \
105 type *tmp; \
107 assert(mem); \
108 if (mem == NULL) \
109 return NULL; \
111 tmp = chunk_allocator(*mem, sizeof(type), chunk_size, size, increase); \
112 if (tmp) \
113 *mem = tmp; \
114 return tmp; \
117 #endif
118 /* vim: set ts=8 sw=8 noexpandtab: */