apk_defines.h 8.74 KB
Newer Older
1 2 3
/* apk_defines.c - Alpine Package Keeper (APK)
 *
 * Copyright (C) 2005-2008 Natanael Copa <n@tanael.org>
4
 * Copyright (C) 2008-2011 Timo Teräs <timo.teras@iki.fi>
5 6
 * All rights reserved.
 *
Timo Teräs's avatar
Timo Teräs committed
7
 * This program is free software; you can redistribute it and/or modify it
8 9 10 11 12 13 14
 * under the terms of the GNU General Public License version 2 as published
 * by the Free Software Foundation. See http://www.gnu.org/ for details.
 */

#ifndef APK_DEFINES_H
#define APK_DEFINES_H

15
#include <endian.h>
16
#include <string.h>
17

18 19
#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))
#define BIT(x)		(1 << (x))
20
#define min(a, b)	((a) < (b) ? (a) : (b))
21
#define max(a, b)	((a) > (b) ? (a) : (b))
22 23 24 25 26 27 28 29 30 31 32 33 34

#ifndef TRUE
#define TRUE 1
#endif

#ifndef FALSE
#define FALSE 0
#endif

#ifndef NULL
#define NULL 0L
#endif

35 36 37 38 39 40
static inline void *ERR_PTR(long error) { return (void*) error; }
static inline void *ERR_CAST(const void *ptr) { return (void*) ptr; }
static inline int PTR_ERR(const void *ptr) { return (int)(long) ptr; }
static inline int IS_ERR(const void *ptr) { return (unsigned long)ptr >= (unsigned long)-4095; }
static inline int IS_ERR_OR_NULL(const void *ptr) { return IS_ERR(ptr) || !ptr; }

41 42 43 44 45 46 47 48 49 50 51 52
#if defined __GNUC__ && __GNUC__ == 2 && __GNUC_MINOR__ < 96
#define __builtin_expect(x, expected_value) (x)
#endif

#ifndef likely
#define likely(x) __builtin_expect((!!(x)),1)
#endif

#ifndef unlikely
#define unlikely(x) __builtin_expect((!!(x)),0)
#endif

53 54 55 56 57 58
#ifndef container_of
#define container_of(ptr, type, member) ({                      \
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
        (type *)( (char *)__mptr - offsetof(type,member) );})
#endif

Natanael Copa's avatar
Natanael Copa committed
59
extern int apk_verbosity;
60
extern unsigned int apk_flags;
61
extern const char *apk_arch;
62
extern char **apk_argv;
63 64 65 66 67

#define APK_FORCE		0x0001
#define APK_SIMULATE		0x0002
#define APK_CLEAN_PROTECTED	0x0004
#define APK_PROGRESS		0x0008
68
#define APK_RECURSIVE		0x0020
69
#define APK_UPDATE_CACHE	0x0080
Timo Teräs's avatar
Timo Teräs committed
70
#define APK_ALLOW_UNTRUSTED	0x0100
71
#define APK_PURGE		0x0200
72
#define APK_INTERACTIVE		0x0400
73
#define APK_NO_NETWORK		0x1000
74
#define APK_OVERLAY_FROM_STDIN	0x2000
Timo Teräs's avatar
Timo Teräs committed
75
#define APK_NO_SCRIPTS		0x4000
76

77 78 79 80 81 82 83 84 85
/* default architecture for APK packages. */
#if defined(__x86_64__)
#define APK_DEFAULT_ARCH        "x86_64"
#elif defined(__i386__)
#define APK_DEFAULT_ARCH        "x86"
#elif defined(__powerpc__) && !defined(__powerpc64__)
#define APK_DEFAULT_ARCH	"ppc"
#elif defined(__powerpc64__)
#define APK_DEFAULT_ARCH	"ppc64"
86 87
#elif defined(__arm__) && defined(__ARM_PCS_VFP) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define APK_DEFAULT_ARCH	"armhf"
88 89
#elif defined(__arm__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#define APK_DEFAULT_ARCH	"armel"
90 91 92 93 94
#else
#warning APK_DEFAULT_ARCH is not set for this architecture
#define APK_DEFAULT_ARCH        "noarch"
#endif

95
#define APK_MAX_REPOS		32	/* see struct apk_package */
96 97 98
#define APK_MAX_TAGS		16	/* see solver; unsigned short */
#define APK_CACHE_CSUM_BYTES	4

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static inline size_t apk_calc_installed_size(size_t size)
{
	const size_t bsize = 4 * 1024;

	return (size + bsize - 1) & ~(bsize - 1);
}
static inline size_t muldiv(size_t a, size_t b, size_t c)
{
	unsigned long long tmp;
	tmp = a;
	tmp *= b;
	tmp /= c;
	return (size_t) tmp;
}

typedef void (*apk_progress_cb)(void *cb_ctx, size_t);

Timo Teräs's avatar
Timo Teräs committed
116 117
void *apk_array_resize(void *array, size_t new_size, size_t elem_size);

118 119
#define APK_ARRAY(array_type_name, elem_type_name)			\
	struct array_type_name {					\
120
		size_t num;						\
121 122
		elem_type_name item[];					\
	};								\
Timo Teräs's avatar
Timo Teräs committed
123 124 125 126 127 128 129 130 131 132 133 134
	static inline void						\
	array_type_name##_init(struct array_type_name **a)		\
	{								\
		*a = apk_array_resize(NULL, 0, 0);			\
	}								\
	static inline void						\
	array_type_name##_free(struct array_type_name **a)		\
	{								\
		*a = apk_array_resize(*a, 0, 0);			\
	}								\
	static inline void						\
	array_type_name##_resize(struct array_type_name **a, size_t size)\
