ApophisZerg游戏引擎主要依赖Vulkan的独立跨平台实现代码1(engine/src/core部分)

1.kmemory.h

#pragma once

#include "defines.h"

typedef enum memory_tag{

    //For temporary use. Should be assigned one of the below or have a new tag created

    MEMORY_TAG_UNKNOWN,

    MEMORY_TAG_ARRAY,

    MEMORY_TAG_LINEAR_ALLOCATOR,

    MEMORY_TAG_DARRAY,

    MEMORY_TAG_DICT,

    MEMORY_TAG_RING_QUEUE,

    MEMROY_TAG_BST,

    MEMORY_TAG_STRING,

    MEMORY_TAG_APPLICATION,

    MEMORY_TAG_JOB,

    MEMORY_TAG_TEXTURE,

    MEMORY_TAG_MATERIAL_INSTANCE,

    MEMORY_TAG_RENDERER,

    MEMORY_TAG_GAME,

    MEMORY_TAG_TRANSFORM,

    MEMORY_TAG_ENTITY,

    MEMORY_TAG_ENTITY_NODE,

    MEMORY_TAG_SCENE,

    MEMORY_TAG_MAX_TAGS

}memory_tag;

KAPI void initialize_memory();

KAPI void shutdown_memory();



 

KAPI void* kallocate(u64 size,memory_tag tag);

KAPI void kfree(void*block,u64 size,memory_tag tag);

KAPI void* kzero_memory(void* block,u64 size);

KAPI void* kcopy_memory(void* dest,const void* source,u64 size);

KAPI void* kset_memory(void* dest,i32 value,u64 size);

KAPI char* get_memory_usage_str();

KAPI u64 get_memory_alloc_count();

2.kmemory.c

#include "core/kmemory.h"

#include "core/logger.h"

#include "platform/platform.h"

#include <string.h>

#include <stdio.h>

struct memory_stats{

    u64 total_allocated;

    u64 tagged_allocations[MEMORY_TAG_MAX_TAGS];


 

};


 

static const char* memory_tag_strings[MEMORY_TAG_MAX_TAGS] = {

    "UNKNOWN",

    "ARRAY",

    "LINEAR_ALLC",

    "DARRAY",

    "DICT",

    "RING_QUEUE",

    "BST",

    "STRING",

    "APPLICATION",

    "JOB",

    "TEXTURE",

    "MAT_INST",

    "RENDERER",

    "GAME",

    "TRANSFORM",

    "ENTITY",

    "ENTITY_NODE",

    "SCENE"};

static struct memory_stats stats;


 

typedef struct memory_system_state{

    struct memory_stats stats;

    u64 alloc_count;


 

}memory_system_state;


 

static memory_system_state* state_ptr;


 

void initialize_memory(u64* memory_requirement,void* state){

   

    //错的地方

   

    //*memory_requirement = sizeof(memory_system_state);

    if(state == 0)

    {

        return;

    }

    state_ptr = state;

    state_ptr->alloc_count = 0;

    platform_zero_memory(&state_ptr->stats,sizeof(state_ptr->stats));




 

}

void shutdown_memory(void* state)

{

    state_ptr = 0;


 

}

void* kallocate(u64 size, memory_tag tag){


 

    if(tag == MEMORY_TAG_UNKNOWN){

        KWARN("kallocate called using MEMORY_TAG_UNKNOWN, Re-class this allocation.");


 

    }

    if(state_ptr)

    {

    state_ptr->stats.total_allocated += size;

    state_ptr->stats.tagged_allocations[tag] += size;

    state_ptr->alloc_count++;

    }

    void* block = platform_allocate(size,false);

    platform_zero_memory(block,size);

    return block;

}

void kfree(void* block,u64 size,memory_tag tag)

{

    if (tag == MEMORY_TAG_UNKNOWN)

    {

        KWARN("kfree called using MEMORY_TAG_UNKNOWN. class this allocation");


 

    }

   

    state_ptr->stats.total_allocated -= size;

    state_ptr->stats.tagged_allocations[tag] -= size;

   

   

    platform_free(block,false);


 

}


 

void* kzero_memory(void* block,u64 size)

{

    return platform_zero_memory(block,size);

}

void* kcopy_memory(void* dest,const void* source,u64 size)

{

    return platform_copy_memory(dest,source,size);

}

void* kset_memory(void* dest,i32 value,u64 size)

{

    return platform_set_memory(dest,value,size);

}

char* get_memory_usage_str()

{

    const u64 gib = 1024 * 1024 * 1024;

    const u64 mib = 1024*1024;

    const u64 kib = 1024;


 

    char buffer[8000] = "System memory use (tagged):\n";

    u64 offset = strlen(buffer);

    for(u32 i = 0;i<MEMORY_TAG_MAX_TAGS;++i)

    {      

        char unit[4] = "XiB";

        float amount = 1.0f;

        if(state_ptr->stats.tagged_allocations[i]>=gib)

        {

            unit[0] = 'G';

            amount = state_ptr->stats.tagged_allocations[i] / (float)gib;

        }else if(state_ptr->stats.tagged_allocations[i] >= mib)

        {

            unit[0] = 'M';

            amount = state_ptr->stats.tagged_allocations[i] / (float)mib;

        }else if(state_ptr->stats.tagged_allocations[i] >= kib)

        {

             unit[0] = 'K';

            amount = state_ptr->stats.tagged_allocations[i] / (float)kib;

           

        }else{

             unit[0] = 'B';

            unit[1] = 0;

            amount = (float)state_ptr->stats.tagged_allocations[i];


 

        }

        i32 length = snprintf(buffer + offset,8000," %s: %.2f%s\n",memory_tag_strings[i],amount,unit);

        offset += length;






 

    }


 

    char* out_string = _strdup(buffer);

    return out_string;



 

}

u64 get_memory_alloc_count()

{

    if(state_ptr)

    {

        return state_ptr->alloc_count;


 

    }

    return 0;



 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Magnum Lehar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值