annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
1 #include <stdio.h>
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
2 #include <stdlib.h>
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
3
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
4 #include "allocate.h"
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
5
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
6 #ifdef CLANG
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
7 #define _CbC_retrun __return
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
8 #define _CbC_environment __environment
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
9 #endif
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
10
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
11 #define SIZE 1024
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
12
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
13 typedef struct DataSegment1 {
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
14 int i; // 4 byte
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
15 char c[10]; // 10 byte
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
16 // padding 2 byte
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
17 } ds;
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
18
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
19 typedef struct metaDataSegment1 {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
20 size_t size; // 8 byte
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
21 ds* ds; // 8 byte
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
22 } mds;
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
23
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
24 typedef struct Context_st {
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
25 ds* ds;
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
26 mds* mds;
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
27 ds* ds_heap;
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
28 mds* mds_heap;
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
29 } Context;
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
30
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
31 __code start_code(Context* context) {
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
32 goto meta_start_code(context);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
33 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
34
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
35 __code meta_start_code(Context* context) {
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
36 goto code1(context);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
37 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
38
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
39 __code code1(Context* context) {
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
40 goto meta_code1(context);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
41 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
42
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
43 __code meta_code1(Context* context) {
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
44 goto allocate(context, SIZE);
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
45 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
46
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
47 __code allocate(Context* context, int size) {
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
48 ds* in = context->ds;
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
49 context->ds += size;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
50 context->mds->ds = in;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
51 context->mds->size = context->ds - in;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
52 context->mds++;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
53 goto meta_allocate(context, in);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
54 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
55
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
56 __code meta_allocate(Context* context, ds* in) {
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
57 goto exit_code(context, in, 0);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
58 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
59
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
60 __code exit_code(Context* context, ds* in, int i) {
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
61 in[i].i = i;
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
62 if (i == SIZE) {
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
63 free(context->ds_heap);
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
64 free(context->mds_heap);
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
65 goto exit(0);
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
66 }
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
67 goto exit_code(context, in, ++i);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
68 }
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
69
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
70 int main() {
6
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
71 Context* context = (Context*)malloc(sizeof(Context));
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
72 context->ds_heap = (ds*)malloc(1024);
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
73 context->mds_heap = (mds*)malloc(1024);
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
74 context->ds = context->ds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
75 context->mds = context->mds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
76 goto start_code(context);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
77 }