制作自己的malloc函数用C [英] making your own malloc function in C

查看:214
本文介绍了制作自己的malloc函数用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要你在此帮助。我对C的平均知识和这里的问题。我将用一些基准来对新的处理器测试一些计算机体系结构的东西(分支未命中,高速缓存未命中)。有关的事情是,基准是C,但我必须不包括任何库的调用。例如,我不能用malloc的,因为我得到的错误

I need your help in this. I have an average knowledge of C and here is the problem. I am about to use some benchmarks to test some computer architecture stuff (branch misses, cache misses) on a new processor. The thing about that is that benchmarks are in C but I must not include any library calls. For example, I cannot used malloc because I am getting the error

"undefined reference to malloc" 

即使我已经包括图书馆。所以,我有写我自己的malloc。我不希望它是超高效的 - 只是做基础。当我想到它,我必须在内存中有一个地址,每次一个malloc发生,我返回一个指针到该地址,并增加了大小BU计数器。 malloc的两次发生在我的计划,所以我甚至都不需要大容量内存。

even if I have included the library. So I have to write my own malloc. I do not want it to be super efficient - just do the basics. As I am thinking it I must have an address in memory and everytime a malloc happens, I return a pointer to that address and increment the counter bu that size. Malloc happens twice in my program so I do not even need large memory.

您能帮我一下吗?我是一个Verilog的设计,没有使用C这么多的经验。

Can you help me on that? I am a verilog designed and do not have so much experience in C.

我见过previous的答案,但都似乎对我来说太复杂了。除此之外,我没有获得K·R的书。

I have seen previous answers but all seem too complicated for me. Besides, I do not have access to K-R book.

干杯!

编辑:也许这可以帮助你更多:
我没有用gcc,但SDE-gcc编译器。这有什么区别?也许这就是为什么我越来越不确定和参考的malloc?

maybe this can help you more: I am not using gcc but sde-gcc compiler. Does it make any difference? Maybe thats why I am getting and undefined reference to malloc?

EDIT2:
我测试MIPS架构:

I am testing a mips architecture:

我已经包括:

#include <stdlib.h>

和错误是:

undefined reference to malloc
relocation truncated to fit: R_MIPS_26 against malloc

和编译器命令ID:

test.o: test.c cap.h
sde-gcc -c -o test.s test.c -EB -march=mips64 -mabi=64 -G -O -ggdb -O2 -S
    sde-as -o test.o test.s EB -march=mips64 -mabi=64 -G -O -ggdb
    as_objects:=test.o init.o

编辑3:
确定我使用的实现上面,它运行没有任何问题。问题是,在做嵌入式编程的时候,你只需要定义您的一切使用,所以我定义我自己的malloc。 SDE-GCC没有认识到malloc函数。

EDIT 3: ok I used implementation above and it run without any problems. The problem is that when doing embedded programming, you just have to define everything you are using so I defined my own malloc. sde-gcc didn't recognized the malloc function.

推荐答案

这是一个非常简单的方法,它可以让你过去的2 mallocs:

This is a very simple approach, which may get you past your 2 mallocs:

static unsigned char our_memory[1024 * 1024]; //reserve 1 MB for malloc
static size_t next_index = 0;

void *malloc(size_t sz)
{
    void *mem;

    if(sizeof our_memory - next_index < sz)
        return NULL;

    mem = &our_memory[next_index];
    next_index += sz;
    return mem;
}

void free(void *mem)
{
   //we cheat, and don't free anything.
}

如果需要,您可能需要将内存块你的手回来,所以如你一直
还给这是对那是无论你需要4个,8个,16个或多个地址的内存地址。

If required, you might need to align the memory piece you hand back, so e.g. you always give back memory addresses that's on an address that's a multiple of 4, 8, 16 or whatever you require.

这篇关于制作自己的malloc函数用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