Esempio n. 1
0
Exp *newVar(int bind) {
    Var *newVarNode = ckMalloc(sizeof(Var));
    newVarNode->bind = bind;

    ExpV *valu = ckMalloc(sizeof(ExpV));
    valu->var = newVarNode;

    Exp *newExpNode = ckMalloc(sizeof(Exp));
    newExpNode->type = T_Var;
    newExpNode->value = valu;
    return newExpNode;
}
Esempio n. 2
0
Exp *newOpn(OpTy type) {
    Opn *newOpnNode = ckMalloc(sizeof(Opn));
    newOpnNode->type = type;

    ExpV *valu = ckMalloc(sizeof(ExpV));
    valu->opn = newOpnNode;

    Exp *newExpNode = ckMalloc(sizeof(Exp));
    newExpNode->type = T_Opn;
    newExpNode->value = valu;
    return newExpNode;
}
Esempio n. 3
0
Exp *newCon(ConTy ty, int conVal) {
    Con *newConNode = ckMalloc(sizeof(Con));
    newConNode->val = conVal;
    newConNode->ty = ty;

    ExpV *valu = ckMalloc(sizeof(ExpV));
    valu->con = newConNode;

    Exp *newExpNode = ckMalloc(sizeof(Exp));
    newExpNode->type = T_Con;
    newExpNode->value = valu;
    return newExpNode;
}
Esempio n. 4
0
Exp *newAbs(Exp *body) {
    Abs *newAbsNode = ckMalloc(sizeof(Abs));
    newAbsNode->body = body;

    ExpV *valu = ckMalloc(sizeof(ExpV));
    valu->abs = newAbsNode;

    Exp *newExpNode = ckMalloc(sizeof(Exp));
    newExpNode->type = T_Abs;
    newExpNode->value = valu;

    return newExpNode;
}
Esempio n. 5
0
/*
 * Create new expression nodes of each type.
 */