135
	{								\
Timo Teräs's avatar
Timo Teräs committed
136
		*a = apk_array_resize(*a, size, sizeof(elem_type_name));\
137
	}								\
Timo Teräs's avatar
Timo Teräs committed
138 139 140
	static inline void						\
	array_type_name##_copy(struct array_type_name **a, struct array_type_name *b)\
	{								\
141
		if (*a == b) return;					\
Timo Teräs's avatar
Timo Teräs committed
142 143 144
		*a = apk_array_resize(*a, b->num, sizeof(elem_type_name));\
		memcpy((*a)->item, b->item, b->num * sizeof(elem_type_name));\
	}								\
145 146 147
	static inline elem_type_name *					\
	array_type_name##_add(struct array_type_name **a)		\
	{								\
Timo Teräs's avatar
Timo Teräs committed
148 149
		int size = 1 + (*a)->num;				\
		*a = apk_array_resize(*a, size, sizeof(elem_type_name));\
150 151 152
		return &(*a)->item[size-1];				\
	}

153 154
APK_ARRAY(apk_string_array, char *);

155 156 157
#define foreach_array_item(iter, array) \
	for (iter = &(array)->item[0]; iter < &(array)->item[(array)->num]; iter++)

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
#define LIST_END (void *) 0xe01
#define LIST_POISON1 (void *) 0xdeadbeef
#define LIST_POISON2 (void *) 0xabbaabba

struct hlist_head {
	struct hlist_node *first;
};

struct hlist_node {
	struct hlist_node *next;
};

static inline int hlist_empty(const struct hlist_head *h)
{
	return !h->first;
}

static inline int hlist_hashed(const struct hlist_node *n)
{
	return n->next != NULL;
}

static inline void __hlist_del(struct hlist_node *n, struct hlist_node **pprev)
{
	*pprev = n->next;
183
	n->next = NULL;
184 185
}

186
static inline void hlist_del(struct hlist_node *n, struct hlist_head *h)
187
{
188 189 190 191 192 193 194
	struct hlist_node **pp = &h->first;

	while (*pp != NULL && *pp != LIST_END && *pp != n)
		pp = &(*pp)->next;

	if (*pp == n)
		__hlist_del(n, pp);
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
}

static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
	struct hlist_node *first = h->first;

	n->next = first ? first : LIST_END;
	h->first = n;
}

static inline void hlist_add_after(struct hlist_node *n, struct hlist_node **prev)
{
	n->next = *prev ? *prev : LIST_END;
	*prev = n;
}

211 212 213 214 215 216 217 218 219 220
static inline struct hlist_node **hlist_tail_ptr(struct hlist_head *h)
{
	struct hlist_node *n = h->first;
	if (n == NULL || n == LIST_END)
		return &h->first;
	while (n->next != NULL && n->next != LIST_END)
		n = n->next;
	return &n->next;
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)

#define hlist_for_each(pos, head) \
	for (pos = (head)->first; pos && pos != LIST_END; \
	     pos = pos->next)

#define hlist_for_each_safe(pos, n, head) \
	for (pos = (head)->first; pos && pos != LIST_END && \
		({ n = pos->next; 1; }); \
	     pos = n)

#define hlist_for_each_entry(tpos, pos, head, member)			 \
	for (pos = (head)->first;					 \
	     pos && pos != LIST_END  &&					 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = pos->next)

#define hlist_for_each_entry_safe(tpos, pos, n, head, member) 		 \
	for (pos = (head)->first;					 \
	     pos && pos != LIST_END && ({ n = pos->next; 1; }) && 	 \
		({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
	     pos = n)

244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274

struct list_head {
	struct list_head *next, *prev;
};

static inline void list_init(struct list_head *list)
{
	list->next = list;
	list->prev = list;
}

static inline void __list_add(struct list_head *new,
			      struct list_head *prev,
			      struct list_head *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

static inline void list_add(struct list_head *new, struct list_head *head)
{
	__list_add(new, head, head->next);
}

static inline void list_add_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}

275
static inline void __list_del(struct list_head *prev, struct list_head *next)
276 277 278 279 280 281 282 283 284 285 286 287
{
	next->prev = prev;
	prev->next = next;
}

static inline void list_del(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

288 289 290 291 292 293 294
static inline void list_del_init(struct list_head *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = NULL;
	entry->prev = NULL;
}

295 296
static inline int list_hashed(const struct list_head *n)
{
297
	return n->next != n && n->next != NULL;
298 299
}

300 301 302 303 304
static inline int list_empty(const struct list_head *n)
{
	return n->next == n;
}

305 306 307 308 309 310 311
static inline struct list_head *__list_pop(struct list_head *head)
{
	struct list_head *n = head->next;
	list_del_init(n);
	return n;
}

312 313
#define list_entry(ptr, type, member) container_of(ptr,type,member)

314 315
#define list_pop(head, type, member) container_of(__list_pop(head),type,member)

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
#define list_for_each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)

#define list_for_each_safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
		pos = n, n = pos->next)

#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

#define list_for_each_entry_safe(pos, n, head, member)			\
	for (pos = list_entry((head)->next, typeof(*pos), member),	\
		n = list_entry(pos->member.next, typeof(*pos), member);	\
	     &pos->member != (head); 					\
	     pos = n, n = list_entry(n->member.next, typeof(*n), member))

334
#endif