简体   繁体   English

二进制伙伴实施问题

[英]Binary Buddies implementation questions

I've read and understand the concepts behind the binary buddies approach to memory allocation, and I'm trying to put it to work in C but I have a few implementation specific questions before I can really get started. 我已经阅读并理解了二进制伙伴方法进行内存分配的概念,并且正在尝试将其用于C语言,但是在真正开始之前,我还有一些实现方面的特定问题。

https://drive.google.com/file/d/0BxJX9LHXUU59OWZ6ZmhvV1lBX2M/view?usp=sharing - This is a link to the assignment specification, my question pertains to problem 5. https://drive.google.com/file/d/0BxJX9LHXUU59OWZ6ZmhvV1lBX2M/view?usp=sharing-这是分配规范的链接,我的问题与问题5有关。

The problem specifies that one call to malloc is to be made at the initialization of the allocator, and all requests for memory must be serviced using the space acquired from this call. 该问题表明在分配器初始化时将对malloc进行一次调用,并且必须使用从此调用获取的空间来满足所有对内存的请求。

  1. It's clear that the initial pointer to this space must be incremented in some way when a call to get_memory() is made, and the new pointer will be returned to the calling process. 显然,在调用get_memory()时,必须以某种方式增加指向此空间的初始指针,并且新指针将返回到调用过程。 How can I increment the pointer by a specific number of bytes? 如何将指针增加特定数量的字节?

  2. I understand that free lists for each block size must be kept, but I'm unsure exactly how these will be initialized and maintained. 我了解必须保留每种块大小的空闲列表,但是我不确定如何初始化和维护这些空闲列表。 What is stored in the free list exactly? 空闲列表中到底存储了什么? The memory pointer? 内存指针?

I apologize if these questions have been asked before, I haven't found a relevant question that provided enough clarity for me to get working. 很抱歉,如果您之前曾问过这些问题,还没有找到一个相关的问题可以使我清楚地开始工作。

For your first question, you just have to increment your pointer like a normal variable. 对于第一个问题,您只需要像普通变量一样增加指针即可。

The value of a pointer corresponds to the address in memory of the data it points to. 指针的值对应于它指向的数据在内存中的地址。 By incrementing it by, say 10, you actually move 10 bytes further into your memory. 通过将其增加例如10,您实际上将10个字节进一步移入了内存。

As for the free list, malloc() creates a structure contingent with the allocated memory block containing informations such as the address of the memory block,its size, and whether it is free or not. 至于空闲列表,malloc()创建一个随分配的内存块而定的结构,该内存块包含诸如内存块的地址,其大小以及是否空闲等信息。

You goal is to create these structures so you can keep track of the status the different memory blocks you have allocated or free with your get_memory() and release_memory() function. 您的目标是创建这些结构,以便可以使用get_memory()和release_memory()函数跟踪已分配或释放的不同内存块的状态。

You might also find this useful : https://stackoverflow.com/a/1957125/4758798 您可能还会发现此有用: https : //stackoverflow.com/a/1957125/4758798

  1. It's clear that the initial pointer to this space must be incremented in some way when a call to get_memory() is made, and the new pointer will be returned to the calling process. 显然,在调用get_memory()时,必须以某种方式增加指向此空间的初始指针,并且新指针将返回到调用过程。 How can I increment the pointer by a specific number of bytes? 如何将指针增加特定数量的字节?

When you call get_memory() , you will return a pointer to the main memory added to some offset. 调用get_memory() ,将返回指向添加到某个偏移量的主存储器的指针。 The word 'increment' implies that you are going to change the value of the initial pointer, which you should not do. “增加”一词意味着您将要更改初始指针的值,而您不应这样做。

Here is some simple code of me subaddressing one big memory block. 这是我给一个大内存块子地址的一些简单代码。

#include <stdlib.h>
#include <stdio.h>

int main (void)
{
  // Allocate a block of memory
  void * memory_block = malloc (512);

  // Now "Split" that memory into two halves.
  void * first_half = memory_block;
  void * second_half = memory_block + 256;

  // We can even keep splitting...
  void * second_first_half = second_half;
  void * second_second_half = second_half + 128;

  // Note that this splitting doesn't actually change the main memory block.  
  // We're just bookmarking locations in it.

  printf ("memory_block %p\n", memory_block);
  printf ("first_half %p\n", first_half);
  printf ("second_half %p\n", second_half);
  printf ("second_first_half %p\n", second_first_half);
  printf ("second_second_half %p\n", second_second_half);

  return 0;
}

  1. I understand that free lists for each block size must be kept, but I'm unsure exactly how these will be initialized and maintained. 我了解必须保留每种块大小的空闲列表,但是我不确定如何初始化和维护这些空闲列表。 What is stored in the free list exactly? 空闲列表中到底存储了什么? The memory pointer? 内存指针?

At a minimum, you probably want to keep track of the memory pointer and the size of that memory block, so something like this... 至少,您可能希望跟踪内存指针和该内存块的大小,所以类似这样的事情...

typedef struct memory_block {
  void * memory;
  size_t size;
} memory_block_t;

There are other ways to represent this though. 不过,还有其他方法可以表示这一点。 For example, you get equivalent information by keeping track of their memory offsets relative to the global malloc. 例如,通过跟踪它们相对于全局malloc的内存偏移量,可以获得等效信息。 I would suggest treating memory as a set of offsets like this: 我建议将内存视为这样的一组偏移量:

void * global_memory; // Assigned by start_memory()

// Functionally equivalent to the above struct
// memory = global_memory + begin;
// size = end - begin;
typedef struct memory_block {
  size_t begin;
  size_t end;
} memory_block_t;

There are multiple approaches to this difficult problem. 解决这个难题有多种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM