annotate src/allocate.c @ 5:b8066055e295

fix
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Wed, 18 Mar 2015 16:19:17 +0900
parents cd262e34ac1a
children 59c1086467f9
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 {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
14 int i; // 4 byte
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
15 char c; // 1 byte
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
16 // padding 3 byte
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
17 } ds, *dsptr; // 8 byte
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
21 dsptr ds; // 8 byte
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
22 } mds, *mdsptr; // 16 byte
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
23
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
24 typedef struct Context {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
25 dsptr ds;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
26 mdsptr mds;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
27 dsptr ds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
28 mdsptr mds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
29 } context, *contextptr;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
30
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
31 __code start_code(contextptr context) {
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
35 __code meta_start_code(contextptr context) {
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
39 __code code1(contextptr context) {
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
43 __code meta_code1(contextptr 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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
47 __code allocate(contextptr context, int size) {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
48 dsptr in = context->ds;
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
56 __code meta_allocate(contextptr context, dsptr in) {
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
60 __code exit_code(contextptr context, dsptr in, int i) {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
61 in[i].i = i;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
62 printf("%d\n", in[i].i);
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
63 if (i == SIZE) {
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
64 free(context->ds_heap);
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
65 free(context->mds_heap);
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
66 goto exit(0);
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
67 }
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
68 goto exit_code(context, in, ++i);
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
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
71 int main() {
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
72 contextptr context = (contextptr)malloc(sizeof(contextptr));
5
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
73 context->ds_heap = (dsptr)malloc(1024);
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
74 context->mds_heap = (mdsptr)malloc(1024);
4
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
75 context->ds = context->ds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
76 context->mds = context->mds_heap;
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
77 goto start_code(context);
cd262e34ac1a allocate example
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
78 }