Exp *newApp(Exp *fun, Exp *arg) {
    App *newAppNode = ckMalloc(sizeof(App));
    newAppNode->fun = fun;
    newAppNode->arg = arg;

    ExpV *valu = ckMalloc(sizeof(ExpV));
    valu->app = newAppNode;

    Exp *newExpNode = ckMalloc(sizeof(Exp));
    newExpNode->type = T_App;
    newExpNode->value = valu;

    return newExpNode;
}
Esempio n. 6
0
char *getText(FILE *f) {
  int c;
  int max = 100;    // current number of bytes in s
  char *s = (char *)ckMalloc(max);
  int len = 0;     // current length of the string represented by s

  while ((c = getc(f)) != EOF) {
    // compress runs of white space into one space
    if (isspace(c)) {
      if ((len > 0) && (s[len-1] != ' ')) {
	s[len] =' ';
	len++;
      }
    } else {
      s[len] = c;
      len++;
    }

    if (len >= max) {
      // allocate more space if necessary.
      max = max * 1.5;
      char *tmp = ckRealloc(s, max);
      s = tmp;

    }
  }

  // get rid of trailing space at the end of the string.
  if (s[len-1] == ' ') {
    s[len-1] = '\0';
  } else 
    s[len] = '\0';

  return s;
}
Esempio n. 7
0
void ckUtil::loadShader(ckID shd_id, const char* vert_file, const char* frag_file, u8 uni_num, u8 att_num, u8 tex_num)
{
    ckID vert_id = ckID::genID();
    char* vert_code = NULL;

    if (vert_file)
    {
        ckResMgr::loadResourceAs(vert_id, vert_file, false);
        ckRes res = ckResMgr::getResource(vert_id);
        vert_code = static_cast<char*>(ckMalloc(res.getDataSize() + 1));

        ckMemMgr::memcpy(vert_code, res.getData<void>(), res.getDataSize());
        vert_code[res.getDataSize()] = '\0';

        ckResMgr::removeResource(vert_id);
    }

    ckID frag_id = ckID::genID();
    char* frag_code = NULL;

    if (frag_file)
    {
        ckResMgr::loadResourceAs(frag_id, frag_file, false);
        ckRes res = ckResMgr::getResource(frag_id);
        frag_code = static_cast<char*>(ckMalloc(res.getDataSize() + 1));

        ckMemMgr::memcpy(frag_code, res.getData<void>(), res.getDataSize());
        frag_code[res.getDataSize()] = '\0';

        ckResMgr::removeResource(frag_id);
    }

    ckDrawMgr::newShader(shd_id, vert_code, frag_code, uni_num, att_num, tex_num);

    if (vert_code)
    {
        ckFree(vert_code);
    }

    if (frag_code)
    {
        ckFree(frag_code);
    }
}
Esempio n. 8
0
void ckMemMgr::createFirst(u32 catcake_version)
{
    if (catcake_version != CATCAKE_VERSION)
    {
        ckThrow(ExceptionInvalidVersionOfHeader);
    }

    if (!(sizeof(s8) == 1 && sizeof(s16) == 2 && sizeof(s32) == 4 && sizeof(s64) == 8 && //
        sizeof(u8) == 1 && sizeof(u16) == 2 && sizeof(u32) == 4 && sizeof(u64) == 8 && //
        sizeof(r32) == 4 && sizeof(r64) == 8))
    {
        ckThrow(ExceptionInvalidSizeOfType);
    }

    destroyLast();

    m_instance = new(ckLowLevelAPI::malloc(sizeof(ckMemMgr)), NULL) ckMemMgr;

    m_instance->m_temp_buf_size = INITIAL_TEMP_BUFFER_SIZE;
    m_instance->m_temp_buf = ckMalloc(m_instance->m_temp_buf_size);
}
Esempio n. 9
0
void* ckMemMgr::allocTempBufferForSystem(u32 size)
{
    ckMemMgr* ins = instance();

    if (size == 0)
    {
        ckThrow(ExceptionInvalidArgument);
    }

    if (size > ins->m_temp_buf_size)
    {
        while (size > ins->m_temp_buf_size)
        {
            ins->m_temp_buf_size *= 2;
        }

        ckFree(ins->m_temp_buf);

        ins->m_temp_buf = ckMalloc(ins->m_temp_buf_size);
    }

    return ins->m_temp_buf;
}
Esempio n. 10
0
void ckUtil::calcNormalAsTriangles(ckVec* normal, const ckPrim::PrimData* prim_data, u16 vert_num, bool is_smoothing)
{
    if (!normal || !prim_data || vert_num == 0)
    {
        ckThrow(ExceptionInvalidArgument);
    }

    if (vert_num < 3)
    {
        for (s32 i = 0; i < vert_num; i++)
        {
            normal[i] = ckVec::Z_UNIT;
        }

        return;
    }

    u16 tri_vert_num = (vert_num / 3) * 3;

    for (s32 i = 0; i < tri_vert_num; i += 3)
    {
        ckVec vec1 = prim_data[i + 1].pos - prim_data[i].pos;
        ckVec vec2 = prim_data[i + 2].pos - prim_data[i].pos;

        normal[i] = normal[i + 1] = normal[i + 2] = vec1.cross(vec2).normalize();
    }

    for (s32 i = tri_vert_num; i < vert_num; i++)
    {
        normal[i] = ckVec::Z_UNIT;
    }

    if (!is_smoothing)
    {
        return;
    }

    u32 mark_buf_size = sizeof(u16) * vert_num;
    u16* mark_buf = static_cast<u16*>(ckMalloc(mark_buf_size));

    ckMemMgr::memset(mark_buf, 0, mark_buf_size);

    for (s32 i = 0; i < vert_num; i++)
    {
        if (mark_buf[i] > 0)
        {
            continue;
        }

        u16 mark = i + 1;
        u16 mark_num = 1;

        const ckVec& pos1 = prim_data[i].pos;
        const ckVec& n1 = normal[i];

        ckVec avgn = n1;

        mark_buf[i] = mark;

        for (s32 j = i + 1; j < vert_num; j++)
        {
            const ckVec& pos2 = prim_data[j].pos;
            const ckVec& n2 = normal[j];

            if (pos1.x == pos2.x && pos1.y == pos2.y && pos1.z == pos2.z)
            {
                r32 inner = n1.dot(n2);

                if (inner > 0.7071f) // cos 45
                {
                    mark_buf[j] = mark;
                    mark_num++;

                    if (inner < 1.0f - ckMath::EPSILON)
                    {
                        avgn += n2;
                    }
                }
            }
        }

        if (mark_num > 1)
        {
            if (avgn.x == 0.0f && avgn.y == 0.0f && avgn.z == 0.0f)
            {
                continue;
            }

            avgn = avgn.normalize();

            for (s32 j = i; j < vert_num; j++)
            {
                if (mark_buf[j] == mark)
                {
                    normal[j] = avgn;
                }
            }
        }
    }

    ckFree(mark_buf);
}
void ckResMgrTest()
{
    /*
        static bool isCreated()
        static void createAfterTask()
        static void destroyBeforeSys()
    */
    {
        ckAssert(!ckResMgr::isCreated());

        ckResMgr::createAfterTask();
        ckResMgr::createAfterTask();

        ckAssert(ckSysMgr::isCreated());

        ckResMgr::destroyBeforeSys();
        ckResMgr::destroyBeforeSys();

        ckAssert(!ckResMgr::isCreated());
    }

    /*
        static u16 getResourceNum()
        static bool hasResource(ckID id)
        static ckRes getResource(ckID id)
        static void addResource(ckID id, ckStr<char, 3> ext, const void* data, u32 data_size, bool is_auto_free)
        static void removeResource(ckID id)
        static const ckRes* getFirstResourceN()
        static const ckRes* getNextResourceN(ckID id)
    */
    {
        ckAssertThrow(ckResMgr::getResourceNum(), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::hasResource(ckID::ZERO), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::getResource(ckID::ZERO), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::addResource(ckID::ZERO, "", NULL, 0, false), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::removeResource(ckID::ZERO), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::getFirstResourceN(), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::getNextResourceN(ckID::ZERO), ckResMgr::ExceptionNotInitialized);

        ckResMgr::createAfterTask();
        ckAssert(ckResMgr::getResourceNum() == 0);

        ckAssert(!ckResMgr::getFirstResourceN());

        ckID id1 = ckID::genID();
        u32 data1 = 123;

        ckResMgr::addResource(id1, "ABC", &data1, sizeof(data1), false);
        ckAssert(ckResMgr::getResourceNum() == 1);
        ckAssert(ckResMgr::hasResource(id1));
        ckAssert(ckResMgr::getResource(id1).getID() == id1);
        ckAssert(ckResMgr::getFirstResourceN()->getID() == id1);
        ckAssert(!ckResMgr::getNextResourceN(id1));

        ckAssertThrow(ckResMgr::getResource(ckID::genID()), ckResMgr::ExceptionNotFound);
        ckAssertThrow(ckResMgr::addResource(id1, "", &data1, sizeof(data1), false), ckResMgr::ExceptionSameIDExists);

        u32 used_memory_size = ckMemMgr::getCurUsedMemorySize();

        ckID id2 = ckID::genID();
        u16* data2 = reinterpret_cast<u16*>(ckMalloc(sizeof(u16)));
        *data2 = 456;

        ckAssert(ckMemMgr::getCurUsedMemorySize() > used_memory_size);

        ckResMgr::addResource(id2, "", data2, sizeof(u16), true);
        ckAssert(ckResMgr::getResourceNum() == 2);
        ckAssert(ckResMgr::hasResource(id2));
        ckAssert(ckResMgr::getResource(id2).getID() == id2);
        ckAssert(ckResMgr::getFirstResourceN()->getID() == id1);
        ckAssert(ckResMgr::getNextResourceN(id1)->getID() == id2);
        ckAssert(!ckResMgr::getNextResourceN(id2));

        ckResMgr::removeResource(id2);
        ckAssert(ckResMgr::getResourceNum() == 1);
        ckAssert(!ckResMgr::hasResource(id2));
        ckAssert(ckResMgr::getFirstResourceN()->getID() == id1);
        ckAssert(!ckResMgr::getNextResourceN(id1));

        ckAssertThrow(ckResMgr::getResource(id2), ckResMgr::ExceptionNotFound);

        ckAssert(ckMemMgr::getCurUsedMemorySize() == used_memory_size);

        ckAssertThrow(ckResMgr::hasResource(ckID::ZERO), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::getResource(id2), ckResMgr::ExceptionNotFound);
        ckAssertThrow(ckResMgr::removeResource(id2), ckResMgr::ExceptionNotFound);
        ckAssertThrow(ckResMgr::getResource(ckID::ZERO), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::addResource(ckID::ZERO, "", &data1, sizeof(data1), false), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::addResource(id2, "", NULL, sizeof(data1), false), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::addResource(id2, "", &data1, 0, false), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::removeResource(ckID::ZERO), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::getNextResourceN(ckID::ZERO), ckResMgr::ExceptionInvalidArgument);

        ckResMgr::destroyBeforeSys();
    }

    /*
        static u16 getTypeNum()
        static void addType(ckStr<char, 3> ext, Initializer init, Finalizer final)
        static void removeType(ckStr<char, 3> ext)
        static const ckStr<char, 3>* getFirstTypeN()
        static const ckStr<char, 3>* getNextTypeN(ckStr<char, 3> ext)
    */
    {
        s_init_count1 = s_final_count1 = 0;
        s_init_count2 = s_final_count2 = 0;

        ckAssertThrow(ckResMgr::getTypeNum(), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::addType("", NULL, NULL), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::removeType(""), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::getFirstTypeN(), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::getNextTypeN(""), ckResMgr::ExceptionNotInitialized);

        ckResMgr::createAfterTask();
        ckAssert(ckResMgr::getTypeNum() == 0);

        ckResMgr::addType("aBc", initializer1, finalizer1);
        ckAssert(ckResMgr::getTypeNum() == 1);
        ckAssert(*ckResMgr::getFirstTypeN() == "ABC");
        ckAssert(!ckResMgr::getNextTypeN("ABC"));

        ckAssertThrow(ckResMgr::addType("abC", initializer1, finalizer1), ckResMgr::ExceptionSameExtensionExists);

        ckID id1 = ckID::genID();
        u32 data1 = 123;

        ckResMgr::addResource(id1, "abC", &data1, sizeof(data1), false);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 0);

        ckRes res1 = ckResMgr::getResource(id1);
        ckAssert(res1.getData<u32>() == &data1);

        ckResMgr::removeResource(id1);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);

        ckResMgr::addType("Def", initializer2, finalizer2);
        ckAssert(ckResMgr::getTypeNum() == 2);
        ckAssert(*ckResMgr::getFirstTypeN() == "ABC");
        ckAssert(*ckResMgr::getNextTypeN("ABC") == "DEF");
        ckAssert(!ckResMgr::getNextTypeN("DEF"));

        ckID id2 = ckID::genID();
        u32 data2 = 456;

        ckResMgr::addResource(id2, "DEF", &data2, sizeof(data2), false);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);
        ckAssert(s_init_count2 == 1 && s_final_count2 == 0);

        ckRes res2 = ckResMgr::getResource(id2);
        ckAssert(res2.getData<u32>() == &data2);

        ckResMgr::removeResource(id2);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);
        ckAssert(s_init_count2 == 1 && s_final_count2 == 1);

        ckResMgr::removeType("ABC");
        ckAssert(ckResMgr::getTypeNum() == 1);
        ckAssert(*ckResMgr::getFirstTypeN() == "DEF");
        ckAssert(!ckResMgr::getNextTypeN("DEF"));

        ckResMgr::addResource(id1, "ABC", &data1, sizeof(data1), false);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);
        ckAssert(s_init_count2 == 1 && s_final_count2 == 1);

        ckAssertThrow(ckResMgr::removeType("123"), ckResMgr::ExceptionNotFound);
        ckAssertThrow(ckResMgr::addType("", initializer1, finalizer1), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::addType("123", NULL, finalizer1), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::addType("123", initializer1, NULL), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::removeType(""), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::getNextTypeN(""), ckResMgr::ExceptionInvalidArgument);

        ckResMgr::destroyBeforeSys();
    }

    /*
        static void loadResource(const char* filename, bool is_type_detect)
        static void loadResourceAs(ckID id, const char* filename, bool is_type_detect)
    */
    {
        ckAssertThrow(ckResMgr::loadResource(NULL, false), ckResMgr::ExceptionNotInitialized);
        ckAssertThrow(ckResMgr::loadResourceAs(ckID::ZERO, NULL, false), ckResMgr::ExceptionNotInitialized);

        s_init_count1 = s_final_count1 = 0;

        ckResMgr::createAfterTask();

        ckResMgr::addType("DAT", initializer1, finalizer1);

        ckResMgr::loadResource(TEST_DATA_DIR "test_data1.dat", true);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 0);
        ckAssert(ckResMgr::getResourceNum() == 1);

        ckRes res1 = ckResMgr::getResource(ckID_("test_data1.dat"));
        ckAssert(res1.getDataSize() == 10);

        ckAssertThrow(ckResMgr::loadResource(TEST_DATA_DIR "test_data1.dat", true), ckResMgr::ExceptionSameIDExists);
        ckAssertThrow(ckResMgr::loadResource("dummy", true), ckResMgr::ExceptionCannotOpenFile);
        ckAssertThrow(ckResMgr::loadResource(TEST_DATA_DIR "test_data2.dat", true), ckResMgr::ExceptionCannotReadFile);

        ckResMgr::removeResource(ckID_("test_data1.dat"));
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);
        ckAssert(ckResMgr::getResourceNum() == 0);

        ckResMgr::loadResource(TEST_DATA_DIR "test_data1.dat", false);
        ckAssert(s_init_count1 == 1 && s_final_count1 == 1);
        ckAssert(ckResMgr::getResourceNum() == 1);

        u32 used_memory_size = ckMemMgr::getCurUsedMemorySize();

        ckResMgr::loadResourceAs(ckID_("test1"), TEST_DATA_DIR "test_data1.dat", true);
        ckAssert(s_init_count1 == 2 && s_final_count1 == 1);
        ckAssert(ckResMgr::getResourceNum() == 2);

        ckRes res2 = ckResMgr::getResource(ckID_("test1"));
        ckAssert(res2.getDataSize() == 10);

        ckResMgr::loadResourceAs(ckID_("test2"), TEST_DATA_DIR "test_data1.dat", false);
        ckAssert(s_init_count1 == 2 && s_final_count1 == 1);
        ckAssert(ckResMgr::getResourceNum() == 3);

        ckResMgr::removeResource(ckID_("test1"));
        ckAssert(s_init_count1 == 2 && s_final_count1 == 2);
        ckAssert(ckResMgr::getResourceNum() == 2);

        ckResMgr::removeResource(ckID_("test2"));
        ckAssert(s_init_count1 == 2 && s_final_count1 == 2);
        ckAssert(ckResMgr::getResourceNum() == 1);

        ckAssert(ckMemMgr::getCurUsedMemorySize() == used_memory_size);

        ckAssertThrow(ckResMgr::loadResource(NULL, true), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::loadResourceAs(ckID::ZERO, "test", true), ckResMgr::ExceptionInvalidArgument);
        ckAssertThrow(ckResMgr::loadResourceAs(ckID::genID(), NULL, true), ckResMgr::ExceptionInvalidArgument);

        ckResMgr::destroyBeforeSys();
    }
}