Mercurial > hg > Members > Moririn
comparison src/allocate.c @ 6:59c1086467f9
fix
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 20 Mar 2015 19:58:02 +0900 |
parents | b8066055e295 |
children | ad48a076a8e5 |
comparison
equal
deleted
inserted
replaced
5:b8066055e295 | 6:59c1086467f9 |
---|---|
9 #endif | 9 #endif |
10 | 10 |
11 #define SIZE 1024 | 11 #define SIZE 1024 |
12 | 12 |
13 typedef struct DataSegment1 { | 13 typedef struct DataSegment1 { |
14 int i; // 4 byte | 14 int i; // 4 byte |
15 char c; // 1 byte | 15 char c[10]; // 10 byte |
16 // padding 3 byte | 16 // padding 2 byte |
17 } ds, *dsptr; // 8 byte | 17 } ds; |
18 | 18 |
19 typedef struct metaDataSegment1 { | 19 typedef struct metaDataSegment1 { |
20 size_t size; // 8 byte | 20 size_t size; // 8 byte |
21 dsptr ds; // 8 byte | 21 ds* ds; // 8 byte |
22 } mds, *mdsptr; // 16 byte | 22 } mds; |
23 | 23 |
24 typedef struct Context { | 24 typedef struct Context_st { |
25 dsptr ds; | 25 ds* ds; |
26 mdsptr mds; | 26 mds* mds; |
27 dsptr ds_heap; | 27 ds* ds_heap; |
28 mdsptr mds_heap; | 28 mds* mds_heap; |
29 } context, *contextptr; | 29 } Context; |
30 | 30 |
31 __code start_code(contextptr context) { | 31 __code start_code(Context* context) { |
32 goto meta_start_code(context); | 32 goto meta_start_code(context); |
33 } | 33 } |
34 | 34 |
35 __code meta_start_code(contextptr context) { | 35 __code meta_start_code(Context* context) { |
36 goto code1(context); | 36 goto code1(context); |
37 } | 37 } |
38 | 38 |
39 __code code1(contextptr context) { | 39 __code code1(Context* context) { |
40 goto meta_code1(context); | 40 goto meta_code1(context); |
41 } | 41 } |
42 | 42 |
43 __code meta_code1(contextptr context) { | 43 __code meta_code1(Context* context) { |
44 goto allocate(context, SIZE); | 44 goto allocate(context, SIZE); |
45 } | 45 } |
46 | 46 |
47 __code allocate(contextptr context, int size) { | 47 __code allocate(Context* context, int size) { |
48 dsptr in = context->ds; | 48 ds* in = context->ds; |
49 context->ds += size; | 49 context->ds += size; |
50 context->mds->ds = in; | 50 context->mds->ds = in; |
51 context->mds->size = context->ds - in; | 51 context->mds->size = context->ds - in; |
52 context->mds++; | 52 context->mds++; |
53 goto meta_allocate(context, in); | 53 goto meta_allocate(context, in); |
54 } | 54 } |
55 | 55 |
56 __code meta_allocate(contextptr context, dsptr in) { | 56 __code meta_allocate(Context* context, ds* in) { |
57 goto exit_code(context, in, 0); | 57 goto exit_code(context, in, 0); |
58 } | 58 } |
59 | 59 |
60 __code exit_code(contextptr context, dsptr in, int i) { | 60 __code exit_code(Context* context, ds* in, int i) { |
61 in[i].i = i; | 61 in[i].i = i; |
62 printf("%d\n", in[i].i); | |
63 if (i == SIZE) { | 62 if (i == SIZE) { |
64 free(context->ds_heap); | 63 free(context->ds_heap); |
65 free(context->mds_heap); | 64 free(context->mds_heap); |
66 goto exit(0); | 65 goto exit(0); |
67 } | 66 } |
68 goto exit_code(context, in, ++i); | 67 goto exit_code(context, in, ++i); |
69 } | 68 } |
70 | 69 |
71 int main() { | 70 int main() { |
72 contextptr context = (contextptr)malloc(sizeof(contextptr)); | 71 Context* context = (Context*)malloc(sizeof(Context)); |
73 context->ds_heap = (dsptr)malloc(1024); | 72 context->ds_heap = (ds*)malloc(1024); |
74 context->mds_heap = (mdsptr)malloc(1024); | 73 context->mds_heap = (mds*)malloc(1024); |
75 context->ds = context->ds_heap; | 74 context->ds = context->ds_heap; |
76 context->mds = context->mds_heap; | 75 context->mds = context->mds_heap; |
77 goto start_code(context); | 76 goto start_code(context); |
78 } | 77 } |