gpt4 book ai didi

linux - 自定义内存分配器 - 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 10:12:14 24 4
gpt4 key购买 nike

我和我的 friend 正在尝试在 linux ubuntu 16.04 中开发自定义内存分配器。我们因为错误而卡住了,顺便说一句,这是我们的第一次我们正在尝试编写类似的代码,所以我们不是最好的调试器错误是:段错误(核心转储)这是代码。任何人都可以帮助我们了解出了什么问题吗?谢谢!

#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <stdio.h>

struct header_t {
size_t size;
unsigned is_free;
struct header_t *next; };

struct header_t *head = NULL, *tail = NULL;
pthread_mutex_t global_malloc_lock;

struct header_t *get_free_block(size_t size)
{
struct header_t *curr = head;
while(curr) {
/* see if there's a free block that can accomodate requested size */
if (curr->is_free && curr->size >= size)
return curr;
curr = curr->next;
}
return NULL;
}

void free(void *block)
{
struct header_t *header, *tmp;
/* program break is the end of the
process's data segment */
void *programbreak;

if (!block)
return;
pthread_mutex_lock(&global_malloc_lock);
header = (struct header_t*)block - 1;

/* sbrk(0) gives the current program break address */


programbreak = sbrk(0);

/*
Check if the block to be freed is the last one in the
linked list. If it is, then we could shrink the size of the
heap and release memory to OS. Else, we will keep the block
but mark it as free. */

if ((char*)block + header->size == programbreak) {
if (head == tail) {
head = tail = NULL;
} else {
tmp = head;
while (tmp) {
if(tmp->next == tail) {
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
/* sbrk() with a negative argument decrements the program break.
So memory is released by the program to OS. */


sbrk(0 - header->size - sizeof(struct header_t));


/* Note: This lock does not really assure thread
safety, because sbrk() itself is not really
thread safe. Suppose there occurs a foregin sbrk(N)
after we find the program break and before we decrement
it, then we end up realeasing the memory obtained by
the foreign sbrk(). */

pthread_mutex_unlock(&global_malloc_lock);
return;
}
header->is_free = 1;
pthread_mutex_unlock(&global_malloc_lock);
}

void *malloc(size_t size)
{
size_t total_size;
void *block;
struct header_t *header;
if (!size)
return NULL;
pthread_mutex_lock(&global_malloc_lock);
header = get_free_block(size);
if (header) {
/* Woah, found a free block to accomodate requested memory. */
header->is_free = 0;
pthread_mutex_unlock(&global_malloc_lock);
return (void*)(header + 1);
}
/* We need to get memory to fit in the requested block and header
from OS. */
total_size = sizeof(struct header_t) + size;
block = sbrk(total_size);
if (block == (void*) -1) {
pthread_mutex_unlock(&global_malloc_lock);
return NULL;
}
header = block;
header->size = size;
header->is_free = 0;
header->next = NULL;
if (!head)
head = header;
if (tail)
tail->next = header;
tail = header;
pthread_mutex_unlock(&global_malloc_lock);
return (void*)(header + 1);
}

void *calloc(size_t num, size_t nsize)
{
size_t size;
void *block;
if (!num || !nsize)
return NULL;
size = num * nsize;
/* check mul overflow */
if (nsize != size / num)
return NULL;
block = malloc(size);
if (!block)
return NULL;
memset(block, 0, size);
return block;
}


void *realloc(void *block, size_t size)
{
struct header_t *header;
void *ret;
if (!block || !size)
return malloc(size);
header = (struct header_t*)block - 1;
if (header->size >= size)
return block;
ret = malloc(size);
if (ret) {
/* Relocate contents to the new bigger block */
memcpy(ret, block, header->size);
/* Free the old memory block */
free(block);
}
return ret;
}

最佳答案

问题的发生是因为函数没有原型(prototype)化 [decalred]。一旦我添加了函数原型(prototype)。代码有效。

有关原型(prototype)制作的更多信息:http://www.trytoprogram.com/c-programming/function-prototype-in-c/

关于linux - 自定义内存分配器 - 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48340425/

24 4 0
文章推荐: javascript - javascript中的可拖动元素
文章推荐: python - 在 django 中为新应用程序设置设置应用程序值
文章推荐: jquery - 使
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com