0

I want to split an allocated memory region in two at a specific point, so that those two regions can be realloc'd separately. I want to do this because I want to free space in the middle of the region.

My idea is to do something like this:

┌───────────────────────────────┐
│ Allocated                     │
└───────────────────────────────┘
              ↓
          split( );
              ↓
┌───────────────┬───────────────┐
│ Allocated     │ Allocated     │
└───────────────┴───────────────┘
              ↓
         realloc( );
              ↓
┌───────────┐   ┌───────────────┐
│ Allocated │   │ Allocated     │
└───────────┘   └───────────────┘

Is this possible in Rust? If so, how?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
D. Pardal
  • 6,173
  • 1
  • 17
  • 37
  • nor it can be done in rust – Kitsu Jul 08 '20 at 10:12
  • 2
    _"I want to do this because I want to free space in the middle of the region."_ This is definitely an XY problem. You may just have to redesign the data structure around the problem. – E_net4 Jul 08 '20 at 10:31
  • @E_net4likesmanythings I didn't want to give many details, but the data structure I want to implement is a kind of linked list. I want O(1) removal, so when I remove a node in the middle, I don't want to move the other nodes around. I guess I'll have to manually keep track of the free parts inside the allocated regions, since it's not possible to make the allocator do that. – D. Pardal Jul 08 '20 at 10:38

1 Answers1

2

This is (currently) not possible.

Rust's memory allocation API is in the alloc module. And as you can see, the functionality you need is not offered there.

In theory, allocators could offer such a feature. However, in practice, this is usually not offered nor used. The standard allocation API in C also does not offer this as far as I know.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305