view src/allocate.c @ 4:cd262e34ac1a

allocate example
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Wed, 18 Mar 2015 12:41:39 +0900
parents e95bf956f853
children b8066055e295
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>

#include "allocate.h"

#ifdef CLANG
#define _CbC_retrun __return
#define _CbC_environment __environment
#endif

typedef struct DataSegment1 {
    int i;    // 4 byte
    char c;   // 1 byte
              // padding 3 byte
} ds, *dsptr; // 8 byte

typedef struct metaDataSegment1 {
    size_t size; // 8 byte
    dsptr ds;    // 8 byte
} mds, *mdsptr;  // 16 byte

typedef struct Context {
    dsptr ds;
    mdsptr mds;
    dsptr ds_heap;
    mdsptr mds_heap;
} context, *contextptr;

__code start_code(contextptr context) {
    goto meta_start_code(context);
}

__code meta_start_code(contextptr context) {
    goto code1(context);
}

__code code1(contextptr context) {
    goto meta_code1(context);
}

__code meta_code1(contextptr context) {
    goto allocate(context, 10);
}

__code allocate(contextptr context, int size) {
    dsptr in = context->ds;
    context->ds += size;
    context->mds->ds = in;
    context->mds->size = context->ds - in;
    context->mds++;
    goto meta_allocate(context, in);
}

__code meta_allocate(contextptr context, dsptr in) {
    goto exit_code(context, in, 0);
}

__code exit_code(contextptr context, dsptr in, int i) {
    in[i].i = i;
    printf("%d\n", in[i].i);
    if (i == 10)
        goto exit(0);
    goto exit_code(context, in, ++i);
}

int main() {
    contextptr context = (contextptr)malloc(sizeof(contextptr));
    context->ds_heap = (dsptr)malloc(sizeof(ds)*1024);
    context->mds_heap = (mdsptr)malloc(sizeof(mds)*1024);
    context->ds = context->ds_heap;
    context->mds = context->mds_heap;
    goto start_code(context);
}