1 /* { dg-do compile } */
2 
3 extern int printf(const char *, ...);
4 struct list_head {
5 	struct list_head *next, *prev;
6 };
7 struct resource {
8 	unsigned long long start;
9 	unsigned long long end;
10 	unsigned long flags;
11 	struct resource *parent, *sibling, *child;
12 };
13 struct pci_dev {
14 	struct list_head bus_list;
15 	struct resource resource[12];
16 };
17 struct pci_bus {
18 	struct list_head devices;
19 	unsigned char secondary;
20 	unsigned char subordinate;
21 };
22 struct resource *pci_bus_resource_n(const struct pci_bus *bus, int n);
find_free_bus_resource(struct pci_bus * bus,unsigned long type)23 static struct resource *find_free_bus_resource(struct pci_bus *bus, unsigned long type) {
24 	int i;
25 	struct resource *r;
26 	unsigned long type_mask = 0x00000100 | 0x00000200 | 0x00002000;
27 	for (i = 0; (r = pci_bus_resource_n(bus, i)) || i < 4; i++) {
28 		if (r && (r->flags & type_mask) == type && !r->parent) return r;
29 	}
30 	return ((void *)0);
31 }
calculate_memsize(unsigned long long size,unsigned long long min_size,unsigned long long size1,unsigned long long old_size,unsigned long long align)32 static unsigned long long calculate_memsize(unsigned long long size, unsigned long long min_size, unsigned long long size1, unsigned long long old_size, unsigned long long align) {
33 	if (old_size == 1 ) old_size = 0;
34 	if (size < old_size) size = old_size;
35 	return size;
36 }
pbus_size_mem(struct pci_bus * bus,unsigned long mask,unsigned long type,unsigned long long min_size,unsigned long long add_size,void * realloc_head)37 void pbus_size_mem(struct pci_bus *bus, unsigned long mask, unsigned long type, unsigned long long min_size, unsigned long long add_size, void *realloc_head) {
38 	struct pci_dev *dev;
39 	unsigned long long min_align, align, size, size0, size1;
40 	int order;
41 	struct resource *b_res = find_free_bus_resource(bus, type);
42 	unsigned long long children_add_size = 0;
43 	if (!b_res) return;
44 	for (dev = ({
45 				const typeof( ((typeof(*dev) *)0)->bus_list ) *__mptr = ((&bus->devices)->next);
46 				(typeof(*dev) *)( (char *)__mptr - __builtin_offsetof(typeof(*dev),bus_list) );
47 				}
48 		   );
49 			&dev->bus_list != (&bus->devices);
50 			dev = ({
51 				const typeof( ((typeof(*dev) *)0)->bus_list ) *__mptr = (dev->bus_list.next);
52 				(typeof(*dev) *)( (char *)__mptr - __builtin_offsetof(typeof(*dev),bus_list) );
53 				}
54 			      )) {
55 		int i;
56 		for (i = 0; i < 12; i++) {
57 			struct resource *r = &dev->resource[i];
58 			unsigned long long r_size;
59 			if (r->parent || (r->flags & mask) != type) continue;
60 			r_size = r->end - r->start + 1;
61 			if (order > 11) {
62 				printf("%d: %pR %#llx\n", i, r, (unsigned long long) align);
63 			}
64 			size += r_size;
65 		}
66 	}
67 	if (children_add_size > add_size) add_size = children_add_size;
68 	size1 = (!realloc_head || (realloc_head && !add_size)) ? size0 : calculate_memsize(size, min_size+add_size, 0, b_res->end - b_res->start + 1, min_align);
69 	if (!size0 && !size1) {
70 		printf("%pR %02x-%02x\n", b_res, bus->secondary, bus->subordinate);
71 	}
72 }
73