C语言 在 C 中制作你自己的 malloc 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13764711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:42:01  来源:igfitidea点击:

making your own malloc function in C

cmalloc

提问by ghostrider

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 it is that benchmarks are in C but I must not include any library calls. For example, I cannot use malloc because I am getting the error

我需要你的帮助。我对 C 有一般的了解,这就是问题所在。我将使用一些基准测试来测试新处理器上的一些计算机架构内容(分支未命中、缓存未命中)。关于它的事情是基准测试在 C 中,但我不能包含任何库调用。例如,我不能使用 malloc 因为我收到错误

"undefined reference to 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 by that size. Malloc happens twice in my program so I do not even need large memory.

即使我已经包括了图书馆。所以我必须编写自己的malloc。我不希望它非常高效 - 只做基础。正如我所想的那样,我必须在内存中拥有一个地址,并且每次发生 malloc 时,我都会返回一个指向该地址的指针并将计数器增加该大小。Malloc 在我的程序中发生了两次,所以我什至不需要大内存。

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

你能帮我吗?我设计了一个 Verilog,在 C 方面没有太多经验。

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

我看过以前的答案,但对我来说似乎都太复杂了。此外,我无法访问 KR 书籍。

Cheers!

干杯!

EDIT: maybe this can help you more: I am not using gcc but the sde-gcc compiler. Does it make any difference? Maybe that's why I am getting an undefined reference to malloc?

编辑:也许这可以为您提供更多帮助:我不是在使用 gcc,而是在使用 sde-gcc 编译器。它有什么区别吗?也许这就是为什么我收到对 malloc 的未定义引用?

EDIT2: I am testing a MIPS architecture:

EDIT2:我正在测试 MIPS 架构:

I have included:

我已经包括:

#include <stdlib.h>

and the errors are:

错误是:

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

and the compiler command id:

和编译器命令 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

EDIT 3: ok, I used implementation above and it runs 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 recognize the malloc function.

编辑 3:好的,我使用了上面的实现并且它运行没有任何问题。问题是在进行嵌入式编程时,您只需要定义您正在使用的所有内容,因此我定义了自己的 malloc。sde-gcc 无法识别 malloc 函数。

回答by nos

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

这是一种非常简单的方法,它可能会让您通过 2 个 malloc:

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.
}

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.

如果需要,您可能需要对齐您交回的内存块,例如,您总是返回位于 4、8、16 倍数或任何您需要的地址上的内存地址。

回答by ChandanK

Trying a thread safe nos answer given above, I am referring his code with some changes as below:

尝试上面给出的线程安全 nos 答案,我指的是他的代码,并进行了一些更改,如下所示:

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

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void *malloc(size_t sz)
{
    void *mem;
    pthread_mutex_lock(&lock);
    if(sizeof our_memory - next_index < sz){
        pthread_mutex_unlock(&lock);
        return NULL;
    }

    mem = &our_memory[next_index];
    next_index += sz;
    pthread_mutex_unlock(&lock);
    return mem;
}

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

回答by AnthonyLambert

You need to link against libc.a or the equivilent for your system. If you don't use the standard C lib you won't get any of the startup code that runs before the main function either. Your program will never run....

您需要链接 libc.a 或系统的等效项。如果您不使用标准 C 库,您也不会获得在 main 函数之前运行的任何启动代码。你的程序永远不会运行......

You could either allocate a block of static data and use that in the place of malloc, like:

您可以分配一个静态数据块并使用它来代替 malloc,例如:

// char* fred = malloc(10000);
// equals

static char [100000] fred;

or call the standard malloc for a large block of continuous memory on startup and write yr own malloc type function to divide that down. In the 2nd case you would start benchmarking after the calling the system's malloc as to not effect the benchmarks.

或者在启动时为一大块连续内存调用标准 malloc 并编写你自己的 malloc 类型函数来划分它。在第二种情况下,您将在调用系统的 malloc 后开始基准测试,以免影响基准测试。