dynarg.h 3.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2013
	Copyright Stichting C.A. Muller Radioastronomiestation, 2013

	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.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

Jeroen Vreeken's avatar
Jeroen Vreeken committed
19
20
21
22
23
24
#ifndef _INCLUDE_DYNARG_H_
#define _INCLUDE_DYNARG_H_

#include <stdarg.h>
#include <malloc.h>

25
26
27


#if defined(__x86_64__)
Jeroen Vreeken's avatar
Jeroen Vreeken committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68

#include <string.h>

/* Todo: This implementation is limited to 6 gps and 8 fps, 
   it does not yet know how to use the overflow area! 
 */

struct _va_dyn {
	unsigned char regs[176];
	size_t overflow;
	unsigned gp_offset;
	unsigned fp_offset;
};

#define va_new(list) \
do {\
	struct _va_dyn *vadyn;\
	\
	vadyn = malloc(sizeof(struct _va_dyn));\
	(list)[0].gp_offset = 8;\
	(list)[0].fp_offset = 48;\
	(list)[0].reg_save_area = vadyn->regs;\
	(list)[0].overflow_arg_area = NULL;\
	vadyn->overflow = 0;\
	vadyn->gp_offset = 8;\
	vadyn->fp_offset = 48;\
	(struct _va_dyn *)&vadyn->regs = vadyn;\
} while (0)

#define va_add(list, type, val)\
do {\
	struct _va_dyn *vadyn;\
	union { \
		double d; \
		unsigned long ul; \
		type v; \
	} u; \
	vadyn = (list)[0].reg_save_area; \
	u.v = (val); \
	\
	if (!strcmp(#type, "double")) { \
69
70
		memcpy(&vadyn->regs[vadyn->fp_offset], &u.d, sizeof(u.d)); \
/*		*(double *)&vadyn->regs[vadyn->fp_offset] = u.d;*/\
Jeroen Vreeken's avatar
Jeroen Vreeken committed
71
72
		vadyn->fp_offset += sizeof(long double);\
	} else { \
73
74
75
		memcpy(&vadyn->regs[vadyn->gp_offset], &u.ul, sizeof(u.ul)); \
/*		*(unsigned long *)&vadyn->regs[vadyn->gp_offset] = \
		    (unsigned long)u.ul;*/\
Jeroen Vreeken's avatar
Jeroen Vreeken committed
76
77
78
79
80
81
82
83
84
85
86
87
88
		vadyn->gp_offset += sizeof(unsigned long);\
	} \
} while (0)

#define va_free(list) \
do { \
	struct _va_dyn *vadyn; \
	vadyn = (list)[0].reg_save_area; \
	free(vadyn); \
} while (0)



89
90

/* endif __x86_64__ */
91
#elif defined(__i386__) || (defined(__arm__) && !defined(__ARM_EABI__))
92

Jeroen Vreeken's avatar
Jeroen Vreeken committed
93
94
95
96
97
98
99
100
101
102
103
104
105



#define va_new(list) \
do { \
	(list) = malloc(sizeof(char *)) + sizeof(char *); \
        ((char **)(list))[-1] = (list); \
} while (0)

#define va_add(list, type, val) \
do { \
        char *ptr; \
        char **clist = (char **)(list); \
106
	type tmpval = val; \
Jeroen Vreeken's avatar
Jeroen Vreeken committed
107
108
109
110
111
112
        size_t size; \
        \
        size = ((size_t)clist[-1] - (size_t)(list)); \
        clist = realloc(&clist[-1], size + sizeof(type) + sizeof(char *)) + sizeof(char *); \
        (list) = (void *)clist; \
        ptr = ((char *)clist + size); \
113
	memcpy(ptr, &tmpval, sizeof(tmpval)); \
Jeroen Vreeken's avatar
Jeroen Vreeken committed
114
115
116
117
118
119
        clist[-1] = ptr + sizeof(type); \
} while (0)

#define va_free(list) free(((char *)(list)) - sizeof(char *))


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158


/* endif __i386__ */
#elif defined(__arm__) && defined(__ARM_EABI__)


#define va_new(list) \
do { \
	char *ptr = malloc(sizeof(char *)) + sizeof(char *)*2; \
	(list).__ap = ptr; \
        ((char **)ptr)[-1] = ptr; \
} while (0)

#define va_add(list, type, val) \
do { \
        char *ptr; \
        char **clist = (char **)(list).__ap; \
	type tmpval = val; \
        size_t size, fill; \
        \
        size = ((size_t)clist[-1] - (size_t)(clist)); \
	if ((size & 0x7) && !(sizeof(type) & 0x7)) \
		fill = 4; \
	else \
		fill = 0; \
        clist = realloc(&clist[-2], \
	    size + fill + sizeof(type) + sizeof(char *)*2) + sizeof(char *)*2; \
        (list).__ap = (void *)clist; \
        ptr = ((char *)clist + size); \
	ptr += fill; \
	memcpy(ptr, &tmpval, sizeof(tmpval)); \
        clist[-1] = ptr + sizeof(type); \
} while (0)

#define va_free(list) free(((char *)(list).__ap) - sizeof(char *)*2)


#endif /* defined(__arm__) && defined(__ARM_EABI__) */

Jeroen Vreeken's avatar
Jeroen Vreeken committed
159
160

#endif /* _INCLUDE_DYNARG_H_ */