source: doc/proposals/modules-alvin/examples/graph/1_size_analysis/size_analysis.c

Last change on this file was 7640ff5, checked in by Alvin Zhang <alvin.zhang@…>, 2 months ago

add graph example for modules proposal

  • Property mode set to 100644
File size: 2.6 KB
Line 
1// First, have a graph of file import dependencies
2// graph/edge_picker.c : graph/node.c graph/edge.c
3// graph/node.c : graph/edge_picker.c graph/edge.c
4// graph/edge.c : graph/node.c
5// graph.c : graph/node.c graph/edge.c
6// main.c : graph.c
7
8// Second, collect all symbols related to types in the modules
9// // graph/edge_picker.c
10// struct Controller {};
11// // graph/node.c
12// struct Node {
13// int num_edges;
14// struct Edge *edges[max_edges_per_node];
15// struct Other o;
16// };
17// const int max_edges_per_node = 100;
18// struct Other {};
19// // graph/edge.c
20// struct Edge {
21// struct Node *nodes[2];
22// struct Other o;
23// };
24// struct Other {};
25// // graph.c
26// struct Graph {
27// struct Node *nodes;
28// struct Edge *edges;
29// int num_nodes;
30// int num_edges;
31// };
32// // main.c
33// struct Arg {
34// int num_iter, seed, num_nodes, num_edges, num_checks;
35// };
36
37// Third, analyze imports to add namespaces and produce an ordering
38// graph/edge_picker.c
39struct graph$edge_picker$$Controller {};
40// graph/node.c
41const int graph$node$$max_edges_per_node = 100;
42struct graph$node$$Other {};
43struct graph$node$$Node {
44 int num_edges;
45 struct graph$edge$$Edge *edges[graph$node$$max_edges_per_node];
46 struct graph$node$$Other o;
47};
48// graph/edge.c
49struct graph$edge$$Other {};
50struct graph$edge$$Edge {
51 struct graph$node$$Node *nodes[2];
52 struct graph$edge$$Other o;
53};
54// graph.c
55struct graph$$Graph {
56 struct graph$node$$Node *nodes;
57 struct graph$node$$Edge *edges;
58 int num_nodes;
59 int num_edges;
60};
61// main.c
62struct Arg {
63 int num_iter, seed, num_nodes, num_edges, num_checks;
64};
65
66// Fourth, print out size/alignment information
67#include <stdio.h>
68#define PRINT_TYPE_INFO(T) \
69 printf("%-40s size = %4zu align = %4zu\n", \
70 #T, (size_t)sizeof(T), (size_t)_Alignof(T))
71int main() {
72 PRINT_TYPE_INFO(struct graph$edge_picker$$Controller);
73 PRINT_TYPE_INFO(struct graph$node$$Other);
74 PRINT_TYPE_INFO(struct graph$node$$Node);
75 PRINT_TYPE_INFO(struct graph$edge$$Other);
76 PRINT_TYPE_INFO(struct graph$edge$$Edge);
77 PRINT_TYPE_INFO(struct graph$$Graph);
78 PRINT_TYPE_INFO(struct Arg);
79}
80
81// Prints:
82// struct graph$edge_picker$$Controller size = 0 align = 1
83// struct graph$node$$Other size = 0 align = 1
84// struct graph$node$$Node size = 808 align = 8
85// struct graph$edge$$Other size = 0 align = 1
86// struct graph$edge$$Edge size = 16 align = 8
87// struct graph$$Graph size = 24 align = 8
88// struct Arg size = 20 align = 4
Note: See TracBrowser for help on using the repository browser.